Write a function to list all possible combinations of models

邮差的信 提交于 2021-02-08 11:59:49

问题


I'm attempting to write a function to run all possible regression models for variables in a dataset. I was able to get it to run each variable, this is what I have so far.

library(tidyverse)
library(broom)
data("mtcars")
model1 <- function (DATA) {
DATA %>%
  map(~lm(mpg ~ .x, data = DATA), tidy)%>%   map(summary) %>% 
  map_dbl("adj.r.squared") %>%
  tidy %>% 
  rename(adj.r.squared = x)
}

model1(mtcars) 

I am new to R and writing functions so I am sure there are some issues with it. I want a tibble of all the adjusted r squared values for all possible models. How do I write a function that will do the same thing for two, three, or more variables?


回答1:


I am not aware of any packages that allow one to automate this. So, let's try a brute force approach. The idea is to generate all possible combinations by hand and iterate over them.

vars <- names(mtcars)[-1]

models <- list()

for (i in 1:5){
  vc <- combn(vars,i)
  for (j in 1:ncol(vc)){
    model <- as.formula(paste0("mpg ~", paste0(vc[,j], collapse = "+")))
    models <- c(models, model)
  }
}

You can use these formulas for run the linear model.

lapply(models, function(x) lm(x, data = mtcars))


来源:https://stackoverflow.com/questions/58883747/write-a-function-to-list-all-possible-combinations-of-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!