我发现了一些奇怪的行为predict
和svyglm
对象从survey
包裹。如果你的新数据在predict
具有一个级别的因子/字符会输出错误:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
如果我将一级变量作为模型的预测变量,则此错误是有意义的,但对于新数据,我没有看到问题。
与常规的glm
这很好用。
MRE:
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
svymodel <- svyglm(api00~sch.wide,design=dstrat)
# errors
predict(svymodel, data.frame(sch.wide=rep("No",10)))
regmodel <- glm(api00~sch.wide,data=apistrat)
# works
predict(regmodel,data.frame(sch.wide=rep("No",10)))
我发现如果我破解该因素的水平,它会起作用,但这不是必要的:
svymodel <- svyglm(api00~sch.wide,design=dstrat)
predict(svymodel, data.frame(sch.wide=factor(rep("No",10),
levels = c("No","random phrase"))))
我是否误解了什么,或者这是一个问题survey
包裹?