使用 glmmTMB 建模计数数据时的错误和警告 [英] Error and warning when modeling count data using glmmTMB
问题描述
我正在尝试使用 glmmTMB 用计数数据拟合零膨胀泊松随机效应模型.但是,我收到了一些错误和警告.
head(data)计算时间学习1 0 259 12 0 199 13 0 571 14 0 927 15 7 254 16 0 877 1字符串(数据)'data.frame': 959 obs.3个变量:$ 计数:int 0 0 0 0 7 0 0 0 0 0 ...$时间:int 259 199 571 927 254 877 555 158 1014 705 ...$ study : 因子 w/10 个级别 "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
时间是一个抵消项,而学习是我必须添加的随机效应.
错误 1
如果我添加一个随机效应,那么下面的随机效应模型报告同样的错误
f1<-glmmTMB(count~1+offset(time)+1|study, data = data, family = poisson, ziformula = ~1|study)f2<-glmmTMB(count~1+offset(time)+1|study, data = subset(data, study=="1"), family = poisson, ziformula = ~1|study)nlminb(start = par,objective = fn,gradient = gr, control = control$optCtrl) 中的 NA/NaN 函数评估错误:梯度函数必须返回长度为 4 的数字向量
错误 2
我想我需要尝试一个更简单的模型,因此我放弃了随机效应.不幸的是,我收到了一个不同的错误.
f3<-glmmTMB(count~1+offset(time), data = data, family = poisson, ziformula = ~1)f4<-glmmTMB(count~1+offset(time), data = subset(data, study=="1"), family = poisson, ziformula = ~1)nlminb(start = par,objective = fn,gradient = gr, control = control$optCtrl) 中的 NA/NaN 函数评估错误:NA/NaN 梯度评估
警告 1
好的.我想知道如何获得没有任何错误的输出.因此,我放弃了偏移项而不是随机效应,从而获得了警告而不是错误.
f5<-glmmTMB(count~1+1|study, data = data, family = poisson, ziformula = ~1|study)f6<-glmmTMB(count~1+1|study, data = subset(data, study=="1"), family = poisson, ziformula = ~1|study)Cholmod 警告矩阵非正定"在文件 ../Supernodal/t_cholmod_super_numeric.c,第 729 行Cholmod 警告矩阵非正定"在文件 ../Supernodal/t_cholmod_super_numeric.c,第 729Cholmod 警告矩阵文件非正定"../Supernodal/t_cholmod_super_numeric.c,文件 ../Supernodal/t_cholmod_super_numeric.c,第 729Cholmod 行警告矩阵非正定",文件 ../Supernodal/t_cholmod_super_line 729Cholmod 警告矩阵非正定"文件 ../Supernodal/t_cholmod_super_numeric.c 中的 729Cholmod 警告矩阵非正定",第 729Cholmod 文件 ../Supernodal/t_cholmod_super_numeric.c 中的第 729Cholmod 警告矩阵非正定",第 729Cholmod 警告矩阵非正定"文件../Supernodal/t_cholmod_super_numeric.c,第 729 行
良好"适合,既没有错误也没有警告
经过一系列改造后,我发现如果我删除两个随机效应项和偏移项中的至少一个,模型将不会报告错误或警告.
f7<-glmmTMB(count~1+1|study, data = data, family = poisson, ziformula = ~1)f8<-glmmTMB(count~1, data = data, family = poisson, ziformula = ~1|study)
问题
是什么问题导致了这些错误和警告?如果可能,我怎样才能拟合 ZIP 混合效果模型并且既不出现错误也不收到警告?
示例
我有一个可重现的例子,它产生了警告 1
count.ex<-rpois(500, 0.2)study.ex<-as.factor(sample(1:5, 500, replace = TRUE))time.ex<-rexp(500, 150)fit.ex<-glmmTMB(count.ex~1+offset(time.ex)+1|study.ex, family = poisson, ziformula = ~1|study.ex)Cholmod 警告矩阵非正定"在文件 ../Supernodal/t_cholmod_super_numeric.c,第 729 行Cholmod 警告矩阵非正定"在文件 ../Supernodal/t_cholmod_super_numeric.c,第 729Cholmod 警告矩阵文件非正定"../Supernodal/t_cholmod_super_numeric.c,第729行模型收敛问题;非正定 Hessian 矩阵.见小插图('疑难解答')
如果没有可重现的示例,这将相当困难!
你的模型有一个明显的问题:你已经在你的模型中指定了
offset(time)
,你几乎肯定想使用offset(log(time))代码>.您的
time
值很大(我可以在您的str()
中看到的值范围为 500-1000;偏移量输入 predicted 值模型为exp(offset)
,要么大得离谱(如果offset
>)或无穷大. 你应该小心保护带括号的随机效应项(例如你使用
~1 + 1|study
而不是~1 + (1|study)
code>; 我不认为它会导致您在这里展示的示例中出现问题,但通常管道 (|
) 运算符的优先级较低,这意味着例如~1+a+b|study
等价于~(1+a+b)|study
...
如果我使用
~count.ex~1+offset(log(time.ex))+(1|study.ex)
在您的示例中,我没有收到任何警告.
I am trying to fit zero inflated poisson random effects model with count data using glmmTMB. However, I received some errors and warnings.
head(data)
count time study
1 0 259 1
2 0 199 1
3 0 571 1
4 0 927 1
5 7 254 1
6 0 877 1
str(data)
'data.frame': 959 obs. of 3 variables:
$ count : int 0 0 0 0 7 0 0 0 0 0 ...
$ time : int 259 199 571 927 254 877 555 158 1014 705 ...
$ study : Factor w/ 10 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
time is an offset term, and study is a random effect I have to add.
Error 1
If I add a random effect, then the following random-effect models report the same error
f1<-glmmTMB(count~1+offset(time)+1|study, data = data, family = poisson, ziformula = ~1|study)
f2<-glmmTMB(count~1+offset(time)+1|study, data = subset(data, study=="1"), family = poisson, ziformula = ~1|study)
NA/NaN function evaluationError in nlminb(start = par, objective = fn, gradient = gr, control = control$optCtrl) :
gradient function must return a numeric vector of length 4
Error 2
I thought I need to try a simpler model, and thus I dropped the random-effect. Unfortunately, I received a different error.
f3<-glmmTMB(count~1+offset(time), data = data, family = poisson, ziformula = ~1)
f4<-glmmTMB(count~1+offset(time), data = subset(data, study=="1"), family = poisson, ziformula = ~1)
NA/NaN function evaluationError in nlminb(start = par, objective = fn, gradient = gr, control = control$optCtrl) :
NA/NaN gradient evaluation
Warning 1
OK. I wonder how I can get an output without any error. So, I dropped the offset term instead of the random-effect, and thus obtained a warning instead of an error.
f5<-glmmTMB(count~1+1|study, data = data, family = poisson, ziformula = ~1|study)
f6<-glmmTMB(count~1+1|study, data = subset(data, study=="1"), family = poisson, ziformula = ~1|study)
Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729
"Good" fit with neither error nor warning
After a bunch of refits, I find that if I drop at least one of two random-effect terms and the offset term, models will report neither an error or a warning.
f7<-glmmTMB(count~1+1|study, data = data, family = poisson, ziformula = ~1)
f8<-glmmTMB(count~1, data = data, family = poisson, ziformula = ~1|study)
Question
What issues caused these errors and warining? If possible, how could I fit a ZIP mixed effect model and receive neither error nor warning?
Example
I have a reproducible example which produced the warning 1
count.ex<-rpois(500, 0.2)
study.ex<-as.factor(sample(1:5, 500, replace = TRUE))
time.ex<-rexp(500, 150)
fit.ex<-glmmTMB(count.ex~1+offset(time.ex)+1|study.ex, family = poisson, ziformula = ~1|study.ex)
Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Cholmod warning 'matrix not positive definite' at file ../Supernodal/t_cholmod_super_numeric.c, line 729Model convergence problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')
This is rather difficult without a reproducible example!
There is one obvious problem with your model: you've specified
offset(time)
in your model, you almost certainly want to useoffset(log(time))
. Yourtime
values are large (the values I can see in yourstr()
range from 500-1000; the offset enters the predicted value in the model asexp(offset)
, which is either ridiculously large (ifoffset < log(.Machine$double.xmax) == 709.78
) or infinite.You should be careful to protect random-effect terms with parentheses (e.g. you use
~1 + 1|study
rather than~1 + (1|study)
; I don't think it causes problems in the examples you show here, but in general the pipe (|
) operator has low precedence, which means that e.g.~1+a+b|study
is equivalent to~(1+a+b)|study
...
If I use
~count.ex~1+offset(log(time.ex))+(1|study.ex)
in your example, I don't get any warnings.
这篇关于使用 glmmTMB 建模计数数据时的错误和警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!