R语言含交互项多元回归中abline绘图异常:男女分组回归线绘制问题排查与解决
Hey there! Let's break down what's going wrong with your code and fix it step by step.
Key Issues Causing Nonsense Lines
Mismatched Dependent Variable
Your modelMODEL3useslog(wage)as the dependent variable, but you're plotting rawwagevalues. The regression lines are calculated for logarithmic wages, so they'll never align with raw wage scatter points—this is the main reason your lines look meaningless.Typos in Variable Names
You wrotetenursqinstead of the correct variable nametenuresq(from thewage1dataset). This typo breaks the model fitting, leading to incorrect coefficients that further mess up your regression lines.Incorrect Plot Labels
Your x-axis label says "Class Size" and y-axis says "Test Score", but you're actually plottingeduc(years of education) andwage/log(wage). These labels are misleading and don't match your data.Potential Risks with
attach()
While not directly causing the line issue, usingattach(wage1)can lead to variable name conflicts. It's better to explicitly reference variables withdataset$variablefor clarity and safety.
Fixed Code (Matching Log-Wage Model)
Since your original model uses log(wage), we'll adjust the plot to use logarithmic wages too, so the lines align with the data:
library(wooldridge) # Fit the corrected model (fixed tenursq -> tenuresq, no attach()) MODEL3 <- lm(log(wage) ~ educ + female + female*educ + exper + expersq + tenure + tenuresq, data = wage1) summary(MODEL3) # Split data by gender for plotting male_data <- subset(wage1, female == 0) female_data <- subset(wage1, female == 1) # Create scatter plot with log(wage) as y-axis (matches model's dependent variable) plot(male_data$educ, log(male_data$wage), pch = 20, col = "red", main = "Log(Wage) vs Years of Education by Gender", xlab = "Years of Education", ylab = "Log(Wage)") points(female_data$educ, log(female_data$wage), pch = 20, col = "green") # Extract coefficients and draw regression lines coefs <- MODEL3$coefficients # Male regression line: intercept = coefs[1], slope = coefs[2] abline(coefs[1], coefs[2], col = "red", lwd = 1.5) # Female regression line: intercept = coefs[1] + coefs[3], slope = coefs[2] + coefs[4] abline(a = coefs[1] + coefs[3], b = coefs[2] + coefs[4], col = "green", lwd = 1.5) # Add legend for clarity legend("topleft", legend = c("Male", "Female"), col = c("red", "green"), pch = 20, lwd = 1.5)
If You Want Raw Wage Regression Lines
If you specifically want to plot raw wages instead of log wages, you'll need to refit the model with wage as the dependent variable:
# Model with raw wage MODEL3_raw <- lm(wage ~ educ + female + female*educ + exper + expersq + tenure + tenuresq, data = wage1) # Plot raw wages plot(male_data$educ, male_data$wage, pch = 20, col = "red", main = "Wage vs Years of Education by Gender", xlab = "Years of Education", ylab = "Wage") points(female_data$educ, female_data$wage, pch = 20, col = "green") # Draw lines using raw wage model coefficients coefs_raw <- MODEL3_raw$coefficients abline(coefs_raw[1], coefs_raw[2], col = "red", lwd = 1.5) abline(a = coefs_raw[1] + coefs_raw[3], b = coefs_raw[2] + coefs_raw[4], col = "green", lwd = 1.5)
内容的提问来源于stack exchange,提问作者Tatanik501

