【发布时间】:2023-04-05 13:09:02
【问题描述】:
下面是我本学期正在做的作业的部分代码:
fit2=glm(card~reports+income+age+owner+dependents+months+share, data=new_credit2, family="binomial")
summary(fit2)
####Part G####
pred_prob=predict(fit2,type="response")
head(pred_prob)
length(pred_prob)
# The contrasts() function indicates that R has created a dummy variable with a 1 for =Yes
contrasts(card)
# The following command creates a vector of 1,319 No elements
glm.pred=rep("No",1319)
#The following command transforms all the elements with predicted probabilities of acceptance
greater than 0.5 from No to Yes
glm.pred[pred_prob>.5]="Yes"
head(glm.pred)
head(card)
#table() produces a confusion matrix to determine how many observations were correctly or
incorrectly classified
table(glm.pred,card)
# mean(): computes fraction of individual for which the prediction was correct
mean(glm.pred==card)
当我运行它时,我得到一个如下所示的矩阵:
card
glm.pred no yes
No 86 232
Yes 210 791
然而,当我运行 mean() 函数来尝试获得正确预测的分数时,我得到的结果为 0。我不确定为什么会发生这种情况,并希望有人能引导我朝着正确的方向前进。
谢谢大家
【问题讨论】:
标签: r logistic-regression confusion-matrix