Plotting a Regression Line Without Extracting Each Coefficient Separately

时间:2018-02-26 17:33:45

标签: r plot

For my Stats class, we are using R to compute all of our statistics, and we are working with numeric data that also has a categorical factor. The way we currently are plotting fitted lines is with lm() and then looking at the summary to grab the coefficients manually, create a mesh, and then use the lines() function. I am wanting a way to do this easier. I have seen the predict() function, but not how to use this along with categories.

For example, the data set found here has 2 numerical variables, and one categorical. I want to be able plot the line of best fit for men and women in this set without having to extract each coefficient individually, as below in my current code.

bank<-read.table("http://www.uwyo.edu/crawford/datasets/bank.txt",header=TRUE)

fit <-lm(salary~years*gender,data=bank)
summary(fit)

yearhat<-seq(0,max(bank$salary),length=1000)
salaryfemalehat=fit$coefficients[1]+fit$coefficients[2]*yearhat
salarymalehat=(fit$coefficients[1]+fit$coefficients[3])+(fit$coefficients[2]+fit$coefficients[4])*yearhat

2 个答案:

答案 0 :(得分:1)

Using what you have, you can get the same predicted values with

yearhat<-seq(0,max(bank$salary),length=1000)
salaryfemalehat <- predict(fit, data.frame(years=yearhat, gender="Female"))
salarymalehat <- predict(fit, data.frame(years=yearhat, gender="Male"))

答案 1 :(得分:0)

To supplement MrFlick, in case of more levels we can try:

dat <- mtcars 
dat$cyl <- as.factor(dat$cyl)
fit <- lm(mpg ~ disp*cyl, data = dat)

plot(dat$disp, dat$mpg)
with(dat,
  for(i in levels(cyl)){
      lines(disp, predict(fit, newdata = data.frame(disp = disp, cyl = i))
            , col = which(levels(cyl) == i))
  }
)

enter image description here