#R스튜디오 시작과 함께 진행
getwd()
## [1] "C:/data"
setwd('c:/data')
getwd()
## [1] "c:/data"
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
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))

set.seed(1)
data(iris)
idx<-createDataPartition(iris$Species, p=0.8)
idx$Resample1
## [1] 1 3 4 6 7 8 9 10 12 14 15 18 19 20 21 22 23 24
## [19] 25 26 27 29 31 32 33 34 35 36 37 38 39 40 41 42 43 45
## [37] 46 47 48 49 51 52 53 56 57 59 60 61 62 63 64 66 67 68
## [55] 69 70 72 73 74 75 76 77 78 82 83 84 85 86 87 88 89 90
## [73] 91 92 93 95 96 97 99 100 101 102 103 105 106 107 109 110 111 112
## [91] 114 115 116 117 119 120 122 124 125 126 127 128 129 130 131 132 133 134
## [109] 135 136 137 138 139 140 142 143 145 146 147 149
table(iris[idx$Resample1,"Species"])
##
## setosa versicolor virginica
## 40 40 40
table(iris[-idx$Resample1,"Species"])
##
## setosa versicolor virginica
## 10 10 10
idx<-createDataPartition(iris$Species, p=0.8,list=FALSE)
train<-iris[idx,]
test<-iris[-idx,]
nrow(train)
## [1] 120
nrow(test)
## [1] 30
120/(120+30)
## [1] 0.8
library(caret)
set.seed(2)
data(iris)
iris
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 7 4.6 3.4 1.4 0.3 setosa
## 8 5.0 3.4 1.5 0.2 setosa
## 9 4.4 2.9 1.4 0.2 setosa
## 10 4.9 3.1 1.5 0.1 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 12 4.8 3.4 1.6 0.2 setosa
## 13 4.8 3.0 1.4 0.1 setosa
## 14 4.3 3.0 1.1 0.1 setosa
## 15 5.8 4.0 1.2 0.2 setosa
## 16 5.7 4.4 1.5 0.4 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 18 5.1 3.5 1.4 0.3 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 21 5.4 3.4 1.7 0.2 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 24 5.1 3.3 1.7 0.5 setosa
## 25 4.8 3.4 1.9 0.2 setosa
## 26 5.0 3.0 1.6 0.2 setosa
## 27 5.0 3.4 1.6 0.4 setosa
## 28 5.2 3.5 1.5 0.2 setosa
## 29 5.2 3.4 1.4 0.2 setosa
## 30 4.7 3.2 1.6 0.2 setosa
## 31 4.8 3.1 1.6 0.2 setosa
## 32 5.4 3.4 1.5 0.4 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 34 5.5 4.2 1.4 0.2 setosa
## 35 4.9 3.1 1.5 0.2 setosa
## 36 5.0 3.2 1.2 0.2 setosa
## 37 5.5 3.5 1.3 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 39 4.4 3.0 1.3 0.2 setosa
## 40 5.1 3.4 1.5 0.2 setosa
## 41 5.0 3.5 1.3 0.3 setosa
## 42 4.5 2.3 1.3 0.3 setosa
## 43 4.4 3.2 1.3 0.2 setosa
## 44 5.0 3.5 1.6 0.6 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 46 4.8 3.0 1.4 0.3 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 48 4.6 3.2 1.4 0.2 setosa
## 49 5.3 3.7 1.5 0.2 setosa
## 50 5.0 3.3 1.4 0.2 setosa
## 51 7.0 3.2 4.7 1.4 versicolor
## 52 6.4 3.2 4.5 1.5 versicolor
## 53 6.9 3.1 4.9 1.5 versicolor
## 54 5.5 2.3 4.0 1.3 versicolor
## 55 6.5 2.8 4.6 1.5 versicolor
## 56 5.7 2.8 4.5 1.3 versicolor
## 57 6.3 3.3 4.7 1.6 versicolor
## 58 4.9 2.4 3.3 1.0 versicolor
## 59 6.6 2.9 4.6 1.3 versicolor
## 60 5.2 2.7 3.9 1.4 versicolor
## 61 5.0 2.0 3.5 1.0 versicolor
## 62 5.9 3.0 4.2 1.5 versicolor
## 63 6.0 2.2 4.0 1.0 versicolor
## 64 6.1 2.9 4.7 1.4 versicolor
## 65 5.6 2.9 3.6 1.3 versicolor
## 66 6.7 3.1 4.4 1.4 versicolor
## 67 5.6 3.0 4.5 1.5 versicolor
## 68 5.8 2.7 4.1 1.0 versicolor
## 69 6.2 2.2 4.5 1.5 versicolor
## 70 5.6 2.5 3.9 1.1 versicolor
## 71 5.9 3.2 4.8 1.8 versicolor
## 72 6.1 2.8 4.0 1.3 versicolor
## 73 6.3 2.5 4.9 1.5 versicolor
## 74 6.1 2.8 4.7 1.2 versicolor
## 75 6.4 2.9 4.3 1.3 versicolor
## 76 6.6 3.0 4.4 1.4 versicolor
## 77 6.8 2.8 4.8 1.4 versicolor
## 78 6.7 3.0 5.0 1.7 versicolor
## 79 6.0 2.9 4.5 1.5 versicolor
## 80 5.7 2.6 3.5 1.0 versicolor
## 81 5.5 2.4 3.8 1.1 versicolor
## 82 5.5 2.4 3.7 1.0 versicolor
## 83 5.8 2.7 3.9 1.2 versicolor
## 84 6.0 2.7 5.1 1.6 versicolor
## 85 5.4 3.0 4.5 1.5 versicolor
## 86 6.0 3.4 4.5 1.6 versicolor
## 87 6.7 3.1 4.7 1.5 versicolor
## 88 6.3 2.3 4.4 1.3 versicolor
## 89 5.6 3.0 4.1 1.3 versicolor
## 90 5.5 2.5 4.0 1.3 versicolor
## 91 5.5 2.6 4.4 1.2 versicolor
## 92 6.1 3.0 4.6 1.4 versicolor
## 93 5.8 2.6 4.0 1.2 versicolor
## 94 5.0 2.3 3.3 1.0 versicolor
## 95 5.6 2.7 4.2 1.3 versicolor
## 96 5.7 3.0 4.2 1.2 versicolor
## 97 5.7 2.9 4.2 1.3 versicolor
## 98 6.2 2.9 4.3 1.3 versicolor
## 99 5.1 2.5 3.0 1.1 versicolor
## 100 5.7 2.8 4.1 1.3 versicolor
## 101 6.3 3.3 6.0 2.5 virginica
## 102 5.8 2.7 5.1 1.9 virginica
## 103 7.1 3.0 5.9 2.1 virginica
## 104 6.3 2.9 5.6 1.8 virginica
## 105 6.5 3.0 5.8 2.2 virginica
## 106 7.6 3.0 6.6 2.1 virginica
## 107 4.9 2.5 4.5 1.7 virginica
## 108 7.3 2.9 6.3 1.8 virginica
## 109 6.7 2.5 5.8 1.8 virginica
## 110 7.2 3.6 6.1 2.5 virginica
## 111 6.5 3.2 5.1 2.0 virginica
## 112 6.4 2.7 5.3 1.9 virginica
## 113 6.8 3.0 5.5 2.1 virginica
## 114 5.7 2.5 5.0 2.0 virginica
## 115 5.8 2.8 5.1 2.4 virginica
## 116 6.4 3.2 5.3 2.3 virginica
## 117 6.5 3.0 5.5 1.8 virginica
## 118 7.7 3.8 6.7 2.2 virginica
## 119 7.7 2.6 6.9 2.3 virginica
## 120 6.0 2.2 5.0 1.5 virginica
## 121 6.9 3.2 5.7 2.3 virginica
## 122 5.6 2.8 4.9 2.0 virginica
## 123 7.7 2.8 6.7 2.0 virginica
## 124 6.3 2.7 4.9 1.8 virginica
## 125 6.7 3.3 5.7 2.1 virginica
## 126 7.2 3.2 6.0 1.8 virginica
## 127 6.2 2.8 4.8 1.8 virginica
## 128 6.1 3.0 4.9 1.8 virginica
## 129 6.4 2.8 5.6 2.1 virginica
## 130 7.2 3.0 5.8 1.6 virginica
## 131 7.4 2.8 6.1 1.9 virginica
## 132 7.9 3.8 6.4 2.0 virginica
## 133 6.4 2.8 5.6 2.2 virginica
## 134 6.3 2.8 5.1 1.5 virginica
## 135 6.1 2.6 5.6 1.4 virginica
## 136 7.7 3.0 6.1 2.3 virginica
## 137 6.3 3.4 5.6 2.4 virginica
## 138 6.4 3.1 5.5 1.8 virginica
## 139 6.0 3.0 4.8 1.8 virginica
## 140 6.9 3.1 5.4 2.1 virginica
## 141 6.7 3.1 5.6 2.4 virginica
## 142 6.9 3.1 5.1 2.3 virginica
## 143 5.8 2.7 5.1 1.9 virginica
## 144 6.8 3.2 5.9 2.3 virginica
## 145 6.7 3.3 5.7 2.5 virginica
## 146 6.7 3.0 5.2 2.3 virginica
## 147 6.3 2.5 5.0 1.9 virginica
## 148 6.5 3.0 5.2 2.0 virginica
## 149 6.2 3.4 5.4 2.3 virginica
## 150 5.9 3.0 5.1 1.8 virginica
idx<-createDataPartition(iris$Species,p=0.6,list=FALSE)
train<-iris[idx,]
valid_test<-iris[-idx,]
idx2<-createDataPartition(valid_test$Species,p=0.5,list=FALSE)
valid<-valid_test[idx2,]
test<-valid_test[-idx2,]
nrow(train)/nrow(iris)
## [1] 0.6
nrow(valid)/nrow(iris)
## [1] 0.2
nrow(test)/nrow(iris)
## [1] 0.2
zs_iris<-preProcess(iris,method=c("center","scale"))
zs_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - centered (4)
## - ignored (1)
## - scaled (4)
zs_iris_p<-predict(zs_iris,iris)
library(psych)
##
## 다음의 패키지를 부착합니다: '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
library(caret)
data(iris)
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
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
par(mfrow=c(1,2))
plot(density(x=iris$Sepal.Width))
plot(density(x=bc_iris_t$Sepal.Width))

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
library(earth)
## 필요한 패키지를 로딩중입니다: Formula
## 필요한 패키지를 로딩중입니다: plotmo
## 필요한 패키지를 로딩중입니다: plotrix
##
## 다음의 패키지를 부착합니다: 'plotrix'
## The following object is masked from 'package:psych':
##
## rescale
## 필요한 패키지를 로딩중입니다: TeachingDemos
data(etitanic)
library(dplyr)
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
data(airquality)
colSums(is.na(airquality))
## 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
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
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_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
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=BostonHousing$medv,plot="scatter",layout=c(3,1))

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~
control<-trainControl(method="repeatedcv",number=10,repeats=3)
model<-train(diabetes~.,data=PimaIndiansDiabetes,method="rf",preProcess=c("center","scale"),trControl=control)
1
## [1] 1
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## 다음의 패키지를 부착합니다: 'randomForest'
## The following object is masked from 'package:psych':
##
## outlier
## The following object is masked from 'package:ggplot2':
##
## margin
## The following object is masked from 'package:dplyr':
##
## combine
model
## Random Forest
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## Pre-processing: centered (8), scaled (8)
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 692, 691, 691, 691, 691, 692, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.7707849 0.4799021
## 5 0.7647642 0.4723862
## 8 0.7642914 0.4712898
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
importance<-varImp(model,scale=FALSE)
print(importance)
## rf variable importance
##
## Overall
## glucose 88.46
## mass 57.54
## age 48.36
## pedigree 43.37
## pressure 30.84
## pregnant 29.09
## insulin 25.25
## triceps 24.10
plot(importance)

