# 24년 1월 31일 빅데이터 분석 3일차
#초기화
getwd()
## [1] "H:/My Drive/R교육/빅데이터 분석 실무과정 교안 및 소스/source data"
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
#라이브러리 로드드
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(readxl)
library(caret)
## Loading required package: lattice
#데이터 불러오기기
data(iris)
#데이터 분할
idx<-createDataPartition(iris$Species, p=0.8, list=FALSE)
# 분할이 잘 되었는지 확인 코드 단!!!! list=FALSE 제외한 상태에서 볼 수 있다
#트레이닝 세트, 테스트 세트
train<-iris[idx,]
test<-iris[-idx,]
nrow(train)
## [1] 120
nrow(test)
## [1] 30
#train:valid:test= 6:2:2 분할
# library(caret)
# set.seed(2)
# data(iris)
# irx<-createDataPartition(iris$Species, p=0.6, list=FALSE)
# trains<-iris[irx,]
# valid_test<-iris[-irx,]
# irxx<-createDataPartition(valid_test, p=0.5, list=FALSE)
## PrePorcessing 전처리 작업
# 1) Z-score 표준화
library(caret)
data(iris)
glimpse(iris)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
set.seed(2)
#preProcess로
zs_iris<-preProcess(iris,method=c("center","scale"))
zs_iris # -ignored (1) Species 는 제외된다다
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - centered (4)
## - ignored (1)
## - scaled (4)
zs_iris_p<-predict(zs_iris,iris)
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
describe(zs_iris_p[,1:4])
## vars n mean sd median trimmed mad min max range skew
## Sepal.Length 1 150 0 1 -0.05 -0.04 1.25 -1.86 2.48 4.35 0.31
## Sepal.Width 2 150 0 1 -0.13 -0.03 1.02 -2.43 3.08 5.51 0.31
## Petal.Length 3 150 0 1 0.34 0.00 1.05 -1.56 1.78 3.34 -0.27
## Petal.Width 4 150 0 1 0.13 -0.02 1.36 -1.44 1.71 3.15 -0.10
## kurtosis se
## Sepal.Length -0.61 0.08
## Sepal.Width 0.14 0.08
## Petal.Length -1.42 0.08
## Petal.Width -1.36 0.08
# 2) Min-Max Scaling
sca_iris<-preProcess(iris,method="range")
sca_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - ignored (1)
## - re-scaling to [0, 1] (4)
sca_iris_p<-predict(sca_iris,iris)
head(sca_iris_p)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 0.22222222 0.6250000 0.06779661 0.04166667 setosa
## 2 0.16666667 0.4166667 0.06779661 0.04166667 setosa
## 3 0.11111111 0.5000000 0.05084746 0.04166667 setosa
## 4 0.08333333 0.4583333 0.08474576 0.04166667 setosa
## 5 0.19444444 0.6666667 0.06779661 0.04166667 setosa
## 6 0.30555556 0.7916667 0.11864407 0.12500000 setosa
library(psych)
describe(sca_iris_p)
## vars n mean sd median trimmed mad min max range skew
## Sepal.Length 1 150 0.43 0.23 0.42 0.42 0.29 0 1 1 0.31
## Sepal.Width 2 150 0.44 0.18 0.42 0.43 0.19 0 1 1 0.31
## Petal.Length 3 150 0.47 0.30 0.57 0.47 0.31 0 1 1 -0.27
## Petal.Width 4 150 0.46 0.32 0.50 0.45 0.43 0 1 1 -0.10
## Species* 5 150 2.00 0.82 2.00 2.00 1.48 1 3 2 0.00
## kurtosis se
## Sepal.Length -0.61 0.02
## Sepal.Width 0.14 0.01
## Petal.Length -1.42 0.02
## Petal.Width -1.36 0.03
## Species* -1.52 0.07
# 3) Box-Cox transformation 데이터를 정규분포에 가깝게 만들거나 데이터의 분산을 안정화
library(caret)
data(iris)
bc_iris = preProcess(iris, method = "BoxCox")
bc_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - Box-Cox transformation (4)
## - ignored (1)
##
## Lambda estimates for Box-Cox transformation:
## -0.1, 0.3, 0.9, 0.6
bc_iris_t=predict(bc_iris,iris)
head(bc_iris_t)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 1.629241 1.520660 1.4 -1.0321154 setosa
## 2 1.589235 1.301297 1.4 -1.0321154 setosa
## 3 1.547563 1.391905 1.3 -1.0321154 setosa
## 4 1.526056 1.347113 1.5 -1.0321154 setosa
## 5 1.609438 1.561856 1.4 -1.0321154 setosa
## 6 1.686399 1.680826 1.7 -0.7048667 setosa
describe(bc_iris_t)
## vars n mean sd median trimmed mad min max range skew
## Sepal.Length 1 150 1.76 0.14 1.76 1.75 0.17 1.46 2.07 0.61 0.04
## Sepal.Width 2 150 1.32 0.20 1.30 1.32 0.20 0.77 1.87 1.10 0.01
## Petal.Length 3 150 3.76 1.77 4.35 3.76 1.85 1.00 6.90 5.90 -0.27
## Petal.Width 4 150 0.07 0.79 0.28 0.09 0.85 -1.25 1.22 2.47 -0.36
## Species* 5 150 2.00 0.82 2.00 2.00 1.48 1.00 3.00 2.00 0.00
## kurtosis se
## Sepal.Length -0.78 0.01
## Sepal.Width 0.02 0.02
## Petal.Length -1.42 0.14
## Petal.Width -1.38 0.06
## Species* -1.52 0.07
#그래프로 정규화 여부 확인, 박스칵스 변환 전과 후
par(mfrow=c(1,2))
plot(density(x=iris$Sepal.Width))
plot(density(x=bc_iris_t$Sepal.Width))

# 4) PCA(Principal Components Analysis) 주성분 분석
library(caret)
data(iris)
pca_iris = preProcess(iris, method = "pca")
pca_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - centered (4)
## - ignored (1)
## - principal component signal extraction (4)
## - scaled (4)
##
## PCA needed 2 components to capture 95 percent of the variance
pca_iris_p = predict(pca_iris, iris)
head(pca_iris_p)
## Species PC1 PC2
## 1 setosa -2.257141 -0.4784238
## 2 setosa -2.074013 0.6718827
## 3 setosa -2.356335 0.3407664
## 4 setosa -2.291707 0.5953999
## 5 setosa -2.381863 -0.6446757
## 6 setosa -2.068701 -1.4842053
describe(pca_iris_p)
## vars n mean sd median trimmed mad min max range skew kurtosis
## Species* 1 150 2 0.82 2.00 2.00 1.48 1.00 3.00 2.00 0.00 -1.52
## PC1 2 150 0 1.71 0.42 0.01 1.96 -2.77 3.30 6.06 -0.26 -1.39
## PC2 3 150 0 0.96 -0.02 0.00 0.89 -2.68 2.65 5.32 -0.07 0.35
## se
## Species* 0.07
## PC1 0.14
## PC2 0.08
# 5) 더미 변수 생성(Creating Dummy Variables) --> R에서는 자동으로 해준다
library(earth)
## Loading required package: Formula
## Loading required package: plotmo
## Loading required package: plotrix
##
## Attaching package: 'plotrix'
## The following object is masked from 'package:psych':
##
## rescale
## Loading required package: TeachingDemos
library(dplyr)
data(etitanic)
glimpse(etitanic)
## Rows: 1,046
## Columns: 6
## $ pclass <fct> 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1…
## $ survived <int> 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1…
## $ sex <fct> female, male, female, male, female, male, female, male, femal…
## $ age <dbl> 29.0000, 0.9167, 2.0000, 30.0000, 25.0000, 48.0000, 63.0000, …
## $ sibsp <int> 0, 1, 1, 1, 1, 0, 1, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1…
## $ parch <int> 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1…
dummies <- dummyVars(survived ~ ., data = etitanic)
dummies
## Dummy Variable Object
##
## Formula: survived ~ .
## 6 variables, 2 factors
## Variables and levels will be separated by '.'
## A less than full rank encoding is used
head(predict(dummies, newdata = etitanic))
## pclass.1st pclass.2nd pclass.3rd sex.female sex.male age sibsp parch
## 1 1 0 0 1 0 29.0000 0 0
## 2 1 0 0 0 1 0.9167 1 2
## 3 1 0 0 1 0 2.0000 1 2
## 4 1 0 0 0 1 30.0000 1 2
## 5 1 0 0 1 0 25.0000 1 2
## 6 1 0 0 0 1 48.0000 0 0
# 6) 대체 k-최근접 이웃(k-Nearest Neighbors, KNN)
data(airquality)
colSums(is.na(airquality)) #airquality data set에 44개의 결측치가 존재한다.
## Ozone Solar.R Wind Temp Month Day
## 37 7 0 0 0 0
library(RANN)
head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
# method=“knnImpute” 를 적용하면 center과 scale이 같이 적용된다.
knn_im<- preProcess(airquality[1:4], method="knnImpute")
head(predict(knn_im, airquality))
## Ozone Solar.R Wind Temp Month Day
## 1 -0.03423409 0.04517615 -0.7259482 -1.1497140 5 1
## 2 -0.18580489 -0.75430487 -0.5556388 -0.6214670 5 2
## 3 -0.91334473 -0.41008388 0.7500660 -0.4101682 5 3
## 4 -0.73145977 1.41095624 0.4378323 -1.6779609 5 4
## 5 -0.69508278 -0.92308420 1.2326091 -2.3118573 5 5
## 6 -0.42831817 -0.28349938 1.4029185 -1.2553634 5 6
glimpse(airquality)
## Rows: 153
## Columns: 6
## $ Ozone <int> 41, 36, 12, 18, NA, 28, 23, 19, 8, NA, 7, 16, 11, 14, 18, 14, …
## $ Solar.R <int> 190, 118, 149, 313, NA, NA, 299, 99, 19, 194, NA, 256, 290, 27…
## $ Wind <dbl> 7.4, 8.0, 12.6, 11.5, 14.3, 14.9, 8.6, 13.8, 20.1, 8.6, 6.9, 9…
## $ Temp <int> 67, 72, 74, 62, 56, 66, 65, 59, 61, 69, 74, 69, 66, 68, 58, 64…
## $ Month <int> 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,…
## $ Day <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,…
knn_im_p<-predict(knn_im,airquality)
colSums(is.na(knn_im_p))
## Ozone Solar.R Wind Temp Month Day
## 0 0 0 0 0 0
describe(knn_im_p)
## vars n mean sd median trimmed mad min max range skew
## Ozone 1 153 -0.04 0.92 -0.31 -0.16 0.72 -1.25 3.82 5.06 1.30
## Solar.R 2 153 -0.01 0.99 0.17 0.03 1.05 -1.99 1.64 3.63 -0.39
## Wind 3 153 0.00 1.00 -0.07 -0.02 0.97 -2.34 3.05 5.39 0.34
## Temp 4 153 0.00 1.00 0.12 0.04 0.94 -2.31 2.02 4.33 -0.37
## Month 5 153 6.99 1.42 7.00 6.99 1.48 5.00 9.00 4.00 0.00
## Day 6 153 15.80 8.86 16.00 15.80 11.86 1.00 31.00 30.00 0.00
## kurtosis se
## Ozone 1.62 0.07
## Solar.R -0.99 0.08
## Wind 0.03 0.08
## Temp -0.46 0.08
## Month -1.32 0.11
## Day -1.22 0.72
#median을 활용
median_im<-preProcess(airquality[1:4],method="medianImpute")
head(predict(median_im,airquality))
## Ozone Solar.R Wind Temp Month Day
## 1 41.0 190 7.4 67 5 1
## 2 36.0 118 8.0 72 5 2
## 3 12.0 149 12.6 74 5 3
## 4 18.0 313 11.5 62 5 4
## 5 31.5 205 14.3 56 5 5
## 6 28.0 205 14.9 66 5 6
median_im_p <-predict(median_im,airquality)
describe(median_im_p)
## vars n mean sd median trimmed mad min max range skew
## Ozone 1 153 39.56 29.05 31.5 35.21 18.53 1.0 168.0 167 1.60
## Solar.R 2 153 186.80 88.05 205.0 191.53 91.92 7.0 334.0 327 -0.46
## Wind 3 153 9.96 3.52 9.7 9.87 3.41 1.7 20.7 19 0.34
## Temp 4 153 77.88 9.47 79.0 78.28 8.90 56.0 97.0 41 -0.37
## Month 5 153 6.99 1.42 7.0 6.99 1.48 5.0 9.0 4 0.00
## Day 6 153 15.80 8.86 16.0 15.80 11.86 1.0 31.0 30 0.00
## kurtosis se
## Ozone 2.70 2.35
## Solar.R -0.90 7.12
## Wind 0.03 0.28
## Temp -0.46 0.77
## Month -1.32 0.11
## Day -1.22 0.72
#Overlayed Density Plots
featurePlot(x=iris[,1:4], y= iris$Species,
plot = 'density',
scales = list(x=list(relation="free"),
y=list(relation="free")),
adjust = 1.5, pch = "|",layout =c(4,1),
auto.key = list(columns =3))

# caret에서 Scatter Plots
library(mlbench)
data(BostonHousing)
glimpse(BostonHousing)
## Rows: 506
## Columns: 14
## $ crim <dbl> 0.00632, 0.02731, 0.02729, 0.03237, 0.06905, 0.02985, 0.08829,…
## $ zn <dbl> 18.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.5, 12.5, 12.5, 12.5, 12.5, 1…
## $ indus <dbl> 2.31, 7.07, 7.07, 2.18, 2.18, 2.18, 7.87, 7.87, 7.87, 7.87, 7.…
## $ chas <fct> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nox <dbl> 0.538, 0.469, 0.469, 0.458, 0.458, 0.458, 0.524, 0.524, 0.524,…
## $ rm <dbl> 6.575, 6.421, 7.185, 6.998, 7.147, 6.430, 6.012, 6.172, 5.631,…
## $ age <dbl> 65.2, 78.9, 61.1, 45.8, 54.2, 58.7, 66.6, 96.1, 100.0, 85.9, 9…
## $ dis <dbl> 4.0900, 4.9671, 4.9671, 6.0622, 6.0622, 6.0622, 5.5605, 5.9505…
## $ rad <dbl> 1, 2, 2, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,…
## $ tax <dbl> 296, 242, 242, 222, 222, 222, 311, 311, 311, 311, 311, 311, 31…
## $ ptratio <dbl> 15.3, 17.8, 17.8, 18.7, 18.7, 18.7, 15.2, 15.2, 15.2, 15.2, 15…
## $ b <dbl> 396.90, 396.90, 392.83, 394.63, 396.90, 394.12, 395.60, 396.90…
## $ lstat <dbl> 4.98, 9.14, 4.03, 2.94, 5.33, 5.21, 12.43, 19.15, 29.93, 17.10…
## $ medv <dbl> 24.0, 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15…
x=BostonHousing[,c("age","lstat","tax")]
y=BostonHousing$medv
featurePlot(x, y, plot = "scatter", layout=c(3, 1))

# 4. 변수 선택(Feature Selection)
# 1) 변수의 중요도(importance)
library(mlbench)
library(caret)
library(dplyr)
data(PimaIndiansDiabetes)
glimpse(PimaIndiansDiabetes)
## Rows: 768
## Columns: 9
## $ pregnant <dbl> 6, 1, 8, 1, 0, 5, 3, 10, 2, 8, 4, 10, 10, 1, 5, 7, 0, 7, 1, 1…
## $ glucose <dbl> 148, 85, 183, 89, 137, 116, 78, 115, 197, 125, 110, 168, 139,…
## $ pressure <dbl> 72, 66, 64, 66, 40, 74, 50, 0, 70, 96, 92, 74, 80, 60, 72, 0,…
## $ triceps <dbl> 35, 29, 0, 23, 35, 0, 32, 0, 45, 0, 0, 0, 0, 23, 19, 0, 47, 0…
## $ insulin <dbl> 0, 0, 0, 94, 168, 0, 88, 0, 543, 0, 0, 0, 0, 846, 175, 0, 230…
## $ mass <dbl> 33.6, 26.6, 23.3, 28.1, 43.1, 25.6, 31.0, 35.3, 30.5, 0.0, 37…
## $ pedigree <dbl> 0.627, 0.351, 0.672, 0.167, 2.288, 0.201, 0.248, 0.134, 0.158…
## $ age <dbl> 50, 31, 32, 21, 33, 30, 26, 29, 53, 54, 30, 34, 57, 59, 51, 3…
## $ diabetes <fct> pos, neg, pos, neg, pos, neg, pos, neg, pos, pos, neg, pos, n…
# PimaIndiansDiabetes data set은 환자의 임신 횟수,
# BMI, 인슐린 수준, 연령 등을 통해 당뇨병을 예측하는 데이터이다.
#훈련설정
control <- trainControl(method = "repeatedcv", number=10, repeats=3) #cv도 돌아간다
#모형훈련
#분류 모형 알고리즘으로 랜덤 포레스트(rf), 전처리(preProcess)로 표준화
# ("center","scale") 학습을 진행한다.
model <- train(diabetes~.,data=PimaIndiansDiabetes,method='rf',
preProcess=c("center","scale"), trControl=control)
importance <- varImp(model,scale=FALSE)
print(importance)
## rf variable importance
##
## Overall
## glucose 92.31
## mass 56.42
## age 46.18
## pedigree 43.79
## pressure 30.55
## pregnant 28.95
## insulin 25.25
## triceps 23.14
plot(importance)

## 예제로 해보자
df<-read_excel("240123_bigdata.xlsx")
glimpse(df)
## Rows: 467
## Columns: 19
## $ 시군구명 <chr> "종로구", "종로구", "종로구", "종로구", "종로구",…
## $ 법정동코드 <dbl> 1111010100, 1111010200, 1111010300, 1111010400, 1…
## $ 법정동 <chr> "청운동", "신교동", "궁정동", "효자동", "창성동",…
## $ 교통사고위험지수_ARI <dbl> 5.696002, 3.073181, 3.590110, 2.426703, 3.431877,…
## $ 신호위반사고 <dbl> 2, 1, 2, 0, 2, 0, 11, 2, 0, 0, 0, 0, 3, 5, 7, 4, …
## $ 중앙선침범사고 <dbl> 1, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0…
## $ 차대사람사고 <dbl> 3, 3, 1, 1, 5, 4, 5, 4, 4, 6, 5, 5, 5, 8, 6, 1, 6…
## $ 차대차사고 <dbl> 13, 6, 9, 6, 4, 6, 31, 6, 1, 4, 2, 6, 13, 32, 36,…
## $ 차량단독사고 <dbl> 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 1, 0, 0, 2, 3, 0, 0…
## $ 교통사고발생건수 <dbl> 16, 9, 10, 7, 9, 11, 39, 10, 5, 10, 8, 11, 18, 42…
## $ 단속건수_신호과속 <dbl> 5829.6667, 803.0000, 0.0000, 0.0000, 0.0000, 0.00…
## $ 신호위반건수 <dbl> 383.000000, 45.000000, 0.000000, 0.000000, 0.0000…
## $ 과속건수 <dbl> 5446.6667, 758.0000, 0.0000, 0.0000, 0.0000, 0.00…
## $ 단속장비설치지점 <dbl> 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 0…
## $ 보호구역총합 <dbl> 2, 2, 1, 0, 0, 0, 0, 0, 2, 0, 1, 0, 2, 0, 1, 0, 0…
## $ 생활인구 <dbl> 3724263.1, 463266.4, 341303.8, 276322.0, 278986.4…
## $ 인구밀도 <dbl> 1327.59926, 165.14195, 121.66560, 98.50135, 99.45…
## $ 상권개수 <dbl> 1.44367035, 0.17958020, 0.13230274, 0.10711326, 0…
## $ 교차로수 <dbl> 10, 1, 3, 1, 3, 2, 3, 0, 2, 1, 2, 0, 3, 1, 7, 2, …
df1<-df %>% select(-시군구명, -법정동코드,-법정동)
glimpse(df1)
## Rows: 467
## Columns: 16
## $ 교통사고위험지수_ARI <dbl> 5.696002, 3.073181, 3.590110, 2.426703, 3.431877,…
## $ 신호위반사고 <dbl> 2, 1, 2, 0, 2, 0, 11, 2, 0, 0, 0, 0, 3, 5, 7, 4, …
## $ 중앙선침범사고 <dbl> 1, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0…
## $ 차대사람사고 <dbl> 3, 3, 1, 1, 5, 4, 5, 4, 4, 6, 5, 5, 5, 8, 6, 1, 6…
## $ 차대차사고 <dbl> 13, 6, 9, 6, 4, 6, 31, 6, 1, 4, 2, 6, 13, 32, 36,…
## $ 차량단독사고 <dbl> 0, 0, 0, 0, 0, 1, 3, 0, 0, 0, 1, 0, 0, 2, 3, 0, 0…
## $ 교통사고발생건수 <dbl> 16, 9, 10, 7, 9, 11, 39, 10, 5, 10, 8, 11, 18, 42…
## $ 단속건수_신호과속 <dbl> 5829.6667, 803.0000, 0.0000, 0.0000, 0.0000, 0.00…
## $ 신호위반건수 <dbl> 383.000000, 45.000000, 0.000000, 0.000000, 0.0000…
## $ 과속건수 <dbl> 5446.6667, 758.0000, 0.0000, 0.0000, 0.0000, 0.00…
## $ 단속장비설치지점 <dbl> 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 3, 0, 0…
## $ 보호구역총합 <dbl> 2, 2, 1, 0, 0, 0, 0, 0, 2, 0, 1, 0, 2, 0, 1, 0, 0…
## $ 생활인구 <dbl> 3724263.1, 463266.4, 341303.8, 276322.0, 278986.4…
## $ 인구밀도 <dbl> 1327.59926, 165.14195, 121.66560, 98.50135, 99.45…
## $ 상권개수 <dbl> 1.44367035, 0.17958020, 0.13230274, 0.10711326, 0…
## $ 교차로수 <dbl> 10, 1, 3, 1, 3, 2, 3, 0, 2, 1, 2, 0, 3, 1, 7, 2, …
model1 <- train(교통사고위험지수_ARI~.,data=df1,method='rf',
preProcess=c("center","scale"), trControl=control)
importance <- varImp(model1,scale=FALSE)
print(importance)
## rf variable importance
##
## Overall
## 교통사고발생건수 6366756.4
## 차대차사고 373080.6
## 차대사람사고 42058.5
## 신호위반사고 24075.6
## 생활인구 16516.6
## 상권개수 2661.8
## 중앙선침범사고 2281.9
## 단속장비설치지점 2119.3
## 인구밀도 1899.4
## 단속건수_신호과속 1760.5
## 과속건수 1562.7
## 신호위반건수 1511.6
## 보호구역총합 1400.1
## 교차로수 1282.0
## 차량단독사고 885.3
plot(importance)

