Logistic回归的相反预测结果 - glm()

时间:2014-01-21 04:27:03

标签: r logistic-regression

我的预测器有两个级别:

>table(predictor)

predictor
    1     2 
12115  4257 

我的回答也有两个级别:

> table(response)
response
    1     0 
12115  4257 

所以,显然,这里存在完全线性的相关性。但是当我之后进行逻辑回归和预测时,我得到了这个:

logit = glm(response~predictor, data=data, family="binomial")
pred = predict(logit, newdata=data, type="response")

然而,预测给出了与实际数据完全相反的结果:

pred                       1     0
  2.90070146547072e-12 12115     0
  0.999999999997099        0  4257

有人知道这里出了什么问题吗?怎么能解释这个?

谢谢!

1 个答案:

答案 0 :(得分:0)

预测因子(显然)是1,2级的因子。然而,响应是0,1级的一个因素。

首先,确保预测变量实际存储为因子;不是整数。

predictor <- as.factor(predictor)

“然而,预测给出了与实际数据完全相反的结果”

'实际数据'是什么意思:预测变量{1,2}的实际值,或响应{0,1}的实际值?

如果要使用predict()的输出向量来预测原始预测变量,则不能使用原始预测变量,需要对其进行阈值处理并将其索引到其水平向量c(1,2)。或者只使用ifelse

pred <- predict(logit, newdata=data, type="response")
threshold <- 0.5 # or whatever threshold you use
pred <- ifelse(pred>threshold, 1, 2)

另外,请向我们展示@John要求的表格(预测器,响应)。

相关问题