逻辑回归混淆矩阵

Logistic regression confusion matrix

下面是我本学期作业的部分代码:

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。我不确定为什么会这样,希望有人能引导我的方向是正确的。

谢谢大家

如果这确实是您的输出,请注意 Yes - yes 和 No - no 的不同拼写。干杯