glmmTMB 中变量之间的对比

Contrast between variables in glmmTMB

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

> 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。我想做一个disphp的对比测试。所以,我尝试:

> 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))’

如何避免这个错误?

谢谢!

问题其实很简单: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))