在 flexsurv 中使用广义伽玛函数估计生存失败
Estimating survival with a generalized gamma function in flexsurv fails
我有患者的生存数据。其中一些受到审查。我想使用 R 中的 flexsruv
包将 广义伽玛 函数拟合到经验数据。而我想要拟合的所有其他函数(例如 exponential、weibull 等)工作得很好,我得到以下 dist = "gengamma"
的错误代码:
Error in optim(method = "BFGS", par = c(5.02274354115438, -0.0670900421918298, :
non-finite finite-difference value [2]
这是我的数据:
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))
我收到以下错误代码:
library(flexsurv)
flexsurvreg(Surv(time, status) ~ 1, data = db.survival, dist="gengamma")
但是,当我选择 "CG"
或 "SANN"
方法时,我得到了结果(但它们不同)。
我的问题是:为什么我首先会收到错误消息?我的代码有误吗?
非常感谢您的帮助!
我认为您的代码没有问题。错误来自 flexsurvreg
使用的 optim
。默认情况下,optim
使用梯度的有限差分近似值,如 help("optim")
中所述
gr
A function to return the gradient for the "BFGS"
, "CG"
and
"L-BFGS-B"
methods. If it is NULL
, 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.
您可以通过以下调用查看代码失败的地方
> 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
之后您会看到 optim
做了几次迭代 运行 但在某些 C 代码中失败了。此外,您可以看到 flexsurv
没有向 optim
提供 lower
或 upper
参数,而这些参数可以与方法 "L-BFGS-B"
一起使用。您可以在 this vignette 中看到其中一个参数的支持受到限制,因此这可能对您有所帮助。
我有患者的生存数据。其中一些受到审查。我想使用 R 中的 flexsruv
包将 广义伽玛 函数拟合到经验数据。而我想要拟合的所有其他函数(例如 exponential、weibull 等)工作得很好,我得到以下 dist = "gengamma"
的错误代码:
Error in optim(method = "BFGS", par = c(5.02274354115438, -0.0670900421918298, :
non-finite finite-difference value [2]
这是我的数据:
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))
我收到以下错误代码:
library(flexsurv)
flexsurvreg(Surv(time, status) ~ 1, data = db.survival, dist="gengamma")
但是,当我选择 "CG"
或 "SANN"
方法时,我得到了结果(但它们不同)。
我的问题是:为什么我首先会收到错误消息?我的代码有误吗?
非常感谢您的帮助!
我认为您的代码没有问题。错误来自 flexsurvreg
使用的 optim
。默认情况下,optim
使用梯度的有限差分近似值,如 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.
您可以通过以下调用查看代码失败的地方
> 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
之后您会看到 optim
做了几次迭代 运行 但在某些 C 代码中失败了。此外,您可以看到 flexsurv
没有向 optim
提供 lower
或 upper
参数,而这些参数可以与方法 "L-BFGS-B"
一起使用。您可以在 this vignette 中看到其中一个参数的支持受到限制,因此这可能对您有所帮助。