Skip to content

add butcher methods for survival::coxph() - #243

Merged
juliasilge merged 5 commits into
mainfrom
coxph-234
Jan 18, 2023
Merged

juliasilge merged 5 commits into
mainfrom
coxph-234

Conversation

@simonpcouch

Copy link
Copy Markdown
Contributor

Related to #234. Also no NEWS update.🐻

There's a $terms slot that carries around a hefty environment that we'd usually sub out with rlang::base_env(), but the predict.coxph() source taps into it quite a bit.

library(survival)
library(butcher)

example_data <-
  tibble::tibble(
    time = rpois(1000, 2) + 1,
    status = rbinom(1000, 1, .5),
    x = rpois(1000, .5),
    covar = rbinom(1000, 1, .5)
  )

example_data
#> # A tibble: 1,000 × 4
#>     time status     x covar
#>    <dbl>  <int> <int> <int>
#>  1     1      0     0     0
#>  2     2      0     0     0
#>  3     2      1     0     1
#>  4     3      0     1     0
#>  5     5      0     1     1
#>  6     1      0     0     1
#>  7     2      0     0     1
#>  8     7      1     0     1
#>  9     2      1     2     1
#> 10     2      0     1     1
#> # … with 990 more rows

make_big_model <- function() {
  boop <- runif(1e6)
  coxph(Surv(time, status) ~ x + strata(covar), example_data)
}

res <- make_big_model()

weigh(res)
#> # A tibble: 20 × 2
#>    object                    size
#>    <chr>                    <dbl>
#>  1 terms                 8.01    
#>  2 formula               8.00    
#>  3 residuals             0.0722  
#>  4 y                     0.0177  
#>  5 linear.predictors     0.00805 
#>  6 call                  0.00146 
#>  7 concordance           0.000752
#>  8 coefficients          0.00028 
#>  9 means                 0.00028 
#> 10 wald.test             0.00028 
#> 11 var                   0.000224
#> 12 xlevels.strata(covar) 0.000176
#> 13 method                0.000112
#> 14 loglik                0.000064
#> 15 score                 0.000056
#> 16 iter                  0.000056
#> 17 n                     0.000056
#> 18 nevent                0.000056
#> 19 assign.x              0.000056
#> 20 timefix               0.000056
weigh(butcher(res))
#> # A tibble: 20 × 2
#>    object                    size
#>    <chr>                    <dbl>
#>  1 terms                 8.01    
#>  2 residuals             0.0722  
#>  3 linear.predictors     0.00805 
#>  4 formula               0.00149 
#>  5 call                  0.00146 
#>  6 concordance           0.000752
#>  7 coefficients          0.00028 
#>  8 means                 0.00028 
#>  9 wald.test             0.00028 
#> 10 var                   0.000224
#> 11 xlevels.strata(covar) 0.000176
#> 12 method                0.000112
#> 13 loglik                0.000064
#> 14 score                 0.000056
#> 15 iter                  0.000056
#> 16 n                     0.000056
#> 17 nevent                0.000056
#> 18 assign.x              0.000056
#> 19 timefix               0.000056
#> 20 y                     0.000048

# try to make those terms more minimal....
attr(res$terms, ".Environment") <- rlang::base_env()

# but noooo!
predict(res, example_data[1:100,])
#> Error in is.data.frame(data): object 'example_data' not found

Created on 2022-12-22 with reprex v2.0.2

There's an environment on the terms object that is quite "heavy," though coxph errors out if it's reset with `base_env()`.

@juliasilge juliasilge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 🚀

@juliasilge
juliasilge merged commit 11a6dc2 into main Jan 18, 2023
@juliasilge
juliasilge deleted the coxph-234 branch January 18, 2023 22:48
@github-actions

github-actions Bot commented Feb 2, 2023

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions Bot locked and limited conversation to collaborators Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants