如何通过组合R中的所有变量来修改这些dplyr代码以进行多元线性回归 [英] How can I modify these dplyr code for multiple linear regression by combination of all variables in R

查看:59
本文介绍了如何通过组合R中的所有变量来修改这些dplyr代码以进行多元线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有以下数据

ind1 <- rnorm(99)
ind2 <- rnorm(99)
ind3 <- rnorm(99)
ind4 <- rnorm(99)
ind5 <- rnorm(99)
dep <- rnorm(99, mean=ind1)
group <- rep(c("A", "B", "C"), each=33)
df <- data.frame(dep,group, ind1, ind2, ind3, ind4, ind5)

以下代码按组计算因变量和2个自变量之间的多元线性回归,这正是我想要做的.但我想一次针对所有独立变量组合对回归dep变量.那么如何在此代码中结合其他模型呢?

the following code is calculating multiple linear regression between dependend variable and 2 independent variables by group which is exactly what I want to do. But I want to regress dep variable against all combination pair of independent variables at once. So how can I combine other models in this code?

df %>% 
  nest(-group) %>% 
  mutate(fit = map(data, ~ lm(dep ~ ind1 + ind2, data = .)),
         results1 = map(fit, glance),
         results2 = map(fit, tidy)) %>% 
  unnest(results1) %>% 
  unnest(results2) %>% 
  select(group, term, estimate, r.squared, p.value, AIC) %>% 
  mutate(estimate = exp(estimate)) 

提前谢谢!

推荐答案

不是完整的答案.考虑在使用 lapply <进行初始构建后,使用 rapply 构建所有可能的线性公式组合./code>和 combn ,然后传递到您的整洁方法中:

Not a full tidy answer. Consider building all possible combinations of linear formulas with rapply after initial build with lapply and combn then pass into your tidy method:

indvar_list <- lapply(1:5, function(x) 
                 combn(paste0("ind", 1:5), x, , simplify = FALSE))

formulas_list <- rapply(indvar_list, function(x)
                   as.formula(paste("dep ~", paste(x, collapse="+"))))

run_model <- function(f) {    
    df %>% 
      nest(-group) %>% 
      mutate(fit = map(data, ~ lm(f, data = .)),
             results1 = map(fit, glance),
             results2 = map(fit, tidy)) %>% 
      unnest(results1) %>% 
      unnest(results2) %>% 
      select(group, term, estimate, r.squared, p.value, AIC) %>% 
      mutate(estimate = exp(estimate))
}

tibble_list <- lapply(formulas_list, run_model)

这篇关于如何通过组合R中的所有变量来修改这些dplyr代码以进行多元线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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