Jack
Jack

Reputation: 857

How to run ggpredict() in a loop following multiple regression models?

The aim is to get the output of the predicted probabilities of several regression models. First i run several regression models using the following code:

library(dplyr)
library(tidyr)
library(broom)
library(ggeffects)

mtcars$cyl=as.factor(mtcars$cyl)

df <-  mtcars %>%
  group_by(cyl) %>%
  do(model1 = tidy(lm(mpg  ~ wt + gear + am , data = .),  conf.int=TRUE)) %>%   
  gather(model_name, model, -cyl) %>%                        ## make it long format
  unnest() 

I would like to get the predicted probabilities of my predictor weight (wt). If i want to run the code manually for each different cylinder (cyl), it will look as the following:

#Filter by number of cylinders
df=filter(mtcars, cyl==4)
#Save the regression
mod= lm(mpg  ~ wt + gear + am, data = df)
#Run the predictive probabilities
pred <- ggpredict(mod, terms = c("wt"))

This will be the code for only the first cylinder cyl==4, then we would have to run the same code for the second (cyl==6) and the third (cyl==8). This is a bit cumbersome. My aim is to automize that as i do for the regression analyses in the first code above. Also, I would like to get these results in the same format as the first code. In other words, they should be in a format that could be plotted afterwards. Can someone help me with that?

Upvotes: 4

Views: 313

Answers (1)

Emmanuel
Emmanuel

Reputation: 132

Rerun the models with ggpredict() on the inside:

df <- mtcars %>%
  group_by(cyl) %>%
  do(model1 = ggpredict(lm(mpg ~ wt + gear + am, data= .), terms = c("wt"))) %>%
  gather(model_name, model, -cyl) %>% unnest_legacy()

You can then plot wt (in the 'x' column) against 'predicted'. Note that you'll get a warning message on these data.

Upvotes: 1

Related Questions