逻辑模型的拟合优度检验

  • 本文关键字:检验 拟合 模型 r
  • 更新时间 :
  • 英文 :


我想知道g是什么意思,为什么要在研究中使用Lemeshow拟合优度(GOF(检验? "逻辑回归的混淆矩阵"有什么问题? 此消息 :

混淆矩阵(cnfmat(中的错误: 找不到函数"混乱矩阵">

# ..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)))

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

更一般地说,研究中的任何拟合优度 (GOF( 检验都用于确定模型与数据变异性的拟合优度。

此外,您会收到错误,因为confusionMatrix()函数是caretR 包的一部分。 首先在 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)

最新更新