larmarange / broom.helpers

A set of functions to facilitate manipulation of tibbles produced by broom
https://larmarange.github.io/broom.helpers/
GNU General Public License v3.0
21 stars 8 forks source link

Support with coxphf model #237

Closed hichew22 closed 7 months ago

hichew22 commented 11 months ago

Hello,

I am trying to use Cox regression with the Firth-penalized likelihood within tbl_uvregression( ) from the {gtsummary} package as follows:

tbl_uvregression <- trial %>%
  select(death,
         ttdeath,
         age) %>%
  tbl_uvregression(
    method = coxphf,
    y = Surv(ttdeath, death),
    exponentiate = TRUE,
    pvalue_fun = ~ style_pvalue(.x, digits = 2)
  ) %>%
  add_nevent() %>%
  bold_p() %>%
  bold_labels()

However, the following error occurs:

Error in `mutate()`:
ℹ In argument: `model = map(...)`.
Caused by error in `map()`:
ℹ In index: 1.
Caused by error in `value[[3L]]()`:
! Error in cbind(obj$mm1, obj$timedata): number of rows of matrices must match (see arg 2)
Backtrace:
  1. ... %>% bold_labels()
 16. purrr::map(...)
 17. purrr:::map_("list", .x, .f, ..., .progress = .progress)
 21. gtsummary (local) .f(.x[[i]], ...)
 22. gtsummary:::safe_model_construction(.x, method, data, method.args)
 23. base::tryCatch(...)
 24. base (local) tryCatchList(expr, classes, parentenv, handlers)
 25. base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 26. value[[3L]](cond)

I posted this question on the {gtsummary} GitHub and the creator recommended that I ask here if this model might be able to be supported. https://github.com/ddsjoberg/gtsummary/issues/1559

Any help would be appreciated, thank you!

larmarange commented 11 months ago

I have done a quick diagnosis using the CRAN version of cosphf and I have identified two issues that should be reported to the coxphf team and handled directly in their package.

  1. It seems that coxphf() fail in case of missing values (wich is the case for the age variable). The function works if missing values are removed before calling coxphf(). To be consistent with other package, missing values should be managed directly by the package.
  2. The package includes a tidier but it seems that the method is not properly declared. If you call broom::tidy(), the coxphf tidier is not used and the function failed. You need to use specifically the coxphf::tidy.coxphf() tidier and to pass it explicitly to broom.helpers or gtsummary

If you remove missing values and if you indicate the proper tidier, everything seems to work correctly.

library(coxphf)
library(broom.helpers)
library(survival)
library(gtsummary)
#> #Uighur
#> 
#> Attachement du package : 'gtsummary'
#> L'objet suivant est masqué depuis 'package:broom.helpers':
#> 
#>     all_continuous
library(tidyverse)

df <- gtsummary::trial

mod <- coxphf(
  Surv(ttdeath, death) ~ age + grade,
  data = df
)
#> Error in cbind(obj$mm1, obj$timedata): le nombre de lignes des matrices doit correspondre (voir argument 2)

df <- df |> tidyr::drop_na(age, grade)

mod <- coxphf(
  Surv(ttdeath, death) ~ age + grade,
  data = df
)

broom::tidy(mod)
#> coxphf(formula = Surv(ttdeath, death) ~ age + grade, data = df)
#> 
#> Model fitted by Penalized ML
#> Confidence intervals and p-values by Profile Likelihood 
#> 
#>                 coef    se(coef) exp(coef) lower 0.95 upper 0.95     Chisq
#> age      0.006553054 0.007024429  1.006575  0.9928443   1.020559 0.8727594
#> gradeII  0.179710038 0.253931543  1.196870  0.7284027   1.966637 0.5074864
#> gradeIII 0.582291957 0.238403535  1.790137  1.1288125   2.866911 6.1274020
#>                   p
#> age      0.35019253
#> gradeII  0.47622901
#> gradeIII 0.01331023
#> 
#> Likelihood ratio test=7.323796 on 3 df, p=0.06226301, n=189
#> Wald test = 7.377032 on 3 df, p = 0.06080364
#> 
#> Covariance-Matrix:
#>                    age       gradeII      gradeIII
#> age       4.934260e-05 -4.848146e-05 -2.820656e-05
#> gradeII  -4.848146e-05  6.448123e-02  3.224237e-02
#> gradeIII -2.820656e-05  3.224237e-02  5.683625e-02
#> Error in co[, -2, drop = FALSE]: nombre de dimensions incorrect
tidy.coxphf(mod)
#> # A tibble: 3 × 5
#>   term     estimate std.error statistic p.value
#>   <chr>       <dbl>     <dbl>     <dbl>   <dbl>
#> 1 age       0.00655   0.00702     0.873  0.350 
#> 2 gradeII   0.180     0.254       0.507  0.476 
#> 3 gradeIII  0.582     0.238       6.13   0.0133

