library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
df<-read.csv("c:/data/disease.csv")
glimpse(df)
## Rows: 4
## Columns: 194
## $ year <int> 1999, 2000, 2001, 2002
## $ Afghanistan <int> 0, 0, 0, 0
## $ Albania <dbl> 89.0, 132.0, 54.0, 4.9
## $ Algeria <dbl> 25.0, 0.0, 14.0, 0.7
## $ Andorra <dbl> 245.0, 138.0, 312.0, 12.4
## $ Angola <dbl> 217.0, 57.0, 45.0, 5.9
## $ Antigua...Barbuda <dbl> 102.0, 128.0, 45.0, 4.9
## $ Argentina <dbl> 193.0, 25.0, 221.0, 8.3
## $ Armenia <dbl> 21.0, 179.0, 11.0, 3.8
## $ Australia <dbl> 261.0, 72.0, 212.0, 10.4
## $ Austria <dbl> 279.0, 75.0, 191.0, 9.7
## $ Azerbaijan <dbl> 21.0, 46.0, 5.0, 1.3
## $ Bahamas <dbl> 122.0, 176.0, 51.0, 6.3
## $ Bahrain <int> 42, 63, 7, 2
## $ Bangladesh <int> 0, 0, 0, 0
## $ Barbados <dbl> 143.0, 173.0, 36.0, 6.3
## $ Belarus <dbl> 142.0, 373.0, 42.0, 14.4
## $ Belgium <dbl> 295.0, 84.0, 212.0, 10.5
## $ Belize <dbl> 263.0, 114.0, 8.0, 6.8
## $ Benin <dbl> 34.0, 4.0, 13.0, 1.1
## $ Bhutan <dbl> 23.0, 0.0, 0.0, 0.4
## $ Bolivia <dbl> 167.0, 41.0, 8.0, 3.8
## $ Bosnia.Herzegovina <dbl> 76.0, 173.0, 8.0, 4.6
## $ Botswana <dbl> 173.0, 35.0, 35.0, 5.4
## $ Brazil <dbl> 245.0, 145.0, 16.0, 7.2
## $ Brunei <dbl> 31.0, 2.0, 1.0, 0.6
## $ Bulgaria <dbl> 231.0, 252.0, 94.0, 10.3
## $ Burkina.Faso <dbl> 25.0, 7.0, 7.0, 4.3
## $ Burundi <dbl> 88.0, 0.0, 0.0, 6.3
## $ Cote.d.Ivoire <int> 37, 1, 7, 4
## $ Cabo.Verde <int> 144, 56, 16, 4
## $ Cambodia <dbl> 57.0, 65.0, 1.0, 2.2
## $ Cameroon <dbl> 147.0, 1.0, 4.0, 5.8
## $ Canada <dbl> 240.0, 122.0, 100.0, 8.2
## $ Central.African.Republic <dbl> 17.0, 2.0, 1.0, 1.8
## $ Chad <dbl> 15.0, 1.0, 1.0, 0.4
## $ Chile <dbl> 130.0, 124.0, 172.0, 7.6
## $ China <int> 79, 192, 8, 5
## $ Colombia <dbl> 159.0, 76.0, 3.0, 4.2
## $ Comoros <dbl> 1.0, 3.0, 1.0, 0.1
## $ Congo <dbl> 76.0, 1.0, 9.0, 1.7
## $ Cook.Islands <dbl> 0.0, 254.0, 74.0, 5.9
## $ Costa.Rica <dbl> 149.0, 87.0, 11.0, 4.4
## $ Croatia <dbl> 230.0, 87.0, 254.0, 10.2
## $ Cuba <dbl> 93.0, 137.0, 5.0, 4.2
## $ Cyprus <dbl> 192.0, 154.0, 113.0, 8.2
## $ Czech.Republic <dbl> 361.0, 170.0, 134.0, 11.8
## $ North.Korea <int> 0, 0, 0, 0
## $ DR.Congo <dbl> 32.0, 3.0, 1.0, 2.3
## $ Denmark <dbl> 224.0, 81.0, 278.0, 10.4
## $ Djibouti <dbl> 15.0, 44.0, 3.0, 1.1
## $ Dominica <dbl> 52.0, 286.0, 26.0, 6.6
## $ Dominican.Republic <dbl> 193.0, 147.0, 9.0, 6.2
## $ Ecuador <dbl> 162.0, 74.0, 3.0, 4.2
## $ Egypt <dbl> 6.0, 4.0, 1.0, 0.2
## $ El.Salvador <dbl> 52.0, 69.0, 2.0, 2.2
## $ Equatorial.Guinea <dbl> 92.0, 0.0, 233.0, 5.8
## $ Eritrea <dbl> 18.0, 0.0, 0.0, 0.5
## $ Estonia <dbl> 224.0, 194.0, 59.0, 9.5
## $ Ethiopia <dbl> 20.0, 3.0, 0.0, 0.7
## $ Fiji <int> 77, 35, 1, 2
## $ Finland <int> 263, 133, 97, 10
## $ France <dbl> 127.0, 151.0, 370.0, 11.8
## $ Gabon <dbl> 347.0, 98.0, 59.0, 8.9
## $ Gambia <dbl> 8.0, 0.0, 1.0, 2.4
## $ Georgia <dbl> 52.0, 100.0, 149.0, 5.4
## $ Germany <dbl> 346.0, 117.0, 175.0, 11.3
## $ Ghana <dbl> 31.0, 3.0, 10.0, 1.8
## $ Greece <dbl> 133.0, 112.0, 218.0, 8.3
## $ Grenada <dbl> 199.0, 438.0, 28.0, 11.9
## $ Guatemala <dbl> 53.0, 69.0, 2.0, 2.2
## $ Guinea <dbl> 9.0, 0.0, 2.0, 0.2
## $ Guinea.Bissau <dbl> 28.0, 31.0, 21.0, 2.5
## $ Guyana <dbl> 93.0, 302.0, 1.0, 7.1
## $ Haiti <dbl> 1.0, 326.0, 1.0, 5.9
## $ Honduras <int> 69, 98, 2, 3
## $ Hungary <dbl> 234.0, 215.0, 185.0, 11.3
## $ Iceland <dbl> 233.0, 61.0, 78.0, 6.6
## $ India <dbl> 9.0, 114.0, 0.0, 2.2
## $ Indonesia <dbl> 5.0, 1.0, 0.0, 0.1
## $ Iran <int> 0, 0, 0, 0
## $ Iraq <dbl> 9.0, 3.0, 0.0, 0.2
## $ Ireland <dbl> 313.0, 118.0, 165.0, 11.4
## $ Israel <dbl> 63.0, 69.0, 9.0, 2.5
## $ Italy <dbl> 85.0, 42.0, 237.0, 6.5
## $ Jamaica <dbl> 82.0, 88.0, 9.0, 3.4
## $ Japan <int> 77, 202, 16, 7
## $ Jordan <dbl> 6.0, 21.0, 1.0, 0.5
## $ Kazakhstan <dbl> 124.0, 246.0, 12.0, 6.8
## $ Kenya <dbl> 58.0, 22.0, 2.0, 1.8
## $ Kiribati <int> 21, 34, 1, 1
## $ Kuwait <int> 0, 0, 0, 0
## $ Kyrgyzstan <dbl> 31.0, 88.0, 6.0, 2.4
## $ Laos <dbl> 62.0, 0.0, 123.0, 6.2
## $ Latvia <dbl> 281.0, 216.0, 62.0, 10.5
## $ Lebanon <dbl> 20.0, 55.0, 31.0, 1.9
## $ Lesotho <dbl> 82.0, 50.0, 0.0, 2.8
## $ Liberia <dbl> 19.0, 152.0, 2.0, 3.1
## $ Libya <int> 0, 0, 0, 0
## $ Lithuania <dbl> 343.0, 244.0, 56.0, 12.9
## $ Luxembourg <dbl> 236.0, 133.0, 271.0, 11.4
## $ Madagascar <dbl> 26.0, 15.0, 4.0, 0.8
## $ Malawi <dbl> 8.0, 11.0, 1.0, 1.5
## $ Malaysia <dbl> 13.0, 4.0, 0.0, 0.3
## $ Maldives <int> 0, 0, 0, 0
## $ Mali <dbl> 5.0, 1.0, 1.0, 0.6
## $ Malta <dbl> 149.0, 100.0, 120.0, 6.6
## $ Marshall.Islands <int> 0, 0, 0, 0
## $ Mauritania <int> 0, 0, 0, 0
## $ Mauritius <dbl> 98.0, 31.0, 18.0, 2.6
## $ Mexico <dbl> 238.0, 68.0, 5.0, 5.5
## $ Micronesia <dbl> 62.0, 50.0, 18.0, 2.3
## $ Monaco <int> 0, 0, 0, 0
## $ Mongolia <dbl> 77.0, 189.0, 8.0, 4.9
## $ Montenegro <dbl> 31.0, 114.0, 128.0, 4.9
## $ Morocco <dbl> 12.0, 6.0, 10.0, 0.5
## $ Mozambique <dbl> 47.0, 18.0, 5.0, 1.3
## $ Myanmar <dbl> 5.0, 1.0, 0.0, 0.1
## $ Namibia <dbl> 376.0, 3.0, 1.0, 6.8
## $ Nauru <int> 49, 0, 8, 1
## $ Nepal <dbl> 5.0, 6.0, 0.0, 0.2
## $ Netherlands <dbl> 251.0, 88.0, 190.0, 9.4
## $ New.Zealand <dbl> 203.0, 79.0, 175.0, 9.3
## $ Nicaragua <dbl> 78.0, 118.0, 1.0, 3.5
## $ Niger <dbl> 3.0, 2.0, 1.0, 0.1
## $ Nigeria <dbl> 42.0, 5.0, 2.0, 9.1
## $ Niue <int> 188, 200, 7, 7
## $ Norway <dbl> 169.0, 71.0, 129.0, 6.7
## $ Oman <dbl> 22.0, 16.0, 1.0, 0.7
## $ Pakistan <int> 0, 0, 0, 0
## $ Palau <dbl> 306.0, 63.0, 23.0, 6.9
## $ Panama <dbl> 285.0, 104.0, 18.0, 7.2
## $ Papua.New.Guinea <dbl> 44.0, 39.0, 1.0, 1.5
## $ Paraguay <dbl> 213.0, 117.0, 74.0, 7.3
## $ Peru <dbl> 163.0, 160.0, 21.0, 6.1
## $ Philippines <dbl> 71.0, 186.0, 1.0, 4.6
## $ Poland <dbl> 343.0, 215.0, 56.0, 10.9
## $ Portugal <int> 194, 67, 339, 11
## $ Qatar <dbl> 1.0, 42.0, 7.0, 0.9
## $ South.Korea <dbl> 140.0, 16.0, 9.0, 9.8
## $ Moldova <dbl> 109.0, 226.0, 18.0, 6.3
## $ Romania <dbl> 297.0, 122.0, 167.0, 10.4
## $ Russian.Federation <dbl> 247.0, 326.0, 73.0, 11.5
## $ Rwanda <dbl> 43.0, 2.0, 0.0, 6.8
## $ St..Kitts...Nevis <dbl> 194.0, 205.0, 32.0, 7.7
## $ St..Lucia <dbl> 171.0, 315.0, 71.0, 10.1
## $ St..Vincent...the.Grenadines <dbl> 120.0, 221.0, 11.0, 6.3
## $ Samoa <dbl> 105.0, 18.0, 24.0, 2.6
## $ San.Marino <int> 0, 0, 0, 0
## $ Sao.Tome...Principe <dbl> 56.0, 38.0, 140.0, 4.2
## $ Saudi.Arabia <dbl> 0.0, 5.0, 0.0, 0.1
## $ Senegal <dbl> 9.0, 1.0, 7.0, 0.3
## $ Serbia <dbl> 283.0, 131.0, 127.0, 9.6
## $ Seychelles <dbl> 157.0, 25.0, 51.0, 4.1
## $ Sierra.Leone <dbl> 25.0, 3.0, 2.0, 6.7
## $ Singapore <dbl> 60.0, 12.0, 11.0, 1.5
## $ Slovakia <dbl> 196.0, 293.0, 116.0, 11.4
## $ Slovenia <dbl> 270.0, 51.0, 276.0, 10.6
## $ Solomon.Islands <dbl> 56.0, 11.0, 1.0, 1.2
## $ Somalia <int> 0, 0, 0, 0
## $ South.Africa <dbl> 225.0, 76.0, 81.0, 8.2
## $ Spain <int> 284, 157, 112, 10
## $ Sri.Lanka <dbl> 16.0, 104.0, 0.0, 2.2
## $ Sudan <dbl> 8.0, 13.0, 0.0, 1.7
## $ Suriname <dbl> 128.0, 178.0, 7.0, 5.6
## $ Swaziland <dbl> 90.0, 2.0, 2.0, 4.7
## $ Sweden <dbl> 152.0, 60.0, 186.0, 7.2
## $ Switzerland <dbl> 185.0, 100.0, 280.0, 10.2
## $ Syria <int> 5, 35, 16, 1
## $ Tajikistan <dbl> 2.0, 15.0, 0.0, 0.3
## $ Thailand <dbl> 99.0, 258.0, 1.0, 6.4
## $ Macedonia <dbl> 106.0, 27.0, 86.0, 3.9
## $ Timor.Leste <dbl> 1.0, 1.0, 4.0, 0.1
## $ Togo <dbl> 36.0, 2.0, 19.0, 1.3
## $ Tonga <dbl> 36.0, 21.0, 5.0, 1.1
## $ Trinidad...Tobago <dbl> 197.0, 156.0, 7.0, 6.4
## $ Tunisia <dbl> 51.0, 3.0, 20.0, 1.3
## $ Turkey <dbl> 51.0, 22.0, 7.0, 1.4
## $ Turkmenistan <dbl> 19.0, 71.0, 32.0, 2.2
## $ Tuvalu <int> 6, 41, 9, 1
## $ Uganda <dbl> 45.0, 9.0, 0.0, 8.3
## $ Ukraine <dbl> 206.0, 237.0, 45.0, 8.9
## $ United.Arab.Emirates <dbl> 16.0, 135.0, 5.0, 2.8
## $ United.Kingdom <dbl> 219.0, 126.0, 195.0, 10.4
## $ Tanzania <dbl> 36.0, 6.0, 1.0, 5.7
## $ USA <dbl> 249.0, 158.0, 84.0, 8.7
## $ Uruguay <dbl> 115.0, 35.0, 220.0, 6.6
## $ Uzbekistan <dbl> 25.0, 101.0, 8.0, 2.4
## $ Vanuatu <dbl> 21.0, 18.0, 11.0, 0.9
## $ Venezuela <dbl> 333.0, 100.0, 3.0, 7.7
## $ Vietnam <int> 111, 2, 1, 2
## $ Yemen <dbl> 6.0, 0.0, 0.0, 0.1
## $ Zambia <dbl> 32.0, 19.0, 4.0, 2.5
## $ Zimbabwe <dbl> 64.0, 18.0, 4.0, 4.7
library(reshape)
##
## 다음의 패키지를 부착합니다: 'reshape'
## The following object is masked from 'package:dplyr':
##
## rename
df1<-melt(df,id="year")
df1 %>% glimpse
## Rows: 772
## Columns: 3
## $ year <int> 1999, 2000, 2001, 2002, 1999, 2000, 2001, 2002, 1999, 2000, 2…
## $ variable <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Albania, …
## $ value <dbl> 0.0, 0.0, 0.0, 0.0, 89.0, 132.0, 54.0, 4.9, 25.0, 0.0, 14.0, …
colSums(is.na(df1))
## year variable value
## 0 0 0
names(df1)[2:3]<-c("country","disease")
names(df1)
## [1] "year" "country" "disease"
df1 %>% filter(year==2000) %>% summarize(m=mean(disease))
## m
## 1 81.01036
df1 %>% filter(year==2000) %>% filter(disease>81.01036) %>% NROW->result
print(result)
## [1] 76
#실기 2유형 풀이 ----
library(dplyr)
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
library(pROC)
## Type 'citation("pROC")' for a citation.
##
## 다음의 패키지를 부착합니다: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
train <- read.csv("c:/data/train.csv",fileEncoding = 'euc-kr')
test <- read.csv("c:/data/test.csv",fileEncoding = 'euc-kr')
# 데이터 구조 확인
glimpse(train)
## Rows: 3,500
## Columns: 11
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ total <dbl> 68282840, 2136000, 3197000, 16077620, 29050000, 11379000, 1005…
## $ max <int> 11264000, 2136000, 1639000, 4935000, 24000000, 9552000, 761200…
## $ refund <int> 6860000, 300000, NA, NA, NA, 462000, 4582000, 29524000, NA, NA…
## $ product <chr> "기타", "스포츠", "남성 캐주얼", "기타", "보석", "디자이너", "…
## $ store <chr> "강남점", "잠실점", "관악점", "광주점", "본 점", "일산점", "…
## $ day <int> 19, 2, 2, 18, 2, 3, 5, 63, 18, 1, 25, 3, 2, 27, 84, 152, 26, 2…
## $ count <dbl> 3.894737, 1.500000, 2.000000, 2.444444, 1.500000, 1.666667, 2.…
## $ week <dbl> 0.52702703, 0.00000000, 0.00000000, 0.31818182, 0.00000000, 0.…
## $ cycle <int> 17, 1, 1, 16, 85, 42, 42, 5, 15, 0, 13, 89, 16, 10, 4, 2, 13, …
## $ gender <int> 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1,…
#str(test)
# 데이터 전처리
# 성별을 factor로 변환
train$gender <- ifelse(train$gender==0,"여성","남성")
train$gender <- as.factor(train$gender)
glimpse(train)
## Rows: 3,500
## Columns: 11
## $ cust_id <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ total <dbl> 68282840, 2136000, 3197000, 16077620, 29050000, 11379000, 1005…
## $ max <int> 11264000, 2136000, 1639000, 4935000, 24000000, 9552000, 761200…
## $ refund <int> 6860000, 300000, NA, NA, NA, 462000, 4582000, 29524000, NA, NA…
## $ product <chr> "기타", "스포츠", "남성 캐주얼", "기타", "보석", "디자이너", "…
## $ store <chr> "강남점", "잠실점", "관악점", "광주점", "본 점", "일산점", "…
## $ day <int> 19, 2, 2, 18, 2, 3, 5, 63, 18, 1, 25, 3, 2, 27, 84, 152, 26, 2…
## $ count <dbl> 3.894737, 1.500000, 2.000000, 2.444444, 1.500000, 1.666667, 2.…
## $ week <dbl> 0.52702703, 0.00000000, 0.00000000, 0.31818182, 0.00000000, 0.…
## $ cycle <int> 17, 1, 1, 16, 85, 42, 42, 5, 15, 0, 13, 89, 16, 10, 4, 2, 13, …
## $ gender <fct> 여성, 여성, 남성, 남성, 여성, 여성, 여성, 여성, 여성, 남성, 여…
colSums(is.na(train))
## cust_id total max refund product store day count week cycle
## 0 0 0 2295 0 0 0 0 0 0
## gender
## 0
train$refund <-ifelse(is.na(train$refund),0,train$refund)
colSums(is.na(train))
## cust_id total max refund product store day count week cycle
## 0 0 0 0 0 0 0 0 0 0
## gender
## 0
# levels(train$gender) <- make.names(levels(train$gender))
train_control <- trainControl(method = "cv", number = 5,
classProbs = TRUE,
summaryFunction = twoClassSummary)
model <- train(gender ~ .,
data = train,
method = "rf",
trControl = train_control, metric = "ROC")
model
## Random Forest
##
## 3500 samples
## 10 predictor
## 2 classes: '남성', '여성'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 2800, 2800, 2799, 2800, 2801
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.6632964 0.1649297 0.9331461
## 37 0.6499021 0.3381409 0.8008293
## 72 0.6435890 0.3381352 0.8017393
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
glimpse(test)
## Rows: 2,482
## Columns: 10
## $ cust_id <int> 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507, 3508, 3509, 35…
## $ total <dbl> 70900400, 310533100, 305264140, 7594080, 1795790, 13000000, 42…
## $ max <int> 22000000, 38558000, 14825000, 5225000, 1411200, 2160000, 69920…
## $ refund <int> 4050000, 48034700, 30521000, NA, NA, NA, 39566000, NA, NA, 230…
## $ product <chr> "골프", "농산물", "가공식품", "주방용품", "수산품", "화장품", …
## $ store <chr> "부산본점", "잠실점", "본 점", "부산본점", "청량리점", "잠실…
## $ day <int> 13, 90, 101, 5, 3, 5, 144, 1, 1, 28, 21, 3, 23, 30, 3, 8, 23, …
## $ count <dbl> 1.461538, 2.433333, 14.623762, 2.000000, 2.666667, 2.200000, 2…
## $ week <dbl> 0.78947368, 0.36986301, 0.08327691, 0.00000000, 0.12500000, 0.…
## $ cycle <int> 26, 3, 3, 47, 8, 61, 2, 0, 0, 12, 14, 2, 15, 11, 112, 29, 12, …
colSums(is.na(test))
## cust_id total max refund product store day count week cycle
## 0 0 0 1611 0 0 0 0 0 0
test$refund <- ifelse(is.na(test$refund),0,test$refund)
colSums(is.na(test))
## cust_id total max refund product store day count week cycle
## 0 0 0 0 0 0 0 0 0 0
# 평가용 데이터에 예측 적용
predictions <- predict(model, newdata = test, type = "raw")
# 예측 결과를 데이터 프레임에 추가
test$pred <- predictions
# 결과 확인
#pred_count <- length(predictions)
#test_count <- nrow(test)
#pred_count
#test_count
head(test$pred)
## [1] 여성 여성 여성 여성 여성 여성
## Levels: 남성 여성
test$pred <- ifelse(test$pred=='여성',0,1)
test %>% glimpse
## Rows: 2,482
## Columns: 11
## $ cust_id <int> 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507, 3508, 3509, 35…
## $ total <dbl> 70900400, 310533100, 305264140, 7594080, 1795790, 13000000, 42…
## $ max <int> 22000000, 38558000, 14825000, 5225000, 1411200, 2160000, 69920…
## $ refund <dbl> 4050000, 48034700, 30521000, 0, 0, 0, 39566000, 0, 0, 23062000…
## $ product <chr> "골프", "농산물", "가공식품", "주방용품", "수산품", "화장품", …
## $ store <chr> "부산본점", "잠실점", "본 점", "부산본점", "청량리점", "잠실…
## $ day <int> 13, 90, 101, 5, 3, 5, 144, 1, 1, 28, 21, 3, 23, 30, 3, 8, 23, …
## $ count <dbl> 1.461538, 2.433333, 14.623762, 2.000000, 2.666667, 2.200000, 2…
## $ week <dbl> 0.78947368, 0.36986301, 0.08327691, 0.00000000, 0.12500000, 0.…
## $ cycle <int> 26, 3, 3, 47, 8, 61, 2, 0, 0, 12, 14, 2, 15, 11, 112, 29, 12, …
## $ pred <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
df <- test[11]
write.csv(df,"c:/data/goorm_type2.csv",row.names=FALSE)
result<- read.csv("c:/data/goorm_type2.csv")
head(result)
## pred
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
table(result)
## pred
## 0 1
## 2246 236
#실기 2유형 교재 232~235 ----
library(dplyr)
library(caret)
# 데이터 불러오기
df <- read.csv("c:/data/travel_data.csv")
# 데이터 구조살피기
glimpse(df)
## Rows: 1,987
## Columns: 10
## $ INDEX <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, …
## $ Age <int> 31, 31, 34, 28, 28, 25, 31, 31, 28, 33, 31, 26, 32…
## $ Employment.Type <chr> "Government Sector", "Private Sector/Self Employed…
## $ GraduateOrNot <chr> "Yes", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Y…
## $ AnnualIncome <int> 400000, 1250000, 500000, 700000, 700000, 1150000, …
## $ FamilyMembers <int> 6, 7, 4, 3, 8, 4, 4, 3, 6, 3, 9, 5, 6, 6, 3, 7, 4,…
## $ ChronicDiseases <int> 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1,…
## $ FrequentFlyer <chr> "No", "No", "No", "No", "Yes", "No", "No", "Yes", …
## $ EverTravelledAbroad <chr> "No", "No", "No", "No", "No", "No", "No", "Yes", "…
## $ TravelInsurance <int> 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0,…
# 데이터 분할
set.seed(123)
train_list <- createDataPartition(y=df$TravelInsurance,
p=0.75,list=FALSE)
df_train <- df[train_list,]
df_test <- df[-train_list,]
dim(df_train)
## [1] 1491 10
dim(df_test)
## [1] 496 10
df_train %>% glimpse
## Rows: 1,491
## Columns: 10
## $ INDEX <int> 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17…
## $ Age <int> 31, 31, 28, 28, 25, 31, 28, 33, 31, 26, 32, 31, 34…
## $ Employment.Type <chr> "Government Sector", "Private Sector/Self Employed…
## $ GraduateOrNot <chr> "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Y…
## $ AnnualIncome <int> 400000, 1250000, 700000, 700000, 1150000, 1350000,…
## $ FamilyMembers <int> 6, 7, 3, 8, 4, 3, 6, 3, 9, 5, 6, 6, 7, 4, 7, 5, 2,…
## $ ChronicDiseases <int> 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,…
## $ FrequentFlyer <chr> "No", "No", "No", "Yes", "No", "Yes", "Yes", "Yes"…
## $ EverTravelledAbroad <chr> "No", "No", "No", "No", "No", "Yes", "Yes", "No", …
## $ TravelInsurance <int> 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1,…
df_train %>% mutate(index="train") -> df_train
df_test %>% mutate(index="test") -> df_test
# 데이터 행 결합
bind_rows(df_train,df_test)-> full
full %>% glimpse
## Rows: 1,987
## Columns: 11
## $ INDEX <int> 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17…
## $ Age <int> 31, 31, 28, 28, 25, 31, 28, 33, 31, 26, 32, 31, 34…
## $ Employment.Type <chr> "Government Sector", "Private Sector/Self Employed…
## $ GraduateOrNot <chr> "Yes", "Yes", "Yes", "Yes", "No", "Yes", "Yes", "Y…
## $ AnnualIncome <int> 400000, 1250000, 700000, 700000, 1150000, 1350000,…
## $ FamilyMembers <int> 6, 7, 3, 8, 4, 3, 6, 3, 9, 5, 6, 6, 7, 4, 7, 5, 2,…
## $ ChronicDiseases <int> 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0,…
## $ FrequentFlyer <chr> "No", "No", "No", "Yes", "No", "Yes", "Yes", "Yes"…
## $ EverTravelledAbroad <chr> "No", "No", "No", "No", "No", "Yes", "Yes", "No", …
## $ TravelInsurance <int> 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1,…
## $ index <chr> "train", "train", "train", "train", "train", "trai…
summary (full)
## INDEX Age Employment.Type GraduateOrNot
## Min. : 0.0 Min. :25.00 Length:1987 Length:1987
## 1st Qu.: 496.5 1st Qu.:28.00 Class :character Class :character
## Median : 993.0 Median :29.00 Mode :character Mode :character
## Mean : 993.0 Mean :29.65
## 3rd Qu.:1489.5 3rd Qu.:32.00
## Max. :1986.0 Max. :35.00
## AnnualIncome FamilyMembers ChronicDiseases FrequentFlyer
## Min. : 300000 Min. :2.000 Min. :0.0000 Length:1987
## 1st Qu.: 600000 1st Qu.:4.000 1st Qu.:0.0000 Class :character
## Median : 900000 Median :5.000 Median :0.0000 Mode :character
## Mean : 932763 Mean :4.753 Mean :0.2778
## 3rd Qu.:1250000 3rd Qu.:6.000 3rd Qu.:1.0000
## Max. :1800000 Max. :9.000 Max. :1.0000
## EverTravelledAbroad TravelInsurance index
## Length:1987 Min. :0.0000 Length:1987
## Class :character 1st Qu.:0.0000 Class :character
## Mode :character Median :0.0000 Mode :character
## Mean :0.3573
## 3rd Qu.:1.0000
## Max. :1.0000
# 범주형변수 속성정의
full$TravelInsurance <- ifelse (full$TravelInsurance==0,
"미가입","가입")
full$TravelInsurance <- as.factor(full$TravelInsurance)
full$GraduateOrNot <-as.factor(full$GraduateOrNot)
full$FrequentFlyer <-as.factor(full$FrequentFlyer)
full$EverTravelledAbroad <-as.factor(full$EverTravelledAbroad)
# 결측치 확인
colSums(is.na(full))
## INDEX Age Employment.Type GraduateOrNot
## 0 0 0 0
## AnnualIncome FamilyMembers ChronicDiseases FrequentFlyer
## 0 0 0 0
## EverTravelledAbroad TravelInsurance index
## 0 0 0
# 데이터 전처리
data <- preProcess(full, method=c("center","scale"))
data
## Created from 1987 samples and 11 variables
##
## Pre-processing:
## - centered (5)
## - ignored (6)
## - scaled (5)
# install.packages("psych")
# library(psych)
zs_data_p <- predict(data,full)
# describe(zs_date_p)
# 데이터셋 만들기
zs_data_p %>% filter(index=="train") %>% select(-index) -> train
zs_data_p %>% filter(index=="test") %>% select(-index) -> test
#훈련설정
ctrl <- trainControl(method="cv", number=5,
summaryFunction=twoClassSummary,
classProb=TRUE)
#모형훈련
rpfit <- train(TravelInsurance~., data=train,
method='rf', metric="ROC",
trControl=ctrl)
rpfit
## Random Forest
##
## 1491 samples
## 9 predictor
## 2 classes: '가입', '미가입'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 1193, 1194, 1192, 1193, 1192
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.8013346 0.5675366 0.9665630
## 5 0.8029920 0.6030682 0.9321062
## 9 0.8040021 0.6086934 0.9101767
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 9.