Reputation: 850
I have survival data for patients. Some of them are censored. I want to fit a generalized gamma function to the empirical data with the flexsruv
package in R. While all other functions that I want to fit (such as exponential, weibull etc.) are working just fine, I get the following error code for dist = "gengamma"
:
Error in optim(method = "BFGS", par = c(5.02274354115438, -0.0670900421918298, :
non-finite finite-difference value [2]
Here is the data that I have:
db.survival <- data.frame(time = c(101, 111, 185, 707, 85, 58, 427, 672, 90,
1452, 608, 99, 556, 62, 60, 1445, 563, 246,
163, 276, 216, 64, 61, 66, 67, 68, 81, 83,
99, 100, 100, 100, 100, 100, 100, 100, 100),
status = c(1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0))
I receive an error code for:
library(flexsurv)
flexsurvreg(Surv(time, status) ~ 1, data = db.survival, dist="gengamma")
However, when I choose the methods "CG"
or "SANN"
, I get results (but they differ).
My questions is: why do I get an error in the first place? Do I code wrongly?
Thanks a lot in advance for any kind of help!
Upvotes: 1
Views: 1235
Reputation: 4841
I do not think there is something wrong with your code. The error comes from the optim
that flexsurvreg
uses. By default, optim
uses a finite-difference approximation for the gradient as stated in help("optim")
gr
A function to return the gradient for the"BFGS"
,"CG"
and"L-BFGS-B"
methods. If it isNULL
, a finite-difference approximation will be used. For the"SANN"
method it specifies a function to generate a new candidate point. If it is NULL a default Gaussian Markov kernel is used.
You can see where the code fails with the following calls
> options(error = recover)
> flexsurvreg(Surv(time, status) ~ 1, data = db.survival, dist="gengamma", control = list(trace = 100))
initial value 144.298116
iter 10 value 131.067771
iter 20 value 128.013812
iter 30 value 127.761461
iter 40 value 127.753269
Error in optim(control = list(trace = 100), method = "BFGS", par = c(5.02274354115438, :
non-finite finite-difference value [2]
Enter a frame number, or 0 to exit
1: flexsurvreg(Surv(time, status) ~ 1, data = db.survival, dist = "gengamma", control = list(trace = 100))
2: do.call("optim", optim.args)
3: optim(control = list(trace = 100), method = "BFGS", par = c(5.02274354115438, -0.0670900421918298, 0), fn = function (optpars, .
Selection: 3
After which you see that the optim
does run for few iterations but than fails in some C code. Further, you can see that the flexsurv
does not supply the lower
or upper
arguments to optim
which could be used with the method "L-BFGS-B"
. You can see in this vignette that one of the parameters have restricted support so that may help you.
Upvotes: 1