strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
609 stars 91 forks source link

plot_model type = "int" errors when scale() is included in model formula #940

Open MikeDTaylor opened 5 months ago

MikeDTaylor commented 5 months ago

Hi Daniel,

I am having issues plotting interactions when including scaling/transformation/polynomials in models.

plot_model(model, type = "int") works as expected when scaling is not included in the model formula, but when scaling is included I get the following error.

model <- lme4::lmer(mpg ~ scale(hp) * wt + (1 | cyl), data = mtcars)
plot_model(model, type = "int")

Error: Some of the specified terms were not found in the model. Maybe misspelled?

I get the same error if I try to define the terms myself. plot_model(model, type = "pred") works as expected, however, which makes me think my issues may be due to how insight::find_predictors() returns predictors cleaned of scaling/transformation while insight::find_interactions() returns model interactions including scaling/transformation.

Is there another way to use type = "int" when scale or transformations are included in the model formula?

I have a reproducible example below with some notes on the outputs that I am getting. I am using R version 4.4.0, sjPLot 2.8.16, and insight 0.19.11.

Thanks for your help!

data("mtcars")

## Model without scaling predictors
model <- lme4::lmer(mpg ~ hp * wt + (1 | cyl), data = mtcars)

plot_model(model, type = "pred")  
plot_model(model, type = "int")
plot_model(model, type = "int", terms = c("hp", "wt"))
# All work as expected

insight::find_predictors(model, component = "conditional", flatten = TRUE)
# Returns: "hp" "wt"
insight::find_interactions(model, component = "conditional", flatten = TRUE)
# Returns "hp:wt"

# Model with scaled predictor
model <- lme4::lmer(mpg ~ scale(hp) * wt + (1 | cyl), data = mtcars)

plot_model(model, type = "pred") 
# Works as expected
plot_model(model, type = "int")
# Error: Some of the specified `terms` were not found in the model. Maybe misspelled?
plot_model(model, type = "int", terms = c("hp", "wt"))
# Error: Some of the specified `terms` were not found in the model. Maybe misspelled?
plot_model(model, type = "int", terms = c("scale(hp)", "wt"))
# Error: Some of the specified `terms` were not found in the model. Maybe misspelled?

insight::find_predictors(model, component = "conditional", flatten = TRUE)
# Returns: "hp" "wt"
insight::find_interactions(model, component = "conditional", flatten = TRUE)
# Returns: "scale(hp):wt"
strengejacke commented 5 months ago

Thanks, probably the scale() function is not properly removed from the term-names. I'll take a look. Alternatively, you could use the ggeffects package directly, which is internally used by sjPlot. Using ggeffects for predictions is more flexible, and the plot() method is easy to use and highly customizable (see, e.g., here and here).

model <- lme4::lmer(mpg ~ scale(hp) * wt + (1 | cyl), data = mtcars)
ggeffects::predict_response(model, terms = c("hp", "wt"))
#> # Predicted values of mpg
#> 
#> wt: 2.24
#> 
#>  hp | Predicted |       95% CI
#> ------------------------------
#>  50 |     28.52 | 26.90, 30.13
#>  95 |     25.92 | 24.78, 27.06
#> 145 |     23.03 | 21.64, 24.42
#> 195 |     20.15 | 17.97, 22.33
#> 240 |     17.55 | 14.52, 20.58
#> 335 |     12.07 |  7.13, 17.00
#> 
#> wt: 3.22
#> 
#>  hp | Predicted |       95% CI
#> ------------------------------
#>  50 |     21.83 | 20.00, 23.66
#>  95 |     20.46 | 19.15, 21.77
#> 145 |     18.94 | 17.92, 19.96
#> 195 |     17.42 | 16.17, 18.66
#> 240 |     16.05 | 14.31, 17.78
#> 335 |     13.16 | 10.11, 16.20
#> 
#> wt: 4.2
#> 
#>  hp | Predicted |       95% CI
#> ------------------------------
#>  50 |     15.14 | 11.77, 18.51
#>  95 |     15.00 | 12.54, 17.46
#> 145 |     14.84 | 13.28, 16.41
#> 195 |     14.69 | 13.57, 15.81
#> 240 |     14.55 | 13.05, 16.04
#> 335 |     14.25 | 10.96, 17.53
#> 
#> Adjusted for:
#> * cyl = 0 (population-level)
#> 
#> Not all rows are shown in the output. Use `print(..., n = Inf)` to show
#>   all rows.

Created on 2024-05-27 with reprex v2.1.0