knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = FALSE )
singleEventSurvival() supports non-parametric, semi-parametric, and parametric
survival estimators through a common interface. This vignette shows how to compare
those models once a survival dataset has already been prepared from Eunomia.
All examples below assume you already created a survival dataset with the internal
addCohortSurvival() helper and included age and gender columns.
library(OdysseusSurvivalModule) survivalData <- data.frame( subject_id = 1:8, time = c(15, 21, 40, 55, 60, 74, 90, 120), status = c(1, 0, 1, 0, 1, 0, 1, 0), age_years = c(44, 51, 67, 39, 73, 58, 62, 47), gender = c("Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male") )
Supported values for model are:
"km""cox""weibull""exponential""lognormal""loglogistic"All of them return the same high-level structure: a named list with data and
summary per stratum, plus overall.
coxFit <- singleEventSurvival( survivalData = survivalData, timeScale = "days", model = "cox", covariates = c("age_years") ) coxFit[["overall"]]$summary head(coxFit[["overall"]]$data)
The Cox path uses covariates when fitting the model, but the returned object is still survival-oriented. It does not expose regression coefficients or hazard ratios.
weibullFit <- singleEventSurvival( survivalData = survivalData, timeScale = "days", model = "weibull", covariates = c("age_years") ) lognormalFit <- singleEventSurvival( survivalData = survivalData, timeScale = "days", model = "lognormal", covariates = c("age_years") ) weibullFit[["overall"]]$summary lognormalFit[["overall"]]$summary
Parametric models also return a data table with estimated survival, hazard, and
cumulative hazard evaluated on the observed event-time grid.
One practical way to compare models is to extract the same summary fields from each fit.
modelNames <- c("km", "cox", "weibull", "lognormal") fits <- lapply(modelNames, function(modelName) { singleEventSurvival( survivalData = survivalData, timeScale = "days", model = modelName, covariates = if (modelName == "km") NULL else c("age_years") ) }) names(fits) <- modelNames comparison <- data.frame( model = names(fits), medianSurvival = vapply(fits, function(x) x[["overall"]]$summary$medianSurvival, numeric(1)), meanSurvival = vapply(fits, function(x) x[["overall"]]$summary$meanSurvival, numeric(1)), stringsAsFactors = FALSE ) comparison
strata accepts "gender" and "age_group". When both are supplied, the package
fits them separately, not as joint interaction strata.
stratifiedFit <- singleEventSurvival( survivalData = survivalData, timeScale = "days", model = "weibull", covariates = c("age_years"), strata = c("gender", "age_group"), ageBreaks = list(c(18, 49), c(50, 64), c(65, Inf)) ) names(stratifiedFit) stratifiedFit[["gender=Female"]]$summary stratifiedFit[["age_group=65+"]]$summary stratifiedFit$logrank_test_gender stratifiedFit$logrank_test_age_group
Each fitted entry can be plotted from its data component.
curveData <- weibullFit[["overall"]]$data plot( curveData$time, curveData$survival, type = "l", xlab = "Time (days)", ylab = "Survival probability", main = "Weibull survival curve" )
"km" for descriptive, assumption-light summaries."cox" when covariates matter but you still want a survival-curve summary.The advanced usage pattern is mostly about choosing the right model value and then
extracting comparable summaries from the returned list structure.
```
This vignette covered:
For getting started, see the "Getting Started" vignette.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.