0

I am not clear how to follow this, please clarify

So, I have created 200+ models & I just want to put them in a list & pass to map(tidy), I know how to do it manually, its as below, however I cant manually add 200 names as below, looking for a better solution (I guess something with deparse, evaluate or {{}}). They are named model1, model2... model200

library(tidyverse)

model1 <- lm(Sepal.Width~Sepal.Length,data=iris)
model2 <- lm(Sepal.Width~Sepal.Length+Petal.Width,data=iris)
model3 <- lm(Sepal.Width~Sepal.Length+Petal.Length,data=iris)
model4 <- lm(Petal.Length~Sepal.Length,data=iris)
model5 <- lm(Petal.Width~Sepal.Length,data=iris)
model6 <- lm(Petal.Length~Sepal.Length+Petal.Width,data=iris)



list(model1,model2,model3,model4,model5,model6) %>% 
  map(tidy)
Vaibhav Singh
  • 1,159
  • 1
  • 10
  • 25
  • 1
    How did you create these 200 models in the first place? It's usually better to put them in a list from the start. Have 200 separate model objects in your global environment is usually not a good idea. If you have a list of different formulas `forms <- list(Sepal.Width~Sepal.Length, Sepal.Width~Sepal.Length+Petal.Width, Sepal.Width~Sepal.Length+Petal.Length, Petal.Length~Sepal.Length, Petal.Width~Sepal.Length, Petal.Length~Sepal.Length+Petal.Width)` you should even easily pipe that to `lm`: `forms |> lapply(lm, data=iris) |> purrr::map(broom::tidy)` – MrFlick Jun 23 '22 at 13:30
  • The duplicates point to using `mget(ls(pattern = '^model'))` to get a list of all the objects in the global environment that start with "model". Just make sure no other variables that you don't want to use also start with that string. The `pattern=` argument can take any regular expression so you can change that if you need it to be more specific. – MrFlick Jun 23 '22 at 13:33

0 Answers0