A categorical predictor variable assigns each observation to a discrete group so an outcome can be compared across categories. ANOVA uses a categorical predictor with a quantitative target, such as comparing standardized wins across points-scored groups. section 14.5.1
Build a categorical predictor in R by:
- Using mutate() and case_when() to convert standardized points into ranges labeled A through D. section 14.5.1
- Using as.factor() so R treats the labels as factor levels rather than numeric values. section 14.5.1
- Using summary() to check the record count in every category and identify missing assignments. section 14.5.1
NBA points categories
For NBA team data from 2008–17, categorize standardized points allowed as A (< -1), B (-1 to 0), C (0 to 1), or D (> 1). The resulting factor contains 43, 103, 105, and 49 records, respectively, with no missing values. Boxplots then compare each category with standardized regular-season wins. section 14.5.1
nba_stats %>%
mutate(o_pts_cat = case_when(z_o_pts < -1 ~ "A",
z_o_pts >= -1 & z_o_pts < 0 ~ "B",
z_o_pts >= 0 & z_o_pts <= 1 ~ "C",
z_o_pts > 1 ~ "D")) -> nba_stats
nba_stats$o_pts_cat <- as.factor(nba_stats$o_pts_cat)
Inspect the R pipeline that derives a points-allowed category from standardized values and converts it into a factor. section 14.5.1
