具有交互变量的多元回归的 R 中的 Abline 错误

Abline error in R for multiple regression with interaction variable

我想要在同一个情节中为男性和女性两条不同的台词,但是当我 运行 这段代码时,我总是得到无意义的台词。有什么问题,我该如何解决

library(wooldridge)

attach(wage1)

MODEL3=lm(log(wage)~educ+female+female*educ+exper+expersq+tenure+tenursq)
summary(MODEL3)

fix(wage1)
id <- wage1$female >= 1


plot(wage1$educ[!id], wage1$wage[!id],
     pch = 20,
     col = "red",
     main = "",
     xlab = "Class Size",
     ylab = "Test Score")


points(wage1$educ[id], wage1$wage[id],
     pch = 20,
     col = "green")


coefs <- MODEL3$coefficients


abline(coefs[1], coefs[2],
       col = "red",
       lwd = 1.5)


abline(coef = c(coefs[1] + coefs[3], coefs[2] + coefs[4]),
       col = "green", 
       lwd = 1.5 )

您的模型预测 log(wages),但您将未转换的 wages 绘制为图上的点,其中 abline 值表示预测的 log(wages),因此不匹配。如果您使用 log(wages) 作为绘图的 y 轴,则线条更有意义:

library(wooldridge)

attach(wage1)

MODEL3=lm(log(wage)~educ+female+female*educ+exper+expersq+tenure+tenursq)

id <- wage1$female >= 1


plot(wage1$educ[!id], log(wage1$wage[!id]),
     pch = 20,
     col = "red",
     main = "",
     xlab = "Class Size",
     ylab = "Log of Test Score")


points(wage1$educ[id], log(wage1$wage[id]),
     pch = 20,
     col = "green")


coefs <- MODEL3$coefficients


abline(coefs[1], coefs[2],
       col = "red",
       lwd = 1.5)


abline(coef = c(coefs[1] + coefs[3], coefs[2] + coefs[4]),
       col = "green", 
       lwd = 1.5 )

reprex package (v2.0.0)

于 2021-08-19 创建