R中的线性回归和分组依据 [英] Linear Regression and group by in R
问题描述
我想使用lm()
函数在R中进行线性回归.我的数据是一个年度时间序列,其中一个字段表示年份(22年),另一个字段表示州(50个州).我想为每个状态拟合回归,以便最后获得lm响应向量.我可以想象对每个状态进行for循环,然后在循环内进行回归并将每个回归的结果添加到向量中.但是,这似乎不太像R.在SAS中,我将执行"by"语句,而在SQL中,我将执行"group by". R做这件事的方式是什么?
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 states). I want to fit a regression for each state so that at the end I have a vector of lm responses. I can imagine doing for loop for each state then doing the regression inside the loop and adding the results of each regression to a vector. That does not seem very R-like, however. In SAS I would do a 'by' statement and in SQL I would do a 'group by'. What's the R way of doing this?
推荐答案
这是使用lme4
包的一种方法.
Here's one way using the lme4
package.
library(lme4)
d <- data.frame(state=rep(c('NY', 'CA'), c(10, 10)),
year=rep(1:10, 2),
response=c(rnorm(10), rnorm(10)))
xyplot(response ~ year, groups=state, data=d, type='l')
fits <- lmList(response ~ year | state, data=d)
fits
#------------
Call: lmList(formula = response ~ year | state, data = d)
Coefficients:
(Intercept) year
CA -1.34420990 0.17139963
NY 0.00196176 -0.01852429
Degrees of freedom: 20 total; 16 residual
Residual standard error: 0.8201316
这篇关于R中的线性回归和分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!