## 머신러닝
library(caret)
library(mlbench)
data(Sonar)
set.seed(998)
Sonar %>% glimpse() %>% head(1)
## Rows: 208
## Columns: 61
## $ V1 <dbl> 0.0200, 0.0453, 0.0262, 0.0100, 0.0762, 0.0286, 0.0317, 0.0519, …
## $ V2 <dbl> 0.0371, 0.0523, 0.0582, 0.0171, 0.0666, 0.0453, 0.0956, 0.0548, …
## $ V3 <dbl> 0.0428, 0.0843, 0.1099, 0.0623, 0.0481, 0.0277, 0.1321, 0.0842, …
## $ V4 <dbl> 0.0207, 0.0689, 0.1083, 0.0205, 0.0394, 0.0174, 0.1408, 0.0319, …
## $ V5 <dbl> 0.0954, 0.1183, 0.0974, 0.0205, 0.0590, 0.0384, 0.1674, 0.1158, …
## $ V6 <dbl> 0.0986, 0.2583, 0.2280, 0.0368, 0.0649, 0.0990, 0.1710, 0.0922, …
## $ V7 <dbl> 0.1539, 0.2156, 0.2431, 0.1098, 0.1209, 0.1201, 0.0731, 0.1027, …
## $ V8 <dbl> 0.1601, 0.3481, 0.3771, 0.1276, 0.2467, 0.1833, 0.1401, 0.0613, …
## $ V9 <dbl> 0.3109, 0.3337, 0.5598, 0.0598, 0.3564, 0.2105, 0.2083, 0.1465, …
## $ V10 <dbl> 0.2111, 0.2872, 0.6194, 0.1264, 0.4459, 0.3039, 0.3513, 0.2838, …
## $ V11 <dbl> 0.1609, 0.4918, 0.6333, 0.0881, 0.4152, 0.2988, 0.1786, 0.2802, …
## $ V12 <dbl> 0.1582, 0.6552, 0.7060, 0.1992, 0.3952, 0.4250, 0.0658, 0.3086, …
## $ V13 <dbl> 0.2238, 0.6919, 0.5544, 0.0184, 0.4256, 0.6343, 0.0513, 0.2657, …
## $ V14 <dbl> 0.0645, 0.7797, 0.5320, 0.2261, 0.4135, 0.8198, 0.3752, 0.3801, …
## $ V15 <dbl> 0.0660, 0.7464, 0.6479, 0.1729, 0.4528, 1.0000, 0.5419, 0.5626, …
## $ V16 <dbl> 0.2273, 0.9444, 0.6931, 0.2131, 0.5326, 0.9988, 0.5440, 0.4376, …
## $ V17 <dbl> 0.3100, 1.0000, 0.6759, 0.0693, 0.7306, 0.9508, 0.5150, 0.2617, …
## $ V18 <dbl> 0.2999, 0.8874, 0.7551, 0.2281, 0.6193, 0.9025, 0.4262, 0.1199, …
## $ V19 <dbl> 0.5078, 0.8024, 0.8929, 0.4060, 0.2032, 0.7234, 0.2024, 0.6676, …
## $ V20 <dbl> 0.4797, 0.7818, 0.8619, 0.3973, 0.4636, 0.5122, 0.4233, 0.9402, …
## $ V21 <dbl> 0.5783, 0.5212, 0.7974, 0.2741, 0.4148, 0.2074, 0.7723, 0.7832, …
## $ V22 <dbl> 0.5071, 0.4052, 0.6737, 0.3690, 0.4292, 0.3985, 0.9735, 0.5352, …
## $ V23 <dbl> 0.4328, 0.3957, 0.4293, 0.5556, 0.5730, 0.5890, 0.9390, 0.6809, …
## $ V24 <dbl> 0.5550, 0.3914, 0.3648, 0.4846, 0.5399, 0.2872, 0.5559, 0.9174, …
## $ V25 <dbl> 0.6711, 0.3250, 0.5331, 0.3140, 0.3161, 0.2043, 0.5268, 0.7613, …
## $ V26 <dbl> 0.6415, 0.3200, 0.2413, 0.5334, 0.2285, 0.5782, 0.6826, 0.8220, …
## $ V27 <dbl> 0.7104, 0.3271, 0.5070, 0.5256, 0.6995, 0.5389, 0.5713, 0.8872, …
## $ V28 <dbl> 0.8080, 0.2767, 0.8533, 0.2520, 1.0000, 0.3750, 0.5429, 0.6091, …
## $ V29 <dbl> 0.6791, 0.4423, 0.6036, 0.2090, 0.7262, 0.3411, 0.2177, 0.2967, …
## $ V30 <dbl> 0.3857, 0.2028, 0.8514, 0.3559, 0.4724, 0.5067, 0.2149, 0.1103, …
## $ V31 <dbl> 0.1307, 0.3788, 0.8512, 0.6260, 0.5103, 0.5580, 0.5811, 0.1318, …
## $ V32 <dbl> 0.2604, 0.2947, 0.5045, 0.7340, 0.5459, 0.4778, 0.6323, 0.0624, …
## $ V33 <dbl> 0.5121, 0.1984, 0.1862, 0.6120, 0.2881, 0.3299, 0.2965, 0.0990, …
## $ V34 <dbl> 0.7547, 0.2341, 0.2709, 0.3497, 0.0981, 0.2198, 0.1873, 0.4006, …
## $ V35 <dbl> 0.8537, 0.1306, 0.4232, 0.3953, 0.1951, 0.1407, 0.2969, 0.3666, …
## $ V36 <dbl> 0.8507, 0.4182, 0.3043, 0.3012, 0.4181, 0.2856, 0.5163, 0.1050, …
## $ V37 <dbl> 0.6692, 0.3835, 0.6116, 0.5408, 0.4604, 0.3807, 0.6153, 0.1915, …
## $ V38 <dbl> 0.6097, 0.1057, 0.6756, 0.8814, 0.3217, 0.4158, 0.4283, 0.3930, …
## $ V39 <dbl> 0.4943, 0.1840, 0.5375, 0.9857, 0.2828, 0.4054, 0.5479, 0.4288, …
## $ V40 <dbl> 0.2744, 0.1970, 0.4719, 0.9167, 0.2430, 0.3296, 0.6133, 0.2546, …
## $ V41 <dbl> 0.0510, 0.1674, 0.4647, 0.6121, 0.1979, 0.2707, 0.5017, 0.1151, …
## $ V42 <dbl> 0.2834, 0.0583, 0.2587, 0.5006, 0.2444, 0.2650, 0.2377, 0.2196, …
## $ V43 <dbl> 0.2825, 0.1401, 0.2129, 0.3210, 0.1847, 0.0723, 0.1957, 0.1879, …
## $ V44 <dbl> 0.4256, 0.1628, 0.2222, 0.3202, 0.0841, 0.1238, 0.1749, 0.1437, …
## $ V45 <dbl> 0.2641, 0.0621, 0.2111, 0.4295, 0.0692, 0.1192, 0.1304, 0.2146, …
## $ V46 <dbl> 0.1386, 0.0203, 0.0176, 0.3654, 0.0528, 0.1089, 0.0597, 0.2360, …
## $ V47 <dbl> 0.1051, 0.0530, 0.1348, 0.2655, 0.0357, 0.0623, 0.1124, 0.1125, …
## $ V48 <dbl> 0.1343, 0.0742, 0.0744, 0.1576, 0.0085, 0.0494, 0.1047, 0.0254, …
## $ V49 <dbl> 0.0383, 0.0409, 0.0130, 0.0681, 0.0230, 0.0264, 0.0507, 0.0285, …
## $ V50 <dbl> 0.0324, 0.0061, 0.0106, 0.0294, 0.0046, 0.0081, 0.0159, 0.0178, …
## $ V51 <dbl> 0.0232, 0.0125, 0.0033, 0.0241, 0.0156, 0.0104, 0.0195, 0.0052, …
## $ V52 <dbl> 0.0027, 0.0084, 0.0232, 0.0121, 0.0031, 0.0045, 0.0201, 0.0081, …
## $ V53 <dbl> 0.0065, 0.0089, 0.0166, 0.0036, 0.0054, 0.0014, 0.0248, 0.0120, …
## $ V54 <dbl> 0.0159, 0.0048, 0.0095, 0.0150, 0.0105, 0.0038, 0.0131, 0.0045, …
## $ V55 <dbl> 0.0072, 0.0094, 0.0180, 0.0085, 0.0110, 0.0013, 0.0070, 0.0121, …
## $ V56 <dbl> 0.0167, 0.0191, 0.0244, 0.0073, 0.0015, 0.0089, 0.0138, 0.0097, …
## $ V57 <dbl> 0.0180, 0.0140, 0.0316, 0.0050, 0.0072, 0.0057, 0.0092, 0.0085, …
## $ V58 <dbl> 0.0084, 0.0049, 0.0164, 0.0044, 0.0048, 0.0027, 0.0143, 0.0047, …
## $ V59 <dbl> 0.0090, 0.0052, 0.0095, 0.0040, 0.0107, 0.0051, 0.0036, 0.0048, …
## $ V60 <dbl> 0.0032, 0.0044, 0.0078, 0.0117, 0.0094, 0.0062, 0.0103, 0.0053, …
## $ Class <fct> R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R…
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
## 1 0.02 0.0371 0.0428 0.0207 0.0954 0.0986 0.1539 0.1601 0.3109 0.2111 0.1609
## V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22
## 1 0.1582 0.2238 0.0645 0.066 0.2273 0.31 0.2999 0.5078 0.4797 0.5783 0.5071
## V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33
## 1 0.4328 0.555 0.6711 0.6415 0.7104 0.808 0.6791 0.3857 0.1307 0.2604 0.5121
## V34 V35 V36 V37 V38 V39 V40 V41 V42 V43 V44
## 1 0.7547 0.8537 0.8507 0.6692 0.6097 0.4943 0.2744 0.051 0.2834 0.2825 0.4256
## V45 V46 V47 V48 V49 V50 V51 V52 V53 V54 V55
## 1 0.2641 0.1386 0.1051 0.1343 0.0383 0.0324 0.0232 0.0027 0.0065 0.0159 0.0072
## V56 V57 V58 V59 V60 Class
## 1 0.0167 0.018 0.0084 0.009 0.0032 R
inTraining<- createDataPartition(y=Sonar$Class, p=0.8, list=FALSE)
training<- Sonar[inTraining,]
testing<- Sonar[-inTraining,]
nrow(training)
## [1] 167
nrow(testing)
## [1] 41
# 2) trainControl( )함수의 기능
rffit<-train(Class~.,data=training, method = "rf", preProcess=c("center","scale"), trControl=control)
rffit
## Random Forest
##
## 167 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 150, 150, 150, 151, 150, 152, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.8278431 0.6489170
## 31 0.8077451 0.6092366
## 60 0.7877206 0.5689897
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
fitControl<-trainControl(method='repeatedcv', number=5, repeats=3,
classProbs = TRUE, summaryFunction = twoClassSummary)
set.seed(825)
rmfit1 <- train(Class~., data=training, method='rf',
preProcess=c("center","scale"),
trControl=fitControl, verbose =FALSE, metric="ROC")
rmfit1
## Random Forest
##
## 167 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Cross-Validated (5 fold, repeated 3 times)
## Summary of sample sizes: 133, 133, 133, 135, 134, 134, ...
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.9478967 0.9403050 0.7144444
## 31 0.9185648 0.8760349 0.7063889
## 60 0.9138544 0.8647059 0.7022222
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
# 3)Parameter Tuning
set.seed(825)
customGrid <- expand.grid(mtry = 1:10)
rffit2 <- train(Class ~ ., data = training, method = "rf", trControl =
fitControl,tuneGrid = customGrid, metric = "ROC")
rffit2
## Random Forest
##
## 167 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 3 times)
## Summary of sample sizes: 133, 133, 133, 135, 134, 134, ...
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 1 0.9452011 0.9289760 0.7150000
## 2 0.9497967 0.9328976 0.7355556
## 3 0.9494862 0.9287582 0.7138889
## 4 0.9396464 0.9252723 0.7222222
## 5 0.9431350 0.9137255 0.7236111
## 6 0.9359799 0.8910675 0.7269444
## 7 0.9375822 0.9174292 0.7180556
## 8 0.9350050 0.8989107 0.7269444
## 9 0.9381913 0.8989107 0.7311111
## 10 0.9316971 0.8910675 0.7269444
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
customGrid1 <- expand.grid(mtry = 2)
rffit3 <- train(Class ~ ., data = training, method = "rf", trControl =
fitControl,tuneGrid = customGrid1, metric = "ROC")
rffit3
## Random Forest
##
## 167 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 3 times)
## Summary of sample sizes: 133, 134, 133, 134, 134, 133, ...
## Resampling results:
##
## ROC Sens Spec
## 0.9456323 0.9291939 0.7227778
##
## Tuning parameter 'mtry' was held constant at a value of 2
set.seed(825)
rmfit4 <- train(Class~., data=training, method='rf',
preProcess=c("center","scale"),
trControl=fitControl, tuneGrid = customGrid1, verbose =FALSE, metric="ROC")
rmfit4
## Random Forest
##
## 167 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Cross-Validated (5 fold, repeated 3 times)
## Summary of sample sizes: 133, 133, 133, 135, 134, 134, ...
## Resampling results:
##
## ROC Sens Spec
## 0.9463621 0.9176471 0.7141667
##
## Tuning parameter 'mtry' was held constant at a value of 2
# 4) Predict on test set
predict(rmfit4, newdata = testing)
## [1] M R R M M R M R R R R R R R R R M R M M M M M M M M M R R M R M M M M M M M
## [39] M M M
## Levels: M R
predict(rmfit4, newdata = testing, type="prob")
## M R
## 8 0.652 0.348
## 14 0.462 0.538
## 15 0.456 0.544
## 17 0.594 0.406
## 18 0.508 0.492
## 19 0.164 0.836
## 22 0.590 0.410
## 27 0.452 0.548
## 40 0.260 0.740
## 41 0.336 0.664
## 44 0.454 0.546
## 49 0.348 0.652
## 65 0.198 0.802
## 73 0.132 0.868
## 74 0.212 0.788
## 77 0.328 0.672
## 82 0.662 0.338
## 84 0.496 0.504
## 89 0.524 0.476
## 101 0.614 0.386
## 110 0.520 0.480
## 111 0.582 0.418
## 121 0.652 0.348
## 131 0.692 0.308
## 134 0.724 0.276
## 136 0.660 0.340
## 141 0.778 0.222
## 153 0.490 0.510
## 156 0.434 0.566
## 161 0.844 0.156
## 168 0.278 0.722
## 170 0.574 0.426
## 175 0.750 0.250
## 181 0.904 0.096
## 185 0.868 0.132
## 187 0.796 0.204
## 194 0.710 0.290
## 197 0.864 0.136
## 199 0.912 0.088
## 205 0.694 0.306
## 208 0.544 0.456
# 5) 모형평가 Confusion Matrix
library(dplyr)
predict(rmfit4, newdata = testing) %>% confusionMatrix(testing$Class)
## Confusion Matrix and Statistics
##
## Reference
## Prediction M R
## M 19 6
## R 3 13
##
## Accuracy : 0.7805
## 95% CI : (0.6239, 0.8944)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : 0.001099
##
## Kappa : 0.5538
##
## Mcnemar's Test P-Value : 0.504985
##
## Sensitivity : 0.8636
## Specificity : 0.6842
## Pos Pred Value : 0.7600
## Neg Pred Value : 0.8125
## Prevalence : 0.5366
## Detection Rate : 0.4634
## Detection Prevalence : 0.6098
## Balanced Accuracy : 0.7739
##
## 'Positive' Class : M
##
## ChAT GPT
# 필요한 패키지 로드
library(caret)
library(mlbench)
# Sonar 데이터 로드
data(Sonar)
# 데이터 분할을 위한 설정
set.seed(998) # 결과의 재현성을 위해
inTraining <- createDataPartition(y=Sonar$Class, p=0.8, list=FALSE)
training <- Sonar[inTraining, ]
testing <- Sonar[-inTraining, ]
# trainControl 설정
fitControl <- trainControl(method="repeatedcv", number=10, repeats=3,
classProbs=TRUE, # 혼동 행렬을 위해 클래스 확률 계산 필요
savePredictions=TRUE)
# 모델 훈련을 위한 그리드 서치 설정
grid <- expand.grid(mtry=2:4) # mtry 값에 대한 간단한 그리드
# 랜덤 포레스트 모델 훈련
set.seed(998) # 결과의 재현성을 위해
rffit <- train(Class~., data=training, method="rf",
tuneGrid=grid,
trControl=fitControl,
preProcess=c("center", "scale"), # 정규화 적용
metric="ROC") # MSE 대신 RMSE 사용, caret에서 직접적으로 MSE 지원 안 함
## Warning in train.default(x, y, weights = w, ...): The metric "ROC" was not in
## the result set. Accuracy will be used instead.
# 결과 출력
print(rffit)
## Random Forest
##
## 167 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 150, 150, 150, 150, 150, 150, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.8363317 0.6653155
## 3 0.8480964 0.6897012
## 4 0.8343709 0.6622716
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 3.
# 예측 및 성능 평가
predictions <- predict(rffit, testing)
confusionMatrix(predictions, testing$Class) # 혼동 행렬
## Confusion Matrix and Statistics
##
## Reference
## Prediction M R
## M 18 5
## R 4 14
##
## Accuracy : 0.7805
## 95% CI : (0.6239, 0.8944)
## No Information Rate : 0.5366
## P-Value [Acc > NIR] : 0.001099
##
## Kappa : 0.557
##
## Mcnemar's Test P-Value : 1.000000
##
## Sensitivity : 0.8182
## Specificity : 0.7368
## Pos Pred Value : 0.7826
## Neg Pred Value : 0.7778
## Prevalence : 0.5366
## Detection Rate : 0.4390
## Detection Prevalence : 0.5610
## Balanced Accuracy : 0.7775
##
## 'Positive' Class : M
##
# Rsquare 계산 (회귀 문제가 아니므로 R²는 직접 계산할 수 없음, 분류 문제에는 일반적으로 사용되지 않음)
## 실전 모형 구축축
library(dplyr)
library(ggplot2)
library(readxl)
library(caret)
library(caTools)
library(glmnet)
## Loading required package: Matrix
## Loaded glmnet 4.1-8
df<-read.csv("diagnosis.csv")
glimpse(df)
## Rows: 569
## Columns: 32
## $ id <int> 842302, 842517, 84300903, 84348301, 84358402, …
## $ diagnosis <chr> "M", "M", "M", "M", "M", "M", "M", "M", "M", "…
## $ radius_mean <dbl> 17.990, 20.570, 19.690, 11.420, 20.290, 12.450…
## $ texture_mean <dbl> 10.38, 17.77, 21.25, 20.38, 14.34, 15.70, 19.9…
## $ perimeter_mean <dbl> 122.80, 132.90, 130.00, 77.58, 135.10, 82.57, …
## $ area_mean <dbl> 1001.0, 1326.0, 1203.0, 386.1, 1297.0, 477.1, …
## $ smoothness_mean <dbl> 0.11840, 0.08474, 0.10960, 0.14250, 0.10030, 0…
## $ compactness_mean <dbl> 0.27760, 0.07864, 0.15990, 0.28390, 0.13280, 0…
## $ concavity_mean <dbl> 0.30010, 0.08690, 0.19740, 0.24140, 0.19800, 0…
## $ concave.points_mean <dbl> 0.14710, 0.07017, 0.12790, 0.10520, 0.10430, 0…
## $ symmetry_mean <dbl> 0.2419, 0.1812, 0.2069, 0.2597, 0.1809, 0.2087…
## $ fractal_dimension_mean <dbl> 0.07871, 0.05667, 0.05999, 0.09744, 0.05883, 0…
## $ radius_se <dbl> 1.0950, 0.5435, 0.7456, 0.4956, 0.7572, 0.3345…
## $ texture_se <dbl> 0.9053, 0.7339, 0.7869, 1.1560, 0.7813, 0.8902…
## $ perimeter_se <dbl> 8.589, 3.398, 4.585, 3.445, 5.438, 2.217, 3.18…
## $ area_se <dbl> 153.40, 74.08, 94.03, 27.23, 94.44, 27.19, 53.…
## $ smoothness_se <dbl> 0.006399, 0.005225, 0.006150, 0.009110, 0.0114…
## $ compactness_se <dbl> 0.049040, 0.013080, 0.040060, 0.074580, 0.0246…
## $ concavity_se <dbl> 0.05373, 0.01860, 0.03832, 0.05661, 0.05688, 0…
## $ concave.points_se <dbl> 0.015870, 0.013400, 0.020580, 0.018670, 0.0188…
## $ symmetry_se <dbl> 0.03003, 0.01389, 0.02250, 0.05963, 0.01756, 0…
## $ fractal_dimension_se <dbl> 0.006193, 0.003532, 0.004571, 0.009208, 0.0051…
## $ radius_worst <dbl> 25.38, 24.99, 23.57, 14.91, 22.54, 15.47, 22.8…
## $ texture_worst <dbl> 17.33, 23.41, 25.53, 26.50, 16.67, 23.75, 27.6…
## $ perimeter_worst <dbl> 184.60, 158.80, 152.50, 98.87, 152.20, 103.40,…
## $ area_worst <dbl> 2019.0, 1956.0, 1709.0, 567.7, 1575.0, 741.6, …
## $ smoothness_worst <dbl> 0.1622, 0.1238, 0.1444, 0.2098, 0.1374, 0.1791…
## $ compactness_worst <dbl> 0.6656, 0.1866, 0.4245, 0.8663, 0.2050, 0.5249…
## $ concavity_worst <dbl> 0.71190, 0.24160, 0.45040, 0.68690, 0.40000, 0…
## $ concave.points_worst <dbl> 0.26540, 0.18600, 0.24300, 0.25750, 0.16250, 0…
## $ symmetry_worst <dbl> 0.4601, 0.2750, 0.3613, 0.6638, 0.2364, 0.3985…
## $ fractal_dimension_worst <dbl> 0.11890, 0.08902, 0.08758, 0.17300, 0.07678, 0…
df1<-df %>% select(-id)
df1$diagnosis <- as.factor(df1$diagnosis)
glimpse(df1)
## Rows: 569
## Columns: 31
## $ diagnosis <fct> M, M, M, M, M, M, M, M, M, M, M, M, M, M, M, M…
## $ radius_mean <dbl> 17.990, 20.570, 19.690, 11.420, 20.290, 12.450…
## $ texture_mean <dbl> 10.38, 17.77, 21.25, 20.38, 14.34, 15.70, 19.9…
## $ perimeter_mean <dbl> 122.80, 132.90, 130.00, 77.58, 135.10, 82.57, …
## $ area_mean <dbl> 1001.0, 1326.0, 1203.0, 386.1, 1297.0, 477.1, …
## $ smoothness_mean <dbl> 0.11840, 0.08474, 0.10960, 0.14250, 0.10030, 0…
## $ compactness_mean <dbl> 0.27760, 0.07864, 0.15990, 0.28390, 0.13280, 0…
## $ concavity_mean <dbl> 0.30010, 0.08690, 0.19740, 0.24140, 0.19800, 0…
## $ concave.points_mean <dbl> 0.14710, 0.07017, 0.12790, 0.10520, 0.10430, 0…
## $ symmetry_mean <dbl> 0.2419, 0.1812, 0.2069, 0.2597, 0.1809, 0.2087…
## $ fractal_dimension_mean <dbl> 0.07871, 0.05667, 0.05999, 0.09744, 0.05883, 0…
## $ radius_se <dbl> 1.0950, 0.5435, 0.7456, 0.4956, 0.7572, 0.3345…
## $ texture_se <dbl> 0.9053, 0.7339, 0.7869, 1.1560, 0.7813, 0.8902…
## $ perimeter_se <dbl> 8.589, 3.398, 4.585, 3.445, 5.438, 2.217, 3.18…
## $ area_se <dbl> 153.40, 74.08, 94.03, 27.23, 94.44, 27.19, 53.…
## $ smoothness_se <dbl> 0.006399, 0.005225, 0.006150, 0.009110, 0.0114…
## $ compactness_se <dbl> 0.049040, 0.013080, 0.040060, 0.074580, 0.0246…
## $ concavity_se <dbl> 0.05373, 0.01860, 0.03832, 0.05661, 0.05688, 0…
## $ concave.points_se <dbl> 0.015870, 0.013400, 0.020580, 0.018670, 0.0188…
## $ symmetry_se <dbl> 0.03003, 0.01389, 0.02250, 0.05963, 0.01756, 0…
## $ fractal_dimension_se <dbl> 0.006193, 0.003532, 0.004571, 0.009208, 0.0051…
## $ radius_worst <dbl> 25.38, 24.99, 23.57, 14.91, 22.54, 15.47, 22.8…
## $ texture_worst <dbl> 17.33, 23.41, 25.53, 26.50, 16.67, 23.75, 27.6…
## $ perimeter_worst <dbl> 184.60, 158.80, 152.50, 98.87, 152.20, 103.40,…
## $ area_worst <dbl> 2019.0, 1956.0, 1709.0, 567.7, 1575.0, 741.6, …
## $ smoothness_worst <dbl> 0.1622, 0.1238, 0.1444, 0.2098, 0.1374, 0.1791…
## $ compactness_worst <dbl> 0.6656, 0.1866, 0.4245, 0.8663, 0.2050, 0.5249…
## $ concavity_worst <dbl> 0.71190, 0.24160, 0.45040, 0.68690, 0.40000, 0…
## $ concave.points_worst <dbl> 0.26540, 0.18600, 0.24300, 0.25750, 0.16250, 0…
## $ symmetry_worst <dbl> 0.4601, 0.2750, 0.3613, 0.6638, 0.2364, 0.3985…
## $ fractal_dimension_worst <dbl> 0.11890, 0.08902, 0.08758, 0.17300, 0.07678, 0…
prop.table(table(df1$diagnosis))
##
## B M
## 0.6274165 0.3725835
any(is.na(df1)) # null 값이 없는지지
## [1] FALSE
## 데이터분할
set.seed(3)
inTraining<-createDataPartition(y=df1$diagnosis, p=0.8, list=FALSE)
training <- df1[inTraining,]
testing <- df1[-inTraining,]
##trainControl( )함수을 조율한다.
my_trainControl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 3,
## Estimate class probabilities
classProbs = TRUE,
## Evaluate performance using
## the following function
summaryFunction = twoClassSummary)
## glmnet model 학습한다
glmnetfit <- train(diagnosis~.,data=training,
method="glmnet",
tuneLength = 20,
trControl = my_trainControl,
verbose = FALSE,
metric = "ROC")
glmnetfit %>% head
## $method
## [1] "glmnet"
##
## $modelInfo
## $modelInfo$label
## [1] "glmnet"
##
## $modelInfo$library
## [1] "glmnet" "Matrix"
##
## $modelInfo$type
## [1] "Regression" "Classification"
##
## $modelInfo$parameters
## parameter class label
## 1 alpha numeric Mixing Percentage
## 2 lambda numeric Regularization Parameter
##
## $modelInfo$grid
## function (x, y, len = NULL, search = "grid")
## {
## if (search == "grid") {
## numLev <- if (is.character(y) | is.factor(y))
## length(levels(y))
## else NA
## if (!is.na(numLev)) {
## fam <- ifelse(numLev > 2, "multinomial", "binomial")
## }
## else fam <- "gaussian"
## if (!is.matrix(x) && !inherits(x, "sparseMatrix"))
## x <- Matrix::as.matrix(x)
## init <- glmnet::glmnet(x, y, family = fam, nlambda = len +
## 2, alpha = 0.5)
## lambda <- unique(init$lambda)
## lambda <- lambda[-c(1, length(lambda))]
## lambda <- lambda[1:min(length(lambda), len)]
## out <- expand.grid(alpha = seq(0.1, 1, length = len),
## lambda = lambda)
## }
## else {
## out <- data.frame(alpha = runif(len, min = 0, 1), lambda = 2^runif(len,
## min = -10, 3))
## }
## out
## }
## <bytecode: 0x0000021ea2a7b9e8>
##
## $modelInfo$loop
## function (grid)
## {
## alph <- unique(grid$alpha)
## loop <- data.frame(alpha = alph)
## loop$lambda <- NA
## submodels <- vector(mode = "list", length = length(alph))
## for (i in seq(along = alph)) {
## np <- grid[grid$alpha == alph[i], "lambda"]
## loop$lambda[loop$alpha == alph[i]] <- np[which.max(np)]
## submodels[[i]] <- data.frame(lambda = np[-which.max(np)])
## }
## list(loop = loop, submodels = submodels)
## }
## <bytecode: 0x0000021ea35fecb0>
##
## $modelInfo$fit
## function (x, y, wts, param, lev, last, classProbs, ...)
## {
## numLev <- if (is.character(y) | is.factor(y))
## length(levels(y))
## else NA
## theDots <- list(...)
## if (all(names(theDots) != "family")) {
## if (!is.na(numLev)) {
## fam <- ifelse(numLev > 2, "multinomial", "binomial")
## }
## else fam <- "gaussian"
## theDots$family <- fam
## }
## if (!is.null(wts))
## theDots$weights <- wts
## if (!is.matrix(x) && !inherits(x, "sparseMatrix"))
## x <- Matrix::as.matrix(x)
## modelArgs <- c(list(x = x, y = y, alpha = param$alpha), theDots)
## out <- do.call(glmnet::glmnet, modelArgs)
## if (!is.na(param$lambda[1]))
## out$lambdaOpt <- param$lambda[1]
## out
## }
## <bytecode: 0x0000021ea0b79580>
##
## $modelInfo$predict
## function (modelFit, newdata, submodels = NULL)
## {
## if (!is.matrix(newdata) && !inherits(newdata, "sparseMatrix"))
## newdata <- Matrix::as.matrix(newdata)
## if (length(modelFit$obsLevels) < 2) {
## out <- predict(modelFit, newdata, s = modelFit$lambdaOpt,
## type = "response")
## }
## else {
## out <- predict(modelFit, newdata, s = modelFit$lambdaOpt,
## type = "class")
## }
## if (is.matrix(out))
## out <- out[, 1]
## if (!is.null(submodels)) {
## if (length(modelFit$obsLevels) < 2) {
## tmp <- as.list(as.data.frame(predict(modelFit, newdata,
## s = submodels$lambda), stringsAsFactors = TRUE))
## }
## else {
## tmp <- predict(modelFit, newdata, s = submodels$lambda,
## type = "class")
## tmp <- if (is.matrix(tmp))
## as.data.frame(tmp, stringsAsFactors = FALSE)
## else as.character(tmp)
## tmp <- as.list(tmp)
## }
## out <- c(list(out), tmp)
## }
## out
## }
## <bytecode: 0x0000021ea3747ba8>
##
## $modelInfo$prob
## function (modelFit, newdata, submodels = NULL)
## {
## obsLevels <- if ("classnames" %in% names(modelFit))
## modelFit$classnames
## else NULL
## if (!is.matrix(newdata) && !inherits(newdata, "sparseMatrix"))
## newdata <- Matrix::as.matrix(newdata)
## probs <- predict(modelFit, newdata, s = modelFit$lambdaOpt,
## type = "response")
## if (length(obsLevels) == 2) {
## probs <- as.vector(probs)
## probs <- as.data.frame(cbind(1 - probs, probs), stringsAsFactors = FALSE)
## colnames(probs) <- modelFit$obsLevels
## }
## else {
## probs <- as.data.frame(probs[, , 1, drop = FALSE], stringsAsFactors = FALSE)
## names(probs) <- modelFit$obsLevels
## }
## if (!is.null(submodels)) {
## tmp <- predict(modelFit, newdata, s = submodels$lambda,
## type = "response")
## if (length(obsLevels) == 2) {
## tmp <- as.list(as.data.frame(tmp, stringsAsFactors = TRUE))
## tmp <- lapply(tmp, function(x, lev) {
## x <- as.vector(x)
## tmp <- data.frame(1 - x, x)
## names(tmp) <- lev
## tmp
## }, lev = modelFit$obsLevels)
## }
## else tmp <- apply(tmp, 3, function(x) data.frame(x))
## probs <- if (is.list(tmp))
## c(list(probs), tmp)
## else list(probs, tmp)
## }
## probs
## }
## <bytecode: 0x0000021e99358df0>
##
## $modelInfo$predictors
## function (x, lambda = NULL, ...)
## {
## if (is.null(lambda)) {
## if (length(lambda) > 1)
## stop("Only one value of lambda is allowed right now")
## if (!is.null(x$lambdaOpt)) {
## lambda <- x$lambdaOpt
## }
## else stop("must supply a value of lambda")
## }
## allVar <- if (is.list(x$beta))
## rownames(x$beta[[1]])
## else rownames(x$beta)
## out <- unlist(predict(x, s = lambda, type = "nonzero"))
## out <- unique(out)
## if (length(out) > 0) {
## out <- out[!is.na(out)]
## out <- allVar[out]
## }
## out
## }
##
## $modelInfo$varImp
## function (object, lambda = NULL, ...)
## {
## if (is.null(lambda)) {
## if (length(lambda) > 1)
## stop("Only one value of lambda is allowed right now")
## if (!is.null(object$lambdaOpt)) {
## lambda <- object$lambdaOpt
## }
## else stop("must supply a value of lambda")
## }
## beta <- predict(object, s = lambda, type = "coef")
## if (is.list(beta)) {
## out <- do.call("cbind", lapply(beta, function(x) x[,
## 1]))
## out <- as.data.frame(out, stringsAsFactors = TRUE)
## }
## else out <- data.frame(Overall = beta[, 1])
## out <- abs(out[rownames(out) != "(Intercept)", , drop = FALSE])
## out
## }
##
## $modelInfo$levels
## function (x)
## if (any(names(x) == "obsLevels")) x$obsLevels else NULL
##
## $modelInfo$tags
## [1] "Generalized Linear Model" "Implicit Feature Selection"
## [3] "L1 Regularization" "L2 Regularization"
## [5] "Linear Classifier" "Linear Regression"
##
## $modelInfo$sort
## function (x)
## x[order(-x$lambda, x$alpha), ]
##
## $modelInfo$trim
## function (x)
## {
## x$call <- NULL
## x$df <- NULL
## x$dev.ratio <- NULL
## x
## }
##
##
## $modelType
## [1] "Classification"
##
## $results
## alpha lambda ROC Sens Spec ROCSD
## 1 0.1000000 0.0001186984 0.9903072 0.9837028 0.95098039 0.01569451
## 2 0.1000000 0.0001840437 0.9903072 0.9837028 0.95098039 0.01569451
## 3 0.1000000 0.0002853627 0.9903072 0.9837028 0.95098039 0.01569451
## 4 0.1000000 0.0004424594 0.9905776 0.9848933 0.95490196 0.01568409
## 5 0.1000000 0.0006860403 0.9909954 0.9848933 0.95490196 0.01545707
## 6 0.1000000 0.0010637162 0.9916160 0.9872332 0.95490196 0.01492288
## 7 0.1000000 0.0016493088 0.9920313 0.9907225 0.95490196 0.01399185
## 8 0.1000000 0.0025572793 0.9924418 0.9942118 0.95686275 0.01328972
## 9 0.1000000 0.0039651020 0.9932797 0.9954023 0.95490196 0.01258019
## 10 0.1000000 0.0061479532 0.9936226 0.9988506 0.95490196 0.01216913
## 11 0.1000000 0.0095324984 0.9936951 1.0000000 0.95686275 0.01241980
## 12 0.1000000 0.0147802892 0.9937651 1.0000000 0.95686275 0.01246242
## 13 0.1000000 0.0229170716 0.9939776 1.0000000 0.95098039 0.01224375
## 14 0.1000000 0.0355332812 0.9936371 1.0000000 0.92156863 0.01200158
## 15 0.1000000 0.0550949132 0.9932942 1.0000000 0.91764706 0.01196972
## 16 0.1000000 0.0854255324 0.9930213 1.0000000 0.90392157 0.01178451
## 17 0.1000000 0.1324536359 0.9925432 0.9988506 0.87254902 0.01186231
## 18 0.1000000 0.2053714526 0.9919323 0.9965517 0.86862745 0.01184048
## 19 0.1000000 0.3184316780 0.9911040 0.9965517 0.84901961 0.01182841
## 20 0.1000000 0.4937333415 0.9902830 0.9965517 0.81568627 0.01161423
## 21 0.1473684 0.0001186984 0.9893437 0.9837028 0.94705882 0.01676998
## 22 0.1473684 0.0001840437 0.9893437 0.9837028 0.94705882 0.01676998
## 23 0.1473684 0.0002853627 0.9894813 0.9837028 0.94705882 0.01677066
## 24 0.1473684 0.0004424594 0.9905776 0.9848933 0.95294118 0.01568409
## 25 0.1473684 0.0006860403 0.9909954 0.9848933 0.95490196 0.01545707
## 26 0.1473684 0.0010637162 0.9916160 0.9872332 0.95490196 0.01509304
## 27 0.1473684 0.0016493088 0.9920989 0.9919130 0.95490196 0.01393507
## 28 0.1473684 0.0025572793 0.9925819 0.9942118 0.95686275 0.01343366
## 29 0.1473684 0.0039651020 0.9932097 0.9965517 0.95490196 0.01260780
## 30 0.1473684 0.0061479532 0.9935574 0.9988506 0.95686275 0.01236885
## 31 0.1473684 0.0095324984 0.9937627 1.0000000 0.95686275 0.01244937
## 32 0.1473684 0.0147802892 0.9937651 1.0000000 0.95686275 0.01246242
## 33 0.1473684 0.0229170716 0.9938375 1.0000000 0.94117647 0.01227106
## 34 0.1473684 0.0355332812 0.9938424 1.0000000 0.92156863 0.01202151
## 35 0.1473684 0.0550949132 0.9932266 1.0000000 0.91568627 0.01199550
## 36 0.1473684 0.0854255324 0.9928837 1.0000000 0.90000000 0.01195403
## 37 0.1473684 0.1324536359 0.9925456 0.9988506 0.87058824 0.01192309
## 38 0.1473684 0.2053714526 0.9920651 0.9965517 0.86470588 0.01191565
## 39 0.1473684 0.3184316780 0.9911016 0.9965517 0.83921569 0.01205723
## 40 0.1473684 0.4937333415 0.9896672 0.9977011 0.78431373 0.01221459
## 41 0.1947368 0.0001186984 0.9889307 0.9837028 0.94705882 0.01701448
## 42 0.1947368 0.0001840437 0.9889307 0.9837028 0.94705882 0.01701448
## 43 0.1947368 0.0002853627 0.9894813 0.9837028 0.94705882 0.01677066
## 44 0.1947368 0.0004424594 0.9905100 0.9848933 0.95294118 0.01567358
## 45 0.1947368 0.0006860403 0.9909954 0.9848933 0.95490196 0.01545707
## 46 0.1947368 0.0010637162 0.9916160 0.9872332 0.95490196 0.01509304
## 47 0.1947368 0.0016493088 0.9920289 0.9919130 0.95490196 0.01430693
## 48 0.1947368 0.0025572793 0.9928571 0.9942118 0.95686275 0.01347905
## 49 0.1947368 0.0039651020 0.9932097 0.9953612 0.95490196 0.01278658
## 50 0.1947368 0.0061479532 0.9935574 0.9988506 0.95686275 0.01236885
## 51 0.1947368 0.0095324984 0.9938327 1.0000000 0.95686275 0.01245489
## 52 0.1947368 0.0147802892 0.9938351 1.0000000 0.95686275 0.01249232
## 53 0.1947368 0.0229170716 0.9937699 1.0000000 0.93921569 0.01227621
## 54 0.1947368 0.0355332812 0.9937047 1.0000000 0.92549020 0.01219732
## 55 0.1947368 0.0550949132 0.9932966 1.0000000 0.90980392 0.01197909
## 56 0.1947368 0.0854255324 0.9929586 1.0000000 0.89411765 0.01212607
## 57 0.1947368 0.1324536359 0.9924780 0.9988506 0.86666667 0.01234454
## 58 0.1947368 0.2053714526 0.9919951 0.9965517 0.86078431 0.01218212
## 59 0.1947368 0.3184316780 0.9904134 0.9965517 0.81960784 0.01215888
## 60 0.1947368 0.4937333415 0.9875278 0.9977011 0.72745098 0.01220912
## 61 0.2421053 0.0001186984 0.9885854 0.9837028 0.94509804 0.01691041
## 62 0.2421053 0.0001840437 0.9887931 0.9837028 0.94509804 0.01717836
## 63 0.2421053 0.0002853627 0.9894137 0.9837028 0.94705882 0.01674778
## 64 0.2421053 0.0004424594 0.9903072 0.9837028 0.95294118 0.01580262
## 65 0.2421053 0.0006860403 0.9909253 0.9837438 0.95490196 0.01551730
## 66 0.2421053 0.0010637162 0.9916135 0.9872742 0.95490196 0.01492666
## 67 0.2421053 0.0016493088 0.9919613 0.9919130 0.95490196 0.01463588
## 68 0.2421053 0.0025572793 0.9928571 0.9942118 0.95686275 0.01343458
## 69 0.2421053 0.0039651020 0.9933449 0.9953612 0.95882353 0.01279355
## 70 0.2421053 0.0061479532 0.9935598 0.9988506 0.95686275 0.01253813
## 71 0.2421053 0.0095324984 0.9938351 1.0000000 0.95686275 0.01263583
## 72 0.2421053 0.0147802892 0.9939027 1.0000000 0.95686275 0.01252094
## 73 0.2421053 0.0229170716 0.9937023 1.0000000 0.94117647 0.01244165
## 74 0.2421053 0.0355332812 0.9937047 1.0000000 0.92549020 0.01219732
## 75 0.2421053 0.0550949132 0.9929561 1.0000000 0.90588235 0.01256695
## 76 0.2421053 0.0854255324 0.9930262 1.0000000 0.89215686 0.01236005
## 77 0.2421053 0.1324536359 0.9923428 1.0000000 0.86666667 0.01240861
## 78 0.2421053 0.2053714526 0.9915145 0.9965517 0.85294118 0.01225529
## 79 0.2421053 0.3184316780 0.9895876 0.9965517 0.79803922 0.01203525
## 80 0.2421053 0.4937333415 0.9858761 1.0000000 0.70392157 0.01215269
## 81 0.2894737 0.0001186984 0.9880349 0.9825534 0.94509804 0.01688674
## 82 0.2894737 0.0001840437 0.9885154 0.9837028 0.94509804 0.01718144
## 83 0.2894737 0.0002853627 0.9893437 0.9837028 0.94705882 0.01692356
## 84 0.2894737 0.0004424594 0.9902371 0.9837028 0.95098039 0.01585905
## 85 0.2894737 0.0006860403 0.9909253 0.9848933 0.95294118 0.01551730
## 86 0.2894737 0.0010637162 0.9915459 0.9884236 0.95490196 0.01510006
## 87 0.2894737 0.0016493088 0.9920289 0.9919130 0.95686275 0.01464021
## 88 0.2894737 0.0025572793 0.9927895 0.9942118 0.95686275 0.01360210
## 89 0.2894737 0.0039651020 0.9936202 0.9953612 0.95882353 0.01276418
## 90 0.2894737 0.0061479532 0.9936975 0.9977011 0.95686275 0.01255266
## 91 0.2894737 0.0095324984 0.9937651 1.0000000 0.95686275 0.01265447
## 92 0.2894737 0.0147802892 0.9938351 1.0000000 0.95098039 0.01268392
## 93 0.2894737 0.0229170716 0.9936323 1.0000000 0.93921569 0.01264170
## 94 0.2894737 0.0355332812 0.9935671 1.0000000 0.92549020 0.01258693
## 95 0.2894737 0.0550949132 0.9930962 1.0000000 0.90588235 0.01257469
## 96 0.2894737 0.0854255324 0.9931638 1.0000000 0.88823529 0.01232267
## 97 0.2894737 0.1324536359 0.9925481 1.0000000 0.86470588 0.01227702
## 98 0.2894737 0.2053714526 0.9911016 0.9965517 0.84117647 0.01224721
## 99 0.2894737 0.3184316780 0.9878031 0.9965517 0.77647059 0.01249991
## 100 0.2894737 0.4937333415 0.9847774 1.0000000 0.68627451 0.01276059
## 101 0.3368421 0.0001186984 0.9876944 0.9814039 0.94313725 0.01703207
## 102 0.3368421 0.0001840437 0.9884478 0.9825534 0.94509804 0.01713862
## 103 0.3368421 0.0002853627 0.9892761 0.9837028 0.94705882 0.01690032
## 104 0.3368421 0.0004424594 0.9901019 0.9837028 0.95098039 0.01609267
## 105 0.3368421 0.0006860403 0.9908553 0.9848933 0.95294118 0.01565560
## 106 0.3368421 0.0010637162 0.9914759 0.9884236 0.95490196 0.01527501
## 107 0.3368421 0.0016493088 0.9921665 0.9919130 0.95686275 0.01447813
## 108 0.3368421 0.0025572793 0.9927895 0.9942118 0.95686275 0.01379885
## 109 0.3368421 0.0039651020 0.9936178 0.9953612 0.95882353 0.01297699
## 110 0.3368421 0.0061479532 0.9937651 0.9965107 0.95686275 0.01252540
## 111 0.3368421 0.0095324984 0.9938351 0.9988506 0.95686275 0.01268392
## 112 0.3368421 0.0147802892 0.9939027 1.0000000 0.94901961 0.01267858
## 113 0.3368421 0.0229170716 0.9936999 1.0000000 0.93921569 0.01265990
## 114 0.3368421 0.0355332812 0.9934319 1.0000000 0.92352941 0.01259359
## 115 0.3368421 0.0550949132 0.9932314 1.0000000 0.90196078 0.01249499
## 116 0.3368421 0.0854255324 0.9931638 1.0000000 0.88627451 0.01232267
## 117 0.3368421 0.1324536359 0.9924756 1.0000000 0.86470588 0.01207703
## 118 0.3368421 0.2053714526 0.9904810 0.9965517 0.83137255 0.01223934
## 119 0.3368421 0.3184316780 0.9865546 0.9965517 0.75098039 0.01228401
## 120 0.3368421 0.4937333415 0.9851106 1.0000000 0.65686275 0.01258524
## 121 0.3842105 0.0001186984 0.9875592 0.9814039 0.94313725 0.01732771
## 122 0.3842105 0.0001840437 0.9884478 0.9825534 0.94509804 0.01713862
## 123 0.3842105 0.0002853627 0.9891360 0.9837028 0.94705882 0.01710373
## 124 0.3842105 0.0004424594 0.9899667 0.9837028 0.95098039 0.01626956
## 125 0.3842105 0.0006860403 0.9908553 0.9848933 0.95294118 0.01565560
## 126 0.3842105 0.0010637162 0.9914059 0.9884236 0.95294118 0.01531905
## 127 0.3842105 0.0016493088 0.9921665 0.9919130 0.95686275 0.01447813
## 128 0.3842105 0.0025572793 0.9927895 0.9942118 0.95882353 0.01379885
## 129 0.3842105 0.0039651020 0.9936178 0.9953612 0.95882353 0.01297699
## 130 0.3842105 0.0061479532 0.9937651 0.9965107 0.95686275 0.01273028
## 131 0.3842105 0.0095324984 0.9938351 0.9988506 0.95686275 0.01263583
## 132 0.3842105 0.0147802892 0.9939027 1.0000000 0.94705882 0.01267858
## 133 0.3842105 0.0229170716 0.9936347 1.0000000 0.92941176 0.01278438
## 134 0.3842105 0.0355332812 0.9935695 1.0000000 0.92352941 0.01256195
## 135 0.3842105 0.0550949132 0.9932990 1.0000000 0.90196078 0.01252697
## 136 0.3842105 0.0854255324 0.9931638 1.0000000 0.88039216 0.01232267
## 137 0.3842105 0.1324536359 0.9923380 1.0000000 0.85882353 0.01208533
## 138 0.3842105 0.2053714526 0.9895199 0.9965517 0.81176471 0.01210817
## 139 0.3842105 0.3184316780 0.9855984 1.0000000 0.72941176 0.01254798
## 140 0.3842105 0.4937333415 0.9849705 1.0000000 0.60588235 0.01255646
## 141 0.4315789 0.0001186984 0.9874891 0.9802545 0.94313725 0.01740275
## 142 0.4315789 0.0001840437 0.9883102 0.9825534 0.94509804 0.01735512
## 143 0.4315789 0.0002853627 0.9889984 0.9837028 0.94705882 0.01715290
## 144 0.4315789 0.0004424594 0.9897566 0.9837028 0.95098039 0.01664887
## 145 0.4315789 0.0006860403 0.9909253 0.9848933 0.95294118 0.01564428
## 146 0.4315789 0.0010637162 0.9914059 0.9896141 0.95294118 0.01531905
## 147 0.4315789 0.0016493088 0.9920289 0.9919130 0.95686275 0.01467734
## 148 0.4315789 0.0025572793 0.9928596 0.9942118 0.95882353 0.01376479
## 149 0.4315789 0.0039651020 0.9937554 0.9953612 0.95882353 0.01294345
## 150 0.4315789 0.0061479532 0.9937651 0.9965107 0.95686275 0.01273028
## 151 0.4315789 0.0095324984 0.9939728 0.9977011 0.95490196 0.01265918
## 152 0.4315789 0.0147802892 0.9938351 0.9988506 0.94509804 0.01265032
## 153 0.4315789 0.0229170716 0.9936347 1.0000000 0.92745098 0.01278438
## 154 0.4315789 0.0355332812 0.9937071 1.0000000 0.91960784 0.01255212
## 155 0.4315789 0.0550949132 0.9935043 1.0000000 0.90000000 0.01234665
## 156 0.4315789 0.0854255324 0.9931638 1.0000000 0.87843137 0.01235966
## 157 0.4315789 0.1324536359 0.9918574 1.0000000 0.84705882 0.01207949
## 158 0.4315789 0.2053714526 0.9886241 0.9965517 0.79607843 0.01184569
## 159 0.4315789 0.3184316780 0.9850454 1.0000000 0.70392157 0.01262134
## 160 0.4315789 0.4937333415 0.9854487 1.0000000 0.54117647 0.01206777
## 161 0.4789474 0.0001186984 0.9874191 0.9802545 0.94313725 0.01740736
## 162 0.4789474 0.0001840437 0.9881073 0.9814039 0.94509804 0.01739644
## 163 0.4789474 0.0002853627 0.9889307 0.9837028 0.94705882 0.01730163
## 164 0.4789474 0.0004424594 0.9897566 0.9837028 0.95098039 0.01664887
## 165 0.4789474 0.0006860403 0.9906501 0.9848933 0.95294118 0.01592147
## 166 0.4789474 0.0010637162 0.9912682 0.9896141 0.95294118 0.01546382
## 167 0.4789474 0.0016493088 0.9919613 0.9919130 0.95490196 0.01471165
## 168 0.4789474 0.0025572793 0.9929948 0.9942118 0.95882353 0.01401984
## 169 0.4789474 0.0039651020 0.9937554 0.9953612 0.95882353 0.01294345
## 170 0.4789474 0.0061479532 0.9939027 0.9953612 0.95686275 0.01277566
## 171 0.4789474 0.0095324984 0.9938375 0.9977011 0.95294118 0.01280395
## 172 0.4789474 0.0147802892 0.9937675 0.9988506 0.94509804 0.01282236
## 173 0.4789474 0.0229170716 0.9937071 0.9988506 0.92745098 0.01273169
## 174 0.4789474 0.0355332812 0.9938448 1.0000000 0.91960784 0.01250674
## 175 0.4789474 0.0550949132 0.9935043 1.0000000 0.89803922 0.01232365
## 176 0.4789474 0.0854255324 0.9930238 1.0000000 0.87254902 0.01223888
## 177 0.4789474 0.1324536359 0.9914445 0.9988506 0.83921569 0.01201105
## 178 0.4789474 0.2053714526 0.9878610 0.9965517 0.78627451 0.01172278
## 179 0.4789474 0.3184316780 0.9857239 1.0000000 0.69019608 0.01216235
## 180 0.4789474 0.4937333415 0.9864146 1.0000000 0.47058824 0.01123662
## 181 0.5263158 0.0001186984 0.9872114 0.9790640 0.94313725 0.01740293
## 182 0.5263158 0.0001840437 0.9879673 0.9814039 0.94509804 0.01743674
## 183 0.5263158 0.0002853627 0.9887955 0.9837028 0.94705882 0.01725252
## 184 0.5263158 0.0004424594 0.9896190 0.9837028 0.94901961 0.01687368
## 185 0.5263158 0.0006860403 0.9905148 0.9848933 0.95294118 0.01617532
## 186 0.5263158 0.0010637162 0.9911982 0.9896141 0.95294118 0.01551616
## 187 0.5263158 0.0016493088 0.9918912 0.9919130 0.95686275 0.01473911
## 188 0.5263158 0.0025572793 0.9929972 0.9942118 0.95882353 0.01402624
## 189 0.5263158 0.0039651020 0.9937554 0.9942118 0.95882353 0.01294345
## 190 0.5263158 0.0061479532 0.9938351 0.9953612 0.95686275 0.01274762
## 191 0.5263158 0.0095324984 0.9939752 0.9977011 0.94901961 0.01275719
## 192 0.5263158 0.0147802892 0.9938399 0.9988506 0.94313725 0.01275711
## 193 0.5263158 0.0229170716 0.9939124 0.9988506 0.92549020 0.01271507
## 194 0.5263158 0.0355332812 0.9939148 1.0000000 0.91764706 0.01251177
## 195 0.5263158 0.0550949132 0.9938496 1.0000000 0.89607843 0.01218411
## 196 0.5263158 0.0854255324 0.9932266 1.0000000 0.86862745 0.01204464
## 197 0.5263158 0.1324536359 0.9911644 0.9965517 0.83529412 0.01161608
## 198 0.5263158 0.2053714526 0.9870327 1.0000000 0.77647059 0.01178649
## 199 0.5263158 0.3184316780 0.9863373 1.0000000 0.66862745 0.01147901
## 200 0.5263158 0.4937333415 0.9869603 1.0000000 0.35490196 0.01062969
## 201 0.5736842 0.0001186984 0.9870714 0.9778736 0.94313725 0.01760947
## 202 0.5736842 0.0001840437 0.9879673 0.9814039 0.94509804 0.01743674
## 203 0.5736842 0.0002853627 0.9885878 0.9837028 0.94705882 0.01749272
## 204 0.5736842 0.0004424594 0.9894137 0.9837028 0.94901961 0.01697861
## 205 0.5736842 0.0006860403 0.9902444 0.9860837 0.95294118 0.01659124
## 206 0.5736842 0.0010637162 0.9911306 0.9896141 0.95294118 0.01569023
## 207 0.5736842 0.0016493088 0.9918912 0.9919130 0.95882353 0.01473911
## 208 0.5736842 0.0025572793 0.9927944 0.9942118 0.95882353 0.01425692
## 209 0.5736842 0.0039651020 0.9937578 0.9930624 0.95882353 0.01311610
## 210 0.5736842 0.0061479532 0.9940428 0.9942118 0.95490196 0.01276224
## 211 0.5736842 0.0095324984 0.9939100 0.9965517 0.94705882 0.01290498
## 212 0.5736842 0.0147802892 0.9939776 0.9988506 0.94313725 0.01257632
## 213 0.5736842 0.0229170716 0.9940500 0.9988506 0.92745098 0.01254499
## 214 0.5736842 0.0355332812 0.9941877 1.0000000 0.91372549 0.01213944
## 215 0.5736842 0.0550949132 0.9938496 1.0000000 0.89215686 0.01218411
## 216 0.5736842 0.0854255324 0.9929537 1.0000000 0.86666667 0.01203797
## 217 0.5736842 0.1324536359 0.9903410 0.9965517 0.82156863 0.01146123
## 218 0.5736842 0.2053714526 0.9866198 1.0000000 0.77058824 0.01193730
## 219 0.5736842 0.3184316780 0.9866850 1.0000000 0.66470588 0.01098444
## 220 0.5736842 0.4937333415 0.9873732 1.0000000 0.22549020 0.01023610
## 221 0.6210526 0.0001186984 0.9868661 0.9778736 0.94313725 0.01758218
## 222 0.6210526 0.0001840437 0.9878296 0.9814039 0.94509804 0.01749345
## 223 0.6210526 0.0002853627 0.9885878 0.9837028 0.94705882 0.01749272
## 224 0.6210526 0.0004424594 0.9890708 0.9837028 0.94901961 0.01738519
## 225 0.6210526 0.0006860403 0.9901768 0.9860837 0.95294118 0.01658845
## 226 0.6210526 0.0010637162 0.9910654 0.9919130 0.95294118 0.01567851
## 227 0.6210526 0.0016493088 0.9918236 0.9919130 0.95882353 0.01494450
## 228 0.6210526 0.0025572793 0.9929296 0.9930624 0.95686275 0.01424734
## 229 0.6210526 0.0039651020 0.9936902 0.9919130 0.95882353 0.01330304
## 230 0.6210526 0.0061479532 0.9941804 0.9930624 0.95098039 0.01270268
## 231 0.6210526 0.0095324984 0.9939776 0.9965517 0.94313725 0.01272213
## 232 0.6210526 0.0147802892 0.9940476 0.9988506 0.94313725 0.01258056
## 233 0.6210526 0.0229170716 0.9941201 0.9988506 0.92745098 0.01254881
## 234 0.6210526 0.0355332812 0.9941877 1.0000000 0.90980392 0.01213944
## 235 0.6210526 0.0550949132 0.9938496 1.0000000 0.88627451 0.01218411
## 236 0.6210526 0.0854255324 0.9927461 1.0000000 0.85882353 0.01194461
## 237 0.6210526 0.1324536359 0.9899932 0.9965517 0.81372549 0.01134956
## 238 0.6210526 0.2053714526 0.9864146 1.0000000 0.75882353 0.01154729
## 239 0.6210526 0.3184316780 0.9869603 1.0000000 0.64901961 0.01035906
## 240 0.6210526 0.4937333415 0.9873129 1.0000000 0.11568627 0.01045259
## 241 0.6684211 0.0001186984 0.9865933 0.9778736 0.94313725 0.01772521
## 242 0.6684211 0.0001840437 0.9876219 0.9802545 0.94509804 0.01767408
## 243 0.6684211 0.0002853627 0.9885178 0.9837028 0.94509804 0.01744961
## 244 0.6684211 0.0004424594 0.9890032 0.9837028 0.94901961 0.01736964
## 245 0.6684211 0.0006860403 0.9901067 0.9860837 0.95294118 0.01665078
## 246 0.6684211 0.0010637162 0.9909978 0.9919130 0.95490196 0.01565205
## 247 0.6684211 0.0016493088 0.9920313 0.9919130 0.95882353 0.01484158
## 248 0.6684211 0.0025572793 0.9932797 0.9919130 0.95882353 0.01417141
## 249 0.6684211 0.0039651020 0.9939027 0.9919130 0.95686275 0.01346382
## 250 0.6684211 0.0061479532 0.9941128 0.9930624 0.94901961 0.01288691
## 251 0.6684211 0.0095324984 0.9940452 0.9965517 0.93921569 0.01254755
## 252 0.6684211 0.0147802892 0.9941853 0.9988506 0.93921569 0.01254272
## 253 0.6684211 0.0229170716 0.9942553 0.9988506 0.92549020 0.01235203
## 254 0.6684211 0.0355332812 0.9943253 1.0000000 0.90392157 0.01214710
## 255 0.6684211 0.0550949132 0.9938472 1.0000000 0.87843137 0.01203350
## 256 0.6684211 0.0854255324 0.9925384 1.0000000 0.85490196 0.01158117
## 257 0.6684211 0.1324536359 0.9901333 1.0000000 0.81176471 0.01100715
## 258 0.6684211 0.2053714526 0.9868975 1.0000000 0.75294118 0.01108174
## 259 0.6684211 0.3184316780 0.9873080 1.0000000 0.61372549 0.01049627
## 260 0.6684211 0.4937333415 0.9862938 1.0000000 0.02745098 0.01121097
## 261 0.7157895 0.0001186984 0.9863204 0.9778736 0.94313725 0.01776780
## 262 0.7157895 0.0001840437 0.9872090 0.9814039 0.94509804 0.01782696
## 263 0.7157895 0.0002853627 0.9882401 0.9837028 0.94705882 0.01768320
## 264 0.7157895 0.0004424594 0.9887231 0.9837028 0.94901961 0.01784329
## 265 0.7157895 0.0006860403 0.9899667 0.9860837 0.95294118 0.01685532
## 266 0.7157895 0.0010637162 0.9910678 0.9919130 0.95490196 0.01564007
## 267 0.7157895 0.0016493088 0.9920313 0.9896141 0.95882353 0.01484158
## 268 0.7157895 0.0025572793 0.9932797 0.9919130 0.95882353 0.01417141
## 269 0.7157895 0.0039651020 0.9936975 0.9919130 0.95686275 0.01383502
## 270 0.7157895 0.0061479532 0.9941128 0.9930624 0.94509804 0.01288691
## 271 0.7157895 0.0095324984 0.9940452 0.9965517 0.94117647 0.01254755
## 272 0.7157895 0.0147802892 0.9943229 0.9988506 0.93921569 0.01237859
## 273 0.7157895 0.0229170716 0.9942553 0.9988506 0.92352941 0.01235203
## 274 0.7157895 0.0355332812 0.9942553 1.0000000 0.90196078 0.01214437
## 275 0.7157895 0.0550949132 0.9937071 1.0000000 0.87647059 0.01193718
## 276 0.7157895 0.0854255324 0.9923307 1.0000000 0.85098039 0.01153351
## 277 0.7157895 0.1324536359 0.9897880 1.0000000 0.80980392 0.01063291
## 278 0.7157895 0.2053714526 0.9873756 1.0000000 0.74901961 0.01052565
## 279 0.7157895 0.3184316780 0.9874481 1.0000000 0.56862745 0.01044798
## 280 0.7157895 0.4937333415 0.9851516 1.0000000 0.00000000 0.01294614
## 281 0.7631579 0.0001186984 0.9861127 0.9767241 0.94313725 0.01778500
## 282 0.7631579 0.0001840437 0.9870714 0.9814039 0.94509804 0.01777278
## 283 0.7631579 0.0002853627 0.9881025 0.9837028 0.94705882 0.01785986
## 284 0.7631579 0.0004424594 0.9887231 0.9837028 0.94901961 0.01784329
## 285 0.7631579 0.0006860403 0.9898290 0.9860837 0.95294118 0.01708399
## 286 0.7631579 0.0010637162 0.9910678 0.9919130 0.95490196 0.01573954
## 287 0.7631579 0.0016493088 0.9920337 0.9896141 0.96078431 0.01485735
## 288 0.7631579 0.0025572793 0.9932821 0.9919130 0.96078431 0.01434842
## 289 0.7631579 0.0039651020 0.9936299 0.9919130 0.95490196 0.01406261
## 290 0.7631579 0.0061479532 0.9943181 0.9930624 0.94509804 0.01273319
## 291 0.7631579 0.0095324984 0.9941804 0.9977011 0.94117647 0.01238600
## 292 0.7631579 0.0147802892 0.9943905 0.9988506 0.93725490 0.01235889
## 293 0.7631579 0.0229170716 0.9943905 0.9988506 0.91764706 0.01210370
## 294 0.7631579 0.0355332812 0.9942553 1.0000000 0.89803922 0.01214437
## 295 0.7631579 0.0550949132 0.9934971 1.0000000 0.87254902 0.01176226
## 296 0.7631579 0.0854255324 0.9922704 1.0000000 0.84509804 0.01083874
## 297 0.7631579 0.1324536359 0.9889573 1.0000000 0.80980392 0.01127959
## 298 0.7631579 0.2053714526 0.9873104 1.0000000 0.74705882 0.01062726
## 299 0.7631579 0.3184316780 0.9871076 1.0000000 0.52352941 0.01070152
## 300 0.7631579 0.4937333415 0.9844586 1.0000000 0.00000000 0.01334716
## 301 0.8105263 0.0001186984 0.9860451 0.9755747 0.94117647 0.01778210
## 302 0.8105263 0.0001840437 0.9870014 0.9814039 0.94509804 0.01787805
## 303 0.8105263 0.0002853627 0.9878972 0.9837028 0.94509804 0.01791332
## 304 0.8105263 0.0004424594 0.9887231 0.9837028 0.95098039 0.01784329
## 305 0.8105263 0.0006860403 0.9897614 0.9860837 0.95098039 0.01706296
## 306 0.8105263 0.0010637162 0.9910678 0.9907635 0.95686275 0.01573954
## 307 0.8105263 0.0016493088 0.9919661 0.9896141 0.96078431 0.01505236
## 308 0.8105263 0.0025572793 0.9934222 0.9919130 0.95882353 0.01454686
## 309 0.8105263 0.0039651020 0.9937699 0.9919130 0.95098039 0.01409502
## 310 0.8105263 0.0061479532 0.9942480 0.9919130 0.94313725 0.01274250
## 311 0.8105263 0.0095324984 0.9945209 0.9965517 0.94117647 0.01235470
## 312 0.8105263 0.0147802892 0.9944581 0.9988506 0.93333333 0.01235026
## 313 0.8105263 0.0229170716 0.9944581 0.9988506 0.91568627 0.01209489
## 314 0.8105263 0.0355332812 0.9942529 1.0000000 0.90000000 0.01192216
## 315 0.8105263 0.0550949132 0.9934971 1.0000000 0.87058824 0.01152701
## 316 0.8105263 0.0854255324 0.9921327 1.0000000 0.84117647 0.01076364
## 317 0.8105263 0.1324536359 0.9881967 1.0000000 0.80588235 0.01089215
## 318 0.8105263 0.2053714526 0.9870352 1.0000000 0.73529412 0.01111114
## 319 0.8105263 0.3184316780 0.9863542 1.0000000 0.50392157 0.01126183
## 320 0.8105263 0.4937333415 0.5000000 1.0000000 0.00000000 0.00000000
## 321 0.8578947 0.0001186984 0.9857022 0.9743842 0.93921569 0.01800210
## 322 0.8578947 0.0001840437 0.9867961 0.9802545 0.94509804 0.01779795
## 323 0.8578947 0.0002853627 0.9877572 0.9837028 0.94509804 0.01805221
## 324 0.8578947 0.0004424594 0.9886530 0.9825534 0.94901961 0.01792936
## 325 0.8578947 0.0006860403 0.9893533 0.9837849 0.95098039 0.01730703
## 326 0.8578947 0.0010637162 0.9906525 0.9896141 0.95882353 0.01620986
## 327 0.8578947 0.0016493088 0.9919661 0.9896141 0.96274510 0.01505236
## 328 0.8578947 0.0025572793 0.9934198 0.9907225 0.96078431 0.01474423
## 329 0.8578947 0.0039651020 0.9939051 0.9919130 0.94901961 0.01389302
## 330 0.8578947 0.0061479532 0.9942480 0.9919130 0.94117647 0.01254048
## 331 0.8578947 0.0095324984 0.9946610 0.9965517 0.93921569 0.01210130
## 332 0.8578947 0.0147802892 0.9945257 0.9977011 0.92549020 0.01235274
## 333 0.8578947 0.0229170716 0.9945257 0.9988506 0.91176471 0.01213340
## 334 0.8578947 0.0355332812 0.9943229 1.0000000 0.89803922 0.01170598
## 335 0.8578947 0.0550949132 0.9931566 1.0000000 0.86666667 0.01139587
## 336 0.8578947 0.0854255324 0.9915097 1.0000000 0.84117647 0.01066832
## 337 0.8578947 0.1324536359 0.9871052 1.0000000 0.80196078 0.01105310
## 338 0.8578947 0.2053714526 0.9866343 1.0000000 0.72941176 0.01123527
## 339 0.8578947 0.3184316780 0.9860282 1.0000000 0.38039216 0.01167663
## 340 0.8578947 0.4937333415 0.5000000 1.0000000 0.00000000 0.00000000
## 341 0.9052632 0.0001186984 0.9852265 0.9709360 0.93921569 0.01792520
## 342 0.9052632 0.0001840437 0.9867285 0.9790640 0.94509804 0.01775784
## 343 0.9052632 0.0002853627 0.9875495 0.9814039 0.94509804 0.01811985
## 344 0.9052632 0.0004424594 0.9881749 0.9825534 0.94901961 0.01838496
## 345 0.9052632 0.0006860403 0.9889332 0.9837849 0.95098039 0.01787793
## 346 0.9052632 0.0010637162 0.9904472 0.9884647 0.95882353 0.01651266
## 347 0.9052632 0.0016493088 0.9919661 0.9884236 0.96470588 0.01518323
## 348 0.9052632 0.0025572793 0.9933522 0.9884236 0.96078431 0.01471764
## 349 0.9052632 0.0039651020 0.9938375 0.9907225 0.94705882 0.01386725
## 350 0.9052632 0.0061479532 0.9942456 0.9919130 0.94117647 0.01239512
## 351 0.9052632 0.0095324984 0.9945934 0.9942118 0.93921569 0.01209956
## 352 0.9052632 0.0147802892 0.9945934 0.9977011 0.92549020 0.01212299
## 353 0.9052632 0.0229170716 0.9945257 0.9977011 0.91176471 0.01188337
## 354 0.9052632 0.0355332812 0.9940452 1.0000000 0.89803922 0.01151991
## 355 0.9052632 0.0550949132 0.9932266 1.0000000 0.86666667 0.01077377
## 356 0.9052632 0.0854255324 0.9907563 1.0000000 0.84313725 0.01098953
## 357 0.9052632 0.1324536359 0.9861514 1.0000000 0.80000000 0.01190133
## 358 0.9052632 0.2053714526 0.9859558 1.0000000 0.71372549 0.01203315
## 359 0.9052632 0.3184316780 0.9857553 1.0000000 0.29607843 0.01214892
## 360 0.9052632 0.4937333415 0.5000000 1.0000000 0.00000000 0.00000000
## 361 0.9526316 0.0001186984 0.9848486 0.9697865 0.93921569 0.01791926
## 362 0.9526316 0.0001840437 0.9862504 0.9756158 0.94117647 0.01796736
## 363 0.9526316 0.0002853627 0.9872766 0.9802545 0.94509804 0.01813602
## 364 0.9526316 0.0004424594 0.9879697 0.9825534 0.94705882 0.01834728
## 365 0.9526316 0.0006860403 0.9886530 0.9814039 0.95098039 0.01850259
## 366 0.9526316 0.0010637162 0.9903096 0.9860837 0.95882353 0.01660425
## 367 0.9526316 0.0016493088 0.9920361 0.9872742 0.96470588 0.01526627
## 368 0.9526316 0.0025572793 0.9932169 0.9872742 0.95882353 0.01468269
## 369 0.9526316 0.0039651020 0.9936975 0.9895320 0.94705882 0.01365296
## 370 0.9526316 0.0061479532 0.9945209 0.9895320 0.94117647 0.01239326
## 371 0.9526316 0.0095324984 0.9945934 0.9930213 0.93921569 0.01209956
## 372 0.9526316 0.0147802892 0.9945209 0.9977011 0.92352941 0.01191120
## 373 0.9526316 0.0229170716 0.9945257 0.9977011 0.90980392 0.01188337
## 374 0.9526316 0.0355332812 0.9941128 1.0000000 0.89607843 0.01136391
## 375 0.9526316 0.0550949132 0.9927509 1.0000000 0.86666667 0.01088144
## 376 0.9526316 0.0854255324 0.9901405 1.0000000 0.84313725 0.01119898
## 377 0.9526316 0.1324536359 0.9859630 1.0000000 0.79803922 0.01247187
## 378 0.9526316 0.2053714526 0.9858906 1.0000000 0.70000000 0.01247468
## 379 0.9526316 0.3184316780 0.9839829 1.0000000 0.21960784 0.01346727
## 380 0.9526316 0.4937333415 0.5000000 1.0000000 0.00000000 0.00000000
## 381 1.0000000 0.0001186984 0.9841350 0.9650657 0.93921569 0.01855363
## 382 1.0000000 0.0001840437 0.9850188 0.9685961 0.94117647 0.01859959
## 383 1.0000000 0.0002853627 0.9865908 0.9767652 0.94509804 0.01841387
## 384 1.0000000 0.0004424594 0.9875592 0.9825534 0.94509804 0.01872120
## 385 1.0000000 0.0006860403 0.9883126 0.9814039 0.95098039 0.01873588
## 386 1.0000000 0.0010637162 0.9901067 0.9861248 0.95882353 0.01661558
## 387 1.0000000 0.0016493088 0.9918309 0.9861248 0.96470588 0.01526805
## 388 1.0000000 0.0025572793 0.9930093 0.9884236 0.95686275 0.01446515
## 389 1.0000000 0.0039651020 0.9940380 0.9895320 0.95098039 0.01354538
## 390 1.0000000 0.0061479532 0.9947938 0.9895320 0.94901961 0.01197678
## 391 1.0000000 0.0095324984 0.9947962 0.9930213 0.93921569 0.01197571
## 392 1.0000000 0.0147802892 0.9948614 0.9965107 0.92156863 0.01160791
## 393 1.0000000 0.0229170716 0.9946586 0.9965107 0.91372549 0.01155116
## 394 1.0000000 0.0355332812 0.9938351 0.9965107 0.89411765 0.01134594
## 395 1.0000000 0.0550949132 0.9924080 0.9976601 0.86470588 0.01085916
## 396 1.0000000 0.0854255324 0.9890394 0.9976601 0.84313725 0.01183595
## 397 1.0000000 0.1324536359 0.9863687 1.0000000 0.79803922 0.01261104
## 398 1.0000000 0.2053714526 0.9852048 1.0000000 0.68039216 0.01266089
## 399 1.0000000 0.3184316780 0.9813025 1.0000000 0.12156863 0.01491709
## 400 1.0000000 0.4937333415 0.5000000 1.0000000 0.00000000 0.00000000
## SensSD SpecSD
## 1 0.023793935 0.04655751
## 2 0.023793935 0.04655751
## 3 0.023793935 0.04655751
## 4 0.021745731 0.04281953
## 5 0.021745731 0.04281953
## 6 0.019322470 0.04281953
## 7 0.015650517 0.05049176
## 8 0.013165607 0.05107912
## 9 0.011922273 0.05049176
## 10 0.006295662 0.05049176
## 11 0.000000000 0.04868718
## 12 0.000000000 0.04868718
## 13 0.000000000 0.05142831
## 14 0.000000000 0.06965806
## 15 0.000000000 0.07179496
## 16 0.000000000 0.07949749
## 17 0.006295662 0.08186371
## 18 0.010521675 0.08273359
## 19 0.010521675 0.08695259
## 20 0.010521675 0.09234297
## 21 0.023793935 0.04969781
## 22 0.023793935 0.04969781
## 23 0.023793935 0.04969781
## 24 0.021745731 0.04477202
## 25 0.021745731 0.04281953
## 26 0.019322470 0.04281953
## 27 0.014911085 0.05049176
## 28 0.013165607 0.05107912
## 29 0.010521675 0.05049176
## 30 0.006295662 0.04868718
## 31 0.000000000 0.04868718
## 32 0.000000000 0.04868718
## 33 0.000000000 0.05569789
## 34 0.000000000 0.06965806
## 35 0.000000000 0.07193332
## 36 0.000000000 0.07747045
## 37 0.006295662 0.08376079
## 38 0.010521675 0.08196082
## 39 0.010521675 0.08998712
## 40 0.008748556 0.09438771
## 41 0.023793935 0.05204332
## 42 0.023793935 0.05204332
## 43 0.023793935 0.04969781
## 44 0.021745731 0.04477202
## 45 0.021745731 0.04281953
## 46 0.019322470 0.04281953
## 47 0.014911085 0.05049176
## 48 0.013165607 0.05107912
## 49 0.012030352 0.05049176
## 50 0.006295662 0.04868718
## 51 0.000000000 0.04868718
## 52 0.000000000 0.04868718
## 53 0.000000000 0.06078540
## 54 0.000000000 0.06896949
## 55 0.000000000 0.07525692
## 56 0.000000000 0.07937231
## 57 0.006295662 0.08451711
## 58 0.010521675 0.08667771
## 59 0.010521675 0.09260103
## 60 0.008748556 0.10644804
## 61 0.027021468 0.05336395
## 62 0.023793935 0.05336395
## 63 0.023793935 0.04969781
## 64 0.023793935 0.04477202
## 65 0.023634524 0.04281953
## 66 0.021294685 0.04281953
## 67 0.014911085 0.05049176
## 68 0.013165607 0.05107912
## 69 0.012030352 0.04921530
## 70 0.006295662 0.04868718
## 71 0.000000000 0.04868718
## 72 0.000000000 0.04868718
## 73 0.000000000 0.06179126
## 74 0.000000000 0.06896949
## 75 0.000000000 0.07816045
## 76 0.000000000 0.08039298
## 77 0.000000000 0.08451711
## 78 0.010521675 0.09236450
## 79 0.010521675 0.08556937
## 80 0.000000000 0.10866672
## 81 0.027037721 0.05336395
## 82 0.023793935 0.05336395
## 83 0.023793935 0.05204332
## 84 0.023793935 0.04655751
## 85 0.021745731 0.04477202
## 86 0.021008527 0.04281953
## 87 0.014911085 0.04617149
## 88 0.013165607 0.05107912
## 89 0.012030352 0.04921530
## 90 0.008748556 0.04868718
## 91 0.000000000 0.04868718
## 92 0.000000000 0.05142831
## 93 0.000000000 0.06818655
## 94 0.000000000 0.06896949
## 95 0.000000000 0.07816045
## 96 0.000000000 0.08786263
## 97 0.000000000 0.08196082
## 98 0.010521675 0.09027395
## 99 0.010521675 0.09572661
## 100 0.000000000 0.10515111
## 101 0.027003398 0.05672385
## 102 0.027037721 0.05336395
## 103 0.023793935 0.05204332
## 104 0.023793935 0.04655751
## 105 0.021745731 0.04477202
## 106 0.021008527 0.04281953
## 107 0.014911085 0.04617149
## 108 0.013165607 0.05107912
## 109 0.012030352 0.04921530
## 110 0.010648570 0.04868718
## 111 0.006295662 0.04868718
## 112 0.000000000 0.05291488
## 113 0.000000000 0.06641363
## 114 0.000000000 0.06934332
## 115 0.000000000 0.07775230
## 116 0.000000000 0.09130343
## 117 0.000000000 0.08196082
## 118 0.010521675 0.09104169
## 119 0.010521675 0.09853156
## 120 0.000000000 0.10486704
## 121 0.027003398 0.05672385
## 122 0.027037721 0.05336395
## 123 0.023793935 0.05204332
## 124 0.023793935 0.04655751
## 125 0.021745731 0.04477202
## 126 0.021008527 0.04477202
## 127 0.014911085 0.04617149
## 128 0.013165607 0.04921530
## 129 0.012030352 0.04921530
## 130 0.010648570 0.04868718
## 131 0.006295662 0.04868718
## 132 0.000000000 0.05204332
## 133 0.000000000 0.06804057
## 134 0.000000000 0.06934332
## 135 0.000000000 0.07775230
## 136 0.000000000 0.09330702
## 137 0.000000000 0.08545309
## 138 0.010521675 0.08927715
## 139 0.000000000 0.10657873
## 140 0.000000000 0.10607375
## 141 0.026918305 0.05672385
## 142 0.027037721 0.05336395
## 143 0.023793935 0.05204332
## 144 0.023793935 0.04655751
## 145 0.021745731 0.04477202
## 146 0.020601510 0.04477202
## 147 0.014911085 0.04617149
## 148 0.013165607 0.04921530
## 149 0.012030352 0.04921530
## 150 0.010648570 0.04868718
## 151 0.008748556 0.05049176
## 152 0.006295662 0.05336395
## 153 0.000000000 0.06853563
## 154 0.000000000 0.07160081
## 155 0.000000000 0.08049187
## 156 0.000000000 0.09130343
## 157 0.000000000 0.08545309
## 158 0.010521675 0.08702117
## 159 0.000000000 0.10756310
## 160 0.000000000 0.12005646
## 161 0.026918305 0.05672385
## 162 0.027003398 0.05336395
## 163 0.023793935 0.05204332
## 164 0.023793935 0.04655751
## 165 0.021745731 0.04477202
## 166 0.020601510 0.04477202
## 167 0.014911085 0.04807060
## 168 0.013165607 0.04921530
## 169 0.012030352 0.04921530
## 170 0.012030352 0.04868718
## 171 0.008748556 0.04981771
## 172 0.006295662 0.05107912
## 173 0.006295662 0.06853563
## 174 0.000000000 0.07160081
## 175 0.000000000 0.08164479
## 176 0.000000000 0.08885292
## 177 0.006295662 0.08865126
## 178 0.010521675 0.09071345
## 179 0.000000000 0.10469623
## 180 0.000000000 0.11662845
## 181 0.026804439 0.05672385
## 182 0.027003398 0.05336395
## 183 0.023793935 0.05204332
## 184 0.023793935 0.05060977
## 185 0.021745731 0.04477202
## 186 0.020601510 0.04477202
## 187 0.014911085 0.04617149
## 188 0.013165607 0.04921530
## 189 0.013165607 0.04921530
## 190 0.012030352 0.04868718
## 191 0.008748556 0.05060977
## 192 0.006295662 0.05234812
## 193 0.006295662 0.06896949
## 194 0.000000000 0.07343808
## 195 0.000000000 0.08416343
## 196 0.000000000 0.08695259
## 197 0.010521675 0.09060378
## 198 0.000000000 0.09572661
## 199 0.000000000 0.10186582
## 200 0.000000000 0.13060791
## 201 0.026635100 0.05672385
## 202 0.027003398 0.05336395
## 203 0.023793935 0.05204332
## 204 0.023793935 0.05291488
## 205 0.021555424 0.04477202
## 206 0.020601510 0.04477202
## 207 0.014911085 0.04410074
## 208 0.013165607 0.04921530
## 209 0.014113949 0.04921530
## 210 0.013165607 0.05049176
## 211 0.010521675 0.05204332
## 212 0.006295662 0.05672385
## 213 0.006295662 0.06309697
## 214 0.000000000 0.07201621
## 215 0.000000000 0.08330847
## 216 0.000000000 0.08591726
## 217 0.010521675 0.08938845
## 218 0.000000000 0.09690350
## 219 0.000000000 0.09665692
## 220 0.000000000 0.10372300
## 221 0.026635100 0.05672385
## 222 0.027003398 0.05336395
## 223 0.023793935 0.05204332
## 224 0.023793935 0.05291488
## 225 0.021555424 0.04477202
## 226 0.017445476 0.04477202
## 227 0.014911085 0.04410074
## 228 0.014113949 0.05107912
## 229 0.014911085 0.04921530
## 230 0.014113949 0.04905340
## 231 0.010521675 0.05234812
## 232 0.006295662 0.05672385
## 233 0.006295662 0.06309697
## 234 0.000000000 0.07682603
## 235 0.000000000 0.08451711
## 236 0.000000000 0.08261332
## 237 0.010521675 0.08612532
## 238 0.000000000 0.10402931
## 239 0.000000000 0.09949561
## 240 0.000000000 0.06991452
## 241 0.026635100 0.05672385
## 242 0.026918305 0.05336395
## 243 0.023793935 0.05336395
## 244 0.023793935 0.05291488
## 245 0.021555424 0.04477202
## 246 0.017445476 0.04281953
## 247 0.014911085 0.04410074
## 248 0.014911085 0.05158275
## 249 0.014911085 0.05107912
## 250 0.014113949 0.05060977
## 251 0.010521675 0.05457985
## 252 0.006295662 0.05457985
## 253 0.006295662 0.06541805
## 254 0.000000000 0.07798215
## 255 0.000000000 0.08865126
## 256 0.000000000 0.08135198
## 257 0.000000000 0.08927715
## 258 0.000000000 0.10293393
## 259 0.000000000 0.12033772
## 260 0.000000000 0.03360862
## 261 0.026635100 0.05672385
## 262 0.027003398 0.05336395
## 263 0.023793935 0.05428759
## 264 0.023793935 0.05291488
## 265 0.021555424 0.04477202
## 266 0.017445476 0.04281953
## 267 0.016137343 0.04410074
## 268 0.014911085 0.05158275
## 269 0.014911085 0.05107912
## 270 0.014113949 0.05336395
## 271 0.010521675 0.05569789
## 272 0.006295662 0.05457985
## 273 0.006295662 0.06760074
## 274 0.000000000 0.07620226
## 275 0.000000000 0.08649397
## 276 0.000000000 0.08280567
## 277 0.000000000 0.08965502
## 278 0.000000000 0.10582975
## 279 0.000000000 0.11696897
## 280 0.000000000 0.00000000
## 281 0.026390236 0.05672385
## 282 0.027003398 0.05336395
## 283 0.023793935 0.05428759
## 284 0.023793935 0.05291488
## 285 0.021555424 0.04736211
## 286 0.017445476 0.04281953
## 287 0.016137343 0.04459400
## 288 0.014911085 0.04719386
## 289 0.014911085 0.05049176
## 290 0.014113949 0.05336395
## 291 0.008748556 0.05569789
## 292 0.006295662 0.05336395
## 293 0.006295662 0.06662290
## 294 0.000000000 0.07557335
## 295 0.000000000 0.08472860
## 296 0.000000000 0.08098448
## 297 0.000000000 0.08965502
## 298 0.000000000 0.10830010
## 299 0.000000000 0.11492801
## 300 0.000000000 0.00000000
## 301 0.026090748 0.05569789
## 302 0.027003398 0.05336395
## 303 0.023793935 0.05336395
## 304 0.023793935 0.05369829
## 305 0.021555424 0.04905340
## 306 0.018020768 0.04351059
## 307 0.016137343 0.04459400
## 308 0.014911085 0.04672805
## 309 0.014911085 0.05142831
## 310 0.014911085 0.05234812
## 311 0.010521675 0.05123461
## 312 0.006295662 0.05291488
## 313 0.006295662 0.07025502
## 314 0.000000000 0.06760074
## 315 0.000000000 0.08517338
## 316 0.000000000 0.08196082
## 317 0.000000000 0.09027395
## 318 0.000000000 0.10785850
## 319 0.000000000 0.12519722
## 320 0.000000000 0.00000000
## 321 0.028965769 0.05672385
## 322 0.026918305 0.05336395
## 323 0.023793935 0.05336395
## 324 0.025476149 0.05291488
## 325 0.023600607 0.04905340
## 326 0.018504540 0.04410074
## 327 0.016137343 0.04499356
## 328 0.015650517 0.04459400
## 329 0.014911085 0.05291488
## 330 0.014911085 0.05351280
## 331 0.010521675 0.05457985
## 332 0.008748556 0.05969603
## 333 0.006295662 0.07036815
## 334 0.000000000 0.07234681
## 335 0.000000000 0.08164479
## 336 0.000000000 0.08196082
## 337 0.000000000 0.08938845
## 338 0.000000000 0.10431566
## 339 0.000000000 0.12899928
## 340 0.000000000 0.00000000
## 341 0.031827374 0.05672385
## 342 0.026804439 0.05336395
## 343 0.027003398 0.05336395
## 344 0.025476149 0.05291488
## 345 0.023600607 0.05369829
## 346 0.018903822 0.04410074
## 347 0.016653824 0.04530189
## 348 0.018956636 0.04459400
## 349 0.015650517 0.05428759
## 350 0.014911085 0.05351280
## 351 0.013165607 0.05457985
## 352 0.008748556 0.05969603
## 353 0.008748556 0.07036815
## 354 0.000000000 0.07234681
## 355 0.000000000 0.08164479
## 356 0.000000000 0.08222728
## 357 0.000000000 0.08683816
## 358 0.000000000 0.10098351
## 359 0.000000000 0.11813624
## 360 0.000000000 0.00000000
## 361 0.033874883 0.05672385
## 362 0.027601142 0.05351280
## 363 0.026918305 0.05336395
## 364 0.025476149 0.05204332
## 365 0.023647687 0.05369829
## 366 0.019560985 0.04672805
## 367 0.019273283 0.04530189
## 368 0.019273283 0.04672805
## 369 0.016266674 0.05428759
## 370 0.016266674 0.05569789
## 371 0.014198367 0.05457985
## 372 0.008748556 0.06397336
## 373 0.008748556 0.06862262
## 374 0.000000000 0.07193332
## 375 0.000000000 0.08164479
## 376 0.000000000 0.07927203
## 377 0.000000000 0.08831414
## 378 0.000000000 0.10517002
## 379 0.000000000 0.10582975
## 380 0.000000000 0.00000000
## 381 0.036798988 0.05457985
## 382 0.029488892 0.05123461
## 383 0.026371172 0.05336395
## 384 0.023812391 0.05336395
## 385 0.023647687 0.05369829
## 386 0.021513612 0.04672805
## 387 0.021513612 0.04530189
## 388 0.016653824 0.04617149
## 389 0.016266674 0.04905340
## 390 0.016266674 0.05291488
## 391 0.014198367 0.05457985
## 392 0.010648570 0.07135040
## 393 0.010648570 0.06505224
## 394 0.010648570 0.06626375
## 395 0.008906249 0.08482244
## 396 0.008906249 0.08222728
## 397 0.000000000 0.08831414
## 398 0.000000000 0.11212532
## 399 0.000000000 0.06541805
## 400 0.000000000 0.00000000
##
## $pred
## NULL
##
## $bestTune
## alpha lambda
## 392 1 0.01478029
glmnetfit$results %>% arrange(desc(ROC)) %>% head
## alpha lambda ROC Sens Spec ROCSD SensSD
## 1 1.0000000 0.014780289 0.9948614 0.9965107 0.9215686 0.01160791 0.01064857
## 2 1.0000000 0.009532498 0.9947962 0.9930213 0.9392157 0.01197571 0.01419837
## 3 1.0000000 0.006147953 0.9947938 0.9895320 0.9490196 0.01197678 0.01626667
## 4 0.8578947 0.009532498 0.9946610 0.9965517 0.9392157 0.01210130 0.01052168
## 5 1.0000000 0.022917072 0.9946586 0.9965107 0.9137255 0.01155116 0.01064857
## 6 0.9052632 0.009532498 0.9945934 0.9942118 0.9392157 0.01209956 0.01316561
## SpecSD
## 1 0.07135040
## 2 0.05457985
## 3 0.05291488
## 4 0.05457985
## 5 0.06505224
## 6 0.05457985
rffit <- train(diagnosis ~ ., data = training,
method = "rf",
tuneLength = 10,
trControl = my_trainControl,
verbose = FALSE,
## Specify which metric to optimize
metric = "ROC")
rffit
## Random Forest
##
## 456 samples
## 30 predictor
## 2 classes: 'B', 'M'
##
## No pre-processing
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 410, 411, 410, 411, 410, 411, ...
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.9895719 0.9720443 0.9313725
## 5 0.9881544 0.9685140 0.9313725
## 8 0.9866911 0.9697044 0.9313725
## 11 0.9871148 0.9720854 0.9333333
## 14 0.9865824 0.9697865 0.9431373
## 17 0.9864617 0.9674056 0.9333333
## 20 0.9857553 0.9687192 0.9235294
## 23 0.9853545 0.9651478 0.9313725
## 26 0.9847532 0.9628489 0.9274510
## 30 0.9851999 0.9628900 0.9254902
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
plot(varImp(glmnetfit, scale = FALSE), top = 10, main = "glmnet")

