我想绘制作为glm模型一部分的每个变量,其中y轴是预测概率,x轴是变量水平或值 . 这是我为了做到这一点而尝试的代码:
数据:
dat <- read.table(text = "target apcalc admit num
0 0 0 21
0 0 1 24
0 1 0 55
0 1 1 72
1 0 0 5
1 0 1 31
1 1 0 11
1 1 1 3", header = TRUE)
glm模型:
f<-glm(target ~ apcalc + admit +num, data = dat,family=binomial(link='logit'))
提供所需图表的循环:
for(i in 1:length(f$var.names)){
plot(predict(f,i.var.names=i,newdata=dat,type='response'))
}
我得到一个奇怪的情节作为输出(x轴上的“索引”和y轴上的“预测(f,i.var.names = i,newdata = dat,type ='response')” . 我该如何修复我的代码是为了得到想要的结果?(我还没有声誉,以便在这里展示它)
2 回答
下图用预测的概率绘制所有变量,
在运行f <-glm(.....)部分时,f $ var.names给出NULL作为输出 . 那里肯定有一些错误 .