I try to calculate a stepwise selection for a vast amount of Poisson regressions based on Chapter 20 "Many models with purrr and broom" in the book “R for Data Science” by Wickham and Grolemund (https://r4ds.had.co.nz/). However using this approach, the calculated models lack a meaningful name, which seems to be problematic when applying the step function. A reproducible example follows below.
library(tidyverse)
library(gapminder)
# Create tibble with variables and models for each corresponding country
by_country <- gapminder %>%
group_by(country, continent) %>%
nest()
# model-fitting function:
country_model <- function(df) {
lm(lifeExp ~ year + pop + gdpPercap, data = df)
}
# apply country_model to each element:
by_country <- by_country %>%
mutate(model = map(data, country_model))
# Stepwise selection
step_select <- mutate(by_country, step_sel = map(model, step))
# This fails
# It works fine if applied to individually calculated models (in this case for Germany), but not if you extract the same model from the tibble
# Individually calculated model
ge <- filter(gapminder, country == "Germany")
ge_mod <- lm(lifeExp ~ year + pop + gdpPercap, data = ge)
step(ge_mod)
# Extract the same model from the tibble
tb_ge_mod <- by_country[[4]][[48]]
step(tb_ge_mod)
# The only difference I can spot between these models is the name of data, which is generic in the tibble, but specific in the individually calculated model:
ge_mod[["call"]]
tb_ge_mod[["call"]]
ge_mod[["call"]][["data"]]
tb_ge_mod[["call"]][["data"]]
# If you replace these names, it works.
tb_ge_mod[["call"]][["data"]] <- ge
tb_ge_mod[["call"]] <- lm(formula = lifeExp ~ year + pop + gdpPercap, data = ge)
step(tb_ge_mod)
However, I have found no way to adjust the names automatically (and this would be only a workaround, anyway). So, is there a way how to apply the stepwise selection here?
question from:
https://stackoverflow.com/questions/66045700/stepwise-selection-for-many-models-in-r 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…