I am trying to fit nested models over which I would like to apply the step function and get a subset of potential predictors for each nested element.
I am currently running into the following error specifically when running the step function:
Error: Problem with
mutate()inputstep_null_to_full. x cannot coerce class ‘"lm"’ to a data.frame i Inputstep_null_to_fullismap2(...). i The error occurred in group 1: ID = 1.
Here is a generated code example:
library(tidyverse)
data <- data.frame(ID = rep(1:10,30),
Year = rep(1981:2010,10),
x1 = rnorm(300,0,1),
x2 = rnorm(300,0,1),
x3 = rnorm(300,0,1),
x4 = rnorm(300,0,1),
x5 = rnorm(300,0,1),
x6 = rnorm(300,0,1),
Y = rnorm(300,0,1))
model_pipe <- data %>%
group_by(ID) %>%
nest() %>%
mutate(mod_intercept = map(data,~ lm(Y ~ 1, data=.)),
mod_full = map(data,~ lm(Y ~ ., data=.)),
mod_full_int = map(data,~ lm(Y ~ .*., data=.)),
step_null_to_full = map2(mod_intercept,mod_full,
~ step(.x, scope = formula(.y), direction = 'forward',trace = 0)))