easystats / parameters

:bar_chart: Computation and processing of models' parameters
https://easystats.github.io/parameters/
GNU General Public License v3.0
424 stars 36 forks source link

Support for survival models? #651

Open williamlorder opened 2 years ago

williamlorder commented 2 years ago

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 commented 2 years ago

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)

williamlorder commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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