model1<-train(diabetes~.,data=PimaIndiansDiabetes,method="knn",preProcess=c("center","scale"),trControl=control)
model1
## k-Nearest Neighbors
##
## 768 samples
## 8 predictor
## 2 classes: 'neg', 'pos'
##
## Pre-processing: centered (8), scaled (8)
## Resampling: Cross-Validated (10 fold, repeated 3 times)
## Summary of sample sizes: 691, 691, 691, 692, 691, 691, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.7329631 0.3900620
## 7 0.7352187 0.3984398
## 9 0.7356402 0.3940878
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 9.
data(Sonar)
set.seed(998)
inTraining<-createDataPartition(y=Sonar$Class,p=.65,list=FALSE)
training<-Sonar[inTraining,]
testing<-Sonar[-inTraining,]
nrow(training)
## [1] 137
nrow(testing)
## [1] 71
set.seed(825)
rffit<-train(Class~.,data=training,method="rf",preProc=c("center","scale"))
rffit
## Random Forest
##
## 137 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 137, 137, 137, 137, 137, 137, ...
## Resampling results across tuning parameters:
##
## mtry Accuracy Kappa
## 2 0.7728421 0.5369022
## 31 0.7554612 0.4988887
## 60 0.7607071 0.5100324
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
rffit1<-train(Class~.,data=training,method="knn",preProc=c("center","scale"))
rffit1
## k-Nearest Neighbors
##
## 137 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 137, 137, 137, 137, 137, 137, ...
## Resampling results across tuning parameters:
##
## k Accuracy Kappa
## 5 0.7430670 0.4709197
## 7 0.7488191 0.4841222
## 9 0.7506448 0.4883310
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 9.
rffit2<-train(Class~.,data=training,method="rpart",preProc=c("center","scale"))
rffit2
## CART
##
## 137 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## Pre-processing: centered (60), scaled (60)
## Resampling: Bootstrapped (25 reps)
## Summary of sample sizes: 137, 137, 137, 137, 137, 137, ...
## Resampling results across tuning parameters:
##
## cp Accuracy Kappa
## 0.031250 0.7149251 0.4304315
## 0.109375 0.7197860 0.4363966
## 0.546875 0.6511694 0.2974635
##
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.109375.
fitControl<-trainControl(method="cv",number=5,classProbs = TRUE,summaryFunction = twoClassSummary)
set.seed(825)
rmfit1<-train(Class~.,data=training,method="rf",trControl=fitControl,verbose=FALSE,metric="ROC")
rmfit1
## Random Forest
##
## 137 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 109, 111, 109, 110, 109
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.9083883 0.8761905 0.6564103
## 31 0.8700824 0.8628571 0.6243590
## 60 0.8487088 0.8619048 0.6243590
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
customGrid<-expand.grid(mtry=1:10)
rffit2<-train(Class~.,data=training,method="rf",trControl=fitControl,tuneGrid=customGrid,metric="ROC")
rffit2
## Random Forest
##
## 137 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 109, 109, 110, 110, 110
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 1 0.9091392 0.8609524 0.6717949
## 2 0.9084982 0.8466667 0.6397436
## 3 0.9115140 0.8609524 0.6551282
## 4 0.9169475 0.8466667 0.6692308
## 5 0.8979121 0.8333333 0.6871795
## 6 0.8955067 0.8466667 0.6551282
## 7 0.8991331 0.8476190 0.6538462
## 8 0.9007326 0.8476190 0.6551282
## 9 0.8988462 0.8476190 0.6384615
## 10 0.8887118 0.8333333 0.6846154
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 4.
rffit4<-train(Class~.,data=training,method="rf",trControl=fitControl,tuneLength=20)
## Warning in train.default(x, y, weights = w, ...): The metric "Accuracy" was not
## in the result set. ROC will be used instead.
rffit4
## Random Forest
##
## 137 samples
## 60 predictor
## 2 classes: 'M', 'R'
##
## No pre-processing
## Resampling: Cross-Validated (5 fold)
## Summary of sample sizes: 110, 110, 109, 110, 109
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.9217033 0.8914286 0.6858974
## 5 0.9152381 0.8780952 0.7025641
## 8 0.9246886 0.8780952 0.7038462
## 11 0.9159707 0.8780952 0.6717949
## 14 0.9114042 0.8657143 0.7051282
## 17 0.9083516 0.8647619 0.6884615
## 20 0.9174481 0.8504762 0.7346154
## 23 0.9041575 0.8514286 0.7038462
## 26 0.8991819 0.8638095 0.7205128
## 29 0.9018926 0.8657143 0.7358974
## 32 0.8959096 0.8514286 0.7205128
## 35 0.8940659 0.8647619 0.7205128
## 38 0.8898535 0.8647619 0.7192308
## 41 0.8989255 0.8504762 0.7358974
## 44 0.8898901 0.8647619 0.7512821
## 47 0.8897070 0.8647619 0.7358974
## 50 0.8896398 0.8647619 0.7358974
## 53 0.8966911 0.8647619 0.7051282
## 56 0.8860195 0.8647619 0.7205128
## 60 0.8910745 0.8647619 0.7666667
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 8.
predict(rmfit1,newdata=head(testing))
## [1] R R M M R R
## Levels: M R
predict(rmfit1,newdata=head(testing),type='prob')
## M R
## 4 0.448 0.552
## 6 0.420 0.580
## 7 0.598 0.402
## 8 0.620 0.380
## 10 0.266 0.734
## 11 0.098 0.902
predict(rmfit1,newdata=testing) %>% confusionMatrix(testing$Class)
## Confusion Matrix and Statistics
##
## Reference
## Prediction M R
## M 33 8
## R 5 25
##
## Accuracy : 0.8169
## 95% CI : (0.7073, 0.8987)
## No Information Rate : 0.5352
## P-Value [Acc > NIR] : 6.544e-07
##
## Kappa : 0.6298
##
## Mcnemar's Test P-Value : 0.5791
##
## Sensitivity : 0.8684
## Specificity : 0.7576
## Pos Pred Value : 0.8049
## Neg Pred Value : 0.8333
## Prevalence : 0.5352
## Detection Rate : 0.4648
## Detection Prevalence : 0.5775
## Balanced Accuracy : 0.8130
##
## 'Positive' Class : M
##
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~
df<-df %>% select(-id)
df$diagnosis<-as.factor(df$diagnosis)
prop.table(table(df$diagnosis))
##
## B M
## 0.6274165 0.3725835
any(is.na(df))
## [1] FALSE
set.seed(3)
inTraining<-createDataPartition(y=df$diagnosis,p=.75,list=FALSE)
training<-df[inTraining,]
testing<-df[-inTraining,]
my_trainControl<-trainControl(method="cv",number=5,classProbs=TRUE,summaryFunction =twoClassSummary)
glmnetfit<-train(diagnosis~.,data=training,method="glmnet",tuneLenth=20,trControl=fitControl,verbose=FALSE,metric="ROC")
library(lubridate)
##
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
library(nycflights13)
today()
## [1] "2024-01-31"
now()
## [1] "2024-01-31 17:11:53 KST"
ymd("2023-01-31")
## [1] "2023-01-31"
mdy("january 1st, 2023")
## [1] "2023-01-01"
dmy("31-jan-2023")
## [1] "2023-01-31"
ymd(20230131)
## [1] "2023-01-31"
ymd_hms("2017-01-31 20:11:59")
## [1] "2017-01-31 20:11:59 UTC"
mdy_hm("01/31/2017 08:01")
## [1] "2017-01-31 08:01: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)
## # A tibble: 336,776 x 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
## 7 2013 1 1 6 0
## 8 2013 1 1 6 0
## 9 2013 1 1 6 0
## 10 2013 1 1 6 0
## # i 336,766 more rows
flights %>% select(year,month,day,hour,minute) %>% mutate(departure=make_datetime(year,month,day, hour,minute))
## # A tibble: 336,776 x 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
## 7 2013 1 1 6 0 2013-01-01 06:00:00
## 8 2013 1 1 6 0 2013-01-01 06:00:00
## 9 2013 1 1 6 0 2013-01-01 06:00:00
## 10 2013 1 1 6 0 2013-01-01 06:00:00
## # i 336,766 more rows
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 x 4
## origin dest dep_delay arr_delay
## <chr> <chr> <dbl> <dbl>
## 1 EWR IAH 2 11
## 2 LGA IAH 4 20
## 3 JFK MIA 2 33
## 4 JFK BQN -1 -18
## 5 LGA ATL -6 -25
## 6 EWR ORD -4 12
## 7 EWR FLL -5 19
## 8 LGA IAD -3 -14
## 9 JFK MCO -3 -8
## 10 LGA ORD -2 8
## # i 328,511 more rows
datetime<-ymd_hms("2023-01-31 10:30:20")
year(datetime)
## [1] 2023
month(datetime)
## [1] 1
mday(datetime)
## [1] 31
yday(datetime)
## [1] 31
wday(datetime)
## [1] 3
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~
df$y2<-parse_date_time(df$date_added, orders = c("mdy","dmy"),train=F)
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
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)
## ------------------------------------------------------------------------------
##
## 다음의 패키지를 부착합니다: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
df<-read.csv("Data1.csv")
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 <int> 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, ~
## $ 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, ~
#목표변수는 factor
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
set.seed(10)
any(is.na(df))
## [1] FALSE
inTraining<-createDataPartition(y=df$Gender,p=.8,list=FALSE)
training<-df[inTraining,]
testing<-df[-inTraining,]
my_trainControl<-trainControl(method="cv",number=10,classProbs=TRUE,summaryFunction=twoClassSummary)
glmnetfit<-train(Gender~.,data=training, method="rf",preProcess=c("center","scale"),trControl=my_trainControl,verbose=FALSE,metric="ROC")
glmnetfit
## Random Forest
##
## 1541 samples
## 26 predictor
## 2 classes: 'female', 'male'
##
## Pre-processing: centered (26), scaled (26)
## Resampling: Cross-Validated (10 fold)
## Summary of sample sizes: 1387, 1386, 1387, 1388, 1387, 1386, ...
## Resampling results across tuning parameters:
##
## mtry ROC Sens Spec
## 2 0.6549288 0.8151282 0.3291171
## 14 0.6543559 0.7634432 0.3939732
## 26 0.6526770 0.7546398 0.4129464
##
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
predict(glmnetfit,newdata=testing) %>% confusionMatrix(testing$Gender)
## Confusion Matrix and Statistics
##
## Reference
## Prediction female male
## female 194 100
## male 33 57
##
## Accuracy : 0.6536
## 95% CI : (0.6037, 0.7012)
## No Information Rate : 0.5911
## P-Value [Acc > NIR] : 0.006984
##
## Kappa : 0.233
##
## Mcnemar's Test P-Value : 1.047e-08
##
## Sensitivity : 0.8546
## Specificity : 0.3631
## Pos Pred Value : 0.6599
## Neg Pred Value : 0.6333
## Prevalence : 0.5911
## Detection Rate : 0.5052
## Detection Prevalence : 0.7656
## Balanced Accuracy : 0.6088
##
## 'Positive' Class : female
##
predict(glmnetfit,newdata=testing,type="prob")
## female male
## 16 0.544 0.456
## 34 0.642 0.358
## 44 0.460 0.540
## 46 0.528 0.472
## 47 0.586 0.414
## 56 0.734 0.266
## 59 0.968 0.032
## 63 0.848 0.152
## 65 0.754 0.246
## 70 0.648 0.352
## 71 0.356 0.644
## 74 0.586 0.414
## 75 0.510 0.490
## 77 0.596 0.404
## 79 0.464 0.536
## 96 0.788 0.212
## 97 0.828 0.172
## 105 0.388 0.612
## 106 0.442 0.558
## 109 0.652 0.348
## 111 0.796 0.204
## 120 0.588 0.412
## 121 0.748 0.252
## 126 0.692 0.308
## 130 0.848 0.152
## 131 0.502 0.498
## 133 0.542 0.458
## 135 0.546 0.454
## 141 0.774 0.226
## 151 0.420 0.580
## 162 0.790 0.210
## 169 0.732 0.268
## 174 0.738 0.262
## 179 0.616 0.384
## 184 0.484 0.516
## 187 0.570 0.430
## 190 0.632 0.368
## 191 0.544 0.456
## 192 0.608 0.392
## 194 0.292 0.708
## 196 0.464 0.536
## 199 0.690 0.310
## 200 0.730 0.270
## 205 0.796 0.204
## 207 0.726 0.274
## 208 0.758 0.242
## 221 0.792 0.208
## 223 0.656 0.344
## 226 0.848 0.152
## 227 0.870 0.130
## 228 0.876 0.124
## 230 0.632 0.368
## 234 0.534 0.466
## 236 0.466 0.534
## 248 0.522 0.478
## 250 0.358 0.642
## 251 0.664 0.336
## 261 0.712 0.288
## 269 0.730 0.270
## 275 0.772 0.228
## 281 0.614 0.386
## 285 0.686 0.314
## 302 0.448 0.552
## 303 0.596 0.404
## 304 0.466 0.534
## 306 0.498 0.502
## 307 0.680 0.320
## 315 0.588 0.412
## 317 0.606 0.394
## 325 0.926 0.074
## 327 0.656 0.344
## 333 0.650 0.350
## 334 0.658 0.342
## 336 0.678 0.322
## 337 0.514 0.486
## 339 0.434 0.566
## 347 0.454 0.546
## 352 0.522 0.478
## 356 0.560 0.440
## 364 0.574 0.426
## 366 0.664 0.336
## 367 0.610 0.390
## 370 0.476 0.524
## 372 0.496 0.504
## 375 0.434 0.566
## 377 0.498 0.502
## 384 0.596 0.404
## 393 0.622 0.378
## 400 0.768 0.232
## 404 0.652 0.348
## 406 0.538 0.462
## 407 0.740 0.260
## 412 0.686 0.314
## 414 0.612 0.388
## 421 0.812 0.188
## 422 0.590 0.410
## 434 0.718 0.282
## 436 0.348 0.652
## 444 0.342 0.658
## 445 0.696 0.304
## 449 0.744 0.256
## 455 0.596 0.404
## 457 0.722 0.278
## 458 0.324 0.676
## 459 0.610 0.390
## 466 0.676 0.324
## 472 0.538 0.462
## 481 0.772 0.228
## 490 0.748 0.252
## 491 0.570 0.430
## 496 0.832 0.168
## 511 0.572 0.428
## 520 0.592 0.408
## 527 0.272 0.728
## 532 0.826 0.174
## 534 0.612 0.388
## 537 0.722 0.278
## 545 0.804 0.196
## 547 0.666 0.334
## 555 0.586 0.414
## 558 0.594 0.406
## 560 0.618 0.382
## 570 0.744 0.256
## 572 0.650 0.350
## 580 0.568 0.432
## 582 0.520 0.480
## 583 0.750 0.250
## 586 0.528 0.472
## 588 0.532 0.468
## 592 0.522 0.478
## 598 0.498 0.502
## 604 0.500 0.500
## 605 0.382 0.618
## 619 0.726 0.274
## 634 0.714 0.286
## 646 0.810 0.190
## 651 0.558 0.442
## 663 0.564 0.436
## 668 0.682 0.318
## 670 0.472 0.528
## 672 0.324 0.676
## 674 0.598 0.402
## 676 0.456 0.544
## 679 0.420 0.580
## 680 0.616 0.384
## 689 0.660 0.340
## 690 0.494 0.506
## 695 0.750 0.250
## 696 0.728 0.272
## 699 0.718 0.282
## 702 0.618 0.382
## 704 0.640 0.360
## 705 0.584 0.416
## 706 0.638 0.362
## 707 0.544 0.456
## 715 0.790 0.210
## 716 0.692 0.308
## 718 0.566 0.434
## 731 0.318 0.682
## 741 0.576 0.424
## 744 0.620 0.380
## 746 0.398 0.602
## 752 0.448 0.552
## 757 0.676 0.324
## 760 0.680 0.320
## 762 0.802 0.198
## 767 0.218 0.782
## 770 0.266 0.734
## 772 0.388 0.612
## 773 0.480 0.520
## 779 0.388 0.612
## 783 0.674 0.326
## 790 0.664 0.336
## 792 0.522 0.478
## 797 0.448 0.552
## 800 0.592 0.408
## 804 0.588 0.412
## 808 0.524 0.476
## 812 0.874 0.126
## 821 0.668 0.332
## 824 0.638 0.362
## 825 0.424 0.576
## 837 0.536 0.464
## 841 0.676 0.324
## 854 0.572 0.428
## 866 0.692 0.308
## 870 0.504 0.496
## 871 0.508 0.492
## 879 0.662 0.338
## 886 0.382 0.618
## 896 0.850 0.150
## 903 0.782 0.218
## 909 0.786 0.214
## 920 0.562 0.438
## 928 0.284 0.716
## 936 0.674 0.326
## 942 0.406 0.594
## 952 0.730 0.270
## 954 0.574 0.426
## 969 0.638 0.362
## 982 0.576 0.424
## 987 0.568 0.432
## 994 0.450 0.550
## 999 0.624 0.376
## 1010 0.754 0.246
## 1017 0.398 0.602
## 1021 0.646 0.354
## 1026 0.634 0.366
## 1027 0.644 0.356
## 1028 0.704 0.296
## 1029 0.350 0.650
## 1035 0.732 0.268
## 1036 0.476 0.524
## 1038 0.678 0.322
## 1040 0.382 0.618
## 1046 0.534 0.466
## 1052 0.558 0.442
## 1060 0.708 0.292
## 1063 0.716 0.284
## 1070 0.668 0.332
## 1073 0.608 0.392
## 1077 0.660 0.340
## 1079 0.624 0.376
## 1085 0.660 0.340
## 1086 0.490 0.510
## 1088 0.550 0.450
## 1092 0.628 0.372
## 1097 0.798 0.202
## 1103 0.454 0.546
## 1105 0.482 0.518
## 1112 0.662 0.338
## 1114 0.600 0.400
## 1124 0.562 0.438
## 1134 0.594 0.406
## 1136 0.626 0.374
## 1140 0.500 0.500
## 1141 0.542 0.458
## 1146 0.396 0.604
## 1155 0.636 0.364
## 1165 0.760 0.240
## 1167 0.922 0.078
## 1176 0.558 0.442
## 1178 0.518 0.482
## 1191 0.024 0.976
## 1195 0.906 0.094
## 1198 0.806 0.194
## 1208 0.622 0.378
## 1214 0.672 0.328
## 1217 0.214 0.786
## 1220 0.524 0.476
## 1235 0.626 0.374
## 1246 0.770 0.230
## 1247 0.518 0.482
## 1258 0.614 0.386
## 1261 0.570 0.430
## 1262 0.642 0.358
## 1263 0.636 0.364
## 1265 0.548 0.452
## 1266 0.694 0.306
## 1267 0.378 0.622
## 1272 0.356 0.644
## 1281 0.718 0.282
## 1290 0.522 0.478
## 1295 0.772 0.228
## 1296 0.346 0.654
## 1303 0.676 0.324
## 1305 0.372 0.628
## 1320 0.618 0.382
## 1324 0.414 0.586
## 1332 0.550 0.450
## 1334 0.958 0.042
## 1339 0.896 0.104
## 1344 0.426 0.574
## 1347 0.582 0.418
## 1353 0.866 0.134
## 1358 0.532 0.468
## 1362 0.446 0.554
## 1365 0.598 0.402
## 1369 0.888 0.112
## 1371 0.720 0.280
## 1383 0.454 0.546
## 1392 0.736 0.264
## 1401 0.704 0.296
## 1408 0.732 0.268
## 1415 0.290 0.710
## 1418 0.272 0.728
## 1424 0.718 0.282
## 1440 0.498 0.502
## 1445 0.896 0.104
## 1458 0.042 0.958
## 1460 0.930 0.070
## 1461 0.136 0.864
## 1462 0.890 0.110
## 1464 0.510 0.490
## 1468 0.712 0.288
## 1476 0.584 0.416
## 1480 0.692 0.308
## 1485 0.822 0.178
## 1486 0.792 0.208
## 1487 0.678 0.322
## 1489 0.646 0.354
## 1490 0.606 0.394
## 1493 0.552 0.448
## 1498 0.616 0.384
## 1499 0.724 0.276
## 1500 0.576 0.424
## 1501 0.524 0.476
## 1506 0.738 0.262
## 1509 0.560 0.440
## 1520 0.648 0.352
## 1523 0.494 0.506
## 1527 0.584 0.416
## 1529 0.678 0.322
## 1536 0.496 0.504
## 1537 0.478 0.522
## 1549 0.622 0.378
## 1551 0.192 0.808
## 1556 0.566 0.434
## 1558 0.602 0.398
## 1569 0.596 0.404
## 1578 0.434 0.566
## 1580 0.560 0.440
## 1581 0.532 0.468
## 1589 0.396 0.604
## 1600 0.558 0.442
## 1601 0.690 0.310
## 1623 0.636 0.364
## 1626 0.746 0.254
## 1627 0.280 0.720
## 1629 0.650 0.350
## 1630 0.472 0.528
## 1634 0.654 0.346
## 1637 0.656 0.344
## 1646 0.584 0.416
## 1650 0.654 0.346
## 1652 0.566 0.434
## 1654 0.476 0.524
## 1657 0.548 0.452
## 1662 0.456 0.544
## 1664 0.674 0.326
## 1671 0.408 0.592
## 1676 0.502 0.498
## 1678 0.672 0.328
## 1681 0.538 0.462
## 1685 0.582 0.418
## 1689 0.602 0.398
## 1691 0.702 0.298
## 1692 0.620 0.380
## 1695 0.604 0.396
## 1721 0.472 0.528
## 1737 0.664 0.336
## 1745 0.508 0.492
## 1747 0.620 0.380
## 1762 0.476 0.524
## 1779 0.566 0.434
## 1782 0.572 0.428
## 1785 0.730 0.270
## 1788 0.368 0.632
## 1806 0.460 0.540
## 1810 0.490 0.510
## 1812 0.658 0.342
## 1815 0.782 0.218
## 1821 0.632 0.368
## 1832 0.720 0.280
## 1846 0.568 0.432
## 1848 0.556 0.444
## 1857 0.572 0.428
## 1869 0.370 0.630
## 1870 0.502 0.498
## 1871 0.452 0.548
## 1878 0.576 0.424
## 1882 0.462 0.538
## 1885 0.556 0.444
## 1887 0.500 0.500
## 1899 0.408 0.592
## 1902 0.480 0.520
## 1905 0.496 0.504
## 1907 0.568 0.432
## 1908 0.670 0.330
## 1911 0.506 0.494
## 1915 0.518 0.482
## 1916 0.636 0.364
## 1924 0.602 0.398
## 1925 0.550 0.450