R语言——一秒Logistic回归分析

R语言——一秒Logistic回归分析

    • 话不多说,盘它
    • 代码:
    • 问题区:

话不多说,盘它

代码:

// 小龙虾:简易Logistic回归共享分析
// 直接复制进R.Studio,编译即可

# Read in Data
# myfile<-read.csv(file.choose(),header=FALSE) 
install.packages("AER")
library(AER)
data(CreditCard)
View(CreditCard)
summary(CreditCard)

bankcard <- CreditCard
write.csv(bankcard,file = "bankcard.csv")

# 一次生成训练和测试数据集
set.seed(102)
select <- sample(1:nrow(bankcard),nrow(bankcard)*0.7)
train <- bankcard[select,]
test <- bankcard[-select,]


card_glm <- glm(formula = card ~ ., family = "binomial", data = train)
summary(card_glm)



result <- predict(card_glm, newdata = test, type = "response")
test.y_hat <- ifelse(result > 0.5, "yes", "no")

result_all <- data.frame(Prediction=test.y_hat, 
              Prob_for_yes=round(result,3),
              Actual=test$card)
result_all

# 模型准确度评价
accuracy.lr <- sum(test.y_hat==test$card) / length(test.y_hat)
accuracy.lr

cm1 <- table(test$card, test.y_hat, dnn = c("Actual","Prediction"))
cm1


# 逐步回归,筛选变量
step(card_glm2, direction = "both", data = train)

bankcard <- subset(CreditCard, 
                   select = c(card, reports, expenditure, dependents, active))


# 更换变量后二次生成训练和测试数据集
set.seed(102)
select <- sample(1:nrow(bankcard),nrow(bankcard)*0.8)
train <- bankcard[select,]
test <- bankcard[-select,]


card_glm2 <- glm(formula = card ~ ., family = "binomial", data = train)
summary(card_glm2)


result <- predict(card_glm2, newdata = test, type = "response")
test.y_hat <- ifelse(result > 0.5, "yes", "no")

result_all <- data.frame(Prediction=test.y_hat, 
                         Prob_for_yes=round(result,3),
                         Actual=test$card);


accuracy.lr <- sum(test.y_hat==test$card) / length(test.y_hat)
accuracy.lr

cm2 <- table(test$card, test.y_hat, dnn = c("Actual","Prediction"))
cm2


问题区:

  1. Warning messages:
    1: glm.fit:算法没有聚合
    2: glm.fit:拟合機率算出来是数值零或一

说明:在此模型中,该问题不影响结果。但无法解决,本人修改迭代次数后,仍然如此。

  1. 联系方式: [email protected]

你可能感兴趣的:(R数据分析)