我正在运行几个回归模型,并希望将结果放在数据框中。我尝试了几种不成功的方法。
# Generate some fake data: three responses and eight covariates
df <- data.frame(y1 = rnorm(50), y2 = rnorm(50), y3 = rnorm(50),
aa = rnorm(50), ab = rnorm(50), ca = rnorm(50),
cd = rpois(50, 30), v8 = runif(50))
# Create a vector of covariate names
xs <- c("aa","ab", "ca","cd","v8")
# Initialize a list whose length is that of the vector xs
rl1 <- vector('list', 5)
rl2 <- vector('list', 5)
rl3 <- vector('list', 5)
for(i in 1:5) { fm1<- as.formula(paste('y1', xs[i], sep = '~'))
fm2 <- as.formula(paste('y2', xs[i], sep = '~'))
fm3 <- as.formula(paste('y3', xs[i], sep = '~'))
rl1[[i]] <- summary(lm(fm1, data = df) )
rl2[[i]] <- summary(lm(fm2, data = df) )
rl3[[i]] <- summary(lm(fm3, data = df) )
}
rl1
rl2
###
#The goal is to:
#create a data frame for results that looks like this
Variable t value Pr(>|t|)
aa -1.032 0.3070
ab -0.121 0.9043
ca 0.041 0.967
cd 0.429 0.670
v8 -1.204 0.2346
and create a data frame for results that looks like this
Variable rl1.t value r2.t value
aa -1.032 -1.846
ab -0.121 -0.230
ca 0.041 1.347
cd 0.429 0.211
v8 -1.204 0.214
最后,我发现这个代码以这种方式运行回归,我想表示赞赏并表示感谢,尽管我不记得来源。
答案 0 :(得分:1)
您提到的方法使用了许多软件包(dplyr
,tidyr
,purrr
和broom
)。所有这些都方便地位于tidyverse
元数据包中,这有助于安装所有这些软件包。
tidyverse
包具有基于整洁数据和整洁数据工具的工作流程。您可以阅读更多相关信息here。
您的示例,使用tidyverse
软件包如下:
library(tidyverse)
library(broom) # is part of the tidyverse but has to be attached on its own.
df <- data.frame(y1 = rnorm(50), y2 = rnorm(50), y3 = rnorm(50),
aa = rnorm(50), ab = rnorm(50), ca = rnorm(50),
cd = rpois(50, 30), v8 = runif(50))
xs <- c("aa","ab", "ca","cd","v8")
results <- tibble(covariates = xs, data = list(df)) %>%
mutate(fm1 = map2(data, covariates, ~lm(as.formula(paste('y1', .y, sep = '~')),data = .x)),
fm2 = map2(data, covariates, ~lm(as.formula(paste('y2', .y, sep = '~')),data = .x)),
fm3 = map2(data, covariates, ~lm(as.formula(paste('y3', .y, sep = '~')),data = .x))) %>%
gather(model, result, fm1:fm3) %>%
mutate(stats = map(result, tidy))
results %>%
select(-data, -result) %>%
spread(model, stats) %>%
unnest(.sep = "_")
# A tibble: 10 × 16
covariates fm1_term fm1_estimate fm1_std.error fm1_statistic fm1_p.value fm2_term fm2_estimate
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
1 aa (Intercept) 0.186250282 0.10901934 1.7084150 0.09401936 (Intercept) -0.13702761
2 aa aa -0.122391949 0.09736266 -1.2570727 0.21480983 aa 0.07086972
3 ab (Intercept) 0.235819869 0.11000625 2.1436951 0.03715205 (Intercept) -0.07707552
4 ab ab 0.210060898 0.11165387 1.8813580 0.06599560 ab 0.26121398
5 ca (Intercept) 0.192849006 0.11315134 1.7043458 0.09478244 (Intercept) -0.13487163
6 ca ca 0.028244577 0.11597189 0.2435467 0.80861978 ca 0.01340660
7 cd (Intercept) 0.336090343 0.67322033 0.4992279 0.61989940 (Intercept) -1.50990351
8 cd cd -0.004866459 0.02170098 -0.2242506 0.82351414 cd 0.04484769
9 v8 (Intercept) 0.037664728 0.18697545 0.2014421 0.84120415 (Intercept) 0.12866580
10 v8 v8 0.353757262 0.35827371 0.9873939 0.32840091 v8 -0.62992103
# ... with 8 more variables: fm2_std.error <dbl>, fm2_statistic <dbl>, fm2_p.value <dbl>, fm3_term <chr>,
# fm3_estimate <dbl>, fm3_std.error <dbl>, fm3_statistic <dbl>, fm3_p.value <dbl>
在此示例中,步骤如下:
broom::tidy
映射到结果列进行整理。然后,您可以使用results
tibble通过unnest
获取所需内容,并通过dplyr
和tidyr
将其操作为您想要的内容。