使用tidyverse和broom进行许多回归:相同的因变量,不同的自变量 [英] Many regressions using tidyverse and broom: Same dependent variable, different independent variables

查看:43
本文介绍了使用tidyverse和broom进行许多回归:相同的因变量,不同的自变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此链接显示在我们具有相同的自变量但可能存在许多不同因变量的情况下如何回答我的问题:

This link shows how to answer my question in the case where we have the same independent variables, but potentially many different dependent variables: Use broom and tidyverse to run regressions on different dependent variables.

但是我的问题是,我如何应用相同的方法(例如tidyverse和broom)来进行许多回归,但情况却相反:因变量相同而自变量不同.与上一个链接中的代码一致,类似:

But my question is, how can I apply the same approach (e.g., tidyverse and broom) to run many regressions where we have the reverse situation: same dependent variables but different independent variable. In line with the code in the previous link, something like:

mod = lm(health ~ cbind(sex,income,happiness) + faculty, ds) %>% tidy()

但是,这段代码并不能完全满足我的要求,而是产生:

However, this code does not do exactly what I want, and instead, produces:

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)

推荐答案

基本上,您需要某种方式来创建所需的所有不同公式.这是一种方法

Basically you'll need some way to create all the different formulas you want. Here's one way

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()

Once you have all your formula, you can map them to lm and then to 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))

这篇关于使用tidyverse和broom进行许多回归:相同的因变量,不同的自变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