这是一个简单的解决方案,使用ddply()
fromplyr
返回每个组的系数:
首先复制数据:
x <- read.table(text="
STA.NAME YEAR SUM.CDD
1 NAME1 1967 760
2 NAME1 1985 800
3 NAME1 1996 740
4 NAME1 2003 810
5 NAME1 2011 790
6 NAME2 1967 700
7 NAME2 1985 690
8 NAME2 1996 850
9 NAME2 2003 790
10 NAME3 1967 760
11 NAME3 1985 800
12 NAME3 1990 740
13 NAME3 1996 810
14 NAME3 2003 790
15 NAME3 2011 800 ", header=TRUE)
现在进行建模:
library(plyr)
ddply(x, .(STA.NAME), function(z)coef(lm(SUM.CDD ~ YEAR, data=z)))
STA.NAME (Intercept) YEAR
1 NAME1 -444.8361 0.6147541
2 NAME2 -6339.2047 3.5702200
3 NAME3 -995.2381 0.8928571
现在,根据您想要做什么,创建一个数据模型可能更简单(也可能更有意义):
fit <- lm(SUM.CDD ~ YEAR + STA.NAME, data=x)
获取摘要:
summary(fit)
Call:
lm(formula = SUM.CDD ~ YEAR + STA.NAME, data = x)
Residuals:
Min 1Q Median 3Q Max
-63.57 -22.21 10.72 18.62 80.72
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2065.6401 1463.5353 -1.411 0.1858
YEAR 1.4282 0.7345 1.945 0.0778 .
STA.NAMENAME2 -15.8586 27.5835 -0.575 0.5769
STA.NAMENAME3 3.9046 24.7089 0.158 0.8773
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 40.8 on 11 degrees of freedom
Multiple R-squared: 0.3056, Adjusted R-squared: 0.1162
F-statistic: 1.614 on 3 and 11 DF, p-value: 0.2424
只提取系数:
coef(fit)
(Intercept) YEAR STA.NAMENAME2 STA.NAMENAME3
-2065.640078 1.428247 -15.858650 3.904632
最后,您可能希望使用交互项拟合模型。此模型为您提供与原始解决方案有效相同的结果plyr
。根据您的数据和目标,这可能是执行此操作的方法:
fit <- lm(SUM.CDD ~ YEAR * STA.NAME, data=x)
summary(fit)
Call:
lm(formula = SUM.CDD ~ YEAR * STA.NAME, data = x)
Residuals:
Min 1Q Median 3Q Max
-57.682 -13.166 -1.012 23.006 63.046
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -444.8361 2280.7464 -0.195 0.850
YEAR 0.6148 1.1447 0.537 0.604
STA.NAMENAME2 -5894.3687 3661.9795 -1.610 0.142
STA.NAMENAME3 -550.4020 3221.8390 -0.171 0.868
YEAR:STA.NAMENAME2 2.9555 1.8406 1.606 0.143
YEAR:STA.NAMENAME3 0.2781 1.6172 0.172 0.867
Residual standard error: 39.17 on 9 degrees of freedom
Multiple R-squared: 0.4763, Adjusted R-squared: 0.1854
F-statistic: 1.637 on 5 and 9 DF, p-value: 0.2451