vincentarelbundock / marginaleffects

R package to compute and plot predictions, slopes, marginal means, and comparisons (contrasts, risk ratios, odds, etc.) for over 100 classes of statistical and ML models. Conduct linear and non-linear hypothesis tests, or equivalence tests. Calculate uncertainty estimates using the delta method, bootstrapping, or simulation-based inference
https://marginaleffects.com
Other
392 stars 43 forks source link

plot_predictions() won't accept mida object #1096

Closed mattjamesmartin closed 2 months ago

mattjamesmartin commented 2 months ago

Bug reports must include:

  1. I am running a logit model using data imputed from the mice package. I am trying to then use plot_predictions() to visualize my pooled results, but it is not working seemingly due to a bug. It won't accept the mida object produced by my model. Below is an example using mtcars:

  2. 
    data(mtcars)
    set.seed(123)

Create random missing values

mtcars_with_missing <- mtcars missing_prop <- 0.1 # Proportion of missing values n_missing <- round(nrow(mtcars) * missing_prop)
rows_to_replace <- sample(1:nrow(mtcars), size = n_missing, replace = FALSE)

Replace values with NA

mtcars_with_missing[rows_to_replace, ] <- NA

Impute data

mtcars_imp <- mice(mtcars_with_missing, m = 5, method = "pmm", seed = 123)

Create mida object

mtcars_imp_logit <- with(data = mtcars_imp, exp = glm(am ~ cyl + hp + wt, family = "binomial"))

plot_predictions(mtcars_imp_logit, condition = "hp")



4. Output: `plot_predictions(mtcars_imp_logit, condition = "hp") Error: Entries in the "condition" argument must be element of:`
vincentarelbundock commented 2 months ago

Try specifying the type and newdata arguments explicitly:

plot_predictions(
    mtcars_imp_logit, 
    condition = "hp", 
    type = "response",
    newdata = mtcars_with_missing)
mattjamesmartin commented 2 months ago

That works! Thank you so much for your fast reply.