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

Variable names for models with no model.frame method #63

Closed
ddsjoberg opened this issue Oct 20, 2020 · 4 comments
Closed

Variable names for models with no model.frame method #63

ddsjoberg opened this issue Oct 20, 2020 · 4 comments

Comments

@ddsjoberg
Copy link
Collaborator

When there is no model.frame() method, the user sees a very informative message. (super helpful!)

The resulting table has column for variable, but the columns are NA. Can we add the term as the variable for these models? In gtsummary, we use the variable name to do further manipulation (and in broom.helpers too), but with no name these variables cannot be selected.

I know the term is not the proper variable name, but I think the printed message is enough of a cue to users that the original variable names are not available.

library(broom.helpers)
library(gtsummary)

# 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
    )
  )

# 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
}

# fit the interval-censored survival model with icenReg::ic_sp()
icenReg::ic_sp(
  survival::Surv(lint, rint, type = "interval2") ~ trt,
  model = "ph",
  bs_samples = 3,
  data = trial2
) %>%
  # tidy up with broom.helpers
  tidy_and_attach(tidy_fun = tidy_ic_sp) %>%
  tidy_identify_variables() %>%
  tidy_add_variable_labels() %>%
  tidy_add_header_rows() %>%
  select(term, variable, var_label, label, estimate)
#> x 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.).
#> # A tibble: 1 x 5
#>   term      variable var_label label     estimate
#>   <chr>     <chr>    <chr>     <chr>        <dbl>
#> 1 trtDrug B <NA>     trtDrug B trtDrug B    0.160

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

@larmarange
Copy link
Owner

This is a good point. Maybe in that case var_type needs to be populated with "unknown" or something similar.

I need to check that out will not break other functions and to do some tests.

@ddsjoberg
Copy link
Collaborator Author

The type for these variables is already indicated as unknown! You're already ahead :)

@larmarange
Copy link
Owner

Fixed now :-)

@ddsjoberg
Copy link
Collaborator Author

YAY! You're so fast!

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

No branches or pull requests

2 participants