我有以下后勤glmer模型:
model <- glmer(cbind(species, round(totalSpecies)-species) ~ poly(year,2) + (1|referenceID) + (1|country),
family = binomial(cloglog), data=huntAll, control=glmerControl(optimizer="bobyqa"))
我正在尝试使用predict(),然后绘制预测值的置信区间。
这是我认为可行的:
pframe <- data.frame(year = seq(1972,2014))
Pred1 <- predict(model,re.form=NA,newdata=pframe,type="response")
pframe$speciesLOGIT<- predict(model,re.form=NA, newdata=pframe)
mm <- model.matrix(terms(model),pframe)
pvar1 <- diag(mm %*% tcrossprod(vcov(model),mm))
cmult <- 1.96
pframe <- data.frame(
pframe
, plo = pframe$speciesLOGIT-cmult*sqrt(pvar1)
, phi = pframe$speciesLOGIT+cmult*sqrt(pvar1)
)
然后用于绘图功能:
CI <- data.frame(
pframe
, ci1 = plogis(pframe$plo)*100
, ci2 = plogis(pframe$phi)*100
)
然后我使用polygon()函数添加了CI中的列。
当我最近在lme4中使用lmer运行线性混合效果模型时,此代码有效。但是当我尝试在这个逻辑glmer中使用它时,我在生成mm时出现以下错误:
Error in model.frame.default(object, data, xlev = xlev) :
variable lengths differ (found for 'poly(year, 2)')
In addition: Warning messages:
1: In round(totalSpecies) - species :
longer object length is not a multiple of shorter object length
2: In cbind(species, round(totalSpecies) - species) :
number of rows of result is not a multiple of vector length (arg 1)
我似乎无法让它发挥作用。 有什么想法吗?