我使用svyglm
运行以下逻辑回归模型和调查数据:
model7 <- svyglm(hivcode~I(age)+I(age)*factor(gendercode)+factor(gendercode)+
factor(maritalstatuscode)+factor(literacycode)+
I(age)*factor(maritalstatuscode)+
factor(residencecode),design=shs,family=quasibinomial)
我试图使用(像这样的)获得预测的概率:
newdata1<-with(new.clean,data.frame(age=mean(age),gendercode=factor(1),
maritalstatuscode=factor(1),
literacycode=factor(1),residencecode=factor(1)))
和
newdataP <- predeict(model7,newdata=newdata1,type="response").
首先我收到以下错误消息:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
我还想知道如何指定具体的因素水平,例如男性&#39;对于性别,&#39;单身&#39;对于预测代码中的婚姻状况等。
答案 0 :(得分:0)
我需要更多关于要评论的错误的信息(可重现的示例效果最好),但对于性别和婚姻状况,请在原始数据shs
中定义这些类(例如levels(shs$gender) <- c("male", "female")
- 性别的顺序应该适应你的实际订单,我怀疑它目前被编码为数字?)。
要根据性别或婚姻状况进行预测,只需构建一个适当的数据框架,其中应包含模型所具有的所有变量。函数expand.grid
可以创造奇迹。
expand.grid(gender = c("male", "female"), maritalstatus = c("married", "single"), numberofchildren = 0:5)
gender maritalstatus numberofchildren
1 male married 0
2 female married 0
3 male single 0
4 female single 0
5 male married 1
6 female married 1
7 male single 1
8 female single 1
9 male married 2
10 female married 2
11 male single 2
12 female single 2
13 male married 3
14 female married 3
15 male single 3
16 female single 3
17 male married 4
18 female married 4
19 male single 4
20 female single 4
21 male married 5
22 female married 5
23 male single 5
24 female single 5