plot(varImp(rffit, scale = FALSE), top = 10, main = "rf")

model_list <- list(glmnet = glmnetfit, randomForest = rffit)
resamples <- resamples(model_list)
summary(resamples)
##
## Call:
## summary.resamples(object = resamples)
##
## Models: glmnet, randomForest
## Number of resamples: 30
##
## ROC
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.9579832 0.9964322 1.0000000 0.9948614 1 1 0
## randomForest 0.9381339 0.9845063 0.9959432 0.9895719 1 1 0
##
## Sens
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.9642857 1.0000000 1.0000000 0.9965107 1 1 0
## randomForest 0.8965517 0.9642857 0.9655172 0.9720443 1 1 0
##
## Spec
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.7647059 0.8823529 0.9411765 0.9215686 1.0000000 1 0
## randomForest 0.6470588 0.9411765 0.9411765 0.9313725 0.9852941 1 0
# Ⅷ. 시계열 데이터 처리
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
library(nycflights13)
ymd(20240131)
## [1] "2024-01-31"
ymd_hms('20240331 15:14:33')
## [1] "2024-03-31 15:14:33 UTC"
ymd_hm('20240331 15:14')
## [1] "2024-03-31 15:14:00 UTC"
data("flights")
glimpse(flights)
## Rows: 336,776
## Columns: 19
## $ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2…
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ dep_time <int> 517, 533, 542, 544, 554, 554, 555, 557, 557, 558, 558, …
## $ sched_dep_time <int> 515, 529, 540, 545, 600, 558, 600, 600, 600, 600, 600, …
## $ dep_delay <dbl> 2, 4, 2, -1, -6, -4, -5, -3, -3, -2, -2, -2, -2, -2, -1…
## $ arr_time <int> 830, 850, 923, 1004, 812, 740, 913, 709, 838, 753, 849,…
## $ sched_arr_time <int> 819, 830, 850, 1022, 837, 728, 854, 723, 846, 745, 851,…
## $ arr_delay <dbl> 11, 20, 33, -18, -25, 12, 19, -14, -8, 8, -2, -3, 7, -1…
## $ carrier <chr> "UA", "UA", "AA", "B6", "DL", "UA", "B6", "EV", "B6", "…
## $ flight <int> 1545, 1714, 1141, 725, 461, 1696, 507, 5708, 79, 301, 4…
## $ tailnum <chr> "N14228", "N24211", "N619AA", "N804JB", "N668DN", "N394…
## $ origin <chr> "EWR", "LGA", "JFK", "JFK", "LGA", "EWR", "EWR", "LGA",…
## $ dest <chr> "IAH", "IAH", "MIA", "BQN", "ATL", "ORD", "FLL", "IAD",…
## $ air_time <dbl> 227, 227, 160, 183, 116, 150, 158, 53, 140, 138, 149, 1…
## $ distance <dbl> 1400, 1416, 1089, 1576, 762, 719, 1065, 229, 944, 733, …
## $ hour <dbl> 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 6, 6, 6…
## $ minute <dbl> 15, 29, 40, 45, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, 0, 59, 0…
## $ time_hour <dttm> 2013-01-01 05:00:00, 2013-01-01 05:00:00, 2013-01-01 0…
flights %>% select(year,month, day, hour, minute) %>% head
## # A tibble: 6 × 5
## year month day hour minute
## <int> <int> <int> <dbl> <dbl>
## 1 2013 1 1 5 15
## 2 2013 1 1 5 29
## 3 2013 1 1 5 40
## 4 2013 1 1 5 45
## 5 2013 1 1 6 0
## 6 2013 1 1 5 58
flights %>% select(year,month, day, hour, minute) %>%
mutate(departure = make_datetime(year,month, day, hour, minute)) %>% head
## # A tibble: 6 × 6
## year month day hour minute departure
## <int> <int> <int> <dbl> <dbl> <dttm>
## 1 2013 1 1 5 15 2013-01-01 05:15:00
## 2 2013 1 1 5 29 2013-01-01 05:29:00
## 3 2013 1 1 5 40 2013-01-01 05:40:00
## 4 2013 1 1 5 45 2013-01-01 05:45:00
## 5 2013 1 1 6 0 2013-01-01 06:00:00
## 6 2013 1 1 5 58 2013-01-01 05:58:00
make_datetime_100<- function(year,month, day, time){
make_datetime(year,month, day, time %/% 100, time %% 100)
}
flights %>%
filter(!is.na(dep_time)|!is.na(arr_time)) %>%
mutate(
dep_time = make_datetime_100(year, month, day, dep_time),
arr_time = make_datetime_100(year, month, day, arr_time),
sched_dep_time = make_datetime_100(year, month, day, sched_dep_time),
sched_arr_time = make_datetime_100(year, month, day, sched_arr_time))%>%
select(origin, dest, ends_with("delay"), ends_with("time"))
## # A tibble: 328,521 × 9
## origin dest dep_delay arr_delay dep_time sched_dep_time
## <chr> <chr> <dbl> <dbl> <dttm> <dttm>
## 1 EWR IAH 2 11 2013-01-01 05:17:00 2013-01-01 05:15:00
## 2 LGA IAH 4 20 2013-01-01 05:33:00 2013-01-01 05:29:00
## 3 JFK MIA 2 33 2013-01-01 05:42:00 2013-01-01 05:40:00
## 4 JFK BQN -1 -18 2013-01-01 05:44:00 2013-01-01 05:45:00
## 5 LGA ATL -6 -25 2013-01-01 05:54:00 2013-01-01 06:00:00
## 6 EWR ORD -4 12 2013-01-01 05:54:00 2013-01-01 05:58:00
## 7 EWR FLL -5 19 2013-01-01 05:55:00 2013-01-01 06:00:00
## 8 LGA IAD -3 -14 2013-01-01 05:57:00 2013-01-01 06:00:00
## 9 JFK MCO -3 -8 2013-01-01 05:57:00 2013-01-01 06:00:00
## 10 LGA ORD -2 8 2013-01-01 05:58:00 2013-01-01 06:00:00
## # ℹ 328,511 more rows
## # ℹ 3 more variables: arr_time <dttm>, sched_arr_time <dttm>, air_time <dbl>
#########################
library(ggplot2)
data(economics)
glimpse(economics)
## Rows: 574
## Columns: 6
## $ date <date> 1967-07-01, 1967-08-01, 1967-09-01, 1967-10-01, 1967-11-01, …
## $ pce <dbl> 506.7, 509.8, 515.6, 512.2, 517.4, 525.1, 530.9, 533.6, 544.3…
## $ pop <dbl> 198712, 198911, 199113, 199311, 199498, 199657, 199808, 19992…
## $ psavert <dbl> 12.6, 12.6, 11.9, 12.9, 12.8, 11.8, 11.7, 12.3, 11.7, 12.3, 1…
## $ uempmed <dbl> 4.5, 4.7, 4.6, 4.9, 4.7, 4.8, 5.1, 4.5, 4.1, 4.6, 4.4, 4.4, 4…
## $ unemploy <dbl> 2944, 2945, 2958, 3143, 3066, 3018, 2878, 3001, 2877, 2709, 2…
library(lubridate)
economics$year <- year(economics$date)
economics$month <- month(economics$date)
economics$day <- day(economics$date)
glimpse(economics)
## Rows: 574
## Columns: 9
## $ date <date> 1967-07-01, 1967-08-01, 1967-09-01, 1967-10-01, 1967-11-01, …
## $ pce <dbl> 506.7, 509.8, 515.6, 512.2, 517.4, 525.1, 530.9, 533.6, 544.3…
## $ pop <dbl> 198712, 198911, 199113, 199311, 199498, 199657, 199808, 19992…
## $ psavert <dbl> 12.6, 12.6, 11.9, 12.9, 12.8, 11.8, 11.7, 12.3, 11.7, 12.3, 1…
## $ uempmed <dbl> 4.5, 4.7, 4.6, 4.9, 4.7, 4.8, 5.1, 4.5, 4.1, 4.6, 4.4, 4.4, 4…
## $ unemploy <dbl> 2944, 2945, 2958, 3143, 3066, 3018, 2878, 3001, 2877, 2709, 2…
## $ year <dbl> 1967, 1967, 1967, 1967, 1967, 1967, 1968, 1968, 1968, 1968, 1…
## $ month <dbl> 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1…
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
economics %>% group_by(year) %>% summarise(m=mean(pop))
## # A tibble: 49 × 2
## year m
## <dbl> <dbl>
## 1 1967 199200.
## 2 1968 200664.
## 3 1969 202649.
## 4 1970 204982.
## 5 1971 207589.
## 6 1972 209838.
## 7 1973 211857.
## 8 1974 213815.
## 9 1975 215891.
## 10 1976 217999.
## # ℹ 39 more rows
####################
df <- read.csv("netflix.csv")
glimpse(df)
## Rows: 8,807
## Columns: 11
## $ show_id <chr> "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s1…
## $ type <chr> "Movie", "TV Show", "TV Show", "TV Show", "TV Show", "TV …
## $ title <chr> "Dick Johnson Is Dead", "Blood & Water", "Ganglands", "Ja…
## $ director <chr> "Kirsten Johnson", "", "Julien Leclercq", "", "", "Mike F…
## $ cast <chr> "", "Ama Qamata, Khosi Ngema, Gail Mabalane, Thabang Mola…
## $ country <chr> "United States", "South Africa", "", "", "India", "", "",…
## $ date_added <chr> "25-Sep-21", "24-Sep-21", "24-Sep-21", "24-Sep-21", "24-S…
## $ release_year <int> 2020, 2021, 2021, 2021, 2021, 2021, 2021, 1993, 2021, 202…
## $ rating <chr> "PG-13", "TV-MA", "TV-MA", "TV-MA", "TV-MA", "TV-MA", "PG…
## $ duration <chr> "90 min", "2 Seasons", "1 Season", "1 Season", "2 Seasons…
## $ listed_in <chr> "Documentaries", "International TV Shows, TV Dramas, TV M…
colSums(is.na(df)) #결측값 무조건 확인
## show_id type title director cast country
## 0 0 0 0 0 0
## date_added release_year rating duration listed_in
## 0 0 0 0 0
df1<-df %>% filter(country == "United Kingdom") %>%
mutate(ymd=dmy(date_added)) %>%
filter(ymd>='2018-01-01' & ymd<='2018-01-31')
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `ymd = dmy(date_added)`.
## Caused by warning:
## ! 15 failed to parse.
df$y2 <- parse_date_time(df$date_added, orders = c("mdy", "dmy"))
glimpse((df$y2))
## POSIXct[1:8807], format: "2021-09-25" "2021-09-24" "2021-09-24" "2021-09-24" "2021-09-24" ...
df %>% filter(country == "United Kingdom") %>%
filter(y2>='2018-01-01' & y2<='2018-01-31')
## show_id type title director
## 1 s5058 TV Show Retribution
## 2 s5075 Movie Bad Day for the Cut Chris Baugh
## 3 s5098 TV Show Lovesick
## 4 s7399 Movie Manolo: The Boy Who Made Shoes for Lizards Michael Roberts
## 5 s8358 TV Show The Inbetweeners
## 6 s8620 Movie Treasures from the Wreck of the Unbelievable Sam Hobkinson
## cast
## 1 Georgina Campbell, Joe Dempsie, Adrian Edmondson, Steve Evets, Laura Fraser, Julie Graham, John Lynch, Gary Lewis, Juliet Stevenson, Joanna Vanderham
## 2 Nigel O'Neill, Susan Lynch, J?zef Pawlowski, Stuart Graham, David Pearse, Brian Milligan, Anna Prochniak, Stella McCusker, Ian McElhinney, Lalor Roddy
## 3 Johnny Flynn, Antonia Thomas, Daniel Ings, Hannah Britland, Joshua McGuire, Richard Thomson, Jessica Ellerby
## 4 Manolo Blahnik, Anna Wintour, Andr? Leon Talley, Paloma Picasso, Candace Bushnell, Iman, Rihanna, Naomi Campbell, Isaac Mizrahi, Rupert Everett, Sofia Coppola, John Galliano
## 5 Simon Bird, James Buckley, Blake Harrison, Joe Thomas, Greg Davies, Emily Head, Martin Trenaman, Belinda Stewart-Wilson, Robin Weaver, Henry Lloyd-Hughes
## 6
## country date_added release_year rating duration
## 1 United Kingdom 30-Jan-18 2016 TV-MA 1 Season
## 2 United Kingdom 18-Jan-18 2017 TV-MA 99 min
## 3 United Kingdom 01-Jan-18 2018 TV-MA 3 Seasons
## 4 United Kingdom 15-Jan-18 2017 TV-MA 89 min
## 5 United Kingdom January 1, 2018 2010 TV-MA 3 Seasons
## 6 United Kingdom 01-Jan-18 2017 TV-G 82 min
## listed_in y2
## 1 British TV Shows, Crime TV Shows, International TV Shows 2018-01-30
## 2 Independent Movies, International Movies, Thrillers 2018-01-18
## 3 British TV Shows, International TV Shows, Romantic TV Shows 2018-01-01
## 4 Documentaries, International Movies 2018-01-15
## 5 British TV Shows, TV Comedies 2018-01-01
## 6 Documentaries, Dramas, International Movies 2018-01-01
### 240131 15:59 실습과제
#원칙 1. 데이터명 Data1.csv
# 2. 목표변수 Gender
# 3. 목표변수 factor
# 4. 데이터전처리는 자유(z, min/max)
# 5. 분석기법 자유
# 6. predict 실제값이랑 예측값 혼동행렬렬
# 목표변수 Gender의 factor
library(dplyr)
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
df<-read.csv("Data1.csv")
df$Gender<-as.factor(df$Gender)
df$Gender<-revalue(df$Gender, replace = c("0"="female", "1"="male"))
glimpse(df)
## Rows: 1,925
## Columns: 27
## $ X <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
## $ Q1 <int> 4, 4, 4, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, …
## $ Q2 <int> 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 2, 2, …
## $ Q3 <int> 2, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 4, 4, 3, 2, 3, …
## $ Q4 <int> 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 2, 4, 4, 4, 2, 2, 4, …
## $ Q5 <int> 4, 4, 2, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 3, 1, 2, …
## $ Q6 <int> 2, 3, 4, 4, 4, 4, 4, 4, 1, 2, 2, 2, 4, 4, 3, 5, 2, 2, 1, 4, …
## $ Q7 <int> 2, 2, 4, 4, 4, 4, 4, 4, 3, 4, 4, 4, 5, 4, 4, 5, 4, 3, 4, 4, …
## $ Q8 <int> 4, 4, 4, 4, 4, 4, 5, 5, 2, 2, 4, 4, 4, 4, 3, 5, 4, 2, 4, 4, …
## $ Q9 <int> 4, 4, 4, 4, 2, 4, 5, 5, 3, 4, 4, 4, 2, 2, 4, 5, 2, 4, 2, 4, …
## $ Q10 <int> 4, 4, 2, 4, 4, 4, 5, 5, 2, 4, 2, 4, 4, 4, 3, 4, 4, 3, 2, 3, …
## $ Q11 <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 3, 4, 4, 4, 4, 5, 4, 3, 3, …
## $ Q12 <int> 4, 4, 4, 4, 4, 4, 5, 5, 3, 4, 4, 3, 4, 3, 3, 4, 5, 4, 4, 2, …
## $ Q13 <int> 4, 4, 4, 4, 4, 4, 5, 5, 4, 4, 4, 4, 2, 4, 4, 4, 5, 4, 4, 4, …
## $ Q14 <int> 4, 4, 4, 4, 4, 4, 5, 5, 5, 4, 4, 4, 3, 4, 5, 4, 5, 4, 4, 4, …
## $ Q15 <int> 4, 4, 3, 4, 4, 4, 4, 2, 3, 4, 4, 3, 1, 4, 4, 4, 5, 4, 4, 4, …
## $ Q16 <int> 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, 4, 4, 4, 5, 4, 5, 4, 4, 4, …
## $ Q17 <int> 4, 3, 4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 3, 2, 4, 5, 4, 4, 3, 4, …
## $ Q18 <int> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 2, 4, 4, 4, …
## $ Q19 <int> 4, 2, 4, 4, 4, 4, 4, 2, 4, 2, 4, 4, 1, 4, 4, 4, 5, 4, 2, 3, …
## $ Q20 <int> 4, 1, 3, 4, 4, 4, 4, 2, 4, 2, 4, 4, 4, 2, 4, 5, 5, 4, 2, 4, …
## $ Gender <fct> female, female, female, female, female, female, female, fema…
## $ EDU <int> 1, 1, 2, 1, 2, 1, 1, 1, 4, 3, 2, 1, 1, 3, 3, 2, 1, 1, 1, 4, …
## $ BF <dbl> 3.4, 4.0, 3.6, 4.2, 4.0, 4.0, 3.6, 3.6, 3.6, 3.2, 4.0, 3.2, …
## $ BM <dbl> 3.2, 3.4, 3.6, 4.0, 3.6, 4.0, 4.6, 4.6, 2.2, 3.2, 3.2, 3.6, …
## $ Happiness <dbl> 4.0, 4.0, 3.8, 4.0, 4.0, 4.0, 4.8, 4.4, 3.8, 4.0, 4.0, 3.4, …
## $ Peace <dbl> 4.0, 2.8, 3.8, 4.0, 4.0, 4.0, 3.8, 2.4, 4.0, 3.2, 4.0, 3.9, …
table(df$Gender)
##
## female male
## 1136 789
# 랜덤포레스트로 결과 보기(정규화Z, repeatedcv, 그리드서치 사용용)
library(caret)
library(mlbench)
set.seed(998) # 결과의 재현성을 위해
inTraining <- createDataPartition(y=df$Gender, p=0.8, list=FALSE)
training <- df[inTraining, ]
testing <- df[-inTraining, ]
# trainControl 설정
fitControl <- trainControl(method="repeatedcv", number=2, repeats=2,
classProbs=TRUE, # 혼동 행렬을 위해 클래스 확률 계산 필요
summaryFunction=twoClassSummary,
savePredictions=TRUE)
# 모델 훈련을 위한 그리드 서치 설정
grid <- expand.grid(mtry=1:2) # mtry 값에 대한 간단한 그리드
# 랜덤 포레스트 모델 훈련
set.seed(998) # 결과의 재현성을 위해
rffit <- train(Gender~., data=training, method="rf",
tuneGrid=grid,
trControl=fitControl,
preProcess=c("center", "scale"),# 정규화 적용
verbose = FALSE, # 훈련 과정 중 상세한 출력을 억제합니다
metric="ROC")
# 결과 출력
print(rffit)
## Random Forest
##
## 1541 samples
## 26 predictor
## 2 classes: 'female', 'male'
##
## Pre-processing: centered (26), scaled (26)
## Resampling: Cross-Validated (2 fold, repeated 2 times)
## Summary of sample sizes: 770, 771, 771, 770
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 1 0.6479403 0.8646924 0.2642405
## 2 0.6481700 0.8239834 0.3433544
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
# 예측 및 성능 평가
predictions_rf <- predict(rffit, testing)
confusionMatrix(predictions_rf, testing$Gender) # 혼동 행렬
## Confusion Matrix and Statistics
##
## Reference
## Prediction female male
## female 183 94
## male 44 63
##
## Accuracy : 0.6406
## 95% CI : (0.5904, 0.6887)
## No Information Rate : 0.5911
## P-Value [Acc > NIR] : 0.0268
##
## Kappa : 0.2182
##
## Mcnemar's Test P-Value : 3.031e-05
##
## Sensitivity : 0.8062
## Specificity : 0.4013
## Pos Pred Value : 0.6606
## Neg Pred Value : 0.5888
## Prevalence : 0.5911
## Detection Rate : 0.4766
## Detection Prevalence : 0.7214
## Balanced Accuracy : 0.6037
##
## 'Positive' Class : female
##
### glmnet으로 수행하기기
glfit <- train(Gender~.,data=training,
method="glmnet",
tuneLength = 20,
trControl = fitControl,
verbose = FALSE,
metric = "ROC")
print(glfit) %>% head
## glmnet
##
## 1541 samples
## 26 predictor
## 2 classes: 'female', 'male'
##
## No pre-processing
## Resampling: Cross-Validated (2 fold, repeated 2 times)
## Summary of sample sizes: 770, 771, 770, 771
## Resampling results across tuning parameters:
##
## alpha lambda ROC Sens Spec
## 0.1000000 0.0004813674 0.6172949 0.7448177 0.379746835
## 0.1000000 0.0007463678 0.6172949 0.7448177 0.379746835
## 0.1000000 0.0011572550 0.6172949 0.7448177 0.379746835
## 0.1000000 0.0017943422 0.6172949 0.7448177 0.379746835
## 0.1000000 0.0027821558 0.6173627 0.7453672 0.380537975
## 0.1000000 0.0043137765 0.6179181 0.7486687 0.378164557
## 0.1000000 0.0066885786 0.6187399 0.7536186 0.375000000
## 0.1000000 0.0103707466 0.6199046 0.7569190 0.369462025
## 0.1000000 0.0160800063 0.6213670 0.7629702 0.358386076
## 0.1000000 0.0249323034 0.6230785 0.7745220 0.344936709
## 0.1000000 0.0386579299 0.6256029 0.7904705 0.327531646
## 0.1000000 0.0599397303 0.6284281 0.8207145 0.294303797
## 0.1000000 0.0929374977 0.6303473 0.8548156 0.250791139
## 0.1473684 0.0004813674 0.6172548 0.7448177 0.379746835
## 0.1473684 0.0007463678 0.6172548 0.7448177 0.379746835
## 0.1473684 0.0011572550 0.6172548 0.7448177 0.379746835
## 0.1473684 0.0017943422 0.6172548 0.7448177 0.379746835
## 0.1473684 0.0027821558 0.6174915 0.7459166 0.378955696
## 0.1473684 0.0043137765 0.6181620 0.7486675 0.377373418
## 0.1473684 0.0066885786 0.6190846 0.7541681 0.372626582
## 0.1473684 0.0103707466 0.6203364 0.7591204 0.366297468
## 0.1473684 0.0160800063 0.6217919 0.7679213 0.354430380
## 0.1473684 0.0249323034 0.6241637 0.7789236 0.340189873
## 0.1473684 0.0386579299 0.6269768 0.7998124 0.318829114
## 0.1473684 0.0599397303 0.6292613 0.8328182 0.281645570
## 0.1473684 0.0929374977 0.6301376 0.8856175 0.210443038
## 0.1947368 0.0004813674 0.6172461 0.7448177 0.378955696
## 0.1947368 0.0007463678 0.6172461 0.7448177 0.378955696
## 0.1947368 0.0011572550 0.6172461 0.7448177 0.378955696
## 0.1947368 0.0017943422 0.6172461 0.7448177 0.378955696
## 0.1947368 0.0027821558 0.6176396 0.7492182 0.376582278
## 0.1947368 0.0043137765 0.6184301 0.7492158 0.375000000
## 0.1947368 0.0066885786 0.6193648 0.7552682 0.371044304
## 0.1947368 0.0103707466 0.6207594 0.7607712 0.364715190
## 0.1947368 0.0160800063 0.6225583 0.7706685 0.344936709
## 0.1947368 0.0249323034 0.6251474 0.7838699 0.330696203
## 0.1947368 0.0386579299 0.6279809 0.8108099 0.308544304
## 0.1947368 0.0599397303 0.6293224 0.8465665 0.263449367
## 0.1947368 0.0929374977 0.6296968 0.9076124 0.174841772
## 0.2421053 0.0004813674 0.6171904 0.7448177 0.378955696
## 0.2421053 0.0007463678 0.6171904 0.7448177 0.378955696
## 0.2421053 0.0011572550 0.6171904 0.7448177 0.378955696
## 0.2421053 0.0017943422 0.6171991 0.7448177 0.378955696
## 0.2421053 0.0027821558 0.6178225 0.7481169 0.377373418
## 0.2421053 0.0043137765 0.6186129 0.7497664 0.374208861
## 0.2421053 0.0066885786 0.6196677 0.7558201 0.369462025
## 0.2421053 0.0103707466 0.6213044 0.7640703 0.360759494
## 0.2421053 0.0160800063 0.6233264 0.7739701 0.342563291
## 0.2421053 0.0249323034 0.6262800 0.7893632 0.328322785
## 0.2421053 0.0386579299 0.6282382 0.8190662 0.297468354
## 0.2421053 0.0599397303 0.6293651 0.8641744 0.238924051
## 0.2421053 0.0929374977 0.6283591 0.9378588 0.137658228
## 0.2894737 0.0004813674 0.6171590 0.7453672 0.378164557
## 0.2894737 0.0007463678 0.6171590 0.7453672 0.378164557
## 0.2894737 0.0011572550 0.6171590 0.7453672 0.378164557
## 0.2894737 0.0017943422 0.6173314 0.7470155 0.378164557
## 0.2894737 0.0027821558 0.6180000 0.7481157 0.378164557
## 0.2894737 0.0043137765 0.6188026 0.7530680 0.371835443
## 0.2894737 0.0066885786 0.6200526 0.7569190 0.369462025
## 0.2894737 0.0103707466 0.6217433 0.7651704 0.359968354
## 0.2894737 0.0160800063 0.6241047 0.7761691 0.341772152
## 0.2894737 0.0249323034 0.6270985 0.7954132 0.321993671
## 0.2894737 0.0386579299 0.6285817 0.8300649 0.285601266
## 0.2894737 0.0599397303 0.6289960 0.8845198 0.208860759
## 0.2894737 0.0929374977 0.6268310 0.9587561 0.085443038
## 0.3368421 0.0004813674 0.6171416 0.7459166 0.378164557
## 0.3368421 0.0007463678 0.6171416 0.7459166 0.378164557
## 0.3368421 0.0011572550 0.6171416 0.7459166 0.378164557
## 0.3368421 0.0017943422 0.6174428 0.7475662 0.378164557
## 0.3368421 0.0027821558 0.6181689 0.7486651 0.375791139
## 0.3368421 0.0043137765 0.6190672 0.7536186 0.371044304
## 0.3368421 0.0066885786 0.6204809 0.7591216 0.368670886
## 0.3368421 0.0103707466 0.6221999 0.7668200 0.351265823
## 0.3368421 0.0160800063 0.6249841 0.7772692 0.340189873
## 0.3368421 0.0249323034 0.6274849 0.8014656 0.318037975
## 0.3368421 0.0386579299 0.6286214 0.8383151 0.280854430
## 0.3368421 0.0599397303 0.6282259 0.8982633 0.185917722
## 0.3368421 0.0929374977 0.6251316 0.9736034 0.067246835
## 0.3842105 0.0004813674 0.6171643 0.7459166 0.378164557
## 0.3842105 0.0007463678 0.6171643 0.7459166 0.378164557
## 0.3842105 0.0011572550 0.6171643 0.7459166 0.378164557
## 0.3842105 0.0017943422 0.6175821 0.7475662 0.376582278
## 0.3842105 0.0027821558 0.6182804 0.7492158 0.375000000
## 0.3842105 0.0043137765 0.6192760 0.7541681 0.371835443
## 0.3842105 0.0066885786 0.6207961 0.7613230 0.367088608
## 0.3842105 0.0103707466 0.6227345 0.7690190 0.349683544
## 0.3842105 0.0160800063 0.6256040 0.7805695 0.336234177
## 0.3842105 0.0249323034 0.6274501 0.8091664 0.308544304
## 0.3842105 0.0386579299 0.6285249 0.8487704 0.268987342
## 0.3842105 0.0599397303 0.6276600 0.9175134 0.161392405
## 0.3842105 0.0929374977 0.6218007 0.9862504 0.041930380
## 0.4315789 0.0004813674 0.6171800 0.7459166 0.378955696
## 0.4315789 0.0007463678 0.6171800 0.7459166 0.378955696
## 0.4315789 0.0011572550 0.6171817 0.7459166 0.378955696
## 0.4315789 0.0017943422 0.6176954 0.7481157 0.378164557
## 0.4315789 0.0027821558 0.6183919 0.7497664 0.375000000
## 0.4315789 0.0043137765 0.6195354 0.7547187 0.372626582
## 0.4315789 0.0066885786 0.6212052 0.7624219 0.363132911
## 0.4315789 0.0103707466 0.6232814 0.7712216 0.346518987
## 0.4315789 0.0160800063 0.6263606 0.7849664 0.333069620
## 0.4315789 0.0249323034 0.6277782 0.8141163 0.299841772
## 0.4315789 0.0386579299 0.6283804 0.8608740 0.250000000
## 0.4315789 0.0599397303 0.6268559 0.9312582 0.142405063
## 0.4315789 0.0929374977 0.6184823 0.9928499 0.018987342
## 0.4789474 0.0004813674 0.6171573 0.7459166 0.378164557
## 0.4789474 0.0007463678 0.6171573 0.7459166 0.378164557
## 0.4789474 0.0011572550 0.6172235 0.7459166 0.378955696
## 0.4789474 0.0017943422 0.6178172 0.7475650 0.378164557
## 0.4789474 0.0027821558 0.6184858 0.7497664 0.372626582
## 0.4789474 0.0043137765 0.6198036 0.7552694 0.370253165
## 0.4789474 0.0066885786 0.6215171 0.7635208 0.362341772
## 0.4789474 0.0103707466 0.6237517 0.7717699 0.345727848
## 0.4789474 0.0160800063 0.6267558 0.7888149 0.329905063
## 0.4789474 0.0249323034 0.6278702 0.8185167 0.296677215
## 0.4789474 0.0386579299 0.6284387 0.8696761 0.234968354
## 0.4789474 0.0599397303 0.6260274 0.9510541 0.106803797
## 0.4789474 0.0929374977 0.6140897 0.9988999 0.006329114
## 0.5263158 0.0004813674 0.6172183 0.7459166 0.378955696
## 0.5263158 0.0007463678 0.6172183 0.7459166 0.378955696
## 0.5263158 0.0011572550 0.6172896 0.7453648 0.378955696
## 0.5263158 0.0017943422 0.6179391 0.7481157 0.378164557
## 0.5263158 0.0027821558 0.6186476 0.7508665 0.373417722
## 0.5263158 0.0043137765 0.6200213 0.7558201 0.369462025
## 0.5263158 0.0066885786 0.6217994 0.7640703 0.357594937
## 0.5263158 0.0103707466 0.6243724 0.7723193 0.344145570
## 0.5263158 0.0160800063 0.6268898 0.7937648 0.329113924
## 0.5263158 0.0249323034 0.6278282 0.8267645 0.291139241
## 0.5263158 0.0386579299 0.6275962 0.8845186 0.213607595
## 0.5263158 0.0599397303 0.6248310 0.9626071 0.084651899
## 0.5263158 0.0929374977 0.6104873 0.9994505 0.001582278
## 0.5736842 0.0004813674 0.6172148 0.7459166 0.378955696
## 0.5736842 0.0007463678 0.6172148 0.7459166 0.378955696
## 0.5736842 0.0011572550 0.6173715 0.7464661 0.378164557
## 0.5736842 0.0017943422 0.6180157 0.7486651 0.376582278
## 0.5736842 0.0027821558 0.6188427 0.7525185 0.373417722
## 0.5736842 0.0043137765 0.6203242 0.7580215 0.369462025
## 0.5736842 0.0066885786 0.6221252 0.7651704 0.356803797
## 0.5736842 0.0103707466 0.6249714 0.7761691 0.339398734
## 0.5736842 0.0160800063 0.6269995 0.7976158 0.327531646
## 0.5736842 0.0249323034 0.6279668 0.8344677 0.284018987
## 0.5736842 0.0386579299 0.6272129 0.8927652 0.195411392
## 0.5736842 0.0599397303 0.6228357 0.9719562 0.071993671
## 0.5736842 0.0929374977 0.6032908 1.0000000 0.000000000
## 0.6210526 0.0004813674 0.6171904 0.7459166 0.378955696
## 0.6210526 0.0007463678 0.6171904 0.7459166 0.378955696
## 0.6210526 0.0011572550 0.6174272 0.7481157 0.378164557
## 0.6210526 0.0017943422 0.6181133 0.7492158 0.375000000
## 0.6210526 0.0027821558 0.6190533 0.7530692 0.373417722
## 0.6210526 0.0043137765 0.6205907 0.7596723 0.367088608
## 0.6210526 0.0066885786 0.6224979 0.7662705 0.356012658
## 0.6210526 0.0103707466 0.6254468 0.7772692 0.337816456
## 0.6210526 0.0160800063 0.6271223 0.8036670 0.316455696
## 0.6210526 0.0249323034 0.6278675 0.8399683 0.279272152
## 0.6210526 0.0386579299 0.6270267 0.9059617 0.183544304
## 0.6210526 0.0599397303 0.6208406 0.9785545 0.062500000
## 0.6210526 0.0929374977 0.5992801 1.0000000 0.000000000
## 0.6684211 0.0004813674 0.6171956 0.7459166 0.378955696
## 0.6684211 0.0007463678 0.6171956 0.7459166 0.378955696
## 0.6684211 0.0011572550 0.6174830 0.7481157 0.378164557
## 0.6684211 0.0017943422 0.6181585 0.7497664 0.375000000
## 0.6684211 0.0027821558 0.6192134 0.7530692 0.373417722
## 0.6684211 0.0043137765 0.6208727 0.7613218 0.366297468
## 0.6684211 0.0066885786 0.6228028 0.7679225 0.350474684
## 0.6684211 0.0103707466 0.6258037 0.7805683 0.337025316
## 0.6684211 0.0160800063 0.6272232 0.8080675 0.313291139
## 0.6684211 0.0249323034 0.6278273 0.8476715 0.268196203
## 0.6684211 0.0386579299 0.6262678 0.9169616 0.168512658
## 0.6684211 0.0599397303 0.6185798 0.9873517 0.041930380
## 0.6684211 0.0929374977 0.5949853 1.0000000 0.000000000
## 0.7157895 0.0004813674 0.6171677 0.7459166 0.378955696
## 0.7157895 0.0007463678 0.6171677 0.7459166 0.378955696
## 0.7157895 0.0011572550 0.6175979 0.7481157 0.378164557
## 0.7157895 0.0017943422 0.6182351 0.7497664 0.375000000
## 0.7157895 0.0027821558 0.6193997 0.7552694 0.373417722
## 0.7157895 0.0043137765 0.6210400 0.7607724 0.366297468
## 0.7157895 0.0066885786 0.6231615 0.7695708 0.348892405
## 0.7157895 0.0103707466 0.6261014 0.7805695 0.337025316
## 0.7157895 0.0160800063 0.6274007 0.8097183 0.310126582
## 0.7157895 0.0249323034 0.6277096 0.8559254 0.259493671
## 0.7157895 0.0386579299 0.6259433 0.9213656 0.154272152
## 0.7157895 0.0599397303 0.6165559 0.9912027 0.026107595
## 0.7157895 0.0929374977 0.5884356 1.0000000 0.000000000
## 0.7631579 0.0004813674 0.6171991 0.7453660 0.378955696
## 0.7631579 0.0007463678 0.6171938 0.7453660 0.378955696
## 0.7631579 0.0011572550 0.6176727 0.7475650 0.378164557
## 0.7631579 0.0017943422 0.6182874 0.7497664 0.375000000
## 0.7631579 0.0027821558 0.6195059 0.7558201 0.371835443
## 0.7631579 0.0043137765 0.6212838 0.7613218 0.365506329
## 0.7631579 0.0066885786 0.6235081 0.7701215 0.348101266
## 0.7631579 0.0103707466 0.6263746 0.7844157 0.336234177
## 0.7631579 0.0160800063 0.6273744 0.8130174 0.306962025
## 0.7631579 0.0249323034 0.6273354 0.8641780 0.250791139
## 0.7631579 0.0386579299 0.6256620 0.9362056 0.130537975
## 0.7631579 0.0599397303 0.6136544 0.9983492 0.011075949
## 0.7631579 0.0929374977 0.5549099 1.0000000 0.000000000
## 0.8105263 0.0004813674 0.6172339 0.7448153 0.378955696
## 0.8105263 0.0007463678 0.6172304 0.7448153 0.378955696
## 0.8105263 0.0011572550 0.6177562 0.7475650 0.378164557
## 0.8105263 0.0017943422 0.6184231 0.7503159 0.373417722
## 0.8105263 0.0027821558 0.6197357 0.7563707 0.370253165
## 0.8105263 0.0043137765 0.6214425 0.7618713 0.361550633
## 0.8105263 0.0066885786 0.6238808 0.7701203 0.346518987
## 0.8105263 0.0103707466 0.6265296 0.7871654 0.337025316
## 0.8105263 0.0160800063 0.6274002 0.8157671 0.303006329
## 0.8105263 0.0249323034 0.6270238 0.8691279 0.240506329
## 0.8105263 0.0386579299 0.6245023 0.9466513 0.118670886
## 0.8105263 0.0599397303 0.6107497 0.9994505 0.004746835
## 0.8105263 0.0929374977 0.5136841 1.0000000 0.000000000
## 0.8578947 0.0004813674 0.6172148 0.7448153 0.378955696
## 0.8578947 0.0007463678 0.6172461 0.7448153 0.378955696
## 0.8578947 0.0011572550 0.6177980 0.7475650 0.378164557
## 0.8578947 0.0017943422 0.6185206 0.7508665 0.373417722
## 0.8578947 0.0027821558 0.6199099 0.7563707 0.370253165
## 0.8578947 0.0043137765 0.6216898 0.7629702 0.359177215
## 0.8578947 0.0066885786 0.6243066 0.7717699 0.345727848
## 0.8578947 0.0103707466 0.6265627 0.7915658 0.335443038
## 0.8578947 0.0160800063 0.6273861 0.8218171 0.297468354
## 0.8578947 0.0249323034 0.6269001 0.8768238 0.225474684
## 0.8578947 0.0386579299 0.6235774 0.9571053 0.097310127
## 0.8578947 0.0599397303 0.6095756 0.9994505 0.001582278
## 0.8578947 0.0929374977 0.5136841 1.0000000 0.000000000
## 0.9052632 0.0004813674 0.6171904 0.7448153 0.378955696
## 0.9052632 0.0007463678 0.6173070 0.7453648 0.378955696
## 0.9052632 0.0011572550 0.6178729 0.7475650 0.377373418
## 0.9052632 0.0017943422 0.6186459 0.7508665 0.373417722
## 0.9052632 0.0027821558 0.6201432 0.7574720 0.370253165
## 0.9052632 0.0043137765 0.6218727 0.7635196 0.359177215
## 0.9052632 0.0066885786 0.6246478 0.7739701 0.344145570
## 0.9052632 0.0103707466 0.6266847 0.7943143 0.333860759
## 0.9052632 0.0160800063 0.6274503 0.8267670 0.292721519
## 0.9052632 0.0249323034 0.6268190 0.8790192 0.219936709
## 0.9052632 0.0386579299 0.6217859 0.9637036 0.082278481
## 0.9052632 0.0599397303 0.6030369 1.0000000 0.000000000
## 0.9052632 0.0929374977 0.5000000 1.0000000 0.000000000
## 0.9526316 0.0004813674 0.6171834 0.7448153 0.378955696
## 0.9526316 0.0007463678 0.6173436 0.7470155 0.379746835
## 0.9526316 0.0011572550 0.6179130 0.7486651 0.376582278
## 0.9526316 0.0017943422 0.6187835 0.7508665 0.374208861
## 0.9526316 0.0027821558 0.6203539 0.7585722 0.369462025
## 0.9526316 0.0043137765 0.6221967 0.7640703 0.357594937
## 0.9526316 0.0066885786 0.6250290 0.7750678 0.340981013
## 0.9526316 0.0103707466 0.6267553 0.7965157 0.332278481
## 0.9526316 0.0160800063 0.6275387 0.8317193 0.288765823
## 0.9526316 0.0249323034 0.6265755 0.8911132 0.206487342
## 0.9526316 0.0386579299 0.6208159 0.9708549 0.075158228
## 0.9526316 0.0599397303 0.6001691 1.0000000 0.000000000
## 0.9526316 0.0929374977 0.5000000 1.0000000 0.000000000
## 1.0000000 0.0004813674 0.6171643 0.7448153 0.378955696
## 1.0000000 0.0007463678 0.6173697 0.7470155 0.378955696
## 1.0000000 0.0011572550 0.6179653 0.7492158 0.375000000
## 1.0000000 0.0017943422 0.6188252 0.7514172 0.374208861
## 1.0000000 0.0027821558 0.6204167 0.7591216 0.367879747
## 1.0000000 0.0043137765 0.6224493 0.7646210 0.356803797
## 1.0000000 0.0066885786 0.6252484 0.7750690 0.339398734
## 1.0000000 0.0103707466 0.6268126 0.7976170 0.327531646
## 1.0000000 0.0160800063 0.6274428 0.8361209 0.283227848
## 1.0000000 0.0249323034 0.6261732 0.8999092 0.192246835
## 1.0000000 0.0386579299 0.6198205 0.9741540 0.071202532
## 1.0000000 0.0599397303 0.5980941 1.0000000 0.000000000
## 1.0000000 0.0929374977 0.5000000 1.0000000 0.000000000
##
## ROC was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 0.1 and lambda = 0.0929375.
## alpha lambda ROC Sens Spec
## "0.1000000" "0.0004813674" "0.6172949" "0.7448177" "0.379746835"
## "0.1000000" "0.0007463678" "0.6172949" "0.7448177" "0.379746835"
## "0.1000000" "0.0011572550" "0.6172949" "0.7448177" "0.379746835"
## "0.1000000" "0.0017943422" "0.6172949" "0.7448177" "0.379746835"
## "0.1000000" "0.0027821558" "0.6173627" "0.7453672" "0.380537975"
## "0.1000000" "0.0043137765" "0.6179181" "0.7486687" "0.378164557"
# 예측 및 성능 평가
predictions_gl <- predict(glfit, testing)
confusionMatrix(predictions_gl, testing$Gender) # 혼동 행렬
## Confusion Matrix and Statistics
##
## Reference
## Prediction female male
## female 203 127
## male 24 30
##
## Accuracy : 0.6068
## 95% CI : (0.5559, 0.6559)
## No Information Rate : 0.5911
## P-Value [Acc > NIR] : 0.2848
##
## Kappa : 0.095
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.8943
## Specificity : 0.1911
## Pos Pred Value : 0.6152
## Neg Pred Value : 0.5556
## Prevalence : 0.5911
## Detection Rate : 0.5286
## Detection Prevalence : 0.8594
## Balanced Accuracy : 0.5427
##
## 'Positive' Class : female
##
## 그래프로 중요도 확인인
# 그래픽 파라미터 설정: 1행 2열의 레이아웃
plot(varImp(rffit, scale = FALSE), top = 10, main = "Variable Importance for rffit")

