Description for Ridge Regression using Package caret
Ridge Regression의 장점
Training Dataset
의 변화에도 회귀계수 추정치가 크게 변하지 않는다.Ridge Regression의 단점
실습 자료 : 유니버셜 은행의 고객 2,500명에 대한 자료(출처 : Data Mining for Business Intelligence, Shmueli et al. 2010)이며, 총 13개의 변수를 포함하고 있다. 이 자료에서 Target은
Personal Loan
이다.
pacman::p_load("data.table",
"tidyverse",
"dplyr",
"ggplot2", "GGally",
"caret",
"doParallel", "parallel") # For 병렬 처리
registerDoParallel(cores=detectCores()) # 사용할 Core 개수 지정
UB <- fread("../Universal Bank_Main.csv") # 데이터 불러오기
UB %>%
as_tibble
# A tibble: 2,500 × 14
ID Age Experience Income `ZIP Code` Family CCAvg Education
<int> <int> <int> <int> <int> <int> <dbl> <int>
1 1 25 1 49 91107 4 1.6 1
2 2 45 19 34 90089 3 1.5 1
3 3 39 15 11 94720 1 1 1
4 4 35 9 100 94112 1 2.7 2
5 5 35 8 45 91330 4 1 2
6 6 37 13 29 92121 4 0.4 2
7 7 53 27 72 91711 2 1.5 2
8 8 50 24 22 93943 1 0.3 3
9 9 35 10 81 90089 3 0.6 2
10 10 34 9 180 93023 1 8.9 3
# ℹ 2,490 more rows
# ℹ 6 more variables: Mortgage <int>, `Personal Loan` <int>,
# `Securities Account` <int>, `CD Account` <int>, Online <int>,
# CreditCard <int>
UB %<>%
data.frame() %>% # Data Frame 형태로 변환
mutate(Personal.Loan = ifelse(Personal.Loan == 1, "yes", "no")) %>% # Target을 문자형 변수로 변환
select(-1) # ID 변수 제거
# Convert to Factor
fac.col <- c("Family", "Education", "Securities.Account",
"CD.Account", "Online", "CreditCard",
# Target
"Personal.Loan")
UB <- UB %>%
mutate_at(fac.col, as.factor) # 범주형으로 변환
glimpse(UB) # 데이터 구조 확인
Rows: 2,500
Columns: 13
$ Age <int> 25, 45, 39, 35, 35, 37, 53, 50, 35, 34, 6…
$ Experience <int> 1, 19, 15, 9, 8, 13, 27, 24, 10, 9, 39, 5…
$ Income <int> 49, 34, 11, 100, 45, 29, 72, 22, 81, 180,…
$ ZIP.Code <int> 91107, 90089, 94720, 94112, 91330, 92121,…
$ Family <fct> 4, 3, 1, 1, 4, 4, 2, 1, 3, 1, 4, 3, 2, 4,…
$ CCAvg <dbl> 1.6, 1.5, 1.0, 2.7, 1.0, 0.4, 1.5, 0.3, 0…
$ Education <fct> 1, 1, 1, 2, 2, 2, 2, 3, 2, 3, 3, 2, 3, 2,…
$ Mortgage <int> 0, 0, 0, 0, 0, 155, 0, 0, 104, 0, 0, 0, 0…
$ Personal.Loan <fct> no, no, no, no, no, no, no, no, no, yes, …
$ Securities.Account <fct> 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,…
$ CD.Account <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ Online <fct> 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1,…
$ CreditCard <fct> 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0,…
ggpairs(UB,
columns = c("Age", "Experience", "Income", # 수치형 예측 변수
"ZIP.Code", "CCAvg", "Mortgage"),
aes(colour = Personal.Loan)) + # Target의 범주에 따라 색깔을 다르게 표현
theme_bw()
ggpairs(UB,
columns = c("Age", "Experience", "Income", # 수치형 예측 변수
"ZIP.Code", "CCAvg", "Mortgage"),
aes(colour = Personal.Loan)) + # Target의 범주에 따라 색깔을 다르게 표현
scale_color_brewer(palette="Purples") + # 특정 색깔 지정
scale_fill_brewer(palette="Purples") + # 특정 색깔 지정
theme_bw()
# Partition (Training Dataset : Test Dataset = 7:3)
y <- UB$Personal.Loan # Target
set.seed(200)
ind <- createDataPartition(y, p = 0.7, list = T) # Index를 이용하여 7:3으로 분할
UB.trd <- UB[ind$Resample1,] # Training Dataset
UB.ted <- UB[-ind$Resample1,] # Test Dataset
Package "caret"
은 통합 API를 통해 R로 기계 학습을 실행할 수 있는 매우 실용적인 방법을 제공한다. Package "caret"
를 통해 Ridge Regression
을 수행하기 위해 옵션 method
에 다양한 방법(Ex: "ridge"
, "foba"
등)을 입력할 수 있지만, 대부분 회귀 문제에 대해서만 분석이 가능하다. 분류와 회귀 문제 모두 가능한 "glmnet"
을 이용하려면 옵션 tuneGrid = expand.grid()
을 통해 탐색하고자 하는 초모수 lambda
의 범위를 직접 지정해줘야 한다.
fitControl <- trainControl(method = "cv", number = 5, # 5-Fold Cross Validation (5-Fold CV)
allowParallel = TRUE) # 병렬 처리
set.seed(200) # For CV
ridge.fit <- train(Personal.Loan ~ ., data = UB.trd,
trControl = fitControl ,
method = "glmnet",
tuneGrid = expand.grid(alpha = 0, # For Ridge Regression
lambda = seq(0, 1, 0.001)), # lambda의 탐색 범위
preProc = c("center", "scale")) # Standardization for 예측 변수
ridge.fit
glmnet
1751 samples
12 predictor
2 classes: 'no', 'yes'
Pre-processing: centered (15), scaled (15)
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 1401, 1401, 1400, 1401, 1401
Resampling results across tuning parameters:
lambda Accuracy Kappa
0.000 0.9514530 0.682422731
0.001 0.9514530 0.682422731
0.002 0.9514530 0.682422731
0.003 0.9514530 0.682422731
0.004 0.9514530 0.682422731
0.005 0.9514530 0.682422731
0.006 0.9514530 0.682422731
0.007 0.9514530 0.682422731
0.008 0.9514530 0.682422731
0.009 0.9514530 0.682422731
0.010 0.9514530 0.682422731
0.011 0.9514530 0.682422731
0.012 0.9514530 0.682422731
0.013 0.9514530 0.682422731
0.014 0.9514530 0.682422731
0.015 0.9514530 0.682422731
0.016 0.9514530 0.682422731
0.017 0.9503101 0.672562190
0.018 0.9497387 0.668299630
0.019 0.9497387 0.668299630
0.020 0.9491673 0.662855819
0.021 0.9491673 0.662855819
0.022 0.9491673 0.662855819
0.023 0.9485958 0.657234464
0.024 0.9480244 0.651426737
0.025 0.9480244 0.651426737
0.026 0.9474530 0.647042949
0.027 0.9474530 0.647042949
0.028 0.9474530 0.647042949
0.029 0.9474530 0.647042949
0.030 0.9463101 0.636313023
0.031 0.9451673 0.626929406
0.032 0.9451673 0.626929406
0.033 0.9451673 0.626929406
0.034 0.9451673 0.626929406
0.035 0.9440244 0.615692735
0.036 0.9434530 0.610579676
0.037 0.9411673 0.589314889
0.038 0.9411673 0.589314889
0.039 0.9411673 0.589314889
0.040 0.9411673 0.589314889
0.041 0.9400244 0.577267497
0.042 0.9388816 0.564805551
0.043 0.9383118 0.560399347
0.044 0.9383118 0.560399347
0.045 0.9388832 0.562607303
0.046 0.9383134 0.558072056
0.047 0.9377436 0.553402013
0.048 0.9377436 0.553402013
0.049 0.9371722 0.547866554
0.050 0.9360293 0.537220742
0.051 0.9360293 0.534865665
0.052 0.9360293 0.534865665
0.053 0.9354579 0.529941322
0.054 0.9331738 0.506634650
0.055 0.9326040 0.501676340
0.056 0.9314611 0.488776993
0.057 0.9314611 0.488776993
0.058 0.9314611 0.488776993
0.059 0.9308897 0.483701882
0.060 0.9308897 0.483701882
0.061 0.9297468 0.472344190
0.062 0.9297468 0.472344190
0.063 0.9297468 0.472344190
0.064 0.9286040 0.458759035
0.065 0.9286040 0.458759035
0.066 0.9280326 0.452025874
0.067 0.9280326 0.452025874
0.068 0.9274611 0.445454328
0.069 0.9274611 0.445454328
0.070 0.9268897 0.438473277
0.071 0.9263199 0.433360719
0.072 0.9257501 0.428086602
0.073 0.9257501 0.428086602
0.074 0.9257501 0.428086602
0.075 0.9257501 0.428086602
0.076 0.9257501 0.428086602
0.077 0.9257501 0.428086602
0.078 0.9263215 0.430641156
0.079 0.9263215 0.430641156
0.080 0.9263215 0.430641156
0.081 0.9263215 0.430641156
0.082 0.9263215 0.430641156
0.083 0.9257501 0.425197345
0.084 0.9251787 0.413245874
0.085 0.9240358 0.398551596
0.086 0.9240358 0.398551596
0.087 0.9234644 0.391031860
0.088 0.9228930 0.383219092
0.089 0.9228930 0.380365077
0.090 0.9223215 0.373025835
0.091 0.9223215 0.373025835
0.092 0.9223215 0.373025835
0.093 0.9217501 0.364082192
0.094 0.9217501 0.364082192
0.095 0.9217501 0.364082192
0.096 0.9217501 0.364082192
0.097 0.9211787 0.356153905
0.098 0.9211787 0.356153905
0.099 0.9206089 0.350482536
0.100 0.9206089 0.350482536
0.101 0.9200374 0.342232769
0.102 0.9200374 0.342232769
0.103 0.9200374 0.342232769
0.104 0.9200391 0.338253971
0.105 0.9194676 0.332190126
0.106 0.9194676 0.332190126
0.107 0.9194676 0.332190126
0.108 0.9194676 0.332190126
0.109 0.9194676 0.332190126
0.110 0.9200391 0.334699856
0.111 0.9200391 0.334699856
0.112 0.9200391 0.334699856
0.113 0.9200391 0.334699856
0.114 0.9200391 0.334699856
0.115 0.9200391 0.334699856
0.116 0.9194676 0.328357589
0.117 0.9194676 0.328357589
0.118 0.9188978 0.322293756
0.119 0.9188978 0.322293756
0.120 0.9183264 0.314043990
0.121 0.9177550 0.307472443
0.122 0.9177550 0.307472443
0.123 0.9183248 0.309980707
0.124 0.9177550 0.303638262
0.125 0.9177550 0.303638262
0.126 0.9171836 0.296824775
0.127 0.9171836 0.296824775
0.128 0.9160407 0.282416496
0.129 0.9160407 0.282416496
0.130 0.9160407 0.282416496
0.131 0.9160407 0.282416496
0.132 0.9154693 0.274791256
0.133 0.9154693 0.274791256
0.134 0.9154693 0.274791256
0.135 0.9154693 0.274791256
0.136 0.9148995 0.268219387
0.137 0.9148995 0.268219387
0.138 0.9148995 0.268219387
0.139 0.9148995 0.268219387
0.140 0.9148995 0.268219387
0.141 0.9148995 0.268219387
0.142 0.9148995 0.268219387
0.143 0.9148995 0.268219387
0.144 0.9148995 0.268219387
0.145 0.9148995 0.268219387
0.146 0.9148995 0.268219387
0.147 0.9143280 0.259628183
0.148 0.9143280 0.259628183
0.149 0.9143280 0.259628183
0.150 0.9143280 0.259628183
0.151 0.9143280 0.259628183
0.152 0.9137582 0.252814213
0.153 0.9137582 0.252814213
0.154 0.9137582 0.252814213
0.155 0.9137582 0.252814213
0.156 0.9131868 0.245188973
0.157 0.9120456 0.229528069
0.158 0.9120456 0.229528069
0.159 0.9120456 0.229528069
0.160 0.9120456 0.229528069
0.161 0.9114758 0.222187967
0.162 0.9114758 0.222187967
0.163 0.9109060 0.214561648
0.164 0.9109060 0.214561648
0.165 0.9103362 0.206632036
0.166 0.9103362 0.206632036
0.167 0.9103362 0.206632036
0.168 0.9103362 0.206632036
0.169 0.9103362 0.206632036
0.170 0.9103362 0.206632036
0.171 0.9103362 0.206632036
0.172 0.9103362 0.206632036
0.173 0.9103362 0.206632036
0.174 0.9103362 0.206632036
0.175 0.9103362 0.206632036
0.176 0.9103362 0.206632036
0.177 0.9103362 0.206632036
0.178 0.9103362 0.206632036
0.179 0.9103362 0.206632036
0.180 0.9097648 0.197677750
0.181 0.9097648 0.197677750
0.182 0.9097648 0.197677750
0.183 0.9097648 0.197677750
0.184 0.9097648 0.197677750
0.185 0.9097648 0.197677750
0.186 0.9097648 0.197677750
0.187 0.9097648 0.197677750
0.188 0.9091950 0.189426387
0.189 0.9086235 0.181498100
0.190 0.9086235 0.181498100
0.191 0.9080521 0.173569813
0.192 0.9080521 0.173569813
0.193 0.9074807 0.165320046
0.194 0.9069092 0.156728842
0.195 0.9069092 0.156728842
0.196 0.9069092 0.156728842
0.197 0.9069092 0.156728842
0.198 0.9069092 0.156728842
0.199 0.9069092 0.156728842
0.200 0.9069092 0.156728842
0.201 0.9069092 0.156728842
0.202 0.9069092 0.156728842
0.203 0.9069092 0.156728842
0.204 0.9069092 0.156728842
0.205 0.9069092 0.156728842
0.206 0.9069092 0.156728842
0.207 0.9063378 0.147774556
0.208 0.9063378 0.147774556
0.209 0.9057664 0.138820270
0.210 0.9057664 0.138820270
0.211 0.9057664 0.138820270
0.212 0.9051950 0.129479388
0.213 0.9051950 0.129479388
0.214 0.9051950 0.129479388
0.215 0.9051950 0.129479388
0.216 0.9051950 0.129479388
0.217 0.9051950 0.129479388
0.218 0.9051950 0.129479388
0.219 0.9051950 0.129479388
0.220 0.9051950 0.129479388
0.221 0.9051950 0.129479388
0.222 0.9046235 0.120138505
0.223 0.9046235 0.120138505
0.224 0.9046235 0.120138505
0.225 0.9046235 0.120138505
0.226 0.9040537 0.111545403
0.227 0.9040537 0.111545403
0.228 0.9034839 0.102588881
0.229 0.9034839 0.102588881
0.230 0.9034839 0.102588881
0.231 0.9034839 0.102588881
0.232 0.9034839 0.102588881
0.233 0.9034839 0.102588881
0.234 0.9034839 0.102588881
0.235 0.9034839 0.102588881
0.236 0.9034839 0.102588881
0.237 0.9034839 0.102588881
0.238 0.9034839 0.102588881
0.239 0.9034839 0.102588881
0.240 0.9034839 0.102588881
0.241 0.9034839 0.102588881
0.242 0.9034839 0.102588881
0.243 0.9034839 0.102588881
0.244 0.9034839 0.102588881
0.245 0.9034839 0.102588881
0.246 0.9034839 0.102588881
0.247 0.9034839 0.102588881
0.248 0.9029125 0.094339115
0.249 0.9023411 0.085747911
0.250 0.9023411 0.085747911
0.251 0.9023411 0.085747911
0.252 0.9023411 0.085747911
0.253 0.9023411 0.085747911
0.254 0.9017696 0.076793624
0.255 0.9017696 0.076793624
0.256 0.9017696 0.076793624
0.257 0.9011982 0.067452742
0.258 0.9011982 0.067452742
0.259 0.9011982 0.067452742
0.260 0.9011982 0.067452742
0.261 0.9011982 0.067452742
0.262 0.9011982 0.067452742
0.263 0.9011982 0.067452742
0.264 0.9011982 0.067452742
0.265 0.9011982 0.067452742
0.266 0.9011982 0.067452742
0.267 0.9011982 0.067452742
0.268 0.9011982 0.067452742
0.269 0.9011982 0.067452742
0.270 0.9011982 0.067452742
0.271 0.9011982 0.067452742
0.272 0.9011982 0.067452742
0.273 0.9011982 0.067452742
0.274 0.9011982 0.067452742
0.275 0.9011982 0.067452742
0.276 0.9006284 0.058109249
0.277 0.9000570 0.048356182
0.278 0.9000570 0.048356182
0.279 0.9000570 0.048356182
0.280 0.9000570 0.048356182
0.281 0.9000570 0.048356182
0.282 0.9000570 0.048356182
0.283 0.9000570 0.048356182
0.284 0.9000570 0.048356182
0.285 0.9000570 0.048356182
0.286 0.9000570 0.048356182
0.287 0.9000570 0.048356182
0.288 0.9000570 0.048356182
0.289 0.9000570 0.048356182
0.290 0.9000570 0.048356182
0.291 0.9000570 0.048356182
0.292 0.9000570 0.048356182
0.293 0.9000570 0.048356182
0.294 0.9000570 0.048356182
0.295 0.9000570 0.048356182
0.296 0.9000570 0.048356182
0.297 0.9000570 0.048356182
0.298 0.8994856 0.038603114
0.299 0.8994856 0.038603114
0.300 0.8994856 0.038603114
0.301 0.8994856 0.038603114
0.302 0.8994856 0.038603114
0.303 0.8989141 0.029262232
0.304 0.8989141 0.029262232
0.305 0.8989141 0.029262232
0.306 0.8989141 0.029262232
0.307 0.8989141 0.029262232
0.308 0.8989141 0.029262232
0.309 0.8989141 0.029262232
0.310 0.8989141 0.029262232
0.311 0.8989141 0.029262232
0.312 0.8983427 0.019509165
0.313 0.8983427 0.019509165
0.314 0.8983427 0.019509165
0.315 0.8983427 0.019509165
0.316 0.8983427 0.019509165
0.317 0.8983427 0.019509165
0.318 0.8983427 0.019509165
0.319 0.8983427 0.019509165
0.320 0.8983427 0.019509165
0.321 0.8983427 0.019509165
0.322 0.8983427 0.019509165
0.323 0.8983427 0.019509165
0.324 0.8983427 0.019509165
0.325 0.8983427 0.019509165
0.326 0.8983427 0.019509165
0.327 0.8983427 0.019509165
0.328 0.8983427 0.019509165
0.329 0.8983427 0.019509165
0.330 0.8983427 0.019509165
0.331 0.8983427 0.019509165
0.332 0.8983427 0.019509165
0.333 0.8983427 0.019509165
0.334 0.8983427 0.019509165
0.335 0.8983427 0.019509165
0.336 0.8983427 0.019509165
0.337 0.8983427 0.019509165
0.338 0.8983427 0.019509165
0.339 0.8983427 0.019509165
0.340 0.8983427 0.019509165
0.341 0.8983427 0.019509165
0.342 0.8983427 0.019509165
0.343 0.8983427 0.019509165
0.344 0.8983427 0.019509165
0.345 0.8983427 0.019509165
0.346 0.8983427 0.019509165
0.347 0.8983427 0.019509165
0.348 0.8983427 0.019509165
0.349 0.8983427 0.019509165
0.350 0.8983427 0.019509165
0.351 0.8983427 0.019509165
0.352 0.8983427 0.019509165
0.353 0.8977729 0.009753067
0.354 0.8977729 0.009753067
0.355 0.8977729 0.009753067
0.356 0.8977729 0.009753067
0.357 0.8977729 0.009753067
0.358 0.8977729 0.009753067
0.359 0.8977729 0.009753067
0.360 0.8977729 0.009753067
0.361 0.8977729 0.009753067
0.362 0.8977729 0.009753067
0.363 0.8977729 0.009753067
0.364 0.8977729 0.009753067
0.365 0.8977729 0.009753067
0.366 0.8977729 0.009753067
0.367 0.8977729 0.009753067
0.368 0.8977729 0.009753067
0.369 0.8977729 0.009753067
0.370 0.8977729 0.009753067
0.371 0.8977729 0.009753067
0.372 0.8977729 0.009753067
0.373 0.8977729 0.009753067
0.374 0.8977729 0.009753067
0.375 0.8977729 0.009753067
0.376 0.8977729 0.009753067
0.377 0.8977729 0.009753067
0.378 0.8977729 0.009753067
0.379 0.8977729 0.009753067
0.380 0.8977729 0.009753067
0.381 0.8972015 0.000000000
0.382 0.8972015 0.000000000
0.383 0.8972015 0.000000000
0.384 0.8972015 0.000000000
0.385 0.8972015 0.000000000
0.386 0.8972015 0.000000000
0.387 0.8972015 0.000000000
0.388 0.8972015 0.000000000
0.389 0.8972015 0.000000000
0.390 0.8972015 0.000000000
0.391 0.8972015 0.000000000
0.392 0.8972015 0.000000000
0.393 0.8972015 0.000000000
0.394 0.8972015 0.000000000
0.395 0.8972015 0.000000000
0.396 0.8972015 0.000000000
0.397 0.8972015 0.000000000
0.398 0.8972015 0.000000000
0.399 0.8972015 0.000000000
0.400 0.8972015 0.000000000
0.401 0.8972015 0.000000000
0.402 0.8972015 0.000000000
0.403 0.8972015 0.000000000
0.404 0.8972015 0.000000000
0.405 0.8972015 0.000000000
0.406 0.8972015 0.000000000
0.407 0.8972015 0.000000000
0.408 0.8972015 0.000000000
0.409 0.8972015 0.000000000
0.410 0.8972015 0.000000000
0.411 0.8972015 0.000000000
0.412 0.8972015 0.000000000
0.413 0.8972015 0.000000000
0.414 0.8972015 0.000000000
0.415 0.8972015 0.000000000
0.416 0.8972015 0.000000000
0.417 0.8972015 0.000000000
0.418 0.8972015 0.000000000
0.419 0.8972015 0.000000000
0.420 0.8972015 0.000000000
0.421 0.8972015 0.000000000
0.422 0.8972015 0.000000000
0.423 0.8972015 0.000000000
0.424 0.8972015 0.000000000
0.425 0.8972015 0.000000000
0.426 0.8972015 0.000000000
0.427 0.8972015 0.000000000
0.428 0.8972015 0.000000000
0.429 0.8972015 0.000000000
0.430 0.8972015 0.000000000
0.431 0.8972015 0.000000000
0.432 0.8972015 0.000000000
0.433 0.8972015 0.000000000
0.434 0.8972015 0.000000000
0.435 0.8972015 0.000000000
0.436 0.8972015 0.000000000
0.437 0.8972015 0.000000000
0.438 0.8972015 0.000000000
0.439 0.8972015 0.000000000
0.440 0.8972015 0.000000000
0.441 0.8972015 0.000000000
0.442 0.8972015 0.000000000
0.443 0.8972015 0.000000000
0.444 0.8972015 0.000000000
0.445 0.8972015 0.000000000
0.446 0.8972015 0.000000000
0.447 0.8972015 0.000000000
0.448 0.8972015 0.000000000
0.449 0.8972015 0.000000000
0.450 0.8972015 0.000000000
0.451 0.8972015 0.000000000
0.452 0.8972015 0.000000000
0.453 0.8972015 0.000000000
0.454 0.8972015 0.000000000
0.455 0.8972015 0.000000000
0.456 0.8972015 0.000000000
0.457 0.8972015 0.000000000
0.458 0.8972015 0.000000000
0.459 0.8972015 0.000000000
0.460 0.8972015 0.000000000
0.461 0.8972015 0.000000000
0.462 0.8972015 0.000000000
0.463 0.8972015 0.000000000
0.464 0.8972015 0.000000000
0.465 0.8972015 0.000000000
0.466 0.8972015 0.000000000
0.467 0.8972015 0.000000000
0.468 0.8972015 0.000000000
0.469 0.8972015 0.000000000
0.470 0.8972015 0.000000000
0.471 0.8972015 0.000000000
0.472 0.8972015 0.000000000
0.473 0.8972015 0.000000000
0.474 0.8972015 0.000000000
0.475 0.8972015 0.000000000
0.476 0.8972015 0.000000000
0.477 0.8972015 0.000000000
0.478 0.8972015 0.000000000
0.479 0.8972015 0.000000000
0.480 0.8972015 0.000000000
0.481 0.8972015 0.000000000
0.482 0.8972015 0.000000000
0.483 0.8972015 0.000000000
0.484 0.8972015 0.000000000
0.485 0.8972015 0.000000000
0.486 0.8972015 0.000000000
0.487 0.8972015 0.000000000
0.488 0.8972015 0.000000000
0.489 0.8972015 0.000000000
0.490 0.8972015 0.000000000
0.491 0.8972015 0.000000000
0.492 0.8972015 0.000000000
0.493 0.8972015 0.000000000
0.494 0.8972015 0.000000000
0.495 0.8972015 0.000000000
0.496 0.8972015 0.000000000
0.497 0.8972015 0.000000000
0.498 0.8972015 0.000000000
0.499 0.8972015 0.000000000
0.500 0.8972015 0.000000000
0.501 0.8972015 0.000000000
0.502 0.8972015 0.000000000
0.503 0.8972015 0.000000000
0.504 0.8972015 0.000000000
0.505 0.8972015 0.000000000
0.506 0.8972015 0.000000000
0.507 0.8972015 0.000000000
0.508 0.8972015 0.000000000
0.509 0.8972015 0.000000000
0.510 0.8972015 0.000000000
0.511 0.8972015 0.000000000
0.512 0.8972015 0.000000000
0.513 0.8972015 0.000000000
0.514 0.8972015 0.000000000
0.515 0.8972015 0.000000000
0.516 0.8972015 0.000000000
0.517 0.8972015 0.000000000
0.518 0.8972015 0.000000000
0.519 0.8972015 0.000000000
0.520 0.8972015 0.000000000
0.521 0.8972015 0.000000000
0.522 0.8972015 0.000000000
0.523 0.8972015 0.000000000
0.524 0.8972015 0.000000000
0.525 0.8972015 0.000000000
0.526 0.8972015 0.000000000
0.527 0.8972015 0.000000000
0.528 0.8972015 0.000000000
0.529 0.8972015 0.000000000
0.530 0.8972015 0.000000000
0.531 0.8972015 0.000000000
0.532 0.8972015 0.000000000
0.533 0.8972015 0.000000000
0.534 0.8972015 0.000000000
0.535 0.8972015 0.000000000
0.536 0.8972015 0.000000000
0.537 0.8972015 0.000000000
0.538 0.8972015 0.000000000
0.539 0.8972015 0.000000000
0.540 0.8972015 0.000000000
0.541 0.8972015 0.000000000
0.542 0.8972015 0.000000000
0.543 0.8972015 0.000000000
0.544 0.8972015 0.000000000
0.545 0.8972015 0.000000000
0.546 0.8972015 0.000000000
0.547 0.8972015 0.000000000
0.548 0.8972015 0.000000000
0.549 0.8972015 0.000000000
0.550 0.8972015 0.000000000
0.551 0.8972015 0.000000000
0.552 0.8972015 0.000000000
0.553 0.8972015 0.000000000
0.554 0.8972015 0.000000000
0.555 0.8972015 0.000000000
0.556 0.8972015 0.000000000
0.557 0.8972015 0.000000000
0.558 0.8972015 0.000000000
0.559 0.8972015 0.000000000
0.560 0.8972015 0.000000000
0.561 0.8972015 0.000000000
0.562 0.8972015 0.000000000
0.563 0.8972015 0.000000000
0.564 0.8972015 0.000000000
0.565 0.8972015 0.000000000
0.566 0.8972015 0.000000000
0.567 0.8972015 0.000000000
0.568 0.8972015 0.000000000
0.569 0.8972015 0.000000000
0.570 0.8972015 0.000000000
0.571 0.8972015 0.000000000
0.572 0.8972015 0.000000000
0.573 0.8972015 0.000000000
0.574 0.8972015 0.000000000
0.575 0.8972015 0.000000000
0.576 0.8972015 0.000000000
0.577 0.8972015 0.000000000
0.578 0.8972015 0.000000000
0.579 0.8972015 0.000000000
0.580 0.8972015 0.000000000
0.581 0.8972015 0.000000000
0.582 0.8972015 0.000000000
0.583 0.8972015 0.000000000
0.584 0.8972015 0.000000000
0.585 0.8972015 0.000000000
0.586 0.8972015 0.000000000
0.587 0.8972015 0.000000000
0.588 0.8972015 0.000000000
0.589 0.8972015 0.000000000
0.590 0.8972015 0.000000000
0.591 0.8972015 0.000000000
0.592 0.8972015 0.000000000
0.593 0.8972015 0.000000000
0.594 0.8972015 0.000000000
0.595 0.8972015 0.000000000
0.596 0.8972015 0.000000000
0.597 0.8972015 0.000000000
0.598 0.8972015 0.000000000
0.599 0.8972015 0.000000000
0.600 0.8972015 0.000000000
0.601 0.8972015 0.000000000
0.602 0.8972015 0.000000000
0.603 0.8972015 0.000000000
0.604 0.8972015 0.000000000
0.605 0.8972015 0.000000000
0.606 0.8972015 0.000000000
0.607 0.8972015 0.000000000
0.608 0.8972015 0.000000000
0.609 0.8972015 0.000000000
0.610 0.8972015 0.000000000
0.611 0.8972015 0.000000000
0.612 0.8972015 0.000000000
0.613 0.8972015 0.000000000
0.614 0.8972015 0.000000000
0.615 0.8972015 0.000000000
0.616 0.8972015 0.000000000
0.617 0.8972015 0.000000000
0.618 0.8972015 0.000000000
0.619 0.8972015 0.000000000
0.620 0.8972015 0.000000000
0.621 0.8972015 0.000000000
0.622 0.8972015 0.000000000
0.623 0.8972015 0.000000000
0.624 0.8972015 0.000000000
0.625 0.8972015 0.000000000
0.626 0.8972015 0.000000000
0.627 0.8972015 0.000000000
0.628 0.8972015 0.000000000
0.629 0.8972015 0.000000000
0.630 0.8972015 0.000000000
0.631 0.8972015 0.000000000
0.632 0.8972015 0.000000000
0.633 0.8972015 0.000000000
0.634 0.8972015 0.000000000
0.635 0.8972015 0.000000000
0.636 0.8972015 0.000000000
0.637 0.8972015 0.000000000
0.638 0.8972015 0.000000000
0.639 0.8972015 0.000000000
0.640 0.8972015 0.000000000
0.641 0.8972015 0.000000000
0.642 0.8972015 0.000000000
0.643 0.8972015 0.000000000
0.644 0.8972015 0.000000000
0.645 0.8972015 0.000000000
0.646 0.8972015 0.000000000
0.647 0.8972015 0.000000000
0.648 0.8972015 0.000000000
0.649 0.8972015 0.000000000
0.650 0.8972015 0.000000000
0.651 0.8972015 0.000000000
0.652 0.8972015 0.000000000
0.653 0.8972015 0.000000000
0.654 0.8972015 0.000000000
0.655 0.8972015 0.000000000
0.656 0.8972015 0.000000000
0.657 0.8972015 0.000000000
0.658 0.8972015 0.000000000
0.659 0.8972015 0.000000000
0.660 0.8972015 0.000000000
0.661 0.8972015 0.000000000
0.662 0.8972015 0.000000000
0.663 0.8972015 0.000000000
0.664 0.8972015 0.000000000
0.665 0.8972015 0.000000000
0.666 0.8972015 0.000000000
0.667 0.8972015 0.000000000
0.668 0.8972015 0.000000000
0.669 0.8972015 0.000000000
0.670 0.8972015 0.000000000
0.671 0.8972015 0.000000000
0.672 0.8972015 0.000000000
0.673 0.8972015 0.000000000
0.674 0.8972015 0.000000000
0.675 0.8972015 0.000000000
0.676 0.8972015 0.000000000
0.677 0.8972015 0.000000000
0.678 0.8972015 0.000000000
0.679 0.8972015 0.000000000
0.680 0.8972015 0.000000000
0.681 0.8972015 0.000000000
0.682 0.8972015 0.000000000
0.683 0.8972015 0.000000000
0.684 0.8972015 0.000000000
0.685 0.8972015 0.000000000
0.686 0.8972015 0.000000000
0.687 0.8972015 0.000000000
0.688 0.8972015 0.000000000
0.689 0.8972015 0.000000000
0.690 0.8972015 0.000000000
0.691 0.8972015 0.000000000
0.692 0.8972015 0.000000000
0.693 0.8972015 0.000000000
0.694 0.8972015 0.000000000
0.695 0.8972015 0.000000000
0.696 0.8972015 0.000000000
0.697 0.8972015 0.000000000
0.698 0.8972015 0.000000000
0.699 0.8972015 0.000000000
0.700 0.8972015 0.000000000
0.701 0.8972015 0.000000000
0.702 0.8972015 0.000000000
0.703 0.8972015 0.000000000
0.704 0.8972015 0.000000000
0.705 0.8972015 0.000000000
0.706 0.8972015 0.000000000
0.707 0.8972015 0.000000000
0.708 0.8972015 0.000000000
0.709 0.8972015 0.000000000
0.710 0.8972015 0.000000000
0.711 0.8972015 0.000000000
0.712 0.8972015 0.000000000
0.713 0.8972015 0.000000000
0.714 0.8972015 0.000000000
0.715 0.8972015 0.000000000
0.716 0.8972015 0.000000000
0.717 0.8972015 0.000000000
0.718 0.8972015 0.000000000
0.719 0.8972015 0.000000000
0.720 0.8972015 0.000000000
0.721 0.8972015 0.000000000
0.722 0.8972015 0.000000000
0.723 0.8972015 0.000000000
0.724 0.8972015 0.000000000
0.725 0.8972015 0.000000000
0.726 0.8972015 0.000000000
0.727 0.8972015 0.000000000
0.728 0.8972015 0.000000000
0.729 0.8972015 0.000000000
0.730 0.8972015 0.000000000
0.731 0.8972015 0.000000000
0.732 0.8972015 0.000000000
0.733 0.8972015 0.000000000
0.734 0.8972015 0.000000000
0.735 0.8972015 0.000000000
0.736 0.8972015 0.000000000
0.737 0.8972015 0.000000000
0.738 0.8972015 0.000000000
0.739 0.8972015 0.000000000
0.740 0.8972015 0.000000000
0.741 0.8972015 0.000000000
0.742 0.8972015 0.000000000
0.743 0.8972015 0.000000000
0.744 0.8972015 0.000000000
0.745 0.8972015 0.000000000
0.746 0.8972015 0.000000000
0.747 0.8972015 0.000000000
0.748 0.8972015 0.000000000
0.749 0.8972015 0.000000000
0.750 0.8972015 0.000000000
0.751 0.8972015 0.000000000
0.752 0.8972015 0.000000000
0.753 0.8972015 0.000000000
0.754 0.8972015 0.000000000
0.755 0.8972015 0.000000000
0.756 0.8972015 0.000000000
0.757 0.8972015 0.000000000
0.758 0.8972015 0.000000000
0.759 0.8972015 0.000000000
0.760 0.8972015 0.000000000
0.761 0.8972015 0.000000000
0.762 0.8972015 0.000000000
0.763 0.8972015 0.000000000
0.764 0.8972015 0.000000000
0.765 0.8972015 0.000000000
0.766 0.8972015 0.000000000
0.767 0.8972015 0.000000000
0.768 0.8972015 0.000000000
0.769 0.8972015 0.000000000
0.770 0.8972015 0.000000000
0.771 0.8972015 0.000000000
0.772 0.8972015 0.000000000
0.773 0.8972015 0.000000000
0.774 0.8972015 0.000000000
0.775 0.8972015 0.000000000
0.776 0.8972015 0.000000000
0.777 0.8972015 0.000000000
0.778 0.8972015 0.000000000
0.779 0.8972015 0.000000000
0.780 0.8972015 0.000000000
0.781 0.8972015 0.000000000
0.782 0.8972015 0.000000000
0.783 0.8972015 0.000000000
0.784 0.8972015 0.000000000
0.785 0.8972015 0.000000000
0.786 0.8972015 0.000000000
0.787 0.8972015 0.000000000
0.788 0.8972015 0.000000000
0.789 0.8972015 0.000000000
0.790 0.8972015 0.000000000
0.791 0.8972015 0.000000000
0.792 0.8972015 0.000000000
0.793 0.8972015 0.000000000
0.794 0.8972015 0.000000000
0.795 0.8972015 0.000000000
0.796 0.8972015 0.000000000
0.797 0.8972015 0.000000000
0.798 0.8972015 0.000000000
0.799 0.8972015 0.000000000
0.800 0.8972015 0.000000000
0.801 0.8972015 0.000000000
0.802 0.8972015 0.000000000
0.803 0.8972015 0.000000000
0.804 0.8972015 0.000000000
0.805 0.8972015 0.000000000
0.806 0.8972015 0.000000000
0.807 0.8972015 0.000000000
0.808 0.8972015 0.000000000
0.809 0.8972015 0.000000000
0.810 0.8972015 0.000000000
0.811 0.8972015 0.000000000
0.812 0.8972015 0.000000000
0.813 0.8972015 0.000000000
0.814 0.8972015 0.000000000
0.815 0.8972015 0.000000000
0.816 0.8972015 0.000000000
0.817 0.8972015 0.000000000
0.818 0.8972015 0.000000000
0.819 0.8972015 0.000000000
0.820 0.8972015 0.000000000
0.821 0.8972015 0.000000000
0.822 0.8972015 0.000000000
0.823 0.8972015 0.000000000
0.824 0.8972015 0.000000000
0.825 0.8972015 0.000000000
0.826 0.8972015 0.000000000
0.827 0.8972015 0.000000000
0.828 0.8972015 0.000000000
0.829 0.8972015 0.000000000
0.830 0.8972015 0.000000000
0.831 0.8972015 0.000000000
0.832 0.8972015 0.000000000
0.833 0.8972015 0.000000000
0.834 0.8972015 0.000000000
0.835 0.8972015 0.000000000
0.836 0.8972015 0.000000000
0.837 0.8972015 0.000000000
0.838 0.8972015 0.000000000
0.839 0.8972015 0.000000000
0.840 0.8972015 0.000000000
0.841 0.8972015 0.000000000
0.842 0.8972015 0.000000000
0.843 0.8972015 0.000000000
0.844 0.8972015 0.000000000
0.845 0.8972015 0.000000000
0.846 0.8972015 0.000000000
0.847 0.8972015 0.000000000
0.848 0.8972015 0.000000000
0.849 0.8972015 0.000000000
0.850 0.8972015 0.000000000
0.851 0.8972015 0.000000000
0.852 0.8972015 0.000000000
0.853 0.8972015 0.000000000
0.854 0.8972015 0.000000000
0.855 0.8972015 0.000000000
0.856 0.8972015 0.000000000
0.857 0.8972015 0.000000000
0.858 0.8972015 0.000000000
0.859 0.8972015 0.000000000
0.860 0.8972015 0.000000000
0.861 0.8972015 0.000000000
0.862 0.8972015 0.000000000
0.863 0.8972015 0.000000000
0.864 0.8972015 0.000000000
0.865 0.8972015 0.000000000
0.866 0.8972015 0.000000000
0.867 0.8972015 0.000000000
0.868 0.8972015 0.000000000
0.869 0.8972015 0.000000000
0.870 0.8972015 0.000000000
0.871 0.8972015 0.000000000
0.872 0.8972015 0.000000000
0.873 0.8972015 0.000000000
0.874 0.8972015 0.000000000
0.875 0.8972015 0.000000000
0.876 0.8972015 0.000000000
0.877 0.8972015 0.000000000
0.878 0.8972015 0.000000000
0.879 0.8972015 0.000000000
0.880 0.8972015 0.000000000
0.881 0.8972015 0.000000000
0.882 0.8972015 0.000000000
0.883 0.8972015 0.000000000
0.884 0.8972015 0.000000000
0.885 0.8972015 0.000000000
0.886 0.8972015 0.000000000
0.887 0.8972015 0.000000000
0.888 0.8972015 0.000000000
0.889 0.8972015 0.000000000
0.890 0.8972015 0.000000000
0.891 0.8972015 0.000000000
0.892 0.8972015 0.000000000
0.893 0.8972015 0.000000000
0.894 0.8972015 0.000000000
0.895 0.8972015 0.000000000
0.896 0.8972015 0.000000000
0.897 0.8972015 0.000000000
0.898 0.8972015 0.000000000
0.899 0.8972015 0.000000000
0.900 0.8972015 0.000000000
0.901 0.8972015 0.000000000
0.902 0.8972015 0.000000000
0.903 0.8972015 0.000000000
0.904 0.8972015 0.000000000
0.905 0.8972015 0.000000000
0.906 0.8972015 0.000000000
0.907 0.8972015 0.000000000
0.908 0.8972015 0.000000000
0.909 0.8972015 0.000000000
0.910 0.8972015 0.000000000
0.911 0.8972015 0.000000000
0.912 0.8972015 0.000000000
0.913 0.8972015 0.000000000
0.914 0.8972015 0.000000000
0.915 0.8972015 0.000000000
0.916 0.8972015 0.000000000
0.917 0.8972015 0.000000000
0.918 0.8972015 0.000000000
0.919 0.8972015 0.000000000
0.920 0.8972015 0.000000000
0.921 0.8972015 0.000000000
0.922 0.8972015 0.000000000
0.923 0.8972015 0.000000000
0.924 0.8972015 0.000000000
0.925 0.8972015 0.000000000
0.926 0.8972015 0.000000000
0.927 0.8972015 0.000000000
0.928 0.8972015 0.000000000
0.929 0.8972015 0.000000000
0.930 0.8972015 0.000000000
0.931 0.8972015 0.000000000
0.932 0.8972015 0.000000000
0.933 0.8972015 0.000000000
0.934 0.8972015 0.000000000
0.935 0.8972015 0.000000000
0.936 0.8972015 0.000000000
0.937 0.8972015 0.000000000
0.938 0.8972015 0.000000000
0.939 0.8972015 0.000000000
0.940 0.8972015 0.000000000
0.941 0.8972015 0.000000000
0.942 0.8972015 0.000000000
0.943 0.8972015 0.000000000
0.944 0.8972015 0.000000000
0.945 0.8972015 0.000000000
0.946 0.8972015 0.000000000
0.947 0.8972015 0.000000000
0.948 0.8972015 0.000000000
0.949 0.8972015 0.000000000
0.950 0.8972015 0.000000000
0.951 0.8972015 0.000000000
0.952 0.8972015 0.000000000
0.953 0.8972015 0.000000000
0.954 0.8972015 0.000000000
0.955 0.8972015 0.000000000
0.956 0.8972015 0.000000000
0.957 0.8972015 0.000000000
0.958 0.8972015 0.000000000
0.959 0.8972015 0.000000000
0.960 0.8972015 0.000000000
0.961 0.8972015 0.000000000
0.962 0.8972015 0.000000000
0.963 0.8972015 0.000000000
0.964 0.8972015 0.000000000
0.965 0.8972015 0.000000000
0.966 0.8972015 0.000000000
0.967 0.8972015 0.000000000
0.968 0.8972015 0.000000000
0.969 0.8972015 0.000000000
0.970 0.8972015 0.000000000
0.971 0.8972015 0.000000000
0.972 0.8972015 0.000000000
0.973 0.8972015 0.000000000
0.974 0.8972015 0.000000000
0.975 0.8972015 0.000000000
0.976 0.8972015 0.000000000
0.977 0.8972015 0.000000000
0.978 0.8972015 0.000000000
0.979 0.8972015 0.000000000
0.980 0.8972015 0.000000000
0.981 0.8972015 0.000000000
0.982 0.8972015 0.000000000
0.983 0.8972015 0.000000000
0.984 0.8972015 0.000000000
0.985 0.8972015 0.000000000
0.986 0.8972015 0.000000000
0.987 0.8972015 0.000000000
0.988 0.8972015 0.000000000
0.989 0.8972015 0.000000000
0.990 0.8972015 0.000000000
0.991 0.8972015 0.000000000
0.992 0.8972015 0.000000000
0.993 0.8972015 0.000000000
0.994 0.8972015 0.000000000
0.995 0.8972015 0.000000000
0.996 0.8972015 0.000000000
0.997 0.8972015 0.000000000
0.998 0.8972015 0.000000000
0.999 0.8972015 0.000000000
1.000 0.8972015 0.000000000
Tuning parameter 'alpha' was held constant at a value of 0
Accuracy was used to select the optimal model using the
largest value.
The final values used for the model were alpha = 0 and lambda = 0.016.
plot(ridge.fit) # Plot
ridge.fit$bestTune # lambda의 최적값
alpha lambda
17 0 0.016
Result!
lambda
= 0.016일 때 정확도가 가장 높은 것을 알 수 있으며, lambda
= 0.016를 가지는 모형을 최적의 훈련된 모형으로 선택한다.
16 x 1 sparse Matrix of class "dgCMatrix"
s1
(Intercept) -3.311
Age 0.023
Experience 0.013
Income 1.362
ZIP.Code 0.072
Family2 -0.172
Family3 0.353
Family4 0.339
CCAvg 0.374
Education2 0.632
Education3 0.631
Mortgage 0.087
Securities.Account1 -0.170
CD.Account1 0.615
Online1 -0.057
CreditCard1 -0.286
Result!
데이터 “UB.trd”의 Target “Personal.Loan”은 “no”와 “yes” 2개의 클래스를 가지며, “Factor” 변환하면 알파벳순으로 수준을 부여하기 때문에 “yes”가 두 번째 클래스가 된다. 즉, “yes”에 속할 확률(= 개인 대출 제의를 수락할 확률)을 \(p\)라고 할 때, 추정된 회귀계수를 이용하여 다음과 같은 모형식을 얻을 수 있다.
\[
\begin{align*}
\log{\frac{p}{1-p}} = &-3.311 +0.023 Z_{\text{Age}} +0.013 Z_{\text{Experience}} + 1.362 Z_{\text{Income}} \\
&+0.072 Z_{\text{ZIP.Code}} -0.172 Z_{\text{Family2}} + 0.353 Z_{\text{Family3}} + 0.339 Z_{\text{Family4}} \\
&+ 0.374 Z_{\text{CCAvg}} + 0.632 Z_{\text{Education2}} + 0.631 Z_{\text{Education3}} + 0.087 Z_{\text{Mortgage}} \\
&-0.170 Z_{\text{Securities.Account1}} + 0.615 Z_{\text{CD.Account1}} -0.057 Z_{\text{Online1}} -0.286 Z_{\text{CreditCard1}}
\end{align*}
\]
여기서, \(Z_{\text{예측 변수}}\)는 표준화한 예측 변수를 의미한다.
범주형 예측 변수(“Family”, “Education”, “Securities.Account”, “CD.Account”, “Online”, “CreditCard”)는 더미 변환이 수행되었는데, 예를 들어, Family2
는 가족 수가 2명인 경우 “1”값을 가지고 2명이 아니면 “0”값을 가진다.
Caution!
모형 평가를 위해 Test Dataset
에 대한 예측 class/확률
이 필요하며, 함수 predict()
를 이용하여 생성한다.
# 예측 class 생성
test.ridge.class <- predict(ridge.fit,
newdata = UB.ted[,-9]) # Test Dataset including Only 예측 변수
test.ridge.class %>%
as_tibble
# A tibble: 749 × 1
value
<fct>
1 no
2 no
3 no
4 no
5 no
6 no
7 no
8 no
9 no
10 no
# ℹ 739 more rows
CM <- caret::confusionMatrix(test.ridge.class, UB.ted$Personal.Loan,
positive = "yes") # confusionMatrix(예측 class, 실제 class, positive = "관심 class")
CM
Confusion Matrix and Statistics
Reference
Prediction no yes
no 669 38
yes 4 38
Accuracy : 0.9439
95% CI : (0.925, 0.9593)
No Information Rate : 0.8985
P-Value [Acc > NIR] : 6.273e-06
Kappa : 0.6164
Mcnemar's Test P-Value : 3.543e-07
Sensitivity : 0.50000
Specificity : 0.99406
Pos Pred Value : 0.90476
Neg Pred Value : 0.94625
Prevalence : 0.10147
Detection Rate : 0.05073
Detection Prevalence : 0.05607
Balanced Accuracy : 0.74703
'Positive' Class : yes
# 예측 확률 생성
test.ridge.prob <- predict(ridge.fit,
newdata = UB.ted[,-9], # Test Dataset including Only 예측 변수
type = "prob") # 예측 확률 생성
test.ridge.prob %>%
as_tibble
# A tibble: 749 × 2
no yes
<dbl> <dbl>
1 0.993 0.00672
2 0.978 0.0220
3 0.994 0.00583
4 0.997 0.00301
5 0.976 0.0239
6 0.986 0.0140
7 0.952 0.0484
8 0.886 0.114
9 0.909 0.0913
10 0.956 0.0438
# ℹ 739 more rows
test.ridge.prob <- test.ridge.prob[,2] # "Personal.Loan = yes"에 대한 예측 확률
ac <- UB.ted$Personal.Loan # Test Dataset의 실제 class
pp <- as.numeric(test.ridge.prob) # 예측 확률을 수치형으로 변환
Caution!
Package "pROC"
를 통해 출력한 ROC 곡선은 다양한 함수를 이용해서 그래프를 수정할 수 있다.
# 함수 plot.roc() 이용
plot.roc(ridge.roc,
col="gray", # Line Color
print.auc = TRUE, # AUC 출력 여부
print.auc.col = "red", # AUC 글씨 색깔
print.thres = TRUE, # Cutoff Value 출력 여부
print.thres.pch = 19, # Cutoff Value를 표시하는 도형 모양
print.thres.col = "red", # Cutoff Value를 표시하는 도형의 색깔
auc.polygon = TRUE, # 곡선 아래 면적에 대한 여부
auc.polygon.col = "gray90") # 곡선 아래 면적의 색깔
# 함수 ggroc() 이용
ggroc(ridge.roc) +
annotate(geom = "text", x = 0.9, y = 1.0,
label = paste("AUC = ", auc),
size = 5,
color="red") +
theme_bw()
pacman::p_load("Epi")
# install_version("etm", version = "1.1", repos = "http://cran.us.r-project.org")
ROC(pp, ac, plot = "ROC") # ROC(예측 확률, 실제 class)
pacman::p_load("ROCR")
ridge.pred <- prediction(pp, ac) # prediction(예측 확률, 실제 class)
ridge.perf <- performance(ridge.pred, "tpr", "fpr") # performance(, "민감도", "1-특이도")
plot(ridge.perf, col = "gray") # ROC Curve
perf.auc <- performance(ridge.pred, "auc") # AUC
auc <- attributes(perf.auc)$y.values
legend("bottomright", legend = auc, bty = "n")
ridge.perf <- performance(ridge.pred, "lift", "rpp") # Lift Chart
plot(ridge.perf, main = "lift curve",
colorize = T, # Coloring according to cutoff
lwd = 2)
Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".