Search code examples
gtsummarynlme

tbl_regression on nlme object


Can someone help me figure out how to use tbl_regression on this nlme::lme model?

I made this example using the storm dataset from dplyr... This is what I've tried so far:

head(storms) storms_lme <- lme(wind ~ 1 + year, random = ~ 1 | name, data = storms, method='ML', control = lmeControl(opt = "optim"))

tbl_regression(storms_lme)

Error in UseMethod("filter") : no applicable method for 'filter' applied to an object of class "NULL"

tbl_regression(storms_lme, effects="fixed", broom.mixed::tidy(storms_lme, effects = "fixed"))

Error in tidy_add_variable_labels(res, labels = variable_labels, interaction_sep = interaction_sep) : Review ?syntax for examples and details. ! broom::tidy() failed to tidy the model. ✔ tidy_parameters() used instead. Add tidy_fun = broom.helpers::tidy_parameters to quiet these messages. Unable to identify the list of variables.

This is usually due to an error calling stats::model.frame(x)or stats::model.matrix(x). It could be the case if that type of model does not implement these methods. Rarely, this error may occur if the model object was created within a functional programming framework (e.g. using lappy(), purrr::map(), etc.). Error in tidy_add_variable_labels(): The labels argument must be a named list, list of formulas, a single formula, or empty. Review ?syntax for examples and details. Backtrace:

  1. gtsummary::tbl_regression(...)
  2. broom.helpers::tidy_plus_plus(...)
  3. broom.helpers::tidy_add_variable_labels(res, labels = variable_labels, interaction_sep = interaction_sep)

Solution

  • It looks like the model type is not fully supported. I would make a support request on the {broom.helpers} GitHub page.

    storms_lme <- nlme::lme(wind ~ 1 + year,  random = ~ 1 | name, data = dplyr::storms, method='ML', control = nlme::lmeControl(opt = "optim"))
    
    broom.helpers::tidy_plus_plus(
      storms_lme,
      tidy_fun = \(x, ...) broom.mixed::tidy(x, effects = "fixed", ...)
    )
    #> ✖ Unable to identify the list of variables.
    #> 
    #> This is usually due to an error calling `stats::model.frame(x)`or `stats::model.matrix(x)`.
    #> It could be the case if that type of model does not implement these methods.
    #> Rarely, this error may occur if the model object was created within
    #> a functional programming framework (e.g. using `lappy()`, `purrr::map()`, etc.).
    #> Error in UseMethod("filter"): no applicable method for 'filter' applied to an object of class "NULL"
    

    Created on 2025-01-17 with reprex v2.1.1

    If you don't need all the features added by broom.helpers and gtsummary, you can print the model terms as a gtsummary table. Example below!

    pak::pak("ddsjoberg/gtsummary")
    #> ℹ Loading metadata database
    #> ✔ Loading metadata database ... done
    #> 
    #> 
    #> ℹ No downloads are needed
    #> ✔ 1 pkg + 62 deps: kept 61 [5.3s]
    library(gtsummary)
    
    storms_lme <- nlme::lme(wind ~ 1 + year,  random = ~ 1 | name, data = dplyr::storms, method='ML', control = nlme::lmeControl(opt = "optim"))
    
    tbl <-
      broom.mixed::tidy(storms_lme, effects = "fixed", conf.int = TRUE) |> 
      dplyr::relocate(p.value, .after = conf.high) |> 
      as_gtsummary() |> 
      modify_column_hide(c("effect", "std.error", "df", "statistic")) |> 
      modify_fmt_fun(c(estimate, conf.low, conf.high) ~ label_style_sigfig(digits = 3), 
                     p.value ~ label_style_pvalue()) |> 
      modify_header(term = "**Characteristic**", 
                    estimate = "**Coefficient**",
                    conf.low = "**95% CI**", 
                    p.value = "**p-value**") |> 
      modify_column_merge(pattern = "{conf.low}, {conf.high}") |> 
      modify_abbreviation("CI = Confidence Interval") |> 
      modify_column_alignment(term, "left")
    

    enter image description here Created on 2025-01-17 with reprex v2.1.1