plot(varImp(glfit, scale = FALSE), top = 10, main = "Variable Importance for glfit")

model_list <- list(glmnet = glfit, randomForest = rffit)
resamples <- resamples(model_list)
summary(resamples)
##
## Call:
## summary.resamples(object = resamples)
##
## Models: glmnet, randomForest
## Number of resamples: 4
##
## ROC
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.6172486 0.6264811 0.6318053 0.6303473 0.6356715 0.6405300 0
## randomForest 0.6393032 0.6431736 0.6466478 0.6481700 0.6516442 0.6600814 0
##
## Sens
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.8109890 0.8357143 0.8570000 0.8548156 0.8761013 0.8942731 0
## randomForest 0.8131868 0.8178341 0.8248899 0.8239834 0.8310391 0.8329670 0
##
## Spec
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## glmnet 0.2151899 0.2270570 0.2452532 0.2507911 0.2689873 0.2974684 0
## randomForest 0.3164557 0.3330696 0.3465190 0.3433544 0.3568038 0.3639241 0
## rf 모델과 glm 모델 혼동행렬 결과 데이터 및 그래프 비교
cm_rf <- confusionMatrix(predictions_rf, testing$Gender)
print(cm_rf)
## Confusion Matrix and Statistics
##
## Reference
## Prediction female male
## female 183 94
## male 44 63
##
## Accuracy : 0.6406
## 95% CI : (0.5904, 0.6887)
## No Information Rate : 0.5911
## P-Value [Acc > NIR] : 0.0268
##
## Kappa : 0.2182
##
## Mcnemar's Test P-Value : 3.031e-05
##
## Sensitivity : 0.8062
## Specificity : 0.4013
## Pos Pred Value : 0.6606
## Neg Pred Value : 0.5888
## Prevalence : 0.5911
## Detection Rate : 0.4766
## Detection Prevalence : 0.7214
## Balanced Accuracy : 0.6037
##
## 'Positive' Class : female
##
cm_gl <- confusionMatrix(predictions_gl, testing$Gender)
print(cm_gl)
## Confusion Matrix and Statistics
##
## Reference
## Prediction female male
## female 203 127
## male 24 30
##
## Accuracy : 0.6068
## 95% CI : (0.5559, 0.6559)
## No Information Rate : 0.5911
## P-Value [Acc > NIR] : 0.2848
##
## Kappa : 0.095
##
## Mcnemar's Test P-Value : <2e-16
##
## Sensitivity : 0.8943
## Specificity : 0.1911
## Pos Pred Value : 0.6152
## Neg Pred Value : 0.5556
## Prevalence : 0.5911
## Detection Rate : 0.5286
## Detection Prevalence : 0.8594
## Balanced Accuracy : 0.5427
##
## 'Positive' Class : female
##
performance_metrics <- data.frame(
Model = c("Random Forest", "GLMNET"),
Accuracy = c(cm_rf$overall['Accuracy'], cm_gl$overall['Accuracy']),
Sensitivity = c(cm_rf$byClass['Sensitivity'], cm_gl$byClass['Sensitivity']),
Specificity = c(cm_rf$byClass['Specificity'], cm_gl$byClass['Specificity'])
)
library(tidyr)
##
## Attaching package: 'tidyr'
## The following objects are masked from 'package:Matrix':
##
## expand, pack, unpack
performance_long <- performance_metrics %>%
pivot_longer(-Model, names_to = "Metric", values_to = "Value")
library(ggplot2)
ggplot(performance_long, aes(x = Model, y = Value, fill = Metric)) +
geom_bar(stat = "identity", position = position_dodge()) +
theme_minimal() +
labs(title = "Model Performance Comparison",
y = "Metric Value",
x = NULL) +
scale_fill_brewer(palette = "Pastel1") +
geom_text(aes(label = sprintf("%.2f", Value)),
position = position_dodge(width = 0.9), vjust = -0.25)
