R 언어 - 1초 Logistic 회귀 분석
13500 단어 R 데이터 분석
R 언어 - 1초 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