R 中的组回归
Regression in R with Groups
我导入了一个包含 3 列的 CSV,其中 2 列用于 Y 和 X,第三列标识 X 的类别(我有 20 groups/categories)。我能够 运行 总体水平的回归,但我想 运行 分别对 20 个类别进行回归并存储系数。
我尝试了以下方法:
list2env(split(sample, sample$CATEGORY_DESC), envir = .GlobalEnv)
现在我有 20 个文件,我如何 运行 对这 20 个文件进行回归并将协同效应存储在某处。
由于没有提供数据,我正在生成一些示例数据来展示如何使用 dplyr
和 broom
包 运行 多元回归和存储输出。
下面有20组,每组有不同的x/y值。 20 个回归是 运行,这些回归的输出作为数据框提供:
library(dplyr)
library(broom)
df <- data.frame(group = rep(1:20, 10),
x = rep(1:20, 10) + rnorm(200),
y = rep(1:20, 10) + rnorm(200))
df %>% group_by(group) %>% do(tidy(lm(x ~ y, data = .)))
示例输出:
Source: local data frame [40 x 6]
Groups: group [20]
group term estimate std.error statistic p.value
<int> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 (Intercept) 0.42679228 1.0110422 0.4221310 0.684045203
2 1 y 0.45625124 0.7913256 0.5765657 0.580089051
3 2 (Intercept) 1.99367392 0.4731639 4.2134955 0.002941805
4 2 y 0.05101438 0.1909607 0.2671460 0.796114398
5 3 (Intercept) 3.14391308 0.8417638 3.7349114 0.005747126
6 3 y 0.08418715 0.2453441 0.3431391 0.740336702
使用 lmList(包 nlme)的快速解决方案:
library(nlme)
lmList(x ~ y | group, data=df)
Call:
Model: x ~ y | group
Data: df
Coefficients:
(Intercept) y
1 0.4786373 0.04978624
2 3.5125369 -0.94751894
3 2.7429958 -0.01208329
4 -5.2231576 2.24589181
5 5.6370824 -0.24223131
6 7.1785581 -0.08077726
7 8.2060808 -0.18283134
8 8.9072851 -0.13090764
9 10.1974577 -0.18514527
10 6.0687105 0.37396911
11 9.0682622 0.23469187
12 15.1081915 -0.29234452
13 17.3147636 -0.30306692
14 13.1352411 0.05873189
15 6.4006623 0.57619151
16 25.4454182 -0.59535396
17 22.0231916 -0.30073768
18 27.7317267 -0.54651597
19 10.9689733 0.45280604
20 23.3495704 -0.14488522
Degrees of freedom: 200 total; 160 residual
Residual standard error: 0.9536226
从@Gopala 的回答中借用了 df 的数据。
也考虑一个基本的解决方案 lapply()
:
regressionList <- lapply(unique(df$group),
function(x) lm(x ~ y, df[df$group==x,]))
并且只有系数:
coeffList <- lapply(unique(df$group),
function(x) lm(x ~ y, df[df$group==x,])$coefficients)
偶数摘要列表:
summaryList <- lapply(unique(df$group),
function(x) summary(lm(x ~ y, df[df$group==x,])))
我导入了一个包含 3 列的 CSV,其中 2 列用于 Y 和 X,第三列标识 X 的类别(我有 20 groups/categories)。我能够 运行 总体水平的回归,但我想 运行 分别对 20 个类别进行回归并存储系数。
我尝试了以下方法:
list2env(split(sample, sample$CATEGORY_DESC), envir = .GlobalEnv)
现在我有 20 个文件,我如何 运行 对这 20 个文件进行回归并将协同效应存储在某处。
由于没有提供数据,我正在生成一些示例数据来展示如何使用 dplyr
和 broom
包 运行 多元回归和存储输出。
下面有20组,每组有不同的x/y值。 20 个回归是 运行,这些回归的输出作为数据框提供:
library(dplyr)
library(broom)
df <- data.frame(group = rep(1:20, 10),
x = rep(1:20, 10) + rnorm(200),
y = rep(1:20, 10) + rnorm(200))
df %>% group_by(group) %>% do(tidy(lm(x ~ y, data = .)))
示例输出:
Source: local data frame [40 x 6]
Groups: group [20]
group term estimate std.error statistic p.value
<int> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 (Intercept) 0.42679228 1.0110422 0.4221310 0.684045203
2 1 y 0.45625124 0.7913256 0.5765657 0.580089051
3 2 (Intercept) 1.99367392 0.4731639 4.2134955 0.002941805
4 2 y 0.05101438 0.1909607 0.2671460 0.796114398
5 3 (Intercept) 3.14391308 0.8417638 3.7349114 0.005747126
6 3 y 0.08418715 0.2453441 0.3431391 0.740336702
使用 lmList(包 nlme)的快速解决方案:
library(nlme)
lmList(x ~ y | group, data=df)
Call:
Model: x ~ y | group
Data: df
Coefficients:
(Intercept) y
1 0.4786373 0.04978624
2 3.5125369 -0.94751894
3 2.7429958 -0.01208329
4 -5.2231576 2.24589181
5 5.6370824 -0.24223131
6 7.1785581 -0.08077726
7 8.2060808 -0.18283134
8 8.9072851 -0.13090764
9 10.1974577 -0.18514527
10 6.0687105 0.37396911
11 9.0682622 0.23469187
12 15.1081915 -0.29234452
13 17.3147636 -0.30306692
14 13.1352411 0.05873189
15 6.4006623 0.57619151
16 25.4454182 -0.59535396
17 22.0231916 -0.30073768
18 27.7317267 -0.54651597
19 10.9689733 0.45280604
20 23.3495704 -0.14488522
Degrees of freedom: 200 total; 160 residual
Residual standard error: 0.9536226
从@Gopala 的回答中借用了 df 的数据。
也考虑一个基本的解决方案 lapply()
:
regressionList <- lapply(unique(df$group),
function(x) lm(x ~ y, df[df$group==x,]))
并且只有系数:
coeffList <- lapply(unique(df$group),
function(x) lm(x ~ y, df[df$group==x,])$coefficients)
偶数摘要列表:
summaryList <- lapply(unique(df$group),
function(x) summary(lm(x ~ y, df[df$group==x,])))