此链接显示了在我们具有相同的自变量但可能有许多不同的因变量的情况下如何回答我的问题:Use broom and tidyverse to run regressions on different dependent variables .
但我的问题是,如何应用相同的方法(例如 tidyverse 和 broom)来运行许多回归,其中我们有相反的情况:相同的因变量但不同的自变量。与上一个链接中的代码一致,类似于:
mod = lm(health ~ cbind(sex,income,happiness) + faculty, ds) %>% tidy()
但是,这段代码并不完全符合我的要求,而是生成:
Call:
lm(formula = income ~ cbind(sex, health) + faculty, data = ds)
Coefficients:
(Intercept) cbind(sex, health)sex
945.049 -47.911
cbind(sex, health)health faculty
2.342 1.869
这相当于:
lm(formula = income ~ sex + health + faculty, data = ds)
最佳答案
基本上,您需要某种方法来创建您想要的所有不同公式。这是一种方法
qq <- expression(sex,income,happiness)
formulae <- lapply(qq, function(v) bquote(health~.(v)+faculty))
# [[1]]
# health ~ sex + faculty
# [[2]]
# health ~ income + faculty
# [[3]]
# health ~ happiness + faculty
获得所有公式后,您可以将它们映射到 lm
,然后映射到 tidy()
library(purrr)
library(broom)
formulae %>% map(~lm(.x, ds)) %>% map_dfr(tidy, .id="model")
# A tibble: 9 x 6
# model term estimate std.error statistic p.value
# <chr> <chr> <dbl> <dbl> <dbl> <dbl>
# 1 1 (Intercept) 19.5 0.504 38.6 1.13e-60
# 2 1 sex 0.755 0.651 1.16 2.49e- 1
# 3 1 faculty -0.00360 0.291 -0.0124 9.90e- 1
# 4 2 (Intercept) 19.8 1.70 11.7 3.18e-20
# 5 2 income -0.000244 0.00162 -0.150 8.81e- 1
# 6 2 faculty 0.143 0.264 0.542 5.89e- 1
# 7 3 (Intercept) 18.4 1.88 9.74 4.79e-16
# 8 3 happiness 0.205 0.299 0.684 4.96e- 1
# 9 3 faculty 0.141 0.262 0.539 5.91e- 1
使用示例数据
set.seed(11)
ds <- data.frame(income = rnorm(100, mean=1000,sd=200),
happiness = rnorm(100, mean = 6, sd=1),
health = rnorm(100, mean=20, sd = 3),
sex = c(0,1),
faculty = c(0,1,2,3))
关于r - 使用 tidyverse 和 broom : Same dependent variable, 不同自变量的许多回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61512343/