tidy_plus_plus(mod, tidy_fun = tidy.coxphf)
#> # A tibble: 4 × 19
#>   term     variable var_label var_class var_type    var_nlevels contrasts      
#>   <chr>    <chr>    <chr>     <chr>     <chr>             <int> <chr>          
#> 1 age      age      Age       numeric   continuous           NA <NA>           
#> 2 gradeI   grade    Grade     factor    categorical           3 contr.treatment
#> 3 gradeII  grade    Grade     factor    categorical           3 contr.treatment
#> 4 gradeIII grade    Grade     factor    categorical           3 contr.treatment
#> # ℹ 12 more variables: contrasts_type <chr>, reference_row <lgl>, label <chr>,
#> #   n_obs <dbl>, n_event <dbl>, exposure <dbl>, estimate <dbl>,
#> #   std.error <dbl>, statistic <dbl>, p.value <dbl>, conf.low <dbl>,
#> #   conf.high <dbl>

tbl_regression(mod, tidy_fun = coxphf::tidy.coxphf, exponentiate = TRUE) |> 
  as_kable()
Characteristic HR 95% CI p-value
Age 1.01 0.99, 1.02 0.4
Grade
I
II 1.20 0.73, 1.97 0.5
III 1.79 1.12, 2.86 0.013

df |> 
  select(death, ttdeath, age, grade) |> 
  tbl_uvregression(
    method = coxphf,
    y = Surv(ttdeath, death),
    exponentiate = TRUE,
    tidy_fun = coxphf::tidy.coxphf
  ) |> 
  add_nevent() |> 
  as_kable()
Characteristic N Event N HR 95% CI p-value
Age 189 103 1.01 0.99, 1.02 0.3
Grade 189 103
I
II 1.21 0.73, 1.98 0.5
III 1.80 1.13, 2.87 0.013

Created on 2023-10-08 with reprex v2.0.2

hichew22 commented 11 months ago

Thank you very much for your help!

When I run this code:

gtsummary::trial %>%
  select(ttdeath, death, age, grade) %>%
  drop_na(age, grade) %>%
  tbl_uvregression(
    method = coxphf,
    y = Surv(ttdeath, death),
    exponentiate = TRUE,
    tidy_fun = coxphf::tidy.coxphf
  ) %>%
  add_nevent()

I get the following error: Error: 'tidy.coxphf' is not an exported object from 'namespace:coxphf'

Would you be able to help me with this?

larmarange commented 11 months ago

Are you using cran version or dev version of coxphf?

hichew22 commented 11 months ago

I believe I am using the CRAN version, installed with install.packages ('coxphf').

larmarange commented 11 months ago

Regarding CRAN version, tidy.coxphf() is an exported object. cf. https://cran.r-project.org/web/packages/coxphf/coxphf.pdf

It seems to be still the case with the dev version, cf. https://github.com/georgheinze/coxphf/blob/master/R/coxphf-tidiers.R

Could you try to reinstall the package?

hichew22 commented 11 months ago

Hello,

I re-installed the package as follows:

> install.packages("coxphf")
Warning in install.packages :
  lzma decoder corrupt data

  There is a binary version available but the source version is later:
       binary source needs_compilation
coxphf 1.13.1 1.13.4              TRUE

Do you want to install from sources the package which needs compilation? (Yes/no/cancel) Yes
installing the source package ‘coxphf’

trying URL 'https://cran.rstudio.com/src/contrib/coxphf_1.13.4.tar.gz'
Content type 'application/x-gzip' length 30421 bytes (29 KB)
==================================================
downloaded 29 KB

* installing *source* package ‘coxphf’ ...
** package ‘coxphf’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
gfortran -mmacosx-version-min=10.13 -fno-optimize-sibling-calls  -fPIC  -Wall -g -O2  -c  coxphf.f90 -o coxphf.o
make: gfortran: No such file or directory
make: *** [coxphf.o] Error 1
ERROR: compilation failed for package ‘coxphf’
* removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/coxphf’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/coxphf’
Warning in install.packages :
  installation of package ‘coxphf’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/6h/lb103m897q14p9dv7t7__9qh0000gn/T/RtmpXyzlTO/downloaded_packages’

And still get the same error. When I do ?tidy.coxphf() I also get the response:

No documentation for ‘tidy.coxphf’ in specified packages and libraries:
you could try ‘??tidy.coxphf’

When I try to use your example and call: tidy.coxphf(mod) I get the error: Error in tidy.coxphf(mod) : could not find function "tidy.coxphf"

Do you know what else I could try? Thank you!

larmarange commented 11 months ago

The installation failed. See the error message.

Restart R and re install the package before charging anything in memory

larmarange commented 11 months ago

You could also try to update R to version 4.2

hichew22 commented 11 months ago

Got it, it works now! Thank you very much for all your help!