#4교시(실습)
getwd()
## [1] "D:/도교공/20240129-20240201"
setwd('d:/data')
#install.packages('plyr')

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(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, …
df$Gender<-as.factor(df$Gender)

glimpse(df) #factor함수로 바뀌었는지 확인(Gender)
## 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> 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, …
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
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
library(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
library(mlbench)
set.seed(999)
inTraining<- createDataPartition(y = df$Gender,
                                 p = .7, list = FALSE)
training <- df[ inTraining,]
testing <-df[-inTraining,]
fitControl <- trainControl(method = "repeatedcv",
                           number = 5,
                           repeats = 2,
                           classProbs = TRUE,
                           summaryFunction = twoClassSummary)


rffit1 <- train(Gender~., data=training,
                preProcess=c('center','scale'),
                trControl =fitControl,
                verbose = FALSE,
                metric='ROC')
rffit1
## Random Forest 
## 
## 1349 samples
##   26 predictor
##    2 classes: 'female', 'male' 
## 
## Pre-processing: centered (26), scaled (26) 
## Resampling: Cross-Validated (5 fold, repeated 2 times) 
## Summary of sample sizes: 1079, 1080, 1078, 1079, 1080, 1079, ... 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    2    0.6568437  0.8247602  0.3779115
##   14    0.6405988  0.7725865  0.4140868
##   26    0.6385105  0.7594025  0.4203767
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
predict(rffit1,newdata=testing) %>% confusionMatrix(testing$Gender)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction female male
##     female    260  154
##     male       80   82
##                                           
##                Accuracy : 0.5938          
##                  95% CI : (0.5524, 0.6342)
##     No Information Rate : 0.5903          
##     P-Value [Acc > NIR] : 0.4504          
##                                           
##                   Kappa : 0.1178          
##                                           
##  Mcnemar's Test P-Value : 1.823e-06       
##                                           
##             Sensitivity : 0.7647          
##             Specificity : 0.3475          
##          Pos Pred Value : 0.6280          
##          Neg Pred Value : 0.5062          
##              Prevalence : 0.5903          
##          Detection Rate : 0.4514          
##    Detection Prevalence : 0.7188          
##       Balanced Accuracy : 0.5561          
##                                           
##        'Positive' Class : female          
## 
glfit <- train(Gender~.,data=training,
               method="glmnet",
               tuneLength = 20,
               trControl = fitControl,
               verbose = FALSE,
               metric = "ROC")
print(glfit) %>% head
## glmnet 
## 
## 1349 samples
##   26 predictor
##    2 classes: 'female', 'male' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold, repeated 2 times) 
## Summary of sample sizes: 1079, 1080, 1079, 1079, 1079, 1079, ... 
## Resampling results across tuning parameters:
## 
##   alpha      lambda        ROC        Sens       Spec        
##   0.1000000  0.0004899574  0.6350451  0.7952555  0.3778705979
##   0.1000000  0.0007596867  0.6350451  0.7952555  0.3778705979
##   0.1000000  0.0011779061  0.6350451  0.7952555  0.3778705979
##   0.1000000  0.0018263621  0.6350451  0.7952555  0.3778705979
##   0.1000000  0.0028318032  0.6350621  0.7952555  0.3778705979
##   0.1000000  0.0043907555  0.6353122  0.7958805  0.3742342342
##   0.1000000  0.0068079358  0.6359369  0.7996462  0.3724160524
##   0.1000000  0.0105558118  0.6360952  0.8009041  0.3706060606
##   0.1000000  0.0163669528  0.6364425  0.8078105  0.3624651925
##   0.1000000  0.0253772186  0.6375460  0.8134709  0.3498116298
##   0.1000000  0.0393477779  0.6384006  0.8228931  0.3235872236
##   0.1000000  0.0610093505  0.6393013  0.8436164  0.2874037674
##   0.1000000  0.0945959606  0.6399512  0.8750197  0.2430630631
##   0.1473684  0.0004899574  0.6351529  0.7952555  0.3769615070
##   0.1473684  0.0007596867  0.6351529  0.7952555  0.3769615070
##   0.1473684  0.0011779061  0.6351529  0.7952555  0.3769615070
##   0.1473684  0.0018263621  0.6351529  0.7952555  0.3769615070
##   0.1473684  0.0028318032  0.6351869  0.7958805  0.3769615070
##   0.1473684  0.0043907555  0.6355112  0.7990173  0.3751433251
##   0.1473684  0.0068079358  0.6358118  0.7990212  0.3724242424
##   0.1473684  0.0105558118  0.6360792  0.8046737  0.3697133497
##   0.1473684  0.0163669528  0.6370711  0.8078066  0.3597706798
##   0.1473684  0.0253772186  0.6379187  0.8147248  0.3371498771
##   0.1473684  0.0393477779  0.6386731  0.8285299  0.3127190827
##   0.1473684  0.0610093505  0.6396748  0.8586910  0.2702293202
##   0.1473684  0.0945959606  0.6398334  0.8976219  0.2096150696
##   0.1947368  0.0004899574  0.6351192  0.7952555  0.3760524161
##   0.1947368  0.0007596867  0.6351192  0.7952555  0.3760524161
##   0.1947368  0.0011779061  0.6351192  0.7952555  0.3760524161
##   0.1947368  0.0018263621  0.6351192  0.7952555  0.3760524161
##   0.1947368  0.0028318032  0.6353688  0.7958805  0.3751433251
##   0.1947368  0.0043907555  0.6356644  0.7977634  0.3742342342
##   0.1947368  0.0068079358  0.6357727  0.7996462  0.3715233415
##   0.1947368  0.0105558118  0.6364483  0.8059237  0.3642997543
##   0.1947368  0.0163669528  0.6375898  0.8090684  0.3525389025
##   0.1947368  0.0253772186  0.6380080  0.8153538  0.3317199017
##   0.1947368  0.0393477779  0.6391036  0.8379560  0.3028009828
##   0.1947368  0.0610093505  0.6397084  0.8706093  0.2575675676
##   0.1947368  0.0945959606  0.6396152  0.9240134  0.1662571663
##   0.2421053  0.0004899574  0.6351645  0.7952555  0.3760524161
##   0.2421053  0.0007596867  0.6351645  0.7952555  0.3760524161
##   0.2421053  0.0011779061  0.6351645  0.7952555  0.3760524161
##   0.2421053  0.0018263621  0.6351645  0.7952555  0.3760524161
##   0.2421053  0.0028318032  0.6353691  0.7971384  0.3751433251
##   0.2421053  0.0043907555  0.6357550  0.7990212  0.3733333333
##   0.2421053  0.0068079358  0.6360289  0.8027909  0.3715233415
##   0.2421053  0.0105558118  0.6366451  0.8071816  0.3615888616
##   0.2421053  0.0163669528  0.6375172  0.8103263  0.3452989353
##   0.2421053  0.0253772186  0.6381877  0.8222524  0.3317199017
##   0.2421053  0.0393477779  0.6393529  0.8454992  0.2910483210
##   0.2421053  0.0610093505  0.6400560  0.8838050  0.2376904177
##   0.2421053  0.0945959606  0.6385856  0.9447484  0.1364373464
##   0.2894737  0.0004899574  0.6351021  0.7958844  0.3769615070
##   0.2894737  0.0007596867  0.6351021  0.7958844  0.3769615070
##   0.2894737  0.0011779061  0.6351021  0.7958844  0.3769615070
##   0.2894737  0.0018263621  0.6351589  0.7965094  0.3760524161
##   0.2894737  0.0028318032  0.6355735  0.7977634  0.3760524161
##   0.2894737  0.0043907555  0.6356807  0.7983962  0.3733415233
##   0.2894737  0.0068079358  0.6361137  0.8046737  0.3679197379
##   0.2894737  0.0105558118  0.6369606  0.8046698  0.3552497952
##   0.2894737  0.0163669528  0.6375296  0.8115841  0.3407698608
##   0.2894737  0.0253772186  0.6386046  0.8266431  0.3245126945
##   0.2894737  0.0393477779  0.6393484  0.8536635  0.2856101556
##   0.2894737  0.0610093505  0.6398019  0.9051572  0.2042178542
##   0.2894737  0.0945959606  0.6362922  0.9629560  0.1030466830
##   0.3368421  0.0004899574  0.6351192  0.7958844  0.3778624079
##   0.3368421  0.0007596867  0.6351192  0.7958844  0.3778624079
##   0.3368421  0.0011779061  0.6351192  0.7958844  0.3778624079
##   0.3368421  0.0018263621  0.6352041  0.7965094  0.3769533170
##   0.3368421  0.0028318032  0.6356186  0.7977634  0.3760524161
##   0.3368421  0.0043907555  0.6358243  0.7990252  0.3733415233
##   0.3368421  0.0068079358  0.6363524  0.8046737  0.3661179361
##   0.3368421  0.0105558118  0.6372418  0.8065566  0.3534398034
##   0.3368421  0.0163669528  0.6378365  0.8140998  0.3389598690
##   0.3368421  0.0253772186  0.6388253  0.8329324  0.3145372645
##   0.3368421  0.0393477779  0.6397943  0.8618239  0.2792956593
##   0.3368421  0.0610093505  0.6388151  0.9158412  0.1834398034
##   0.3368421  0.0945959606  0.6336973  0.9780267  0.0632678133
##   0.3842105  0.0004899574  0.6351475  0.7958844  0.3778624079
##   0.3842105  0.0007596867  0.6351475  0.7958844  0.3778624079
##   0.3842105  0.0011779061  0.6351475  0.7958844  0.3778624079
##   0.3842105  0.0018263621  0.6352554  0.7958844  0.3769533170
##   0.3842105  0.0028318032  0.6355567  0.7983923  0.3751433251
##   0.3842105  0.0043907555  0.6358355  0.8002791  0.3742506143
##   0.3842105  0.0068079358  0.6364470  0.8046698  0.3652170352
##   0.3842105  0.0105558118  0.6372305  0.8090723  0.3489271089
##   0.3842105  0.0163669528  0.6381506  0.8178616  0.3416707617
##   0.3842105  0.0253772186  0.6392053  0.8354481  0.3090990991
##   0.3842105  0.0393477779  0.6400865  0.8712461  0.2612285012
##   0.3842105  0.0610093505  0.6375977  0.9265252  0.1635544636
##   0.3842105  0.0945959606  0.6286838  0.9868160  0.0370352170
##   0.4315789  0.0004899574  0.6351764  0.7958805  0.3769533170
##   0.4315789  0.0007596867  0.6351764  0.7958805  0.3769533170
##   0.4315789  0.0011779061  0.6351764  0.7958805  0.3769533170
##   0.4315789  0.0018263621  0.6354373  0.7977634  0.3769533170
##   0.4315789  0.0028318032  0.6355625  0.7983962  0.3760442260
##   0.4315789  0.0043907555  0.6358979  0.8021659  0.3724488124
##   0.4315789  0.0068079358  0.6367046  0.8040409  0.3624815725
##   0.4315789  0.0105558118  0.6372690  0.8103263  0.3480180180
##   0.4315789  0.0163669528  0.6381897  0.8222563  0.3389598690
##   0.4315789  0.0253772186  0.6391772  0.8404717  0.3045864046
##   0.4315789  0.0393477779  0.6399233  0.8800354  0.2458394758
##   0.4315789  0.0610093505  0.6361654  0.9409788  0.1436445536
##   0.4315789  0.0945959606  0.6229345  0.9956014  0.0081408681
##   0.4789474  0.0004899574  0.6351988  0.7952516  0.3778624079
##   0.4789474  0.0007596867  0.6351988  0.7952516  0.3778624079
##   0.4789474  0.0011779061  0.6352046  0.7952516  0.3769533170
##   0.4789474  0.0018263621  0.6355504  0.7977634  0.3769533170
##   0.4789474  0.0028318032  0.6356136  0.7983962  0.3742424242
##   0.4789474  0.0043907555  0.6359084  0.8040487  0.3724488124
##   0.4789474  0.0068079358  0.6367757  0.8034198  0.3597624898
##   0.4789474  0.0105558118  0.6374024  0.8097013  0.3471089271
##   0.4789474  0.0163669528  0.6385391  0.8222484  0.3371498771
##   0.4789474  0.0253772186  0.6394652  0.8467571  0.3000655201
##   0.4789474  0.0393477779  0.6391423  0.8913365  0.2232350532
##   0.4789474  0.0610093505  0.6346219  0.9529009  0.1237837838
##   0.4789474  0.0945959606  0.6170770  0.9993750  0.0027108927
##   0.5263158  0.0004899574  0.6351532  0.7958805  0.3769533170
##   0.5263158  0.0007596867  0.6351532  0.7958805  0.3769533170
##   0.5263158  0.0011779061  0.6351532  0.7952555  0.3769533170
##   0.5263158  0.0018263621  0.6356528  0.7977634  0.3769533170
##   0.5263158  0.0028318032  0.6357276  0.7990252  0.3751515152
##   0.5263158  0.0043907555  0.6361073  0.8046737  0.3706388206
##   0.5263158  0.0068079358  0.6368777  0.8040487  0.3570597871
##   0.5263158  0.0105558118  0.6377519  0.8103302  0.3489271089
##   0.5263158  0.0163669528  0.6387972  0.8235102  0.3308353808
##   0.5263158  0.0253772186  0.6396745  0.8530346  0.2973546274
##   0.5263158  0.0393477779  0.6383453  0.9038994  0.2069615070
##   0.5263158  0.0610093505  0.6321491  0.9648388  0.0930794431
##   0.5263158  0.0945959606  0.6113287  1.0000000  0.0000000000
##   0.5736842  0.0004899574  0.6351022  0.7958805  0.3769533170
##   0.5736842  0.0007596867  0.6351022  0.7958805  0.3769533170
##   0.5736842  0.0011779061  0.6351474  0.7952555  0.3769533170
##   0.5736842  0.0018263621  0.6354996  0.7983923  0.3769533170
##   0.5736842  0.0028318032  0.6359093  0.7996502  0.3751515152
##   0.5736842  0.0043907555  0.6362714  0.8040448  0.3688370188
##   0.5736842  0.0068079358  0.6368837  0.8053066  0.3570679771
##   0.5736842  0.0105558118  0.6378956  0.8109552  0.3489271089
##   0.5736842  0.0163669528  0.6387495  0.8304206  0.3226617527
##   0.5736842  0.0253772186  0.6395895  0.8567964  0.2928419328
##   0.5736842  0.0393477779  0.6376917  0.9145912  0.1942997543
##   0.5736842  0.0610093505  0.6292302  0.9748939  0.0704914005
##   0.5736842  0.0945959606  0.6034396  1.0000000  0.0000000000
##   0.6210526  0.0004899574  0.6350965  0.7958805  0.3769533170
##   0.6210526  0.0007596867  0.6350965  0.7958805  0.3769533170
##   0.6210526  0.0011779061  0.6352382  0.7952555  0.3769533170
##   0.6210526  0.0018263621  0.6354831  0.7977673  0.3760442260
##   0.6210526  0.0028318032  0.6359036  0.8002791  0.3751515152
##   0.6210526  0.0043907555  0.6363393  0.8040448  0.3670270270
##   0.6210526  0.0068079358  0.6370883  0.8059355  0.3534643735
##   0.6210526  0.0105558118  0.6380480  0.8147248  0.3461916462
##   0.6210526  0.0163669528  0.6387900  0.8329324  0.3199590500
##   0.6210526  0.0253772186  0.6397055  0.8618278  0.2829074529
##   0.6210526  0.0393477779  0.6366753  0.9183608  0.1798198198
##   0.6210526  0.0610093505  0.6254559  0.9836753  0.0487714988
##   0.6210526  0.0945959606  0.5886789  1.0000000  0.0000000000
##   0.6684211  0.0004899574  0.6350908  0.7952555  0.3769533170
##   0.6684211  0.0007596867  0.6350908  0.7952555  0.3769533170
##   0.6684211  0.0011779061  0.6353294  0.7971344  0.3769533170
##   0.6684211  0.0018263621  0.6353919  0.7977673  0.3760442260
##   0.6684211  0.0028318032  0.6358239  0.8015369  0.3742588043
##   0.6684211  0.0043907555  0.6364557  0.8027909  0.3661179361
##   0.6684211  0.0068079358  0.6371274  0.8071934  0.3516380016
##   0.6684211  0.0105558118  0.6380475  0.8159709  0.3470925471
##   0.6684211  0.0163669528  0.6389551  0.8329363  0.3154381654
##   0.6684211  0.0253772186  0.6395234  0.8699843  0.2693693694
##   0.6684211  0.0393477779  0.6353241  0.9246423  0.1599344799
##   0.6684211  0.0610093505  0.6224439  0.9893239  0.0307207207
##   0.6684211  0.0945959606  0.5823404  1.0000000  0.0000000000
##   0.7157895  0.0004899574  0.6351022  0.7952555  0.3769533170
##   0.7157895  0.0007596867  0.6351022  0.7952555  0.3769533170
##   0.7157895  0.0011779061  0.6353749  0.7977634  0.3769533170
##   0.7157895  0.0018263621  0.6355167  0.7977673  0.3751433251
##   0.7157895  0.0028318032  0.6357154  0.8021659  0.3733579034
##   0.7157895  0.0043907555  0.6364609  0.8027909  0.3652088452
##   0.7157895  0.0068079358  0.6371433  0.8078223  0.3525389025
##   0.7157895  0.0105558118  0.6381956  0.8172288  0.3452907453
##   0.7157895  0.0163669528  0.6391728  0.8354520  0.3145372645
##   0.7157895  0.0253772186  0.6389955  0.8768947  0.2630303030
##   0.7157895  0.0393477779  0.6341467  0.9321816  0.1499918100
##   0.7157895  0.0610093505  0.6196222  0.9943475  0.0081408681
##   0.7157895  0.0945959606  0.5499635  1.0000000  0.0000000000
##   0.7631579  0.0004899574  0.6351190  0.7952555  0.3769533170
##   0.7631579  0.0007596867  0.6351190  0.7952555  0.3769533170
##   0.7631579  0.0011779061  0.6353746  0.7977634  0.3769533170
##   0.7631579  0.0018263621  0.6355739  0.7977673  0.3751515152
##   0.7631579  0.0028318032  0.6357949  0.8040487  0.3724570025
##   0.7631579  0.0043907555  0.6364926  0.8034159  0.3633988534
##   0.7631579  0.0068079358  0.6374535  0.8071934  0.3498280098
##   0.7631579  0.0105558118  0.6383905  0.8191077  0.3434725635
##   0.7631579  0.0163669528  0.6391234  0.8379638  0.3100163800
##   0.7631579  0.0253772186  0.6384875  0.8812854  0.2476740377
##   0.7631579  0.0393477779  0.6331026  0.9409748  0.1373300573
##   0.7631579  0.0610093505  0.6151331  0.9981171  0.0036117936
##   0.7631579  0.0945959606  0.5451183  1.0000000  0.0000000000
##   0.8105263  0.0004899574  0.6350907  0.7946266  0.3778542179
##   0.8105263  0.0007596867  0.6350907  0.7946266  0.3778542179
##   0.8105263  0.0011779061  0.6354882  0.7977634  0.3769533170
##   0.8105263  0.0018263621  0.6356417  0.7990252  0.3751515152
##   0.8105263  0.0028318032  0.6359826  0.8034198  0.3724488124
##   0.8105263  0.0043907555  0.6366454  0.8027909  0.3615970516
##   0.8105263  0.0068079358  0.6374625  0.8084434  0.3525389025
##   0.8105263  0.0105558118  0.6385638  0.8216234  0.3398607699
##   0.8105263  0.0163669528  0.6393214  0.8423624  0.3072972973
##   0.8105263  0.0253772186  0.6379717  0.8907115  0.2314004914
##   0.8105263  0.0393477779  0.6314703  0.9497642  0.1228910729
##   0.8105263  0.0610093505  0.6117166  0.9993750  0.0009009009
##   0.8105263  0.0945959606  0.5129035  1.0000000  0.0000000000
##   0.8578947  0.0004899574  0.6351079  0.7946266  0.3778542179
##   0.8578947  0.0007596867  0.6351136  0.7946266  0.3778542179
##   0.8578947  0.0011779061  0.6355284  0.7977634  0.3769533170
##   0.8578947  0.0018263621  0.6356874  0.7990252  0.3751515152
##   0.8578947  0.0028318032  0.6361471  0.8034198  0.3706388206
##   0.8578947  0.0043907555  0.6366510  0.8034237  0.3624979525
##   0.8578947  0.0068079358  0.6377201  0.8097013  0.3516380016
##   0.8578947  0.0105558118  0.6385657  0.8228774  0.3362489762
##   0.8578947  0.0163669528  0.6393727  0.8461360  0.3027764128
##   0.8578947  0.0253772186  0.6374586  0.8976140  0.2178132678
##   0.8578947  0.0393477779  0.6296838  0.9604403  0.1075348075
##   0.8578947  0.0610093505  0.6071518  1.0000000  0.0000000000
##   0.8578947  0.0945959606  0.5034280  1.0000000  0.0000000000
##   0.9052632  0.0004899574  0.6351306  0.7946266  0.3778542179
##   0.9052632  0.0007596867  0.6351477  0.7946266  0.3778542179
##   0.9052632  0.0011779061  0.6355625  0.7977634  0.3769533170
##   0.9052632  0.0018263621  0.6357670  0.7996502  0.3751515152
##   0.9052632  0.0028318032  0.6361579  0.8040448  0.3706388206
##   0.9052632  0.0043907555  0.6367871  0.8040527  0.3615970516
##   0.9052632  0.0068079358  0.6377820  0.8090723  0.3525389025
##   0.9052632  0.0105558118  0.6388101  0.8253931  0.3362489762
##   0.9052632  0.0163669528  0.6391865  0.8505189  0.3000737101
##   0.9052632  0.0253772186  0.6368321  0.9045322  0.2114905815
##   0.9052632  0.0393477779  0.6274989  0.9698624  0.0867403767
##   0.9052632  0.0610093505  0.6019734  1.0000000  0.0000000000
##   0.9052632  0.0945959606  0.5000000  1.0000000  0.0000000000
##   0.9526316  0.0004899574  0.6351419  0.7946266  0.3778542179
##   0.9526316  0.0007596867  0.6351817  0.7946266  0.3778542179
##   0.9526316  0.0011779061  0.6355509  0.7977673  0.3769533170
##   0.9526316  0.0018263621  0.6358352  0.7996502  0.3751515152
##   0.9526316  0.0028318032  0.6361463  0.8040448  0.3697297297
##   0.9526316  0.0043907555  0.6369631  0.8040527  0.3579852580
##   0.9526316  0.0068079358  0.6378381  0.8115841  0.3525389025
##   0.9526316  0.0105558118  0.6386029  0.8285377  0.3299180999
##   0.9526316  0.0163669528  0.6395048  0.8542846  0.2964701065
##   0.9526316  0.0253772186  0.6363895  0.9101926  0.2042588043
##   0.9526316  0.0393477779  0.6252104  0.9755228  0.0704832105
##   0.9526316  0.0610093505  0.5913148  1.0000000  0.0000000000
##   0.9526316  0.0945959606  0.5000000  1.0000000  0.0000000000
##   1.0000000  0.0004899574  0.6351249  0.7952555  0.3778542179
##   1.0000000  0.0007596867  0.6352329  0.7952555  0.3769533170
##   1.0000000  0.0011779061  0.6354715  0.7977673  0.3760442260
##   1.0000000  0.0018263621  0.6357838  0.8002791  0.3751597052
##   1.0000000  0.0028318032  0.6362262  0.8040448  0.3688288288
##   1.0000000  0.0043907555  0.6370370  0.8053105  0.3597952498
##   1.0000000  0.0068079358  0.6378662  0.8122052  0.3525307125
##   1.0000000  0.0105558118  0.6385377  0.8297956  0.3235790336
##   1.0000000  0.0163669528  0.6392025  0.8586792  0.2928501229
##   1.0000000  0.0253772186  0.6352693  0.9095637  0.1925225225
##   1.0000000  0.0393477779  0.6235541  0.9817964  0.0523996724
##   1.0000000  0.0610093505  0.5870672  1.0000000  0.0000000000
##   1.0000000  0.0945959606  0.5000000  1.0000000  0.0000000000
## 
## ROC was used to select the optimal model using the largest value.
## The final values used for the model were alpha = 0.3842105 and lambda
##  = 0.03934778.
##  alpha       lambda         ROC         Sens        Spec          
##  "0.1000000" "0.0004899574" "0.6350451" "0.7952555" "0.3778705979"
##  "0.1000000" "0.0007596867" "0.6350451" "0.7952555" "0.3778705979"
##  "0.1000000" "0.0011779061" "0.6350451" "0.7952555" "0.3778705979"
##  "0.1000000" "0.0018263621" "0.6350451" "0.7952555" "0.3778705979"
##  "0.1000000" "0.0028318032" "0.6350621" "0.7952555" "0.3778705979"
##  "0.1000000" "0.0043907555" "0.6353122" "0.7958805" "0.3742342342"
pred_g <- predict(glfit, testing)
confusionMatrix(pred_g, testing$Gender)
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction female male
##     female    294  188
##     male       46   48
##                                           
##                Accuracy : 0.5938          
##                  95% CI : (0.5524, 0.6342)
##     No Information Rate : 0.5903          
##     P-Value [Acc > NIR] : 0.4504          
##                                           
##                   Kappa : 0.075           
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.8647          
##             Specificity : 0.2034          
##          Pos Pred Value : 0.6100          
##          Neg Pred Value : 0.5106          
##              Prevalence : 0.5903          
##          Detection Rate : 0.5104          
##    Detection Prevalence : 0.8368          
##       Balanced Accuracy : 0.5340          
##                                           
##        'Positive' Class : female          
## 

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.