2

作为一个可重现的示例,让我们使用下一个无意义的示例:

> library(glmmTMB)
> summary(glmmTMB(am ~ disp + hp + (1|carb), data = mtcars))
 Family: gaussian  ( identity )
Formula:          am ~ disp + hp + (1 | carb)
Data: mtcars

     AIC      BIC   logLik deviance df.resid 
    34.1     41.5    -12.1     24.1       27 

Random effects:

Conditional model:
 Groups   Name        Variance  Std.Dev. 
 carb     (Intercept) 2.011e-11 4.485e-06
 Residual             1.244e-01 3.528e-01
Number of obs: 32, groups:  carb, 6

Dispersion estimate for gaussian family (sigma^2): 0.124 

Conditional model:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept)  0.7559286  0.1502385   5.032 4.87e-07 ***
disp        -0.0042892  0.0008355  -5.134 2.84e-07 ***
hp           0.0043626  0.0015103   2.889  0.00387 ** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

实际上,我真正的模型家庭是 nbinom2。我想在 和 之间进行对比disp测试hp。所以,我尝试:

> glht(glmmTMB(am ~ disp + hp + (1|carb), data = mtcars), linfct = matrix(c(0,1,-1)))
Error in glht.matrix(glmmTMB(am ~ disp + hp + (1 | carb), data = mtcars),  : 
  ‘ncol(linfct)’ is not equal to ‘length(coef(model))’

我怎样才能避免这个错误?

谢谢!

4

1 回答 1

3

问题其实很简单:linfct需要一个列数等于参数个数的矩阵。您指定matrix(c(0,1,-1))时未指定行数或列数,因此 R 默认创建了一个列矩阵。添加nrow=1似乎有效。

library(glmmTMB)
library(multcomp)
m1<- glmmTMB(am ~ disp + hp + (1|carb), data = mtcars)
modelparm.glmmTMB <- function (model, coef. = function(x) fixef(x)[[component]],
                               vcov. = function(x) vcov(x)[[component]],
                               df = NULL, component="cond", ...) {
    multcomp:::modelparm.default(model, coef. = coef., vcov. = vcov.,
                        df = df, ...)
}        
glht(m1, linfct = matrix(c(0,1,-1),nrow=1))
于 2020-06-08T00:34:11.733 回答