You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Mismatched Dependent Variable
    Your model MODEL3 uses log(wage) as the dependent variable, but you're plotting raw wage values. 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.

  2. Typos in Variable Names
    You wrote tenursq instead of the correct variable name tenuresq (from the wage1 dataset). This typo breaks the model fitting, leading to incorrect coefficients that further mess up your regression lines.

  3. Incorrect Plot Labels
    Your x-axis label says "Class Size" and y-axis says "Test Score", but you're actually plotting educ (years of education) and wage/log(wage). These labels are misleading and don't match your data.

  4. Potential Risks with attach()
    While not directly causing the line issue, using attach(wage1) can lead to variable name conflicts. It's better to explicitly reference variables with dataset$variable for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 06:08:12