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

Allow tbl_regression() when no model frame method #673

Closed
wants to merge 3 commits into from

Conversation

ddsjoberg
Copy link
Owner

What changes are proposed in this pull request?
Update allows a user to utilize tbl_regression() when there is no model.frame() method for the model type. This means that no reference rows can be added. If the underlying data were labelled, the labels cannot be retrieved.

library(gtsummary)
#> #Uighur

# make up some interval censored data 
trial2 <-
  trial %>% 
  dplyr::mutate(
    lint = dplyr::case_when(
      death == 1 ~ runif(200) + 2,
      death == 0 ~ ttdeath
    ),
    rint = dplyr::case_when(
      death == 1 ~ ttdeath,
      death == 0 ~ Inf
    )
  )

# fit the interval-censored survival model with icenReg::ic_sp()
mod1 <-
  icenReg::ic_sp(
    survival::Surv(lint, rint, type = "interval2") ~ trt,
    model = "ph",
    bs_samples = 3,
    data = trial2
  ) 

# Write a custom tidier
tidy_ic_sp <- function(x, exponentiate =  FALSE, conf.level = 0.95, ...) {
  tidy <-
    tibble::tibble(
      term = names(x[["coefficients"]]),
      estimate = x[["coefficients"]],
      std.error = sqrt(diag(x[["var"]])),
      statistic = summary(x)$summaryParameters[, "z-value"],
      p.value = summary(x)$summaryParameters[, "p"],
      conf.low = confint(x, level = conf.level)[, 1],
      conf.high = confint(x, level = conf.level)[, 2]
    )
  
  if (exponentiate == TRUE)
    tidy <- dplyr::mutate_at(tidy, vars(estimate, conf.low, conf.high), exp)
  
  tidy
}

# check that the custom tidier works
tidy_ic_sp(mod1, exponentiate = TRUE)
#> # A tibble: 1 x 7
#>   term      estimate std.error statistic p.value conf.low conf.high
#>   <chr>        <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>
#> 1 trtDrug B     1.17     0.148      1.08   0.281    0.878      1.57


tbl_regression(
  mod1,
  exponentiate = TRUE,
  tidy_fun = tidy_ic_sp,
  label = list("trtDrug B" = "Drug B (Drug A is Ref.)")
) %>% 
  modify_header(estimate ~ "**HR**") %>%
  modify_footnote(estimate ~ "HR = Hazard Ratio", abbreviation = TRUE) %>%
  as_kable() # convert to kable to render properly on GH
#> x Unable to identify the list of variables.
#>   
#>   This is usually due to an error calling `stats::model.frame(x)`.
#>   It could be the case if that type of model does not implement this method.
#>   Rarely, this error may occur if the model object was created within
#>   a functional programming framework (e.g. using `lappy()`, `purrr::map()`, etc.).
#> x Unable to identify the list of variables.
#>   
#>   This is usually due to an error calling `stats::model.frame(x)`.
#>   It could be the case if that type of model does not implement this method.
#>   Rarely, this error may occur if the model object was created within
#>   a functional programming framework (e.g. using `lappy()`, `purrr::map()`, etc.).
#> x `trtDrug B` terms have not been found in `x`.
#> x There was an error calling `stats::model.frame(x)`.
#>   
#>   Most likely, this is because the argument passed in `x =` was
#>   misspelled, does not exist, or is not a regression model.
#>   
#>   Rarely, this error may occur if the model object was created within
#>   a functional programming framework (e.g. using `lappy()`, `purrr::map()`, etc.).
#>   Review the GitHub issue linked below for a possible solution.
#>   The model N will not be available in the output.
#>   https://github.com/ddsjoberg/gtsummary/issues/231
Characteristic HR 95% CI p-value
Drug B (Drug A is Ref.) 1.17 0.88, 1.57 0.3

Created on 2020-10-06 by the reprex package (v0.3.0)


Checklist for PR reviewer

  • PR branch has pulled the most recent updates from master branch. Ensure the pull request branch and your local version match and both have the latest updates from the master branch.
  • If an update was made to tbl_summary(), was the same change implemented for tbl_svysummary()?
  • If a new function was added, function included in _pkgdown.yml
  • If a bug was fixed, a unit test was added for the bug check
  • Run pkgdown::build_site(). Check the R console for errors, and review the rendered website.
  • Code coverage is suitable for any new functions/features. Review coverage with covr::report(). Before you run, set Sys.setenv(NOT_CRAN=true) and begin in a fresh R session without any packages loaded.
  • R CMD Check runs without errors, warnings, and notes
  • NEWS.md has been updated with the changes from this pull request under the heading "# gtsummary (development version)". If there is an issue associated with the pull request, reference it in parantheses at the end update (see NEWS.md for examples).
  • usethis::use_spell_check() runs with no spelling errors in documentation
  • When the branch is ready to be merged into master, increment the version number using usethis::use_version(which = "dev"), run codemetar::write_codemeta(), approve, and merge the PR.

@ddsjoberg
Copy link
Owner Author

@zabore can you review this one? we'll need this to allow for summarizing models with no model.frame() method.

@ddsjoberg
Copy link
Owner Author

this will now be incorporated into broom.helpers, so we don't need to make the update any longer
larmarange/broom.helpers#63

@ddsjoberg ddsjoberg closed this Oct 20, 2020
@ddsjoberg ddsjoberg deleted the no_model_frame_method branch March 19, 2021 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants