Created
October 1, 2025 10:20
-
-
Save carlislerainey/c7cff60219bf4b484673523bfc3a347e to your computer and use it in GitHub Desktop.
R code to illustrate fitting duration models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load packages | |
| library(survival) | |
| # load data | |
| canc <- survival::cancer |> | |
| mutate(sex = case_when(sex == 1 ~ "Male", | |
| sex == 2 ~ "Female")) | |
| # quick look | |
| glimpse(canc) | |
| # model | |
| f <- Surv(time, status) ~ age + sex + ph.karno | |
| # Exponential | |
| fit_exp <- survreg(f, data = canc, dist = "exp") | |
| # Log-Normal | |
| fit_ln <- survreg(f, data = canc, dist = "lognormal") | |
| # Weibull | |
| fit_wei <- survreg(f, data = canc, dist = "weibull") | |
| # Rayleigh | |
| fit_ray <- survreg(f, data = canc, dist = "rayleigh") | |
| # Extreme Value (Gumbel) | |
| fit_extr <- survreg(f, data = canc, dist = "extreme") | |
| # Gaussian (Normal) | |
| fit_gaus <- survreg(f, data = canc, dist = "gaussian") | |
| # Logistic | |
| fit_logis <- survreg(f, data = canc, dist = "logistic") | |
| # Log-Logistic | |
| fit_llogis <- survreg(f, data = canc, dist = "loglogistic") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment