Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Created October 1, 2025 10:20
Show Gist options
  • Select an option

  • Save carlislerainey/c7cff60219bf4b484673523bfc3a347e to your computer and use it in GitHub Desktop.

Select an option

Save carlislerainey/c7cff60219bf4b484673523bfc3a347e to your computer and use it in GitHub Desktop.
R code to illustrate fitting duration models
# 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