how to plot the linear regression in R?

不问归期 提交于 2019-12-03 09:49:49

问题


I want to make the following case of linear regression in R

year<-rep(2008:2010,each=4)
quarter<-rep(1:4,3)
cpi<-c(162.2,164.6,166.5,166.0,166.4,167.0,168.6,169.5,170.0,172.0,173.3,174.0)
plot(cpi,xaxt="n",ylab="CPI",xlab="")
axis(1,labels=paste(year,quarter,sep="C"),at=1:12,las=3)
fit<-lm(cpi~year+quarter)

I want to plot the line that shows the linear regression of the data that I process. I have tried with:

abline(fit)
abline(fit$coefficients[[1]],c(fit$coefficients[[2]],fit$coefficients[[3]]))

The problem is that my formula is of the form:

y=a+b*year+c*quarter

and not something simpler like:

y=a+b*year

so how I can draw that line that shows the linear regression?

Is it possible to draw the line with abline?


回答1:


Are you looking for the predict function?

E.g.: using lines(predict(fit)) will give:

You could also use this for predicting future data aligning with the calculated coefficients. E.g.

# plot the existing data with space for the predicted line
plot(c(cpi,rep(NA,12)),xaxt="n",ylab="CPI",xlab="",ylim=c(162,190))

# plot the future predictions as a line using the next 3 year periods
lines(13:24,
      predict(
        fit,
        newdata=data.frame(year=rep(c(2011,2012,2013),each=4),quarter=rep(1:4,3))
             )
     )

year<-rep(2008:2013,each=4)
axis(1,labels=paste(year,quarter,sep="C"),at=1:24,las=3)




回答2:


cpi<-c(162.2,164.6,166.5,166.0,166.4,167.0,168.6,169.5,170.0,172.0,173.3,174.0)
cpits <- ts(cpi, start=2008, frequency=4)
plot(decompose(cpits))




回答3:


Humbug. These are all reasonable solutions, but they don't do what you ask for. Now what you ask for is slightly cooler and completely impractical, but can be done using rgl.

f <- function(x, y, coefs){
  z <- coefs[1] + coefs[2] * x + coefs[3] * y
  z
}

x <- seq(from=min(year), to=max(year), length.out=100)
y <- seq(from=min(quarter), to=max(quarter), length.out=100)

z <- outer(x, y, f, coefs=coef(fit))

Now where the magic happens in rgl:

library(rgl)

persp3d(x, y, z, col="lightblue")

It isn't done justice here, but it's pretty and you can move it about.

And what the hell, let's add your original points

points3d(year, quarter, cpi, size=5, col="red")




回答4:


The error lies in the way you're data was formatted. Here is another option:

year<-seq(from=2008,to=2010.75,by=.25)
cpi<-c(162.2,164.6,166.5,166.0,166.4,167.0,168.6,169.5,170.0,172.0,173.3,174.0)
df <- data.frame(year,cpi)
plot(df)+abline(lm(df$cpi~df$year))

Then you can reformat the axes labels if you like.




回答5:


The Predict.Plot and TkPredict functions in the TeachingDemos package will plot the relationship between one of the predictors and the response variable conditioned on the values of the other predictors. Predict.Plot makes it fairly simple to see multiple lines from different conditions while TkPredict lets you interactively change the values being conditioned on (and will produce the Predict.Plot code to recreate the current plot).

These functions are general for regression models on multiple predictors, but will not be as good as decompose for a time series.



来源:https://stackoverflow.com/questions/14451782/how-to-plot-the-linear-regression-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!