R中的预测概率
我有一个带有虚拟变量的logit模型,我想绘制renting=0和renting=1时的预测变量。创建R中的预测概率,r,probability,logistic-regression,R,Probability,Logistic Regression,我有一个带有虚拟变量的logit模型,我想绘制renting=0和renting=1时的预测变量。创建cex2对象后,我收到一条错误消息,该对象显示: Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : variable lengths differ (found for 'clt2') In addition: Warning message:
cex2
对象后,我收到一条错误消息,该对象显示:
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : variable lengths differ (found for 'clt2')
In addition: Warning message:
'newdata' had 100 rows but variables found have 4139 rows
任何想法都将不胜感激
model1<-glm(adaycare~fincbtax+I(fincbtax^2)+clt2+clt6+got6+bot6+renting, family="binomial")
summary(model1)
exp(coef(model1))
#predicted values
newcex <-with(cex,data.frame(fincbtax=mean(fincbtax),clt2=mean(clt2),clt6=mean(clt6),got6=mean(got6),bot6=mean(bot6),renting=(1:0)))
newcex$rentingProb<-predict(model1,newdata=newcex,type="response")
cex2<-with(cex,data.frame(fincbtax=rep(seq(from=10000,to=500000,length.out=50),2),renting=(rep(0:1,each=50))))
cex3<-cbind(cex2,predict(model1,cex2,type="link",se=TRUE))
cex3 <- within(cex2, {
PredictedProb <- plogis(fit)
LL <- plogis(model1 - (1.96 * se.fit))
UL <- plogis(fit + (1.96 * se.fit))
})
model1具体哪一步导致错误?我敢打赌,错误是因为试图在predict
中使用cex2
。如果要在predict
中使用data.frame,则data.framecex2
中应包含所有解释变量。如果您想将一些值设置为它们的平均值,您可以像使用newcex
一样执行此操作,R将把它们循环到适当的长度。感谢您的回复。错误发生在cex2行,尝试将cex2
分配到data.frame(fincbtax=rep(seq(from=10000,to=500000,length.out=50),2),renting=(rep(0:1,each=50))
,然后(但我关于需要cex2
中所有解释变量的第一条评论仍然有效)。