R로 배우는 Machine Learning 기반 마케팅 분석
Verena Pflieger
Data Scientist at INWT Statistics
1) 데이터셋을 훈련 데이터와 테스트 데이터로 분할
# Generating random index for training and test set
# set.seed ensures reproducibility of random components
set.seed(534381)
churnData$isTrain <- rbinom(nrow(churnData), 1, 0.66)
train <- subset(churnData, churnData$isTrain == 1)
test <- subset(churnData, churnData$isTrain == 0)
2) 훈련 데이터를 기반으로 모델 구축
# Modeling logitTrainNew
logitTrainNew <- glm( returnCustomer ~ title + newsletter +
websiteDesign + paymentMethod + couponDiscount +
purchaseValue + throughAffiliate +
shippingFees + dvd + blueray + vinyl +
videogameDownload + prodOthers + prodRemitted,
family = binomial, data = train)
# Out-of-sample prediction for logitTrainNew
test$predNew <- predict(logitTrainNew, type = "response",
newdata = test)
# Calculating the confusion matrix
confMatrixNew <- confusion.matrix(test$returnCustomer, test$predNew,
threshold = 0.3)
confMatrixNew
# Calculating the accuracy
accuracyNew <- sum(diag(confMatrixNew)) / sum(confMatrixNew)
accuracyNew
obs
pred 0 1
0 11939 2449
1 716 350
0.7951987

교차 검증 정확도 계산
library(boot)
# Accuracy function with threshold = 0.3
Acc03 <- function(r, pi = 0) {
cm <- confusion.matrix(r, pi, threshold = 0.3)
acc <- sum(diag(cm)) / sum(cm)
return(acc)}
# Accuracy
set.seed(534381)
cv.glm(churnData, logitModelNew, cost = Acc03, K = 6)$delta
0.7943894
| 로지스틱 회귀 학습 내용 | |
|---|---|
| 학습한 내용... | 이탈 가능성이 높은 온라인 쇼핑몰 고객을 예측하는 방법 |
| 이진 로지스틱 회귀를 사용하여 확률을 계산하는 방법 | |
| 임계값 선택이 매우 중요하다는 점 |
| 모델에서 얻은 학습 내용 | |
|---|---|
| 학습한 내용... | 뉴스레터에 가입한 고객은 재방문 가능성이 높다는 점 |
| 쿠폰을 사용한 고객은 재방문 가능성이 낮다는 점 | |
| 배송비가 없는 고객은 재방문 가능성이 높다는 점 | |
| 기타... |
R로 배우는 Machine Learning 기반 마케팅 분석