chjackson / flexsurv

The flexsurv R package for flexible parametric survival and multi-state modelling
http://chjackson.github.io/flexsurv/
53 stars 28 forks source link

Customizing own distribution for flexsurvreg #124

Closed esther1262 closed 1 year ago

esther1262 commented 2 years ago

Hi again I have been struggling with the package for my simulation and just found out this forum. I appreciate if you can help with all my questions.

In the standard accelerated failure time model with log-logistic distribution, covariates are wrapped inside the scale parameter. If I remember correctly, flexsurvreg outputs AFT estimates for log-logistic. Say the parameters: shape=a, scale, coefficients=gamma

The customized hazard for log-logistic, with covariates, would like to be

baseline_hazard exp(X'beta) = a t^(a-1) / (1+t^a) * exp(X'beta)

a = shape scale is forced to be 1 beta = odds ratios of covariates, should equal to -a * gamma

How can I create this hazard function and use it with flexsurvreg? I recoded the following function but didnt seem right.

cus_hz_llog <- function (x, shape = 1, scale = 0, log = FALSE) 
{
    h <- dbase("llogis", log = log, x = x, shape = shape, scale = scale)
    for (i in seq_along(h)) assign(names(h)[i], h[[i]])
    if (log) 
        ret[ind] <- log(shape) + (shape - 1) * log(x) - log(1 + x^shape)
    else ret[ind] <- (shape) * (x)^{shape - 1} / (1 + x^shape)
    ret
}
cus_hz_llog <- Vectorize (cus_hz_llog)

custom.llog <- list(name="llog", pars=c("shape"), location=1, 
                    transforms=c(log), inv.transforms=c(exp), 
                    inits=function(t)1/mean(t))

Best Regards

chjackson commented 2 years ago

Not sure what you mean by "scale is forced to be 1". Do you mean if the covariates all have values of zero, then you know that the scale parameter is 1? I would just implement a model with a scale and a shape parameter, define the location parameter to be the scale, and then use a fixedpars argument in your flexsurvreg call to fix the baseline scale to 1.

esther1262 commented 2 years ago

Assuming covariates Z include the intercept, there are two major parameters in standard log-logistic distribution. Z can only affect the scale parameter, scale = exp(-shape * gamma'Z) . shape = a baseline hazard = scale * a * t^(a-1) / (1 + scale * t^a), while scale = exp(-shape*gamma0), scale cannot be 1 unless we remove the intercept as well. If we force scale = 1, the hazard given Z is the same for all Z right? In other words, Z won't affect the hazard anymore.

For my case, I want to take out the scale parameter and modify the log-logistic distribution to have only one parameter (shape). The hazard function is still affected by covariates but no longer via scale.

baseline hazard = a * t^(a-1) / (1 + t^a) hazard | Z = a * t^(a-1) / (1 + t^a) * exp(b'Z)

Thanks

chjackson commented 2 years ago

Is the example in Section 1.1 of the Supplementary Examples vignette https://cran.r-project.org/web/packages/flexsurv/vignettes/flexsurv-examples.pdf anything like what you want? A proportional hazards version of a model (generalized gamma) that is normally defined as an AFT model?

esther1262 commented 2 years ago

That might be the one. Thanks