如何使用PURR迭代lm reg中的每个协变量和结果的组合 [英] How to use purrr to iterate over every combo of covariates and outcomes in lm reg
本文介绍了如何使用PURR迭代lm reg中的每个协变量和结果的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对我来说,一种常见的情况是,我需要运行基本相同的回归模型,但运行一系列不同的结果,而对于敏感度分析,我需要同时迭代不同的协变量集。
我仍然是R的新手,但将下面的代码与Purrr一起使用,我能够迭代结果和协变量,但它当然会并行地遍历成对的列表,当我需要它来遍历每个列表中的每个组合时。
对于结果和协变量的所有组合如何迭代,有哪些选项?
另外,有人知道为什么以下代码不能与";map2一起使用吗?&我收到错误消息,";as_mapper(.f,...):参数&q;.f&q;缺失,没有默认&q;
library(dplyr)
library(purrr)
dataset <- tibble(
y1=rnorm(n=100),
y2=rnorm(n=100),
x1=rnorm(n=100),
x2=rnorm(n=100))
outcomes <- dataset %>%
select(y1,y2)
covars <- dataset %>%
select(x1,x2)
paramlist <- list(covarL,outcomeL)
paramlist %>%
pmap(~lm(.y ~ .x,data=dataset))
推荐答案
在更大的tidyVerse中有很多方法可以做到这一点。我是dplyr::rowwise
这种计算的粉丝。我们可以使用colnames
而不是实际数据,然后创建一个像tibble
WITHtidyr::expand_grid
这样的矩阵,它包含结果和Covar的所有组合。然后,我们可以使用dplyr::rowwise
和list()
内部的lm
以及接受字符串作为输入的reformulate
。要获得结果,我们可以使用broom::tidy
。
library(dplyr)
library(purrr)
library(tidyr)
dataset <- tibble(
y1=rnorm(n=100),
y2=rnorm(n=100),
x1=rnorm(n=100),
x2=rnorm(n=100))
outcomes <- dataset %>%
select(y1,y2) %>% colnames
covars <- dataset %>%
select(x1,x2) %>% colnames
paramlist <- expand_grid(outcomes, covars)
paramlist %>%
rowwise %>%
mutate(mod = list(lm(reformulate(outcomes, covars), data = dataset)),
res = list(broom::tidy(mod)))
#> # A tibble: 4 x 4
#> # Rowwise:
#> outcomes covars mod res
#> <chr> <chr> <list> <list>
#> 1 y1 x1 <lm> <tibble [2 x 5]>
#> 2 y1 x2 <lm> <tibble [2 x 5]>
#> 3 y2 x1 <lm> <tibble [2 x 5]>
#> 4 y2 x2 <lm> <tibble [2 x 5]>
由reprex package(v2.0.1)于2021-09-06创建
我们可以用{Purrr}代替dplyr::rowwise
做同样的事情:
paramlist %>%
mutate(mod = map2(outcomes, covars, ~ lm(reformulate(.y, .x), data = dataset)),
res = map(mod, broom::tidy))
#> # A tibble: 4 x 4
#> outcomes covars mod res
#> <chr> <chr> <list> <list>
#> 1 y1 x1 <lm> <tibble [2 x 5]>
#> 2 y1 x2 <lm> <tibble [2 x 5]>
#> 3 y2 x1 <lm> <tibble [2 x 5]>
#> 4 y2 x2 <lm> <tibble [2 x 5]>
由reprex package(v2.0.1)于2021-09-06创建
另一个纯粹的解决方案是使用嵌套的map
调用。由于它是嵌套的,因此我们需要flatten
结果,然后才能对其使用map(summary)
。
# outcomes and covars are the same strings as above
outcomes %>%
map(~ map(covars, function(.y) lm(reformulate(.y, .x), data = dataset))) %>%
flatten %>%
map(summary)
#> [[1]]
#>
#> Call:
#> lm(formula = reformulate(.y, .x), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.20892 -0.56744 -0.08498 0.55445 2.10146
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.0009328 0.0923062 -0.010 0.992
#> x1 -0.0809739 0.0932059 -0.869 0.387
#>
#> Residual standard error: 0.9173 on 98 degrees of freedom
#> Multiple R-squared: 0.007643, Adjusted R-squared: -0.002483
#> F-statistic: 0.7548 on 1 and 98 DF, p-value: 0.3871
#>
#>
#> [[2]]
#>
#> Call:
#> lm(formula = reformulate(.y, .x), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.11442 -0.59186 -0.08153 0.61642 2.10575
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.02048 0.09461 -0.216 0.829
#> x2 -0.05159 0.10805 -0.477 0.634
#>
#> Residual standard error: 0.9197 on 98 degrees of freedom
#> Multiple R-squared: 0.002321, Adjusted R-squared: -0.007859
#> F-statistic: 0.228 on 1 and 98 DF, p-value: 0.6341
#>
#>
#> [[3]]
#>
#> Call:
#> lm(formula = reformulate(.y, .x), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.3535 -0.7389 -0.2023 0.6236 3.8627
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.08178 0.10659 -0.767 0.445
#> x1 -0.08476 0.10763 -0.788 0.433
#>
#> Residual standard error: 1.059 on 98 degrees of freedom
#> Multiple R-squared: 0.006289, Adjusted R-squared: -0.003851
#> F-statistic: 0.6202 on 1 and 98 DF, p-value: 0.4329
#>
#>
#> [[4]]
#>
#> Call:
#> lm(formula = reformulate(.y, .x), data = dataset)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.4867 -0.7020 -0.1935 0.5869 3.7574
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.06575 0.10875 -0.605 0.547
#> x2 0.12388 0.12420 0.997 0.321
#>
#> Residual standard error: 1.057 on 98 degrees of freedom
#> Multiple R-squared: 0.01005, Adjusted R-squared: -5.162e-05
#> F-statistic: 0.9949 on 1 and 98 DF, p-value: 0.321
由reprex package(v2.0.1)于2021-09-06创建
这篇关于如何使用PURR迭代lm reg中的每个协变量和结果的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文