{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"private_outputs":true,"provenance":[],"authorship_tag":"ABX9TyMkS2xKZMMHJ3eViJZCIDEX"},"kernelspec":{"name":"ir","display_name":"R"},"language_info":{"name":"R"}},"cells":[{"cell_type":"code","source":["install.packages(\"caret\")\n","install.packages(\"naniar\")\n","install.packages(\"randomForest\")\n","install.packages(\"tidyverse\")\n","install.packages(\"psych\")\n","install.packages(\"mice\")\n","install.packages(\"pROC\")\n","install.packages(\"readxl\")\n","install.packages(\"openxlsx\")\n","install.packages(\"ggplot2\")"],"metadata":{"id":"_z67-XXqBMNn"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["library(caret)\n","library(tidyverse)\n","library(psych) #데이터 탐색\n","library(naniar) #결측치 처리 패키지\n","library(mice) #결측치 처리 패키지\n","library(pROC)\n","library(randomForest)\n","\n","library(readxl)\n","library(openxlsx)\n","library(ggplot2)"],"metadata":{"id":"X8gj3gvugQhX"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["washer <- read_excel(\"input/washer_분석용 데이터.xls\")\n","\n","washer <- washer[, -c(17, 18, 19)] #필요없는 끝에 열 삭제\n","# 종속 변수를 팩터로 변환\n","washer$BrandSwitching <- as.factor(washer$BrandSwitching)\n","washer"],"metadata":{"id":"yy_NjClAdwj5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["nrow(washer)"],"metadata":{"id":"Uxm91rgLglBZ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["ggplot(washer, aes(x = Age_Group)) +\n"," geom_histogram(aes(y = ..density..), binwidth = 1)+\n"," geom_density(color = \"red\", size = 1)"],"metadata":{"id":"Uv2Sbyqmdwqo"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["ggplot(washer, aes(x = FamilyLifeCycle)) +\n"," geom_histogram(aes(y = ..density..), binwidth = 1)+\n"," geom_density(color = \"red\", size = 1)"],"metadata":{"id":"lWiW-v4AdwtK"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["ggplot(washer, aes(x = Purchase_Experience_Recency)) +\n"," geom_histogram(aes(y = ..density..), binwidth = 1)+\n"," geom_density(color = \"red\", size = 1)"],"metadata":{"id":"RrYr77Op_24P"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["ggplot(washer, aes(x = Age_Group, fill = factor(BrandSwitching))) +\n"," geom_bar() +\n"," geom_text(aes(label = ..count..), stat = \"count\", position = position_stack(0.5))"],"metadata":{"id":"HinJDORu_6Gv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["ggplot(washer, aes(x = Sex, fill = factor(BrandSwitching))) +\n"," geom_bar() +\n"," geom_text(aes(label = ..count..), stat = \"count\", position = position_stack(0.5))"],"metadata":{"id":"VirKEm2y_8Sl"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["washer$Overall_RFM_Score2 <- cut(washer$Overall_RFM_Score, breaks = seq(0, 600, 20), right = F)"],"metadata":{"id":"6ViQM3Ss_-AD"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["ggplot(washer, aes(Overall_RFM_Score, fill = BrandSwitching)) +\n"," geom_histogram()"],"metadata":{"id":"31XyAv-___KT"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["table(washer$Overall_RFM_Score2, washer$BrandSwitching)\n","prop.table(table(washer$Overall_RFM_Score2, washer$BrandSwitching), 1)"],"metadata":{"id":"GyOOgs37AAkN"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#-----분류 및 예측-----#\n","train.DF <- washer[30:90, ]\n","test.DF <- washer[-c(30:90), ]"],"metadata":{"id":"2ApDcUG0AC-_"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#train 데이터셋을 분할하여 검증(validation) 데이터셋을 만듭니다.\n","set.seed(1)\n","train.index <- createDataPartition(train.DF$BrandSwitching, p = 0.9, list = FALSE)\n","train.0.9.DF <- train.DF[train.index, ]\n","valid.DF <- train.DF[-train.index, ]"],"metadata":{"id":"suFCbMKZAEcD"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# 종속 변수를 팩터로 변환\n","washer$BrandSwitching <- as.factor(washer$BrandSwitching)\n"],"metadata":{"id":"d1cN9LoSgy5f"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["set.seed(1234)\n","rf.model <- randomForest(BrandSwitching ~ Sex + Age_Group + Marriage + FamilyLifeCycle + Purchase_Experience_Recency + LX_Curve_Pattern + Overall_RFM_Score +\n"," Peformance_Experience + Quality_Experience + Utility_Experience + SAT + BrandLove + CCB + Loyalty,\n"," data = train.0.9.DF, importance = T)\n","\n","rf.model"],"metadata":{"id":"t4D0TM--AG2Z"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["valid.predict.class <- predict(rf.model, newdata = valid.DF, type = \"class\")\n","valid.predict.class"],"metadata":{"id":"bwNh3e9BAIAW"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# 랜덤 포레스트 모델을 이미 생성한 경우, 다음과 같이 예측을 수행합니다.\n","\n","# 예측 수행 (loyalty 변수를 중심으로 예측)\n","rf.predictions_loyalty <- predict(rf.model, newdata = washer)\n","\n","# 예측 결과와 실제 레이블 비교\n","actual_labels <- washer$BrandSwitching\n","\n","# Confusion Matrix 생성\n","confusion_matrix_loyalty <- table(Actual = actual_labels, Predicted = rf.predictions_loyalty)\n","\n","# Classification table 출력\n","print(confusion_matrix_loyalty)\n","\n","# 또는 Classification table을 보다 읽기 쉽게 출력하려면 아래와 같이 사용 가능\n","library(caret)\n","confusionMatrix(confusion_matrix_loyalty)\n"],"metadata":{"id":"WNLCX1PlcYNJ"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["varImpPlot(rf.model, pch = 19, main = \"\")\n","\n","round(importance(rf.model), 2)"],"metadata":{"id":"unv0o_8RAK5q"},"execution_count":null,"outputs":[]}]}