how to plot the linear regression in R?

前端 未结 5 1603
时光取名叫无心
时光取名叫无心 2020-12-14 23:43

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         


        
5条回答
  •  春和景丽
    2020-12-15 00:25

    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))
    

    enter image description here

    Then you can reformat the axes labels if you like.

提交回复
热议问题