# 작업형 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, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1~
## $ name <chr> "Allen, Miss. Elisabeth Walton", "Allison, Mrs. Hudson J C (B~
## $ sex <chr> "female", "female", "male", "female", "male", "female", "male~
## $ age <dbl> 29, 25, 48, 63, 39, 53, 71, 47, 18, 24, 26, 24, 50, 36, 37, 4~
## $ sibsp <int> 0, 1, 0, 1, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1~
## $ parch <int> 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0~
## $ ticket <chr> "24160", "113781", "19952", "13502", "112050", "11769", "PC 1~
## $ fare <dbl> 211.3375, 151.5500, 26.5500, 77.9583, 0.0000, 51.4792, 49.504~
## $ cabin <chr> "B5", "C22 C26", "E12", "D7", "A36", "C101", "", "C62 C64", "~
## $ embarked <chr> "S", "S", "S", "S", "S", "S", "C", "C", "C", "C", "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, 654, 655, 655, 654, ...
## Resampling results across tuning parameters:
##
## cp ROC Sens Spec
## 0.02013423 0.8210281 0.5901149 0.9485604
## 0.03691275 0.7879134 0.5931034 0.8998339
## 0.47315436 0.6481898 0.4085057 0.8878738
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.02013423.
confusionMatrix(rffit)
## Cross-Validated (10 fold) Confusion Matrix
##
## (entries are percentual average cell counts across resamples)
##
## Reference
## Prediction 사망 생존
## 사망 24.2 3.0
## 생존 16.8 56.0
##
## Accuracy (average) : 0.8019
predict(rffit,test,type="prob")->rffit1
predict(rffit,test,type="raw")->rffit2
confusionMatrix(rffit2,test$survived)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 사망 생존
## 사망 55 5
## 생존 72 184
##
## Accuracy : 0.7563
## 95% CI : (0.7051, 0.8026)
## No Information Rate : 0.5981
## P-Value [Acc > NIR] : 2.314e-09
##
## Kappa : 0.4451
##
## Mcnemar's Test P-Value : 5.419e-14
##
## Sensitivity : 0.4331
## Specificity : 0.9735
## Pos Pred Value : 0.9167
## Neg Pred Value : 0.7188
## Prevalence : 0.4019
## Detection Rate : 0.1741
## Detection Prevalence : 0.1899
## Balanced Accuracy : 0.7033
##
## '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 2 2 1 2 2 2 1 1 2 2 2 2 2 2 1 2 1 2 1 2 2 1 1 2 1 1 1 2 2 2 2 1 2 2 2
## [38] 1 1 1 2 1 1 2 1 2 2 1 1 2 1 2 2 2 1 1 1 1 2 1 2 1 2 2 1 1 2 2 1 1 1 2 1 2
## [75] 1 2 1 1 1 2 2 1 2 1 2 1 2 2 2 1 2 2 1 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 1 2
## [112] 2 2 2 1 2 1 1 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 1 2 1 2 2 2 1 2 2 1 2 1 2 2 1
## [149] 1 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 2 2 2
## [186] 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 2 2 2 2 2
## [223] 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 2 2 2 2 2
## [260] 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 2 2 2 2 2
## [297] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
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 127 controls (test$survived 사망) < 189 cases (test$survived 생존).
## Area under the curve: 0.7033
result$auc
## Area under the curve: 0.7033