# 작업형 3
rm(list=ls())
# titanic data
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.1.3에서 작성되었습니다
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(recipes)
## Warning: 패키지 'recipes'는 R 버전 4.1.3에서 작성되었습니다
## 
## 다음의 패키지를 부착합니다: 'recipes'
## The following object is masked from 'package:stats':
## 
##     step
library(caret)
## Warning: 패키지 'caret'는 R 버전 4.1.3에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: ggplot2
## Warning: 패키지 'ggplot2'는 R 버전 4.1.3에서 작성되었습니다
## 필요한 패키지를 로딩중입니다: lattice
read.delim("titanic3.txt",header=TRUE,sep=",")->full
full %>% glimpse
## Rows: 1,309
## Columns: 14
## $ pclass    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ~
## $ survived  <int> 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, ~
## $ name      <chr> "Allen, Miss. Elisabeth Walton", "Allison, Master. Hudson Tr~
## $ sex       <chr> "female", "male", "female", "male", "female", "male", "femal~
## $ age       <dbl> 29.00, 0.92, 2.00, 30.00, 25.00, 48.00, 63.00, 39.00, 53.00,~
## $ sibsp     <int> 0, 1, 1, 1, 1, 0, 1, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, ~
## $ parch     <int> 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, ~
## $ ticket    <chr> "24160", "113781", "113781", "113781", "113781", "19952", "1~
## $ fare      <dbl> 211.3375, 151.5500, 151.5500, 151.5500, 151.5500, 26.5500, 7~
## $ cabin     <chr> "B5", "C22 C26", "C22 C26", "C22 C26", "C22 C26", "E12", "D7~
## $ embarked  <chr> "S", "S", "S", "S", "S", "S", "S", "S", "S", "C", "C", "C", ~
## $ boat      <chr> "2", "11", "", "", "", "3", "10", "", "D", "", "", "4", "9",~
## $ body      <int> NA, NA, NA, 135, NA, NA, NA, NA, NA, 22, 124, NA, NA, NA, NA~
## $ home.dest <chr> "St Louis, MO", "Montreal, PQ / Chesterville, ON", "Montreal~
#.1 데이터 분할
train_list<-createDataPartition(y=full$survived,p=0.7,list=FALSE)
full_train<-full[train_list,]
full_test<-full[-train_list,]
NROW(full_train)
## [1] 917
NROW(full_test)
## [1] 392
train<-full_train
test<-full_test
train %>% mutate(index='train')->train
test %>% mutate(index='test')->test
bind_rows(train,test)->full
full %>% select(-boat,-body,-home.dest)->full
full %>% glimpse
## Rows: 1,309
## Columns: 12
## $ pclass   <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ survived <int> 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0~
## $ name     <chr> "Allen, Miss. Elisabeth Walton", "Allison, Miss. Helen Lorain~
## $ sex      <chr> "female", "female", "male", "female", "male", "male", "male",~
## $ age      <dbl> 29, 2, 30, 25, 48, 39, 71, 18, 24, 26, 80, 24, 50, 32, 26, 42~
## $ sibsp    <int> 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0~
## $ parch    <int> 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0~
## $ ticket   <chr> "24160", "113781", "113781", "113781", "19952", "112050", "PC~
## $ fare     <dbl> 211.3375, 151.5500, 151.5500, 151.5500, 26.5500, 0.0000, 49.5~
## $ cabin    <chr> "B5", "C22 C26", "C22 C26", "C22 C26", "E12", "A36", "", "C62~
## $ embarked <chr> "S", "S", "S", "S", "S", "S", "C", "C", "C", "S", "S", "C", "~
## $ index    <chr> "train", "train", "train", "train", "train", "train", "train"~
# 2. 목표변수, 기타변수 변환
full$survived<-ifelse(full$survived==0,"생존","사망")
full$survived<-as.factor(full$survived)
full$pclass<-as.factor(full$pclass)
full$sex<-as.factor(full$sex)
full$embarked<-as.factor(full$embarked)
# 3. 결측치 확인
colSums(is.na(full))
##   pclass survived     name      sex      age    sibsp    parch   ticket 
##        0        0        0        0      263        0        0        0 
##     fare    cabin embarked    index 
##        1        0        0        0
summary(full)
##  pclass  survived       name               sex           age       
##  1:323   사망:500   Length:1309        female:466   Min.   : 0.17  
##  2:277   생존:809   Class :character   male  :843   1st Qu.:21.00  
##  3:709              Mode  :character                Median :28.00  
##                                                     Mean   :29.88  
##                                                     3rd Qu.:39.00  
##                                                     Max.   :80.00  
##                                                     NA's   :263    
##      sibsp            parch          ticket               fare        
##  Min.   :0.0000   Min.   :0.000   Length:1309        Min.   :  0.000  
##  1st Qu.:0.0000   1st Qu.:0.000   Class :character   1st Qu.:  7.896  
##  Median :0.0000   Median :0.000   Mode  :character   Median : 14.454  
##  Mean   :0.4989   Mean   :0.385                      Mean   : 33.295  
##  3rd Qu.:1.0000   3rd Qu.:0.000                      3rd Qu.: 31.275  
##  Max.   :8.0000   Max.   :9.000                      Max.   :512.329  
##                                                      NA's   :1        
##     cabin           embarked    index          
##  Length:1309         :  2    Length:1309       
##  Class :character   C:270    Class :character  
##  Mode  :character   Q:123    Mode  :character  
##                     S:914                      
##                                                
##                                                
## 
table(full$embarked)
## 
##       C   Q   S 
##   2 270 123 914
levels(full$embarked)[1]<-NA
# table( ) 함수는 NA 값을 제외하고 값을 출력시키므로, useNA에 
# always를 지정해 NA에 대한 개수도 출력하도록 하여 빈도를 확인했다. 
# 지정해 NA에 대한 개수도 출력하도록 하여 빈도를 확인했다.
table(full$embarked,useNA="always")
## 
##    C    Q    S <NA> 
##  270  123  914    2
full %>% filter(!is.na(age)&!is.na(fare)&!is.na(embarked))->full
colSums(is.na(full))
##   pclass survived     name      sex      age    sibsp    parch   ticket 
##        0        0        0        0        0        0        0        0 
##     fare    cabin embarked    index 
##        0        0        0        0
# 4 데이터 전처리
recipe(survived~.,data=full) %>% step_YeoJohnson(age,sibsp,parch,fare) %>% 
  step_center(age,sibsp,parch,fare) %>% 
  step_scale(age,sibsp,parch,fare) %>% 
  prep() %>% juice()->data
