When I run flexsurvspline, from the flexsurv package, on the attached dataset , I get the following error for any k>1:
flexsurvspline(Surv(time,dead)~1,data=input_df,k=2)
Error in optim(method = "BFGS", par = c(gamma0 = 0, gamma1 = 0, gamma2 = 0, :
initial value in 'vmmin' is not finite
From reading other posts with similar issues, I gather this is likely an issue with the inits parameter, and that I might have to generate my own inits function for this particular dataset. But I haven't found any guidance on how to do this for spline fits or what goes into that parameter. Am I correct that this is what's causing the error, and if so how should an inits function be determined?
Thanks in advance!
If you use the debugger you can see what's going on inside the function. By specifying debugonce(flexsurvspline)
, and then running the function, you can step through and see what's happening on each step. (NB: capital Q
gets you out of the debugging browser). When I did that, I found that the because more than 30% of the data is in the first time (2), which is also the left boundary know, the function is setting the knots at:
Browse[2]> knots
33.33333% 66.66667%
0.6931472 0.6931472 1.0986123 2.5649494
Note that the first two knots are at the same value, which essentially breaks the function. You can fix this by specifying the interior knot values directly on the log-time scale.
flexsurvspline(Surv(time,dead)~1,data=input_df, knots=c(1.8,2.3))
# Call:
# flexsurvspline(formula = Surv(time, dead) ~ 1, data = input_df,
# knots = c(1.8, 2.3))
#
# Estimates:
# est L95% U95% se
# gamma0 -3.475 -3.783 -3.167 0.157
# gamma1 3.021 2.785 3.257 0.120
# gamma2 4.294 3.341 5.248 0.487
# gamma3 -7.908 -10.305 -5.510 1.223
#
# N = 428, Events: 428, Censored: 0
# Total time at risk: 1401
# Log-likelihood = -725.7712, df = 4
# AIC = 1459.542