与svyglm一起使用 [英] Using predict with svyglm

查看:404
本文介绍了与svyglm一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现predictsurvey包中的svyglm对象有一些奇怪的行为.如果predict中的新数据具有一个级别的因子/字符,则会吐出错误:

I have found some odd behavior with predict and the svyglm object from the survey package. If your newdata in predict has a factor/character with one level it spits out error:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
contrasts can be applied only to factors with 2 or more levels

如果我将一个一级变量用作模型的预测变量,则此错误是有道理的,但对于newdata,我看不到问题.

This error makes sense if I was putting a one level variable as the predictor for a model, but for newdata I don't see the problem.

使用常规的glm可以正常工作.

With regular glm this works fine.

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

我发现,如果我破解该因子的水平,它会起作用,但这不是必须的:

I find that it works if I hack the levels of the factor, but this shouldn't be necessary:

svymodel <- svyglm(api00~sch.wide,design=dstrat)

predict(svymodel, data.frame(sch.wide=factor(rep("No",10),
                                             levels = c("No","random phrase"))))

我误解了吗?还是survey软件包的问题?

Am I misunderstanding something or is this an issue with the survey package?

推荐答案

您没有在newdata中加入因素;您要输入一个字符串.您应该输入一个相同级别的因子作为适合模型的因子(而不是一些随机短语),这是设计的唯一方法矩阵很有意义.

You aren't putting a factor in newdata; you're putting a character string in. You should put in a factor with the same set of levels as the factor used to fit the model (not some random phrase) -- that's the only way the design matrix makes sense.

predict(svymodel, data.frame(sch.wide=factor(rep("No",10),levels=c("No","Yes"))))

predict.lm从拟合的对象中恢复因子水平(我不记得这是在2002年左右,但我可能错了).您可以使用这种方法来自动化:

predict.lm recovers the factor levels from the fitted object (I don't remember this being around in 2002, but I might be wrong). You can use that approach to automate:

predict(svymodel, data.frame(sch.wide=factor(rep("No",10),levels=svymodel$xlevels$sch.wide)))

然后将其放在要包装的物品清单上.

and I'll put that on the list of things to do for the package.

这篇关于与svyglm一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