R中逻辑回归的分类表 [英] Classification table for logistic regression in R

查看:175
本文介绍了R中逻辑回归的分类表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,该数据集由二分因变量( Y )和12个自变量( X1 组成) X12 )存储在csv文件中。这是数据的前5行:

I have a data set consisting of a dichotomous depending variable (Y) and 12 independent variables (X1 to X12) stored in a csv file. Here are the first 5 rows of the data:

Y,X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12  
0,9,3.86,111,126,14,13,1,7,7,0,M,46-50  
1,7074,3.88,232,4654,143,349,2,27,18,6,M,25-30  
1,5120,27.45,97,2924,298,324,3,56,21,0,M,31-35
1,18656,79.32,408,1648,303,8730,286,294,62,28,M,25-30
0,3869,21.23,260,2164,550,320,3,42,203,3,F,18-24

我使用以下代码从数据中构建了逻辑回归模型:

I constructed a logistic regression model from the data using the following code:

mydata <- read.csv("data.csv")     
mylogit <- glm(Y~X1+X2+X3+X4+X5+X6+X7+X8+X9+X10+X11+X12, data=mydata, 
               family="binomial")  
mysteps <- step(mylogit, Y~X1+X2+X3+X4+X5+X6+X7+X8+X9+X10+X11+X12, data=mydata, 
                family="binomial")

我可以使用以下代码获取每个数据的预测概率:

I can obtain the predicted probabilities for each data using the code:

theProbs <- fitted(mysteps)

现在,我想使用数据表的前20行( mydata )创建一个分类表,从中可以确定百分比实际与数据相符的预测概率。请注意,对于因变量( Y ),0表示小于0.5的概率,而1表示大于0.5的概率。

Now, I would like to create a classification table--using the first 20 rows of the data table (mydata)--from which I can determine the percentage of the predicted probabilities that actually agree with the data. Note that for the dependent variable (Y), 0 represents probability that is less than 0.5, and 1 represents probability that is greater than 0.5.

我花了很多时间试图构建分类,但没有成功。如果有人建议可以帮助解决此问题的代码,我将不胜感激。

I have spent many hour trying to construct the classification without success. I would appreciate it very much if someone suggest code that can help to solve this problem.

推荐答案

问题有点老了,但是我认为如果有人在看档案,这可能会有所帮助。
这可以通过xtabs轻松完成

Question is a bit old, but I figure if someone is looking though the archives, this may help. This is easily done by xtabs

classDF <- data.frame(response = mydata$Y, predicted = round(fitted(mysteps),0))

xtabs(~ predicted + response, data = classDF)

将会产生如下表格:

           response
predicted   0   1
        0 339 126
        1 130 394

这篇关于R中逻辑回归的分类表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