1

我正在将一些为模型编写的 k 折交叉验证代码改编为glmer/merMod模型glmmTMB框架。一切似乎都很好,直到我尝试使用模型的输出与训练数据相匹配来预测值并将其指数化为矩阵(然后分解为分位数/箱数以评估预测性能)。我可以使用 glmer 模型让这条线工作,但似乎当我使用 glmmTMB 运行相同的模型时,我得到Error in model.matrix: requires numeric/complex matrix/vector arguments了还有很多其他帖子讨论这个错误代码,我尝试将数据框转换为矩阵形式并更改没有运气的协变量类。前后各部分分开运行%*%工作,但当结合我得到错误。对于上下文,此代码旨在与使用/可用性数据一起运行,因此示例变量可能没有意义,但问题得到了很好的展示。关于发生了什么的任何建议?

library(lme4)
library(glmmTMB)

# Example with mtcars dataset
data(mtcars)

# Model both with glmmTMB and lme4
m1 <- glmmTMB(am ~ mpg + wt + (1|carb), family = poisson, data=mtcars)
m2 <- glmer(am ~ mpg + wt + (1|carb), family = poisson, data=mtcars)

#--- K-fold code (hashed out sections are original glmer version of code where different)---

# define variables
k <- 5
mod <- m1 #m2
dt <- model.frame(mod) #data used

reg.list <- list() # initialize object to store all models used for cross validation
  
# finds the name of the response variable in the model dataframe
  resp <- as.character(attr(terms(mod), "variables"))[attr(terms(mod), "response") + 1]
    
# define column called sets and populates it with character "train"
  dt$sets <- "train"
    
# randomly selects a proportion of the "used"/am records (i.e. am = 1) for testing data 
    dt$sets[sample(which(dt[, resp] == 1), sum(dt[, resp] == 1)/k)] <- "test"
    
# updates the original model using only the subset of "trained" data
    reg <- glmmTMB(formula(mod), data = subset(dt, sets == "train"), family=poisson,
                   control = glmmTMBControl(optimizer = optim, optArgs=list(method="BFGS")))

    #reg <- glmer(formula(mod), data = subset(dt, sets == "train"), family=poisson,
    #            control = glmerControl(optimizer = "bobyqa", optCtrl=list(maxfun=2e5)))
    
 reg.list[[i]] <- reg # store models
    
# uses new model created with training data (i.e. reg) to predict and exponentiate values 
      predall <- exp(as.numeric(model.matrix(terms(reg), dt) %*% glmmTMB::fixef(reg)))
     #predall <- exp(as.numeric(model.matrix(terms(reg), dt) %*% lme4::fixef(reg)))

4

1 回答 1

2

不用太仔细看代码:glmmTMB::fixef(reg)返回一个列表(带有元素cond(条件模型参数)、zi(零膨胀参数)、disp(分散参数)而不是向量。

如果你用它替换这个位glmmTMB::fixef(reg)[["cond"]]可能会起作用。

于 2022-01-20T01:21:07.883 回答