full %>% filter(index=="train") %>% select(-index,-name,-ticket,-cabin)->train
full %>% filter(index=='test') %>% select(-index,-name,-ticket,-cabin)->test
ctrl<-trainControl(method="cv",summaryFunction = twoClassSummary,
                   classProbs = TRUE)
train(survived~.,data=train,
      method="rpart",metric='ROC',
      trControl=ctrl)->rffit
rffit
## CART 
## 
## 727 samples
##   7 predictor
##   2 classes: '사망', '생존' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 654, 654, 655, 655, 653, 654, ... 
## Resampling results across tuning parameters:
## 
##   cp          ROC        Sens       Spec     
##   0.01980198  0.8173234  0.6925806  0.8656146
##   0.02310231  0.7810777  0.6833333  0.8467885
##   0.48184818  0.6412249  0.3980645  0.8843854
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.01980198.
confusionMatrix(rffit)
## Cross-Validated (10 fold) Confusion Matrix 
## 
## (entries are percentual average cell counts across resamples)
##  
##           Reference
## Prediction 사망 생존
##       사망 28.9  7.8
##       생존 12.8 50.5
##                             
##  Accuracy (average) : 0.7937
predict(rffit,test,type="prob")->rffit1
predict(rffit,test,type="raw")->rffit2

confusionMatrix(rffit2,test$survived)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction 사망 생존
##       사망   75   23
##       생존   47  171
##                                          
##                Accuracy : 0.7785         
##                  95% CI : (0.7286, 0.823)
##     No Information Rate : 0.6139         
##     P-Value [Acc > NIR] : 3.198e-10      
##                                          
##                   Kappa : 0.515          
##                                          
##  Mcnemar's Test P-Value : 0.005977       
##                                          
##             Sensitivity : 0.6148         
##             Specificity : 0.8814         
##          Pos Pred Value : 0.7653         
##          Neg Pred Value : 0.7844         
##              Prevalence : 0.3861         
##          Detection Rate : 0.2373         
##    Detection Prevalence : 0.3101         
##       Balanced Accuracy : 0.7481         
##                                          
##        'Positive' Class : 사망           
## 
library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## 다음의 패키지를 부착합니다: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
rffit2_num<-as.numeric(rffit2)
rffit2_num
##   [1] 2 1 1 2 2 2 1 2 1 1 2 1 2 1 2 2 2 2 2 2 1 2 2 2 1 1 2 1 1 2 1 2 1 1 1 2 2
##  [38] 1 1 2 1 2 2 2 2 2 2 1 1 2 2 2 1 1 2 2 1 2 1 1 2 2 1 2 1 2 2 2 1 2 1 2 2 1
##  [75] 2 2 2 2 2 2 2 2 1 1 2 2 1 2 1 2 1 2 1 1 1 1 2 1 1 2 2 1 2 2 2 2 2 2 2 2 1
## [112] 1 2 2 1 1 2 2 2 2 2 1 1 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 1 1 1 1
## [149] 1 2 1 1 1 1 1 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 1 2 2 1 1 2
## [186] 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [223] 2 2 1 2 2 2 2 1 2 1 1 2 2 2 2 2 1 2 1 2 2 1 1 2 2 2 2 2 2 2 1 2 2 1 2 1 2
## [260] 2 1 2 1 2 1 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 1 2 2 2 2 2
## [297] 1 2 2 2 1 2 2 2 2 1 1 2 2 1 2 2 1 2 2 1
result<-roc(test$survived,rffit2_num)
## Setting levels: control = 사망, case = 생존
## Setting direction: controls < cases
result
## 
## Call:
## roc.default(response = test$survived, predictor = rffit2_num)
## 
## Data: rffit2_num in 122 controls (test$survived 사망) < 194 cases (test$survived 생존).
## Area under the curve: 0.7481
result$auc
## Area under the curve: 0.7481