## Minimal example of fitting a Weibull Accelerated Failure Time model
# Simulating survival data with right-censoring
set.seed(1234)
x1 <- rnorm(1000)
x2 <- rbinom(1000, 1, 0.5)
yraw <- rexp(exp(0.01*x1 + 0.01*x2))
# status: 1 = event occurred, 0 = right-censored
status <- rbinom(1000, 1, 0.25)
yobs <- ifelse(status, runif(1, 0, yraw), yraw)
df <- data.frame(
y = yobs,
x1 = x1,
x2 = x2
)
## Fit model using lgspline with Weibull AFT specifics
model_fit <- lgspline(y ~ spl(x1) + x2,
df,
unconstrained_fit_fxn = unconstrained_fit_weibull,
family = weibull_family(),
need_dispersion_for_estimation = TRUE,
dispersion_function = weibull_dispersion_function,
glm_weight_function = weibull_glm_weight_function,
shur_correction_function = weibull_shur_correction,
status = status,
opt = FALSE,
K = 1)
loglik_weibull(log(model_fit$y), log(model_fit$ytilde), status,
sqrt(model_fit$sigmasq_tilde))
Run the code above in your browser using DataLab