Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting back-transformed contrasts via test_predictions in models with log transformed responses #594

Open
gp-esteves opened this issue Oct 28, 2024 · 2 comments

Comments

@gp-esteves
Copy link

Hello all. Firstly, thank you for your work and dedication on this package, it certainly has helped me immensely.
I have a question on an issue I recently encountered in my work using ggeffects.

I have a model with a log-transformed outcome variable. When trying to get predictions using the model, and later conducting hypothesis tests, I realized that while I could back-transform the predictions (via ggpredict) to the original scale, the same could not be done with the contrasts obtained via test_predictions.

I noticed that one could use the scale argument in test_predictions to exponentiate the contrasts, ie:

test_predictions(model, ..., scale = "exp")

But this seems to return the ratio of the predicted responses in each level of the categorical factor (ie pred1/pred2 with 95CIs), instead of the mean difference of the responses on the original scale (ie pred1 - pred2 with 95CIs). I was wondering if this was somehow possible inside of the ggeffects/marginaleffects framework.

A reproducible example follows using mtcars:

library(ggeffects); library(tidyverse)

dat <- mtcars |> mutate(am_cat = as.factor(am))

m2 <- lm(log(disp) ~ am_cat, dat)

# predictions on the log scale

ggpredict(m2, terms="am_cat", back_transform=FALSE) 
#> Model has log transformed response. Predictions are on transformed
#>   scale.
#> # Predicted values of disp
#> 
#> am_cat | Predicted |     95% CI
#> -------------------------------
#> 0      |      5.59 | 5.38, 5.81
#> 1      |      4.83 | 4.57, 5.09

test_predictions(m2, "am_cat") 
#> # Pairwise comparisons
#> 
#> am_cat | Contrast |     95% CI |      p
#> ---------------------------------------
#> 0-1    |     0.76 | 0.42, 1.10 | < .001

# predictions on the original response scale

ggpredict(m2, terms="am_cat", back_transform=TRUE) 
#> Model has log-transformed response. Back-transforming predictions to
#>   original response scale. Standard errors are still on the transformed
#>   scale.
#> # Predicted values of disp
#> 
#> am_cat | Predicted |         95% CI
#> -----------------------------------
#> 0      |    268.60 | 216.77, 332.81
#> 1      |    125.73 |  97.02, 162.93

test_predictions(m2, "am_cat", scale="exp") # this returns the ratio
#> # Pairwise comparisons
#> 
#> am_cat | Contrast |     95% CI |      p
#> ---------------------------------------
#> 0-1    |     2.14 | 1.53, 2.99 | < .001
#> 
#> Contrasts are presented on the exponentiated scale.

# as shown by

ggpredict(m2, terms="am_cat", back_transform=TRUE)$predicted[1]/
  ggpredict(m2, terms="am_cat", back_transform=TRUE)$predicted[2]
#> Model has log-transformed response. Back-transforming predictions to
#>   original response scale. Standard errors are still on the transformed
#>   scale.
#> Model has log-transformed response. Back-transforming predictions to
#>   original response scale. Standard errors are still on the transformed
#>   scale.
#> [1] 2.136294

Created on 2024-10-28 with reprex v2.1.1

Thank you.

@strengejacke
Copy link
Owner

FWIW, it works with emmeans engine, but I'll look into this:

library(ggeffects)
dat <- mtcars
dat$am_cat = as.factor(dat$am)
m2 <- lm(log(disp) ~ am_cat, dat)
ggpredict(m2, terms="am_cat") |> test_predictions(engine = "emmeans")
#> Model has log-transformed response. Back-transforming predictions to
#>   original response scale. Standard errors are still on the transformed
#>   scale.
#> # Pairwise comparisons
#> 
#> am_cat | Contrast |        95% CI |      p
#> ------------------------------------------
#> 0-1    |   142.87 | 76.70, 209.03 | < .001

Created on 2024-11-15 with reprex v2.1.1

@gp-esteves
Copy link
Author

Thank you. I was aware that back-transformed contrasts were possible in emmeans using the "regrid" function, but it's very handy to know that the emmeans engine seems to achieve this as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants