getwd()
## [1] "C:/data"
setwd("C:/data")
getwd()
## [1] "C:/data"
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
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
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)  #이 4줄은 암기.. 반복 쓰이는 코딩
train <- iris[idx,]
test <- iris[-idx,]
nrow(train)
## [1] 120
nrow(test)
## [1] 30
set.seed(2)
data("iris")
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
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
describe(sca_iris_p)      # 최소최대가 0에서 1 사이 됬는지 확인
##              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
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")  # caret을 통해 주성분분석도 할 수 있구나 정도만 알아두기
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
# 더미 변수 생성 (더미 = 파이썬의 원핫인코딩)    ## 팩터데이터도 회귀분석 가능, r로 하면 기준변수(?) 빼고 나옴. 기어가 3,4,5단이면 기준인 3단 빼고 4단, 5단만 나옴
library(earth)
## 필요한 패키지를 로딩중입니다: Formula
## 필요한 패키지를 로딩중입니다: plotmo
## 필요한 패키지를 로딩중입니다: plotrix
## 
## 다음의 패키지를 부착합니다: 'plotrix'
## The following object is masked from 'package:psych':
## 
##     rescale
## 필요한 패키지를 로딩중입니다: TeachingDemos
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
# 대체
data("airquality")
colSums(is.na(airquality))  # 결측치의 수 확인 가능
##   Ozone Solar.R    Wind    Temp   Month     Day 
##      37       7       0       0       0       0
library(RANN)
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
median(airquality$Ozone, na.rm = TRUE) # 중앙값 대체 확인 (교안에 없음)
## [1] 31.5
# 산점도
# for regression, the Boston housing data is used:
library(mlbench)
data("BostonHousing")
x= BostonHousing[,c("age","lstat","tax")]
y= BostonHousing$medv
featurePlot(x,
            y= BostonHousing$medv,
            plot= "scatter",
            layout= c(3,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:dplyr':
## 
##     combine
## The following object is masked from 'package:ggplot2':
## 
##     margin
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=5)      # 텐 폴드 교차검증 (인데 와파가 열악해서 낮춰서 실습함)
model <- train(diabetes~., data=PimaIndiansDiabetes, methods="rf",
               preProcess=c("center","scale"), trControl=control)
model
## Random Forest 
## 
## 768 samples
##   8 predictor
##   2 classes: 'neg', 'pos' 
## 
## Pre-processing: centered (8), scaled (8) 
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 615, 614, 614, 615, 614 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##   2     0.7694848  0.4809709
##   5     0.7577710  0.4552707
##   8     0.7538664  0.4471454
## 
## 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    91.28
## mass       57.41
## age        46.74
## pedigree   42.62
## pressure   31.33
## pregnant   29.25
## insulin    24.83
## triceps    23.59
plot(importance)

model1 <- train(diabetes~., data=PimaIndiansDiabetes, methods="knn",
               preProcess=c("center","scale"), trControl=control)
model1
## Random Forest 
## 
## 768 samples
##   8 predictor
##   2 classes: 'neg', 'pos' 
## 
## Pre-processing: centered (8), scaled (8) 
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 615, 615, 614, 614, 614 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##   2     0.7565232  0.4477705
##   5     0.7578389  0.4560591
##   8     0.7669807  0.4801684
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 8.
library(caret)
data(Sonar)
set.seed(998)
inTraining <- createDataPartition(y = Sonar$Class, p = .75, list= FALSE)
training <- Sonar[inTraining,]
testing <- Sonar[-inTraining,]
set.seed(825)
rffit <- train(Class ~., data=training, 
               method= "rf",
               preProc = c("center", "scale"))
rffit
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## Pre-processing: centered (60), scaled (60) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.7869345  0.5637847
##   31    0.7772599  0.5465693
##   60    0.7626313  0.5153456
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
rffit2 <- train(Class ~., data=training, 
               method= "knn",
               preProc = c("center", "scale"))
rffit2
## k-Nearest Neighbors 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## Pre-processing: centered (60), scaled (60) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 
## Resampling results across tuning parameters:
## 
##   k  Accuracy   Kappa    
##   5  0.7421023  0.4741816
##   7  0.7197420  0.4301948
##   9  0.7119170  0.4136557
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was k = 5.
rffit3 <- train(Class ~., data=training, 
               method= "rpart",
               preProc = c("center", "scale"))
rffit3
## CART 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## Pre-processing: centered (60), scaled (60) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 157, 157, 157, 157, 157, 157, ... 
## Resampling results across tuning parameters:
## 
##   cp          Accuracy   Kappa    
##   0.04109589  0.7285611  0.4489088
##   0.12328767  0.7341192  0.4594893
##   0.53424658  0.6088256  0.1998663
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was cp = 0.1232877.
fitControl <- trainControl(method = "cv",
                           number = 5,
                           classProbs = TRUE,
                           summaryFunction = twoClassSummary)
fitControl
## $method
## [1] "cv"
## 
## $number
## [1] 5
## 
## $repeats
## [1] NA
## 
## $search
## [1] "grid"
## 
## $p
## [1] 0.75
## 
## $initialWindow
## NULL
## 
## $horizon
## [1] 1
## 
## $fixedWindow
## [1] TRUE
## 
## $skip
## [1] 0
## 
## $verboseIter
## [1] FALSE
## 
## $returnData
## [1] TRUE
## 
## $returnResamp
## [1] "final"
## 
## $savePredictions
## [1] FALSE
## 
## $classProbs
## [1] TRUE
## 
## $summaryFunction
## function (data, lev = NULL, model = NULL) 
## {
##     if (length(lev) > 2) {
##         stop(paste("Your outcome has", length(lev), "levels. The twoClassSummary() function isn't appropriate."))
##     }
##     requireNamespaceQuietStop("pROC")
##     if (!all(levels(data[, "pred"]) == lev)) {
##         stop("levels of observed and predicted data do not match")
##     }
##     rocObject <- try(pROC::roc(data$obs, data[, lev[1]], direction = ">", 
##         quiet = TRUE), silent = TRUE)
##     rocAUC <- if (inherits(rocObject, "try-error")) 
##         NA
##     else rocObject$auc
##     out <- c(rocAUC, sensitivity(data[, "pred"], data[, "obs"], 
##         lev[1]), specificity(data[, "pred"], data[, "obs"], lev[2]))
##     names(out) <- c("ROC", "Sens", "Spec")
##     out
## }
## <bytecode: 0x00000228fe8c25e8>
## <environment: namespace:caret>
## 
## $selectionFunction
## [1] "best"
## 
## $preProcOptions
## $preProcOptions$thresh
## [1] 0.95
## 
## $preProcOptions$ICAcomp
## [1] 3
## 
## $preProcOptions$k
## [1] 5
## 
## $preProcOptions$freqCut
## [1] 19
## 
## $preProcOptions$uniqueCut
## [1] 10
## 
## $preProcOptions$cutoff
## [1] 0.9
## 
## 
## $sampling
## NULL
## 
## $index
## NULL
## 
## $indexOut
## NULL
## 
## $indexFinal
## NULL
## 
## $timingSamps
## [1] 0
## 
## $predictionBounds
## [1] FALSE FALSE
## 
## $seeds
## [1] NA
## 
## $adaptive
## $adaptive$min
## [1] 5
## 
## $adaptive$alpha
## [1] 0.05
## 
## $adaptive$method
## [1] "gls"
## 
## $adaptive$complete
## [1] TRUE
## 
## 
## $trim
## [1] FALSE
## 
## $allowParallel
## [1] TRUE
set.seed(825)
rmfit1 <- train(Class ~., data = training,
                method = "rf",
                trControl = fitControl,
                verbose = FALSE,
                metric = "ROC")
rmfit1
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 125, 125, 125, 127, 126 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.9317665  0.9176471  0.7533333
##   31    0.9252381  0.8816176  0.7247619
##   60    0.9146569  0.8580882  0.7514286
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
set.seed(825)
customgrid <- expand.grid(mtry = 1:10)
frfit2 <- train(Class ~ ., data = training, method = "rf", trControl = 
                  fitControl, tuneGrid = customgrid, metric = "ROC")
frfit2
## Random Forest 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 125, 125, 125, 127, 126 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    1    0.9287640  0.9411765  0.6847619
##    2    0.9371078  0.9058824  0.7533333
##    3    0.9399545  0.9058824  0.7380952
##    4    0.9392787  0.8823529  0.7657143
##    5    0.9365546  0.8941176  0.7523810
##    6    0.9297094  0.8941176  0.7523810
##    7    0.9347584  0.8941176  0.7238095
##    8    0.9331863  0.8816176  0.7800000
##    9    0.9364741  0.8816176  0.7390476
##   10    0.9285399  0.8823529  0.7390476
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 3.
set.seed(825)
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 
## 
## 157 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 125, 125, 125, 127, 126 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.9133386  0.8941176  0.6971429
##    5    0.9254359  0.8823529  0.7514286
##    8    0.9377556  0.8823529  0.7800000
##   11    0.9293085  0.8823529  0.7380952
##   14    0.9175280  0.8816176  0.7523810
##   17    0.9265581  0.8816176  0.7247619
##   20    0.9206478  0.8816176  0.7657143
##   23    0.9246569  0.8816176  0.7390476
##   26    0.9183613  0.8816176  0.7523810
##   29    0.9225000  0.8816176  0.7666667
##   32    0.9210399  0.8816176  0.7523810
##   35    0.9198687  0.8698529  0.7247619
##   38    0.9218803  0.8816176  0.7247619
##   41    0.9198109  0.8698529  0.7533333
##   44    0.9176313  0.8580882  0.7666667
##   47    0.9171849  0.8580882  0.7533333
##   50    0.9200123  0.8698529  0.7247619
##   53    0.9167787  0.8698529  0.7666667
##   56    0.9160644  0.8698529  0.7390476
##   60    0.9180795  0.8816176  0.7390476
## 
## 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 M R M M M
## Levels: M R
predict(rmfit1, newdata = head(testing), type='prob')
##        M     R
## 4  0.446 0.554
## 8  0.630 0.370
## 11 0.088 0.912
## 14 0.506 0.494
## 15 0.546 0.454
## 17 0.620 0.380
library(caTools)
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 = 3,
                                classProbs = TRUE,
                                summaryFunction = twoClassSummary)

library(glmnet)
## 필요한 패키지를 로딩중입니다: Matrix
## Loaded glmnet 4.1-8
glmnetfit <- train(diagnosis ~ ., data = training,
                   method = "glmnet",
                   trControl = fitControl,
                   verbose = FALSE,
                   metric = "ROC")
glmnetfit
## glmnet 
## 
## 427 samples
##  30 predictor
##   2 classes: 'B', 'M' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 342, 341, 343, 341, 341 
## Resampling results across tuning parameters:
## 
##   alpha  lambda        ROC        Sens       Spec     
##   0.10   0.0007738036  0.9931304  0.9850454  0.9560484
##   0.10   0.0077380355  0.9941928  0.9962963  0.9372984
##   0.10   0.0773803551  0.9937380  0.9962963  0.9054435
##   0.55   0.0007738036  0.9930087  0.9850454  0.9497984
##   0.55   0.0077380355  0.9943107  0.9962963  0.9497984
##   0.55   0.0773803551  0.9931533  0.9962963  0.8741935
##   1.00   0.0007738036  0.9921904  0.9776380  0.9435484
##   1.00   0.0077380355  0.9941966  0.9962963  0.9370968
##   1.00   0.0773803551  0.9907960  0.9925926  0.8614919
## 
## ROC was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 0.55 and lambda = 0.007738036.
rffit <- train(diagnosis ~ ., data = training,
               method = "rf",
               trControl = fitControl,
               verbose = FALSE,
               metric = "ROC")
rffit
## Random Forest 
## 
## 427 samples
##  30 predictor
##   2 classes: 'B', 'M' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 342, 342, 341, 342, 341 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.9936561  0.9777079  0.9431452
##   16    0.9921875  0.9628232  0.9493952
##   30    0.9908927  0.9702306  0.9493952
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
library(lubridate) # 시간관련함수 패키지
## 
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(nycflights13)
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…
today()
## [1] "2024-01-31"
now()
## [1] "2024-01-31 17:02:55 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"
flights %>% select(year, month, day, hour, minute) %>%
  mutate(departure = make_datetime(year, month, day, hour, minute))
