【问题标题】:Goodness of fit test for logistic model逻辑模型的拟合优度检验
【发布时间】:2020-05-30 11:43:27
【问题描述】:

我想知道 g 的含义以及为什么在研究中使用 Lemeshow 拟合优度 (GOF) 测试? “逻辑回归的混淆矩阵有什么问题? 此消息:

confusionMatrix(cnfmat) 中的错误: 找不到函数“confusionMatrix”

# ..Binary Logistic Regression :

install.packages("caTools")
library(caTools)
require(caTools)
sample = sample.split(diabetes$Outcome, SplitRatio=0.80)
train = subset(diabetes, sample==TRUE)
test = subset(diabetes, sample==FALSE)
nrow(diabetes) ##calculationg the total number of rows 
nrow(train) ## total number of Train data rows >> 0.80 * 768
nrow(test) ## total number of Test data rows   >> 0.20 * 768
str(train) ## Structure of train set

Logis_mod<- glm(Outcome~Pregnancies+Glucose+BloodPressure+SkinThickness+
                  Insulin+BMI+DiabetesPedigreeFunction+Age,family = binomial,data = train)
summary(Logis_mod)
#AIC .. Akaike information criteria ...
#A good model is the one that has minimum AIC among all the other models.

# Testing the Model
glm_probs <- predict(Logis_mod, newdata = test, type = "response")
summary(glm_probs)
glm_pred <- ifelse(glm_probs > 0.5, 1, 0)
summary(glm_pred)

#Avarge prediction for each of the Two outcomes ..
tapply(glm_pred,train$Outcome,mean)

# Confusion Matrix for logistic regression
install.packages("e1071")
library(e1071)
prdval <-predict(Logis_mod,type = "response")
prdbln <-ifelse(prdval > 0.5, 1, 0)
cnfmat <-table(prd=prdbln,act =train$Outcome)
confusionMatrix(cnfmat)

#Odd Ratio :
exp(cbind("OR"=coef(Logis_mod),confint(Logis_mod)))

【问题讨论】:

  • install.packages("caret"); library(caret)
  • 您是否有可以包含在此问题中的示例数据,以便可以复制您的错误?

标签: r


【解决方案1】:

我不确定您指的是什么“g”,但我会假设这是您从 Lemeshow 测试得出的计算统计数据。如果是这种情况,则“g”值表示模型解释数据可变性的程度,并可用于比较基于同一组数据的模型(更好的模型将具有更大的“g”值)。

更一般地说,研究中的任何拟合优度 (GOF) 测试都用于确定您的模型与数据可变性的拟合程度。

此外,您收到错误是因为confusionMatrix() 函数是caret R 包的一部分。通过首先在 R 或 RStudio 中运行以下代码行来安装 caret

install.packages("caret")

然后在你的代码中改变

cnfmat <-table(prd=prdbln,act =train$Outcome)
confusionMatrix(cnfmat)

cnfmat <-data.frame(prd=prdbln,act =train$Outcome, stringsAsFactors = FALSE)
caret::confusionMatrix(cnfmat)

【讨论】:

  • 我这样做了caret::confusionMatrix(cnfmat)but 仍然是错误的>>> !all.equal(nrow(data), ncol(data)) 中的错误:无效的参数类型
  • 看起来像 confusionMarix() 依赖于需要输入向量、数组或 data.frame 的 nrowncol 函数。尝试将cnfmat &lt;-table(prd=prdbln,act =train$Outcome) 更改为cnfmat &lt;-data.frame(prd=prdbln,act =train$Outcome)
  • @MAJDAHMOBAJER 这解决了您的问题吗?如果是这样,请选择答案以结束此问题。最美好的祝愿!
  • 出了点问题 ::confusionMatrix(glm_pred,di_testing$Outcome) 错误:datareference 应该是相同水平的因子。 >
  • @MAJDAHMOBAJER 您使用的是什么版本的 R?我已经更新了我发布的代码,将stringsAsFactors = FALSE 包含在data.frame() 函数中,以防您不使用至少R-4.0.0(其中stringsAsFactors = FALSE 现在是默认值)。
猜你喜欢
  • 2018-08-22
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
相关资源
最近更新 更多