rplotregressionabline

Abline error in R for multiple regression with interaction variable


I want two different lines for men and women in the same plot but I always get non-sense lines when I run this code. What is the problem and how can I solve it

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 )

Solution

  • Your model predicts log(wages), but you are plotting the untransformed wages as the points on your plot with the abline values representing the predicted log(wages), which therefore don't match. If you use log(wages) for the y axis of your plot, the lines make more sense:

    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 )
    

    Created on 2021-08-19 by the reprex package (v2.0.0)