I would like to implement a custom survival function in the flexsurv
package, similar to Section 4 (pg. 12) here:
https://cran.r-project.org/web/packages/flexsurv/vignettes/flexsurv.pdf
Say I have a dataframe, df
, where the status
determines if an item is failed. 1=failed
and 0=non-failed
:
> library(flexsurv)
> library(survival)
> df
equip_id age status
1 20.33812 0
2 28.44830 1
3 22.47019 0
4 47.21489 1
5 30.42093 1
Now say I have a density function:
# must be named 'dcustom.weibull' to denote density function
dcustom.weibull <- function(shape, scale, t)
{
f_t <- (shape/scale) * ((t/scale)^(shape - 1)) * exp(-(t/scale)^shape)
return(f_t)
}
I try creating a custom function for use in the flexsurv
package:
custom.weibull <- list(name = "custom.weibull", pars = c("shape", "scale"),
location = "scale",
transforms = c(log, log),
inv.transforms = c(exp, exp),
inits = function(t){ c(1, median(t)) })
I try calling this with the flexsurv
package:
flexsurvreg(Surv(age, status)~1, data = df, dist = custom.weibull)
I get the following error:
Forming integrated rmst function...
Forming integrated mean function...
Error in (function (shape, scale, t) :
unused arguments (x = c(28.4482952329068, 47.2148908312751, 30.4209324295688), log = TRUE)
Now I realize there are plenty of packages that can do this for a 2-parameter Weibull function (e.g.,fitdistplus
). I'm trying to understand how to set this up with a known density function so that I can eventually implement a different, less traditional density function with different parameters.