问题描述
我正在寻找可以解决这个难题的Tidyverse /扫帚解决方案:
I'm looking for a Tidyverse / broom solution that can solve this puzzle:
假设我有不同的DV 和特定集,我想进行回归分析,考虑每个DV和该特定IV的集合。
我知道我可以在自己的家庭中使用类似的东西,也可以申请家庭,但我真的想使用 tidyverse 来运行它。
Let's say I have different DVs and a specific set of IVS and I want to perform a regression that considers every DV and this specific set of IVs.I know I can use something like for i in or apply family, but I really want to run that using tidyverse.
以下代码作为示例
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))
mod1 <- lm(income ~ sex + faculty, ds)
mod2 <- lm(happiness ~ sex + faculty, ds)
mod3 <- lm(health ~ sex + faculty, ds)
summary(mod1)
summary(mod2)
summary(mod3)
收入,幸福和健康是DV。 Sex and Faculty是IV,它们将用于所有回归。
Income, happiness, and health are DVs. Sex and Faculty are IVs and they will be used for all regressions.
是我发现的最接近的
让我知道是否需要澄清我的问题。
谢谢。
Let me know If I need to clarify my question.Thanks.
推荐答案
由于您有不同的因变量,但有相同的独立变量,因此可以形成这些变量的矩阵,传递给 lm
。
As you have different dependent variables but the same independent, you can form a matrix of these and pass to lm
.
mod = lm(cbind(income, happiness, health) ~ sex + faculty, ds)
我认为扫帚: :tidy
作品
library(broom)
tidy(mod)
# response term estimate std.error statistic p.value
# 1 income (Intercept) 1019.35703873 31.0922529 32.7849205 2.779199e-54
# 2 income sex -54.40337314 40.1399258 -1.3553431 1.784559e-01
# 3 income faculty 19.74808081 17.9511206 1.1001030 2.740100e-01
# 4 happiness (Intercept) 5.97334562 0.1675340 35.6545278 1.505026e-57
# 5 happiness sex 0.05345555 0.2162855 0.2471528 8.053124e-01
# 6 happiness faculty -0.02525431 0.0967258 -0.2610918 7.945753e-01
# 7 health (Intercept) 19.76489553 0.5412676 36.5159396 1.741411e-58
# 8 health sex 0.32399380 0.6987735 0.4636607 6.439296e-01
# 9 health faculty 0.10808545 0.3125010 0.3458723 7.301877e-01
这篇关于使用broom和tidyverse对不同的因变量进行回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!