0

I am running generalized additive models with mgcv::gam and am trying to organize my results using broom:tidy, but tidy apparently does not exponentiate coefficients or confidence intervals for GAMs, though it does for regular glm models. Is there a broom::tidy method for exponentiating coefficients and CIs from GAMs? I ask specifically about tidy because I would like to use the results in regression tables created by gtsummary.

library(tidyverse)
library(magrittr)
library(mgcv)
library(parameters)
library(gtsummary)
library(broom)

# sample data

id <- 1:2000
gender <- sample(0:1, 2000, replace = T)
age <- sample(17:64, 2000, replace = T)
race <- sample(0:1, 2000, replace = T)
health_score <- sample(0:25, 2000, replace = T)
dead <- sample(0:1, 2000, replace = T)
days_enrolled <- sample(30:3000, 2000, replace = T)

df <- data.frame(id, gender, age, race, health_score, dead, days_enrolled)

# model

model <- gam(dead ~ gender + s(age) + race + s(health_score) + offset(log(days_enrolled)),
            data = df, method = "REML", family = nb())

# both give the same output:

tidy(model, parametric = T, conf.int = T)
tidy(model, parametric = T, conf.int = T, exponentiate = T)

Kellan Baker
  • 375
  • 3
  • 11
  • Can you reproduce the same problem with a standard data set, or with a sample of data similar to yours? – Jon Spring Feb 19 '21 at 04:23
  • 1
    I had too many models going and realized upon going back that it doesn't exponentiate either the CIs or the coefficients. My apologies. Edited to clarify and add reprex. – Kellan Baker Feb 19 '21 at 05:20

1 Answers1

0

You can directly use tbl_regression() to exponentiate your results. If this is not what you are after please let me know.

library(tidyverse)
library(mgcv)
library(parameters)
library(gtsummary)
library(broom)

# sample data

id <- 1:2000
gender <- sample(0:1, 2000, replace = T)
age <- sample(17:64, 2000, replace = T)
race <- sample(0:1, 2000, replace = T)
health_score <- sample(0:25, 2000, replace = T)
dead <- sample(0:1, 2000, replace = T)
days_enrolled <- sample(30:3000, 2000, replace = T)

df <- data.frame(id, gender, age, race, health_score, dead, days_enrolled)

# model

model <- gam(dead ~ gender + s(age) + race + s(health_score) + offset(log(days_enrolled)),
             data = df, method = "REML", family = nb())


tbl_regression(model, exponentiate = TRUE)
Mike
  • 3,797
  • 1
  • 11
  • 30
  • This doesn't work. I get an error that says, "Confidence intervals only available for parametric terms. Error: Tibble columns must have compatible sizes. *Size 28: Existing data. *Size 0: Column 'variable'. Only values of size one are recycled." Then there's information about the backtrace in `broom.helpers` and `tibble`. Adding "parametric = T" to the call doesn't help. – Kellan Baker Feb 19 '21 at 16:31
  • 1
    Ok you are right, there is now an open pull request on github for this posted here: https://github.com/ddsjoberg/gtsummary/pull/792 – Mike Feb 19 '21 at 17:41
  • Thanks so much for looking into this, Mike! I am on a secure computer system, so unfortunately I can't access GitHub. Is there any other way around this with `broom` and/or `gtsummary` as-is? – Kellan Baker Feb 19 '21 at 19:29
  • In case useful, `model_parameters` from the `parameters` package works on the `gam` model object, and I can get a tibble out of it. I know it's possible to write custom tidiers to pass to `tbl_regression`, though I haven't been able to figure out how to use `model_parameters` for this purpose. Is it possible? – Kellan Baker Feb 19 '21 at 19:34
  • 1
    great questions! perhaps you can reach out to the creator of the package, he is very responsive and probably could give you an answer very quickly. and it is unfortunate you do not have access to github! – Mike Feb 19 '21 at 21:32