ls()
## character(0)
rm(list = ls())
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
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~
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
set.seed(123)
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
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, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1~
## $ name     <chr> "Allison, Master. Hudson Trevor", "Allison, Miss. Helen Lorai~
## $ sex      <chr> "male", "female", "female", "male", "male", "female", "male",~
## $ age      <dbl> 0.92, 2.00, 25.00, 48.00, 39.00, 53.00, 71.00, 47.00, 24.00, ~
## $ sibsp    <int> 1, 1, 1, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0~
## $ parch    <int> 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ ticket   <chr> "113781", "113781", "113781", "19952", "112050", "11769", "PC~
## $ fare     <dbl> 151.5500, 151.5500, 151.5500, 26.5500, 0.0000, 51.4792, 49.50~
## $ cabin    <chr> "C22 C26", "C22 C26", "C22 C26", "E12", "A36", "C101", "", "C~
## $ embarked <chr> "S", "S", "S", "S", "S", "S", "C", "C", "C", "S", "C", "C", "~
## $ index    <chr> "train", "train", "train", "train", "train", "train", "train"~
full$survived<-ifelse(full$survived==0,"생존","사망")
full$survived<-as.factor(full$survived)
table(full$embarked)
## 
##       C   Q   S 
##   2 270 123 914
summary(is.na(full))
##    pclass         survived          name            sex         
##  Mode :logical   Mode :logical   Mode :logical   Mode :logical  
##  FALSE:1309      FALSE:1309      FALSE:1309      FALSE:1309     
##                                                                 
##     age            sibsp           parch           ticket       
##  Mode :logical   Mode :logical   Mode :logical   Mode :logical  
##  FALSE:1046      FALSE:1309      FALSE:1309      FALSE:1309     
##  TRUE :263                                                      
##     fare           cabin          embarked         index        
##  Mode :logical   Mode :logical   Mode :logical   Mode :logical  
##  FALSE:1308      FALSE:1309      FALSE:1309      FALSE:1309     
##  TRUE :1
full$pclass<-as.factor(full$pclass)
full$sex<-as.factor(full$sex)
full$embarked<-as.factor(full$embarked)
#결치값 확인가능
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                      
##                                                
##                                                
## 
full %>% glimpse
## Rows: 1,309
## Columns: 12
## $ pclass   <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ survived <fct> 사망, 생존, 생존, 사망, 생존, 사망, 생존, 생존, 사망, 생존, ~
## $ name     <chr> "Allison, Master. Hudson Trevor", "Allison, Miss. Helen Lorai~
## $ sex      <fct> male, female, female, male, male, female, male, male, female,~
## $ age      <dbl> 0.92, 2.00, 25.00, 48.00, 39.00, 53.00, 71.00, 47.00, 24.00, ~
## $ sibsp    <int> 1, 1, 1, 0, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0~
## $ parch    <int> 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0~
## $ ticket   <chr> "113781", "113781", "113781", "19952", "112050", "11769", "PC~
## $ fare     <dbl> 151.5500, 151.5500, 151.5500, 26.5500, 0.0000, 51.4792, 49.50~
## $ cabin    <chr> "C22 C26", "C22 C26", "C22 C26", "E12", "A36", "C101", "", "C~
## $ embarked <fct> S, S, S, S, S, S, C, C, C, S, C, C, C, C, C, C, S, C, C, C, S~
## $ index    <chr> "train", "train", "train", "train", "train", "train", "train"~
levels(full$embarked)[1]<-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
levels(full$embarked)
## [1] "C" "Q" "S"
library(recipes)
## Warning: 패키지 'recipes'는 R 버전 4.1.3에서 작성되었습니다
## 
## 다음의 패키지를 부착합니다: 'recipes'
## The following object is masked from 'package:stats':
## 
##     step
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
data %>% glimpse
## Rows: 1,043
## Columns: 12
## $ pclass   <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1~
## $ name     <fct> "Allison, Master. Hudson Trevor", "Allison, Miss. Helen Lorai~
## $ sex      <fct> male, female, female, male, male, female, male, male, female,~
## $ age      <dbl> -2.373513978, -2.236510138, -0.271313496, 1.227445945, 0.6675~
## $ sibsp    <dbl> 1.2828274, 1.2828274, 1.2828274, -0.7239369, -0.7239369, 1.59~
## $ parch    <dbl> 1.7467662, 1.7467662, 1.7467662, -0.6019626, -0.6019626, -0.6~
## $ ticket   <fct> 113781, 113781, 113781, 19952, 112050, 11769, PC 17609, PC 17~
## $ fare     <dbl> 1.8357480, 1.8357480, 1.8357480, 0.3451848, -4.3263865, 0.962~
## $ cabin    <fct> C22 C26, C22 C26, C22 C26, E12, A36, C101, , C62 C64, B35, B5~
## $ embarked <fct> S, S, S, S, S, S, C, C, C, C, C, C, C, C, C, S, C, C, C, S, S~
## $ index    <fct> train, train, train, train, train, train, train, train, train~
## $ survived <fct> 사망, 생존, 생존, 사망, 생존, 사망, 생존, 생존, 사망, 생존, ~
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 
## 
## 722 samples
##   7 predictor
##   2 classes: '사망', '생존' 
## 
## No pre-processing
## Resampling: Cross-Validated (10 fold) 
## Summary of sample sizes: 650, 650, 649, 649, 650, 650, ... 
## Resampling results across tuning parameters:
## 
##   cp          ROC        Sens       Spec     
##   0.02678571  0.7978806  0.5392857  0.9637879
##   0.06071429  0.7698368  0.5678571  0.8956061
##   0.41428571  0.6037734  0.3071429  0.9004040
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.02678571.
predict(rffit,test,type="prob")->rffit1
predict(rffit,test,type="raw")->rffit2
head(rffit2)
## [1] 사망 생존 사망 사망 사망 생존
## Levels: 사망 생존
head(rffit1)
##        사망       생존
## 1 0.9079755 0.09202454
## 2 0.1951754 0.80482456
## 3 0.9079755 0.09202454
## 4 0.9079755 0.09202454
## 5 0.9079755 0.09202454
## 6 0.1951754 0.80482456
levels(test$survived)
## [1] "사망" "생존"
test$survived<-as.factor(test$survived)
confusionMatrix(rffit2,test$survived)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction 사망 생존
##       사망   70    1
##       생존   75  175
##                                           
##                Accuracy : 0.7632          
##                  95% CI : (0.7129, 0.8087)
##     No Information Rate : 0.5483          
##     P-Value [Acc > NIR] : 1.115e-15       
##                                           
##                   Kappa : 0.4995          
##                                           
##  Mcnemar's Test P-Value : < 2.2e-16       
##                                           
##             Sensitivity : 0.4828          
##             Specificity : 0.9943          
##          Pos Pred Value : 0.9859          
##          Neg Pred Value : 0.7000          
##              Prevalence : 0.4517          
##          Detection Rate : 0.2181          
##    Detection Prevalence : 0.2212          
##       Balanced Accuracy : 0.7385          
##                                           
##        'Positive' Class : 사망            
##