本文介绍了glmmTMB中变量之间的对比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As a reproducible example, let's use the next no-sense example:

> 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 之间进行对比测试。因此,我尝试:

Actually, my real model family is nbinom2. I want to make a contrast test between disp and hp. So, I try:

> 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 似乎可行。

The problem is actually fairly simple: linfct needs to be a matrix with the number of columns equal to the number of parameters. You specified matrix(c(0,1,-1)) without specifying numbers of rows or columns, so R made a column matrix by default. Adding nrow=1 seems to work.

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

这篇关于glmmTMB中变量之间的对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:06