gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
206 stars 28 forks source link

Could not to transform the est and confit interval using transform_fun #201

Closed Guiquan-27 closed 1 year ago

Guiquan-27 commented 1 year ago

Would you give an example for that? Or am I wrong? Here is that: I use the smooth_estimates to evaluate the smooth from a binomial logistic gam. and smooth_estimates(fit, n = 100) %>% transform_fun(fun = plogis) will just transform the est column with other columns unchanged.

Thank you very much.

gavinsimpson commented 1 year ago

A reproducible example would be helpful. Your code works

library("mgcv")
library("gratia")

df <- data_sim("eg1", dist = "binary", scale = 0.33, n = 400, seed = 42)
m <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), family = binomial,
         data = dat, method = "REML")
smooth_estimates(m, "s(x0)") |> transform_fun(fun = plogis)

produces

> smooth_estimates(m, "s(x0)") # not transformed
# A tibble: 100 × 6
   smooth type  by       est    se       x0
   <chr>  <chr> <chr>  <dbl> <dbl>    <dbl>
 1 s(x0)  TPRS  NA    -0.558 0.318 0.000239
 2 s(x0)  TPRS  NA    -0.540 0.305 0.0103  
 3 s(x0)  TPRS  NA    -0.522 0.292 0.0204  
 4 s(x0)  TPRS  NA    -0.504 0.280 0.0304  
 5 s(x0)  TPRS  NA    -0.486 0.269 0.0405  
 6 s(x0)  TPRS  NA    -0.467 0.258 0.0506  
 7 s(x0)  TPRS  NA    -0.449 0.248 0.0606  
 8 s(x0)  TPRS  NA    -0.431 0.239 0.0707  
 9 s(x0)  TPRS  NA    -0.413 0.230 0.0807  
10 s(x0)  TPRS  NA    -0.395 0.223 0.0908  
# … with 90 more rows
# ℹ Use `print(n = ...)` to see more rows

> smooth_estimates(m, "s(x0)") %>% transform_fun(fun = plogis)                
# A tibble: 100 × 6
   smooth type  by      est    se       x0
   <chr>  <chr> <chr> <dbl> <dbl>    <dbl>
 1 s(x0)  TPRS  NA    0.364 0.318 0.000239
 2 s(x0)  TPRS  NA    0.368 0.305 0.0103  
 3 s(x0)  TPRS  NA    0.372 0.292 0.0204  
 4 s(x0)  TPRS  NA    0.377 0.280 0.0304  
 5 s(x0)  TPRS  NA    0.381 0.269 0.0405  
 6 s(x0)  TPRS  NA    0.385 0.258 0.0506  
 7 s(x0)  TPRS  NA    0.390 0.248 0.0606  
 8 s(x0)  TPRS  NA    0.394 0.239 0.0707  
 9 s(x0)  TPRS  NA    0.398 0.230 0.0807  
10 s(x0)  TPRS  NA    0.403 0.223 0.0908  
# … with 90 more rows
# ℹ Use `print(n = ...)` to see more rows

But I see that it isn't working for the confidence interval added by add_confint(). I'll fix that...

gavinsimpson commented 1 year ago

Thanks for letting me know; this is now fixed in the GitHub version

Guiquan-27 commented 1 year ago

@gavinsimpson Well done, thank you very much!