如何修改R语言lm()模型默认参考类别,显示raceblack而非racewhite?
raceblack Instead of racewhite Hey there! Let's fix this variable naming in your linear model results—it's a common tweak when you want to frame your comparison the way you need, especially for your resume discrimination study referencing Bertrand & Mullainathan (2004).
Method 1: Relevel the Factor Reference Group (Recommended)
R’s lm() uses the first level of a factor variable as the reference group by default. Right now, it looks like "black" is the first level, so the coefficient shows the difference for white candidates. To flip this so we see the difference for black candidates relative to white, adjust the reference level of your race variable first:
# First, confirm your race variable is a factor (if not, convert it) if (!is.factor(resume$race)) { resume$race <- factor(resume$race) } # Relevel to set "white" as the reference group resume$race <- relevel(resume$race, ref = "white") # Rerun the regression fit <- lm(call ~ race, data = resume) summary(fit)
After this, your output will show raceblack as the coefficient: this represents the difference in callback rates between Black and White candidates, with the intercept being the baseline callback rate for White candidates—perfect for aligning with the study you're referencing.
Method 2: Manually Rename Coefficients (Quick Fix, Not Ideal)
If you don’t want to rerun the model, you can directly modify the names in the existing model object. Note this only changes the display, not the underlying reference group logic, so use this cautiously:
# Modify the coefficient names in the model object names(fit$coefficients)[names(fit$coefficients) == "racewhite"] <- "raceblack" # Update the summary output too summary_fit <- summary(fit) rownames(summary_fit$coefficients)[rownames(summary_fit$coefficients) == "racewhite"] <- "raceblack" # View the updated summary summary_fit
Key Note
Method 1 is always better because it adjusts the model’s actual reference group, making your results logically consistent with how you want to interpret them (comparing Black candidates to White candidates). This avoids any confusion if you share your code or results with others.
内容的提问来源于stack exchange,提问作者Ahmed