## # A tibble: 336,776 × 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
## # ℹ 336,766 more rows
make_datetime_100 <- function(year, month, day, time) {
  make_datetime(year, month, day, time %/% 100, time %% 100)
}
datetime <- ymd_hms("2023-01-31 10:30:20")
year(datetime)
## [1] 2023
library(ggplot2)
data("economics")
economics$year <- year(economics$date)
economics$month <- month(economics$date)
economics$day <- day(economics$date)
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")

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<='2019-01-31')
##    show_id    type                                                    title
## 1    s4167 TV Show                                       Black Earth Rising
## 2    s4184 TV Show                     The World's Most Extraordinary Homes
## 3    s4203 TV Show                                             Horrid Henry
## 4    s4218   Movie                          Monty Python and the Holy Grail
## 5    s4230   Movie                              Bill Hicks: One Night Stand
## 6    s4232   Movie                                   Bill Hicks: Relentless
## 7    s4233   Movie                                  Bill Hicks: Revelations
## 8    s4236   Movie                               Gina Yashere: Skinny B*tch
## 9    s4247   Movie              Tim Minchin And The Heritage Orchestra Live
## 10   s4248   Movie                             Tim Minchin: So F**king Rock
## 11   s4265 TV Show                                   People Just Do Nothing
## 12   s4297 TV Show                                                  Flowers
## 13   s4368 TV Show                                                Sick Note
## 14   s4502 TV Show                                               Wanderlust
## 15   s4509 TV Show                                            Stunt Science
## 16   s4533   Movie                                               Malevolent
## 17   s4543 TV Show                          Monty Python Best Bits (mostly)
## 18   s4545   Movie         Monty Python Live (Mostly): One Down, Five to Go
## 19   s4546   Movie                   Monty Python: Before the Flying Circus
## 20   s4548   Movie                 Monty Python: Live at The Hollywood Bowl
## 21   s4549   Movie                        Monty Python: The Meaning of Live
## 22   s4550 TV Show                          Monty Python's Almost the Truth
## 23   s4552 TV Show                             Monty Python's Flying Circus
## 24   s4553   Movie                             Monty Python's Life of Brian
## 25   s4555   Movie Parrot Sketch Not Included: Twenty Years of Monty Python
## 26   s4556   Movie                              The Meaning of Monty Python
## 27   s4609 TV Show                                  Three Wives One Husband
## 28   s4690 TV Show                                            The Innocents
## 29   s4759 TV Show                                        Amazing Interiors
## 30   s4778 TV Show                                            Baby Ballroom
## 31   s4807   Movie                                                  Calibre
## 32   s4808 TV Show       Churchill\xa1\xafs Secret Agents: The New Recruits
## 33   s4821   Movie                                    Derren Brown: Miracle
## 34   s4933   Movie                       Greg Davies: You Magnificent Beast
## 35   s4952 TV Show                                                   Loaded
## 36   s4958 TV Show                                            Doctor Foster
## 37   s4959   Movie                                                   Revolt
## 38   s4968 TV Show                                 Trump: An American Dream
## 39   s4969 TV Show                                James Acaster: Repertoire
## 40   s4974 TV Show                                                  Requiem
## 41   s4990   Movie                                  Ricky Gervais: Humanity
## 42   s5024   Movie                                   Derren Brown: The Push
## 43   s5031 TV Show                                  Hitler's Circle of Evil
## 44   s5033 TV Show                              The Frankenstein Chronicles
## 45   s5058 TV Show                                              Retribution
## 46   s5075   Movie                                      Bad Day for the Cut
## 47   s5098 TV Show                                                 Lovesick
## 48   s5968   Movie                                                       89
## 49   s5999   Movie                                      2036 Origin Unknown
## 50   s6006   Movie                                        27: Gone Too Soon
## 51   s6150   Movie                                                      Amy
## 52   s6198   Movie                        Auschwitz: Blueprints of Genocide
## 53   s6214 TV Show                                            Bad Education
## 54   s6310   Movie                                           Biggie & Tupac
## 55   s6342 TV Show                                           Blue Planet II
## 56   s6352   Movie                        Bobby Robson: More Than a Manager
## 57   s6399 TV Show                 Cabins in the Wild with Dick Strawbridge
## 58   s6491 TV Show                                            Civilizations
## 59   s6550 TV Show                                               Dad's Army
## 60   s6586   Movie                                                Deathgrip
## 61   s6717   Movie                                               Ex Machina
## 62   s6743 TV Show                                             Father Brown
## 63   s6864   Movie                                        God's Own Country
## 64   s6895 TV Show                          Great Interior Design Challenge
## 65   s6919   Movie                              Hans Zimmer: Live in Prague
## 66   s7026   Movie                                               Hungerford
## 67   s7043   Movie                                           I Am Vengeance
## 68   s7077   Movie                                                   Influx
## 69   s7081 TV Show                                    Inside The Freemasons
## 70   s7084 TV Show                                              Inst@famous
## 71   s7167   Movie                                             Kaleidoscope
## 72   s7245   Movie                                          Kurt & Courtney
## 73   s7327   Movie                      Lock, Stock and Two Smoking Barrels
## 74   s7399   Movie               Manolo: The Boy Who Made Shoes for Lizards
## 75   s7403   Movie                                                     Mara
## 76   s7634   Movie                                        Oasis: Supersonic
## 77   s7847   Movie                                                Red Trees
## 78   s7865 TV Show                                         Revolting Rhymes
## 79   s8202   Movie                                  The Bad Education Movie
## 80   s8251 TV Show                                                 The Code
## 81   s8269   Movie                                         The Darkest Dawn
## 82   s8332 TV Show               The Great British Baking Show: Masterclass
## 83   s8358 TV Show                                         The Inbetweeners
## 84   s8447   Movie                                       The Parole Officer
## 85   s8448   Movie                                                 The Pass
## 86   s8531   Movie                                   The Tigers of Scotland
## 87   s8620   Movie             Treasures from the Wreck of the Unbelievable
##                         director
## 1                               
## 2                               
## 3                               
## 4     Terry Gilliam, Terry Jones
## 5               John Fortenberry
## 6                    Chris Bould
## 7                    Chris Bould
## 8                               
## 9                     Matt Askem
## 10                    Matt Askem
## 11                              
## 12                              
## 13                              
## 14                              
## 15                              
## 16                 Olaf de Fleur
## 17                              
## 18                     Eric Idle
## 19                     Will Yapp
## 20 Terry Hughes, Ian MacNaughton
## 21      Roger Graef, James Rogan
## 22                              
## 23                              
## 24                   Terry Jones
## 25               Ian MacNaughton
## 26                 Albert Sharpe
## 27                              
## 28                              
## 29                              
## 30                              
## 31                   Matt Palmer
## 32                              
## 33                              
## 34                   Peter Orton
## 35                              
## 36                              
## 37                     Joe Miale
## 38                              
## 39                  Dan Lucchesi
## 40                              
## 41               John L. Spencer
## 42                              
## 43                              
## 44                              
## 45                              
## 46                   Chris Baugh
## 47                              
## 48                              
## 49                 Hasraf Dulull
## 50             Simon Napier-Bell
## 51                  Asif Kapadia
## 52                 Mike Rossiter
## 53                              
## 54               Nick Broomfield
## 55                              
## 56 Gabriel Clarke, Torquil Jones
## 57                              
## 58                              
## 59                              
## 60                   Clay Porter
## 61                  Alex Garland
## 62                    Ian Barber
## 63                   Francis Lee
## 64                              
## 65               Tim Van Someren
## 66                   Drew Casson
## 67                   Ross Boyask
## 68                    Luca Vullo
## 69                              
## 70                              
## 71                  Rupert Jones
## 72               Nick Broomfield
## 73                   Guy Ritchie
## 74               Michael Roberts
## 75                   Clive Tonge
## 76                Mat Whitecross
## 77                 Marina Willer
## 78    Jani Lachauer, Jakob Schuh
## 79                Elliot Hegarty
## 80                              
## 81                              
## 82                              
## 83                              
## 84                   John Duigan
## 85               Ben A. Williams
## 86                  Leanne Gater
## 87                 Sam Hobkinson
##                                                                                                                                                                                                                                                                                         cast
## 1                                                                                                                      Michaela Coel, John Goodman, Abena Ayivor, Noma Dumezweni, Ronald Guttman, Danny Sapani, Harriet Walter, Tyrone Huggins, Emmanuel Imani, Lucian Msamati, Tamara Tunie
## 2                                                                                                                                                                                                                                                             Piers Taylor, Caroline Quentin
## 3                                                                                                                                               Lizzie Waterworth, Joanna Ruiz, Emma Tate, Sue Elliot Nicholls, Tamsin Heatley, Wayne Forester, Aidan Cook, Stephanie Anne Mills, David Holt
## 4                                                                                                                                                               Graham Chapman, John Cleese, Eric Idle, Terry Gilliam, Terry Jones, Michael Palin, Connie Booth, Carol Cleveland, Neil Innes
## 5                                                                                                                                                                                                                                                                                 Bill Hicks
## 6                                                                                                                                                                                                                                                                                 Bill Hicks
## 7                                                                                                                                                                                                                                                                                 Bill Hicks
## 8                                                                                                                                                                                                                                                                               Gina Yashere
## 9                                                                                                                                                                                                                                                                 Tim Minchin, Jules Buckley
## 10                                                                                                                                                                                                                                                                               Tim Minchin
## 11                                                                                                                                     Allan Mustafa, Hugo Chegwin, Asim Chaudhry, Lily Brazier, Olivia Jasmine Edwards, Dan Sylvester, Steve Stamp, Ruth Bratt, George Keywood, Maria Louis
## 12                                                                                                                                                                                Olivia Colman, Julian Barratt, Daniel Rigby, Sophia Di Martino, Leila Hoffman, Will Sharpe, Harriet Walter
## 13                                                                                                     Rupert Grint, Nick Frost, Don Johnson, Lindsay Lohan, Pippa Bennett-Warner, Marama Corlett, Karl Theobald, Belinda Stewart-Wilson, Camilla Beeput, Dustin Demri-Burns, Tolu Ogunmefun
## 14                                                                                                        Toni Collette, Steven Mackintosh, Zawe Ashton, Joe Hurst, Emma D'Arcy, Celeste Dring, William Ash, Royce Pierreson, Jeremy Swift, Anastasia Hille, Isis Hainsworth, Sophie Okonedo
## 15                                                                                                                                                                                                                                                                                          
## 16                                                                                                                                                                                                 Florence Pugh, Ben Lloyd-Hughes, Celia Imrie, James Cosmo, Scott Chambers, Georgina Bevan
## 17                                                                                                                                                                                        Hugh Bonneville, Graham Chapman, John Cleese, Terry Gilliam, Eric Idle, Terry Jones, Michael Palin
## 18                                                                                                                                                                                                                         John Cleese, Terry Gilliam, Eric Idle, Terry Jones, Michael Palin
## 19                                                                                                                                                                                                         Graham Chapman, Eric Idle, Terry Jones, Michael Palin, Terry Gilliam, John Cleese
## 20                                                                                                                                                                            Graham Chapman, John Cleese, Terry Gilliam, Eric Idle, Terry Jones, Michael Palin, Neil Innes, Carol Cleveland
## 21                                                                                                                                                                            John Cleese, Terry Gilliam, Eric Idle, Terry Jones, Michael Palin, Mike Myers, Graham Chapman, Stephen Hawking
## 22                                                                                                                                                                                                         Graham Chapman, Eric Idle, John Cleese, Michael Palin, Terry Gilliam, Terry Jones
## 23                                                                                                                                                                                                         John Cleese, Graham Chapman, Terry Jones, Eric Idle, Michael Palin, Terry Gilliam
## 24                                                                                                                                                                        Graham Chapman, John Cleese, Terry Gilliam, Eric Idle, Terry Jones, Michael Palin, Terence Bayler, Carol Cleveland
## 25                                                                                                                                                            Steve Martin, John Cleese, Graham Chapman, Terry Gilliam, Eric Idle, Terry Jones, Michael Palin, Carol Cleveland, Connie Booth
## 26                                                                                                                                                                                                                                                                                          
## 27                                                                                                                                                                                                                                                                                          
## 28                                                                            Sorcha Groundsell, Percelle Ascott, Guy Pearce, Sam Hazeldine, Nadine Marshall, J?hannes Haukur J?hannesson, Laura Birn, Ingunn Beate \xa8\xaayen, Arthur Hughes, Trond Fausa, Lise Risom Olsen, Philip Wright
## 29                                                                                                                                                                                                                                                                                          
## 30                                                                                                                                                                                                                                                              Warren Bullock, Jane Bullock
## 31                                                                                                                                                                                                                          Jack Lowden, Martin McCann, Tony Curran, Ian Pirie, Cameron Jack
## 32                                                                                                                                                                                                                                                                                          
## 33                                                                                                                                                                                                                                                                              Derren Brown
## 34                                                                                                                                                                                                                                                                               Greg Davies
## 35                                                                                                                                                                                   Jim Howick, Samuel Anderson, Jonny Sweet, Nick Helm, Mary McCormack, Aimee-Ffion Edwards, Lolly Adefope
## 36 Suranne Jones, Bertie Carvel, Clare-Hope Ashitey, Cheryl Campbell, Jodie Comer, Victoria Hamilton, Martha Howe-Douglas, Adam James, Thusitha Jayasundera, Sara Stewart, Neil Stuke, Tom Taylor, Robert Pugh, Shazia Nicholls, Cian Barry, Megan Roberts, Charlie Cunniffe, Navin Chowdhry
## 37                                                                                                                          Lee Pace, B?r?nice Marlohe, Jason Flemyng, Sibulele Gcilitshana, Wandile Molebatsi, Sekoati Sk Tsubane, Leroy Gopal, Welile Nzunza, Patrick Bakoba, Tom Fairfoot
## 38                                                                                                                                                                                                                                                                              Donald Trump
## 39                                                                                                                                                                                                                                                                             James Acaster
## 40                                                                                                                       Lydia Wilson, Joel Fry, Brendan Coyle, James Frecheville, Claire Rushbrook, Joanna Scanlan, Pippa Haywood, Tara Fitzgerald, Richard Harrington, Sian Reese-Williams
## 41                                                                                                                                                                                                                                                                             Ricky Gervais
## 42                                                                                                                                                                                                                                                                              Derren Brown
## 43                                                                                                                                                                                                        Alisdair Simpson, Henrik J\xa9\xaargensen, James Lowe, Blake Scott, Peter Turnbull
## 44                                                                                                             Sean Bean, Tom Ward, Richie Campbell, Vanessa Kirby, Ed Stoppard, Ryan Sampson, Robbie Gee, Anna Maxwell Martin, Elliot Cowan, Charlie Creed-Miles, Eloise Smyth, Samuel West
## 45                                                                                                                                     Georgina Campbell, Joe Dempsie, Adrian Edmondson, Steve Evets, Laura Fraser, Julie Graham, John Lynch, Gary Lewis, Juliet Stevenson, Joanna Vanderham
## 46                                                                                                                                    Nigel O'Neill, Susan Lynch, J?zef Pawlowski, Stuart Graham, David Pearse, Brian Milligan, Anna Prochniak, Stella McCusker, Ian McElhinney, Lalor Roddy
## 47                                                                                                                                                                              Johnny Flynn, Antonia Thomas, Daniel Ings, Hannah Britland, Joshua McGuire, Richard Thomson, Jessica Ellerby
## 48                                                                                                                                                                                                                                                        Lee Dixon, Ian Wright, Paul Merson
## 49                                                                                                                                                                                                          Katee Sackhoff, Ray Fearon, Julie Cox, Steven Cree, David Tse, Joe David Walters
## 50                                                                                                                                                                                                                      Janis Joplin, Jimi Hendrix, Amy Winehouse, Jim Morrison, Kurt Cobain
## 51                                                                                                                                                                                                                                                             Amy Winehouse, Lauren Gilbert
## 52                                                                                                                                                                                                                                                                               Roy Marsden
## 53                                                                                                  Jack Whitehall, Mathew Horne, Sarah Solemani, Michelle Gomez, Nikki Runeckles, Kae Alexander, Layton Williams, Ethan Lawrence, Charlie Wernham, Jack Binstead, Jack Bence, Harry Enfield
## 54                                                                                                                                                                                                                                                                           Nick Broomfield
## 55                                                                                                                                                                                                                                                                        David Attenborough
## 56                                                                                                                                                                                                                                                                              Bobby Robson
## 57                                                                                                                                                                                                                                                          Dick Strawbridge, William Hardie
## 58                                                                                                                                                                                                                                                                                          
## 59                                                                                                                                                                             Arthur Lowe, John Le Mesurier, Clive Dunn, John Laurie, Arnold Ridley, Ian Lavender, James Beck, Bill Pertwee
## 60                                                                                                                                                       Brendan Fairclough, Olly Wilkins, Kyle Jameson, Andrew Neethling, Nico Vink, Ryan Howard, Sam Hill, Josh Bryceland, Brandon Semenuk
## 61                                                                                                                 Domhnall Gleeson, Alicia Vikander, Oscar Isaac, Sonoya Mizuno, Claire Selby, Symara Templeman, Gana Bayarsaikhan, Tiffany Pisani, Corey Johnson, Evie Wray, Deborah Rosan
## 62                                                                                                                                                                                           Mark Williams, Sorcha Cusack, Nancy Carroll, Alex Price, Hugo Speer, Tom Chambers, Keith Osborn
## 63                                                                                                                                                                                                                                      Josh O'Connor, Alec Secareanu, Ian Hart, Gemma Jones
## 64                                                                                                                                                                                                                                             Tom Dyckhoff, Daniel Hopwood, Sophie Robinson
## 65                                                                                                                                                                                                                                                                               Hans Zimmer
## 66                                                                                                                                                                                            Drew Casson, Georgia Bradley, Sam Carter, Tom Scarlett, Nigel Morgan, Colin Stark, Kitty Speed
## 67                                                                                                                                                                                                         Stu Bennett, Anna Shaffer, Mark Griffin, Bryan Larkin, Kevin Leslie, Gary Daniels
## 68                                                                                                                                                                                                                                                                                          
## 69                                                                                                                                                                                                                                                                                          
## 70                                                                                                                                                                                                                                                                                          
## 71                                                                                                                                                                   Toby Jones, Anne Reid, Sinead Matthews, Cecilia Noble, Karl Johnson, Frederick Schmidt, Deborah Findlay, Manjinder Virk
## 72                                                                                                                                                                                                                                                                Kurt Cobain, Courtney Love
## 73                                                                                                                                       Jason Flemyng, Dexter Fletcher, Nick Moran, Jason Statham, Steven Mackintosh, Nicholas Rowe, Nick Marcq, Charles Forbes, Vinnie Jones, Lenny McLean
## 74                                                                                                             Manolo Blahnik, Anna Wintour, Andr? Leon Talley, Paloma Picasso, Candace Bushnell, Iman, Rihanna, Naomi Campbell, Isaac Mizrahi, Rupert Everett, Sofia Coppola, John Galliano
## 75                                                                                                                                                Olga Kurylenko, Javier Botet, Mitch Eakins, Lance E. Nichols, Rosie Fellner, Mackenzie Imsand, Dandy Barrett, Craig Conway, Melissa Bolona
## 76                                                                                                                                                                                                                          Oasis, Alan McGee, Paul McGuigan, Liam Gallagher, Noel Gallagher
## 77                                                                                                                                                                                                                                                                         Tim Piggott-Smith
## 78                                                                                                                                                        Rob Brydon, Bertie Carvel, Gemma Chan, Dominic West, Isaac Hempstead Wright, Bel Powley, Tamsin Greig, Rose Leslie, David Walliams
## 79                                                                                                                                  Jack Whitehall, Joanna Scanlan, Iain Glen, Ethan Lawrence, Layton Williams, Kae Alexander, Weruche Opia, Nikki Runeckles, Charlie Wernham, Jack Binstead
## 80                                                                                                                                                                                                                                                                          Marcus du Sautoy
## 81                                                                                                                                                                                                                                                                                          
## 82                                                                                                                                                                                                                                                                Mary Berry, Paul Hollywood
## 83                                                                                                                                 Simon Bird, James Buckley, Blake Harrison, Joe Thomas, Greg Davies, Emily Head, Martin Trenaman, Belinda Stewart-Wilson, Robin Weaver, Henry Lloyd-Hughes
## 84                                                                                                                                             Steve Coogan, Emma Gilmour, Susan Jane Tanner, Iain Mitchell, James Smith, Om Puri, Steven Waddington, Ben Miller, Lena Headey, Jenny Agutter
## 85                                                                                                                                                                                                                Russell Tovey, Arinz? Kene, Lisa McGrillis, Nico Mirallegro, Rory J. Saper
## 86                                                                                                                                                                                                                                                                                 Iain Glen
## 87                                                                                                                                                                                                                                                                                          
##           country         date_added release_year rating   duration
## 1  United Kingdom          25-Jan-19         2018  TV-MA   1 Season
## 2  United Kingdom          18-Jan-19         2019   TV-G  3 Seasons
## 3  United Kingdom          11-Jan-19         2019  TV-Y7  2 Seasons
## 4  United Kingdom          02-Jan-19         1975     PG     92 min
## 5  United Kingdom          31-Dec-18         1991  TV-MA     28 min
## 6  United Kingdom          31-Dec-18         1992  TV-MA     61 min
## 7  United Kingdom          31-Dec-18         1993  TV-MA     56 min
## 8  United Kingdom          31-Dec-18         2008  TV-MA     58 min
## 9  United Kingdom          31-Dec-18         2011  TV-MA    146 min
## 10 United Kingdom          31-Dec-18         2008  TV-MA    127 min
## 11 United Kingdom          22-Dec-18         2018  TV-MA  5 Seasons
## 12 United Kingdom          15-Dec-18         2018  TV-MA  2 Seasons
## 13 United Kingdom          23-Nov-18         2018  TV-MA  2 Seasons
## 14 United Kingdom          19-Oct-18         2018  TV-MA   1 Season
## 15 United Kingdom          15-Oct-18         2018  TV-MA   1 Season
## 16 United Kingdom          05-Oct-18         2018  TV-MA     89 min
## 17 United Kingdom          02-Oct-18         2014  TV-MA   1 Season
## 18 United Kingdom          02-Oct-18         2014  TV-MA    137 min
## 19 United Kingdom          02-Oct-18         2000  TV-MA     56 min
## 20 United Kingdom          02-Oct-18         1982      R     80 min
## 21 United Kingdom          02-Oct-18         2014  TV-MA     94 min
## 22 United Kingdom          02-Oct-18         2009  TV-MA   1 Season
## 23 United Kingdom          02-Oct-18         1974  TV-14  4 Seasons
## 24 United Kingdom          02-Oct-18         1979      R     94 min
## 25 United Kingdom          02-Oct-18         1989  TV-14     72 min
## 26 United Kingdom          02-Oct-18         2013  TV-MA     60 min
## 27 United Kingdom          01-Oct-18         2018  TV-14   1 Season
## 28 United Kingdom          24-Aug-18         2018  TV-MA   1 Season
## 29 United Kingdom          20-Jul-18         2018  TV-PG   1 Season
## 30 United Kingdom          10-Jul-18         2018  TV-MA  2 Seasons
## 31 United Kingdom          29-Jun-18         2018  TV-MA    101 min
## 32 United Kingdom          29-Jun-18         2018  TV-MA   1 Season
## 33 United Kingdom          22-Jun-18         2018  TV-MA     74 min
## 34 United Kingdom          10-Apr-18         2018  TV-MA     66 min
## 35 United Kingdom          01-Apr-18         2017  TV-MA   1 Season
## 36 United Kingdom          31-Mar-18         2017  TV-MA  2 Seasons
## 37 United Kingdom          31-Mar-18         2017      R     87 min
## 38 United Kingdom          30-Mar-18         2018  TV-14   1 Season
## 39 United Kingdom          27-Mar-18         2018  TV-14   1 Season
## 40 United Kingdom          23-Mar-18         2018  TV-MA   1 Season
## 41 United Kingdom          13-Mar-18         2018  TV-MA     79 min
## 42 United Kingdom          27-Feb-18         2018  TV-MA     69 min
## 43 United Kingdom          22-Feb-18         2017  TV-14   1 Season
## 44 United Kingdom          20-Feb-18         2017  TV-MA  2 Seasons
## 45 United Kingdom          30-Jan-18         2016  TV-MA   1 Season
## 46 United Kingdom          18-Jan-18         2017  TV-MA     99 min
## 47 United Kingdom          01-Jan-18         2018  TV-MA  3 Seasons
## 48 United Kingdom          16-May-18         2017  TV-PG     87 min
## 49 United Kingdom          20-Dec-18         2018  TV-14     95 min
## 50 United Kingdom          01-May-18         2017  TV-MA     70 min
## 51 United Kingdom          26-Jul-18         2015      R    128 min
## 52 United Kingdom          19-Jan-19         1994  TV-PG     49 min
## 53 United Kingdom  December 15, 2018         2014  TV-MA  3 Seasons
## 54 United Kingdom          26-Jan-19         2002      R    108 min
## 55 United Kingdom          03-Dec-18         2017   TV-G   1 Season
## 56 United Kingdom          01-Dec-18         2018  TV-14     99 min
## 57 United Kingdom          15-Sep-18         2017  TV-PG   1 Season
## 58 United Kingdom          01-Oct-18         2018  TV-14   1 Season
## 59 United Kingdom     March 31, 2018         1977  TV-PG 10 Seasons
## 60 United Kingdom          01-Mar-18         2017  TV-MA     53 min
## 61 United Kingdom          26-Jul-18         2015      R    108 min
## 62 United Kingdom     March 31, 2018         2017  TV-14  6 Seasons
## 63 United Kingdom          01-May-18         2017  TV-MA    105 min
## 64 United Kingdom   December 1, 2018         2016  TV-PG  2 Seasons
## 65 United Kingdom          01-Mar-18         2017   TV-G    138 min
## 66 United Kingdom          23-Jun-18         2014  TV-MA     79 min
## 67 United Kingdom          22-Dec-18         2018      R     93 min
## 68 United Kingdom          01-Apr-18         2016  TV-PG     90 min
## 69 United Kingdom          15-Sep-18         2017  TV-MA   1 Season
## 70 United Kingdom          31-Dec-18         2018  TV-MA   1 Season
## 71 United Kingdom          08-May-18         2016  TV-MA     99 min
## 72 United Kingdom          08-Jan-19         1998      R     95 min
## 73 United Kingdom          01-Jan-19         1998      R    108 min
## 74 United Kingdom          15-Jan-18         2017  TV-MA     89 min
## 75 United Kingdom          05-Jan-19         2017  TV-MA     99 min
## 76 United Kingdom          28-Dec-18         2016      R    122 min
## 77 United Kingdom          24-Mar-18         2017  TV-PG     80 min
## 78 United Kingdom          01-Feb-18         2017  TV-PG   1 Season
## 79 United Kingdom          15-Dec-18         2015  TV-MA     87 min
## 80 United Kingdom          01-Feb-18         2011  TV-PG   1 Season
## 81 United Kingdom          23-Jun-18         2016  TV-MA     75 min
## 82 United Kingdom          01-Dec-18         2018  TV-PG  4 Seasons
## 83 United Kingdom    January 1, 2018         2010  TV-MA  3 Seasons
## 84 United Kingdom          01-Aug-18         2001      R     94 min
## 85 United Kingdom          01-Jan-19         2016     NR     87 min
## 86 United Kingdom          01-Nov-18         2017   TV-G     58 min
## 87 United Kingdom          01-Jan-18         2017   TV-G     82 min
##                                                                 listed_in
## 1                     British TV Shows, International TV Shows, TV Dramas
## 2                    British TV Shows, Docuseries, International TV Shows
## 3                                 British TV Shows, Kids' TV, TV Comedies
## 4                            Action & Adventure, Classic Movies, Comedies
## 5                                                         Stand-Up Comedy
## 6                                                         Stand-Up Comedy
## 7                                                         Stand-Up Comedy
## 8                                                         Stand-Up Comedy
## 9                                       Music & Musicals, Stand-Up Comedy
## 10                                      Music & Musicals, Stand-Up Comedy
## 11                                          British TV Shows, TV Comedies
## 12                  British TV Shows, International TV Shows, TV Comedies
## 13                  British TV Shows, International TV Shows, TV Comedies
## 14            British TV Shows, International TV Shows, Romantic TV Shows
## 15                   British TV Shows, International TV Shows, Reality TV
## 16                                               Horror Movies, Thrillers
## 17                              British TV Shows, Docuseries, TV Comedies
## 18                                                               Comedies
## 19                                                Comedies, Documentaries
## 20                                                               Comedies
## 21                                                          Documentaries
## 22                                           British TV Shows, Docuseries
## 23            British TV Shows, Classic & Cult TV, International TV Shows
## 24                                  Classic Movies, Comedies, Cult Movies
## 25                                                               Comedies
## 26                                                          Documentaries
## 27                   British TV Shows, International TV Shows, Reality TV
## 28            British TV Shows, International TV Shows, Romantic TV Shows
## 29                   British TV Shows, International TV Shows, Reality TV
## 30                   British TV Shows, International TV Shows, Reality TV
## 31                                Dramas, International Movies, Thrillers
## 32                   British TV Shows, International TV Shows, Reality TV
## 33                                                   International Movies
## 34                                                        Stand-Up Comedy
## 35                  British TV Shows, International TV Shows, TV Comedies
## 36            British TV Shows, International TV Shows, Romantic TV Shows
## 37                                   Action & Adventure, Sci-Fi & Fantasy
## 38                                                             Docuseries
## 39 British TV Shows, International TV Shows, Stand-Up Comedy & Talk Shows
## 40                    British TV Shows, International TV Shows, TV Dramas
## 41                                                        Stand-Up Comedy
## 42                                                                 Movies
## 43                   British TV Shows, Docuseries, International TV Shows
## 44               British TV Shows, Crime TV Shows, International TV Shows
## 45               British TV Shows, Crime TV Shows, International TV Shows
## 46                    Independent Movies, International Movies, Thrillers
## 47            British TV Shows, International TV Shows, Romantic TV Shows
## 48                                                          Sports Movies
## 49                                                       Sci-Fi & Fantasy
## 50                                                          Documentaries
## 51                  Documentaries, International Movies, Music & Musicals
## 52                                                          Documentaries
## 53                                          British TV Shows, TV Comedies
## 54                                        Documentaries, Music & Musicals
## 55                      British TV Shows, Docuseries, Science & Nature TV
## 56                     Documentaries, International Movies, Sports Movies
## 57                   British TV Shows, International TV Shows, Reality TV
## 58                                           British TV Shows, Docuseries
## 59                       British TV Shows, Classic & Cult TV, TV Comedies
## 60                                    International Movies, Sports Movies
## 61                       Dramas, Independent Movies, International Movies
## 62                            British TV Shows, Crime TV Shows, TV Dramas
## 63                       Dramas, Independent Movies, International Movies
## 64                   British TV Shows, International TV Shows, Reality TV
## 65                                 International Movies, Music & Musicals
## 66                Horror Movies, Independent Movies, International Movies
## 67                                                     Action & Adventure
## 68                                    Documentaries, International Movies
## 69                   British TV Shows, Docuseries, International TV Shows
## 70                                                             Reality TV
## 71                                                              Thrillers
## 72                                        Documentaries, Music & Musicals
## 73                       Action & Adventure, Comedies, Independent Movies
## 74                                    Documentaries, International Movies
## 75                                               Horror Movies, Thrillers
## 76                  Documentaries, International Movies, Music & Musicals
## 77                                    Documentaries, International Movies
## 78                                             British TV Shows, Kids' TV
## 79                                                               Comedies
## 80                   British TV Shows, Docuseries, International TV Shows
## 81           Action & Adventure, Independent Movies, International Movies
## 82                                           British TV Shows, Reality TV
## 83                                          British TV Shows, TV Comedies
## 84                                           Action & Adventure, Comedies
## 85                               Dramas, Independent Movies, LGBTQ Movies
## 86                                                          Documentaries
## 87                            Documentaries, Dramas, International Movies
##            y2
## 1  2019-01-25
## 2  2019-01-18
## 3  2019-01-11
## 4  2019-01-02
## 5  2018-12-31
## 6  2018-12-31
## 7  2018-12-31
## 8  2018-12-31
## 9  2018-12-31
## 10 2018-12-31
## 11 2018-12-22
## 12 2018-12-15
## 13 2018-11-23
## 14 2018-10-19
## 15 2018-10-15
## 16 2018-10-05
## 17 2018-10-02
## 18 2018-10-02
## 19 2018-10-02
## 20 2018-10-02
## 21 2018-10-02
## 22 2018-10-02
## 23 2018-10-02
## 24 2018-10-02
## 25 2018-10-02
## 26 2018-10-02
## 27 2018-10-01
## 28 2018-08-24
## 29 2018-07-20
## 30 2018-07-10
## 31 2018-06-29
## 32 2018-06-29
## 33 2018-06-22
## 34 2018-04-10
## 35 2018-04-01
## 36 2018-03-31
## 37 2018-03-31
## 38 2018-03-30
## 39 2018-03-27
## 40 2018-03-23
## 41 2018-03-13
## 42 2018-02-27
## 43 2018-02-22
## 44 2018-02-20
## 45 2018-01-30
## 46 2018-01-18
## 47 2018-01-01
## 48 2018-05-16
## 49 2018-12-20
## 50 2018-05-01
## 51 2018-07-26
## 52 2019-01-19
## 53 2018-12-15
## 54 2019-01-26
## 55 2018-12-03
## 56 2018-12-01
## 57 2018-09-15
## 58 2018-10-01
## 59 2018-03-31
## 60 2018-03-01
## 61 2018-07-26
## 62 2018-03-31
## 63 2018-05-01
## 64 2018-12-01
## 65 2018-03-01
## 66 2018-06-23
## 67 2018-12-22
## 68 2018-04-01
## 69 2018-09-15
## 70 2018-12-31
## 71 2018-05-08
## 72 2019-01-08
## 73 2019-01-01
## 74 2018-01-15
## 75 2019-01-05
## 76 2018-12-28
## 77 2018-03-24
## 78 2018-02-01
## 79 2018-12-15
## 80 2018-02-01
## 81 2018-06-23
## 82 2018-12-01
## 83 2018-01-01
## 84 2018-08-01
## 85 2019-01-01
## 86 2018-11-01
## 87 2018-01-01
# 6가지
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")
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
summary(df)
##        X              Q1              Q2              Q3              Q4       
##  Min.   :   1   Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.: 482   1st Qu.:3.000   1st Qu.:3.000   1st Qu.:2.000   1st Qu.:2.000  
##  Median : 963   Median :4.000   Median :3.000   Median :3.000   Median :3.000  
##  Mean   : 963   Mean   :3.536   Mean   :3.291   Mean   :2.928   Mean   :3.061  
##  3rd Qu.:1444   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1925   Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000  
##        Q5              Q6              Q7              Q8       
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:2.000   1st Qu.:2.000   1st Qu.:2.000   1st Qu.:2.000  
##  Median :3.000   Median :3.000   Median :3.000   Median :3.000  
##  Mean   :3.041   Mean   :2.796   Mean   :3.086   Mean   :3.049  
##  3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000  
##        Q9             Q10             Q11            Q12             Q13       
##  Min.   :1.000   Min.   :1.000   Min.   :1.00   Min.   :1.000   Min.   :1.000  
##  1st Qu.:2.000   1st Qu.:2.000   1st Qu.:3.00   1st Qu.:3.000   1st Qu.:3.000  
##  Median :3.000   Median :3.000   Median :4.00   Median :4.000   Median :4.000  
##  Mean   :3.066   Mean   :2.883   Mean   :3.47   Mean   :3.421   Mean   :3.588  
##  3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.00   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.00   Max.   :5.000   Max.   :5.000  
##       Q14             Q15             Q16             Q17       
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:3.000   1st Qu.:3.000   1st Qu.:3.000   1st Qu.:3.000  
##  Median :4.000   Median :4.000   Median :4.000   Median :4.000  
##  Mean   :3.716   Mean   :3.542   Mean   :3.791   Mean   :3.516  
##  3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000  
##       Q18             Q19             Q20           Gender          EDU       
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   female:1136   Min.   :1.000  
##  1st Qu.:4.000   1st Qu.:3.000   1st Qu.:3.000   male  : 789   1st Qu.:2.000  
##  Median :4.000   Median :3.000   Median :3.000                 Median :3.000  
##  Mean   :3.804   Mean   :3.364   Mean   :3.349                 Mean   :2.616  
##  3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:4.000                 3rd Qu.:3.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000                 Max.   :4.000  
##        BF              BM          Happiness         Peace      
##  Min.   :1.000   Min.   :1.000   Min.   :1.400   Min.   :1.200  
##  1st Qu.:2.600   1st Qu.:2.400   1st Qu.:3.000   1st Qu.:3.200  
##  Median :3.200   Median :3.000   Median :3.600   Median :3.600  
##  Mean   :3.172   Mean   :2.976   Mean   :3.547   Mean   :3.564  
##  3rd Qu.:3.800   3rd Qu.:3.600   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000
colSums(is.na(df))
##         X        Q1        Q2        Q3        Q4        Q5        Q6        Q7 
##         0         0         0         0         0         0         0         0 
##        Q8        Q9       Q10       Q11       Q12       Q13       Q14       Q15 
##         0         0         0         0         0         0         0         0 
##       Q16       Q17       Q18       Q19       Q20    Gender       EDU        BF 
##         0         0         0         0         0         0         0         0 
##        BM Happiness     Peace 
##         0         0         0
control <- trainControl(method="repeatedcv", number=5)
model <- train(Gender ~., data=df, method="rf",
                preProcess=c("center","scale"), trControl=control)
model
## Random Forest 
## 
## 1925 samples
##   26 predictor
##    2 classes: 'female', 'male' 
## 
## Pre-processing: centered (26), scaled (26) 
## Resampling: Cross-Validated (5 fold, repeated 1 times) 
## Summary of sample sizes: 1540, 1540, 1541, 1540, 1539 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.6280537  0.1791601
##   14    0.6202735  0.1781894
##   26    0.6228682  0.1865136
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
predict(model,newdata=df) %>% confusionMatrix(df$Gender)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction female male
##     female   1126    8
##     male       10  781
##                                           
##                Accuracy : 0.9906          
##                  95% CI : (0.9853, 0.9944)
##     No Information Rate : 0.5901          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.9807          
##                                           
##  Mcnemar's Test P-Value : 0.8137          
##                                           
##             Sensitivity : 0.9912          
##             Specificity : 0.9899          
##          Pos Pred Value : 0.9929          
##          Neg Pred Value : 0.9874          
##              Prevalence : 0.5901          
##          Detection Rate : 0.5849          
##    Detection Prevalence : 0.5891          
##       Balanced Accuracy : 0.9905          
##                                           
##        'Positive' Class : female          
## 
# TP : 1135
# FN : 9
# TN : 780
# RP : 11