Linear Regression and group by in R

前端 未结 10 1274
抹茶落季
抹茶落季 2020-11-22 02:27

I want to do a linear regression in R using the lm() function. My data is an annual time series with one field for year (22 years) and another for state (50 sta

10条回答
  •  忘了有多久
    2020-11-22 02:55

    In my opinion is a mixed linear model a better approach for this kind of data. The code below given in the fixed effect the overall trend. The random effects indicate how the trend for each individual state differ from the global trend. The correlation structure takes the temporal autocorrelation into account. Have a look at Pinheiro & Bates (Mixed Effects Models in S and S-Plus).

    library(nlme)
    lme(response ~ year, random = ~year|state, correlation = corAR1(~year))
    

提交回复
热议问题