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

Support for survival models? #651

Open
williamlorder opened this issue Jan 21, 2022 · 5 comments
Open

Support for survival models? #651

williamlorder opened this issue Jan 21, 2022 · 5 comments
Labels
Enhancement 💥 Implemented features can be improved or revised

Comments

@williamlorder
Copy link

Hi,strengejacke. I really like to use your package for table output and plotting. Your package contains many common models, however, I find that your package does not seem to be able to handle survival models (Survival packages are usually used for survival analysis in the R language). Are there any plans to add this part of the support

@IndrajeetPatil
Copy link
Member

Hi,

Can you provide a reproducible example?

Model objects from {survival} package are supported. For example,

library(easystats)
#> # Attaching packages: easystats 0.4.3
#> v insight     0.15.0.3     v datawizard  0.2.2.9000
#> v bayestestR  0.11.5.1     v performance 0.8.0.1   
#> v parameters  0.16.0.1     v effectsize  0.6.0     
#> v modelbased  0.7.1        v correlation 0.7.1.1   
#> v see         0.7.0.1      v report      0.4.0.1
library(survival)

# Create the simplest test data set
test1 <- list(
  time = c(4, 3, 1, 1, 2, 2, 3),
  status = c(1, 1, 1, 0, 1, 1, 0),
  x = c(0, 2, 1, 1, 1, 0, 0),
  sex = c(0, 0, 0, 0, 1, 1, 1)
)

# Fit a stratified model
m <- coxph(Surv(time, status) ~ x + strata(sex), test1)

parameters(m)
#> Parameter | Coefficient |   SE |        95% CI |    z |     p
#> -------------------------------------------------------------
#> x         |        0.80 | 0.82 | [-0.81, 2.41] | 0.98 | 0.329
#> 
#> Uncertainty intervals (equal-tailed) and p values (two-tailed) computed using a
#>   Wald z-distribution approximation.

performance(m)
#> Warning: Response residuals not available to calculate mean square error. (R)MSE is
#>   probably not reliable.
#> Warning: Can't calculate weighted residuals from model.
#> # Indices of model performance
#> 
#> AIC   |   BIC | Nagelkerke's R2 |  RMSE | Sigma
#> -----------------------------------------------
#> 8.655 | 8.265 |           0.215 | 0.469 | 0.000

plot(parameters(m))

Created on 2022-01-21 by the reprex package (v2.0.1.9000)

@IndrajeetPatil IndrajeetPatil added the Reprex 💾 We need a reproducible example for further investigation label Jan 21, 2022
@williamlorder
Copy link
Author

Oh, sorry I didn't check carefully. However I found some minor problems when using a series of packages from easystat for survival analysis. For example, I wanted to calculate the risk ratio, which should not be shown as Coefficient but HR, which could be an unnecessary error as usual. Also for some hypothesis testing in survival analysis, I tried to run the check_model function in the performance package. However an error popped up. Also, for parameters it does not seem to work for the Kaplan-Meier model. You can see the details in the code below.

library(survival)
library(parameters)
library(sjPlot)
#> Install package "strengejacke" from GitHub (`devtools::install_github("strengejacke/strengejacke")`) to load all sj-packages at once!
library(tidyverse)
library(performance)

res.cox <- coxph(Surv(time, status) ~ age + sex + wt.loss, data =  lung)
#Calculating the risk ratio
res.cox %>% parameters(exponentiate = TRUE)
#> Parameter | Coefficient |       SE |       95% CI |     z |     p
#> -----------------------------------------------------------------
#> age       |        1.02 | 9.86e-03 | [1.00, 1.04] |  2.08 | 0.038
#> sex       |        0.59 |     0.10 | [0.42, 0.84] | -2.99 | 0.003
#> wt loss   |        1.00 | 6.20e-03 | [0.99, 1.01] |  0.12 | 0.902
#> 
#> Uncertainty intervals (equal-tailed) and p values (two-tailed) computed using a
#>   Wald z-distribution approximation.
#using check_model() from performance
res.cox %>% check_model()
#> Homogeneity of variance could not be computed. Cannot extract residual variance from objects of class 'coxph'.
#> Error in UseMethod("cooks.distance"): no applicable method for 'cooks.distance' applied to an object of class "coxph"
#using parameter in Kaplan-Meier model through survfit()
survfit(Surv(time, status) ~ 1, data = aml) %>% parameters()
#> Error: Sorry, `model_parameters()` failed with the following error (possible class
#>   'survfit' not supported):
#> 
#>   argument is of length zero

@strengejacke
Copy link
Member

We do not support survfit at the moment, but also because I'm not sure what a meaningful summary would be. survreg, however, is supported.

@bwiernik
Copy link
Contributor

We could do output similar to summary() method (see here https://www.emilyzabor.com/tutorials/survival_analysis_in_r_tutorial.html)

We could choose a set of times using visualization_matrix by default

@williamlorder
Copy link
Author

Great! I will try it out later.Thank you for your help

@strengejacke strengejacke added Enhancement 💥 Implemented features can be improved or revised and removed Reprex 💾 We need a reproducible example for further investigation labels Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement 💥 Implemented features can be improved or revised
Projects
None yet
Development

No branches or pull requests

4 participants