So I'm trying to run multiple models for multiple different variables within a data-set so that I can get the residuals for each model.
Reprex isn't working for this example, so I'm going to do my best to show what's going on.
mydata <- data %>%
mutate(is_usa = if_else(team == "USA", 1, 0)) %>%
nest(-league) %>%
mutate(model_all = map(data, ~lm(betweenness_all ~ is_usa * gp, data = .))) %>%
mutate(model_5v5 = map(data, ~lm(betweenness_5v5 ~ is_usa * gp, data = .))) %>%
mutate(model_ev = map(data, ~lm(betweenness_ev ~ is_usa * gp, data = .))) %>%
mutate(model_pp = map(data, ~lm(betweenness_pp ~ is_usa * gp, data = .))) %>%
mutate(model_pk = map(data, ~lm(betweenness_pk ~ is_usa * gp, data = .))) %>%
mutate(model_all_resid = map(model_all, broom::augment)) %>%
mutate(model_5v5_resid = map(model_5v5, broom::augment)) %>%
mutate(model_ev_resid = map(model_ev, broom::augment)) %>%
mutate(model_pp_resid = map(model_pp, broom::augment)) %>%
mutate(model_pk_resid = map(model_pk, broom::augment))
mydata
which looks like:
Now I can unnest 1 model's residuals perfectly fine
mydata %>%
unnest(model_all_resid)
But trying to unnest multiple models won't work
mydata %>%
unnest(model_all_resid, model_5v5_resid)
Any idea what I'm doing wrong?
For whatever it's worth, I'm largely basing this all on @alistaire's stack-exchange answer here, though I don't get the same issue with mtcars
data