setwd('c:/source data')
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(ggplot2)
library(caret)
## 필요한 패키지를 로딩중입니다: lattice
ls()
## character(0)
set.seed(1)
data(iris)
idx<-createDataPartition(iris$Species,p=0.8)
table(iris[idx$Resample1,"Species"])
## 
##     setosa versicolor  virginica 
##         40         40         40
table(iris[-idx$Resample1,"Species"])
## 
##     setosa versicolor  virginica 
##         10         10         10
idx<-createDataPartition(iris$Species,p=0.8,list=FALSE)
train<-iris[idx,]
test<-iris[-idx,]
nrow(train)
## [1] 120
nrow(test)
## [1] 30
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
data(iris)
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
data(iris)
sca_iris<-preProcess(iris,method="range")
sca_iris
## Created from 150 samples and 5 variables
## 
## Pre-processing:
##   - ignored (1)
##   - re-scaling to [0, 1] (4)
sca_iris_p<-predict(sca_iris,iris)
head(sca_iris_p)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1   0.22222222   0.6250000   0.06779661  0.04166667  setosa
## 2   0.16666667   0.4166667   0.06779661  0.04166667  setosa
## 3   0.11111111   0.5000000   0.05084746  0.04166667  setosa
## 4   0.08333333   0.4583333   0.08474576  0.04166667  setosa
## 5   0.19444444   0.6666667   0.06779661  0.04166667  setosa
## 6   0.30555556   0.7916667   0.11864407  0.12500000  setosa
describe(sca_iris_p)
##              vars   n mean   sd median trimmed  mad min max range  skew
## Sepal.Length    1 150 0.43 0.23   0.42    0.42 0.29   0   1     1  0.31
## Sepal.Width     2 150 0.44 0.18   0.42    0.43 0.19   0   1     1  0.31
## Petal.Length    3 150 0.47 0.30   0.57    0.47 0.31   0   1     1 -0.27
## Petal.Width     4 150 0.46 0.32   0.50    0.45 0.43   0   1     1 -0.10
## Species*        5 150 2.00 0.82   2.00    2.00 1.48   1   3     2  0.00
##              kurtosis   se
## Sepal.Length    -0.61 0.02
## Sepal.Width      0.14 0.01
## Petal.Length    -1.42 0.02
## Petal.Width     -1.36 0.03
## Species*        -1.52 0.07
data(iris)
bc_iris<-preProcess(iris,method="BoxCox")
bc_iris
## Created from 150 samples and 5 variables
## 
## Pre-processing:
##   - Box-Cox transformation (4)
##   - ignored (1)
## 
## Lambda estimates for Box-Cox transformation:
## -0.1, 0.3, 0.9, 0.6
bc_iris_p<-predict(bc_iris,iris)
head(bc_iris_p)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1     1.629241    1.520660          1.4  -1.0321154  setosa
## 2     1.589235    1.301297          1.4  -1.0321154  setosa
## 3     1.547563    1.391905          1.3  -1.0321154  setosa
## 4     1.526056    1.347113          1.5  -1.0321154  setosa
## 5     1.609438    1.561856          1.4  -1.0321154  setosa
## 6     1.686399    1.680826          1.7  -0.7048667  setosa
describe(bc_iris_p)
##              vars   n mean   sd median trimmed  mad   min  max range  skew
## Sepal.Length    1 150 1.76 0.14   1.76    1.75 0.17  1.46 2.07  0.61  0.04
## Sepal.Width     2 150 1.32 0.20   1.30    1.32 0.20  0.77 1.87  1.10  0.01
## Petal.Length    3 150 3.76 1.77   4.35    3.76 1.85  1.00 6.90  5.90 -0.27
## Petal.Width     4 150 0.07 0.79   0.28    0.09 0.85 -1.25 1.22  2.47 -0.36
## Species*        5 150 2.00 0.82   2.00    2.00 1.48  1.00 3.00  2.00  0.00
##              kurtosis   se
## Sepal.Length    -0.78 0.01
## Sepal.Width      0.02 0.02
## Petal.Length    -1.42 0.14
## Petal.Width     -1.38 0.06
## Species*        -1.52 0.07
par(mfrow=c(1,2))
plot(density(x=iris$Sepal.Width))
plot(density(x=bc_iris_p$Sepal.Width))

data(iris)
pca_iris<-preProcess(iris,method="pca")
pca_iris
## Created from 150 samples and 5 variables
## 
## Pre-processing:
##   - centered (4)
##   - ignored (1)
##   - principal component signal extraction (4)
##   - scaled (4)
## 
## PCA needed 2 components to capture 95 percent of the variance
pca_iris_p<-predict(pca_iris,iris)
head(pca_iris_p)
##   Species       PC1        PC2
## 1  setosa -2.257141 -0.4784238
## 2  setosa -2.074013  0.6718827
## 3  setosa -2.356335  0.3407664
## 4  setosa -2.291707  0.5953999
## 5  setosa -2.381863 -0.6446757
## 6  setosa -2.068701 -1.4842053
describe(bc_iris_p)
##              vars   n mean   sd median trimmed  mad   min  max range  skew
## Sepal.Length    1 150 1.76 0.14   1.76    1.75 0.17  1.46 2.07  0.61  0.04
## Sepal.Width     2 150 1.32 0.20   1.30    1.32 0.20  0.77 1.87  1.10  0.01
## Petal.Length    3 150 3.76 1.77   4.35    3.76 1.85  1.00 6.90  5.90 -0.27
## Petal.Width     4 150 0.07 0.79   0.28    0.09 0.85 -1.25 1.22  2.47 -0.36
## Species*        5 150 2.00 0.82   2.00    2.00 1.48  1.00 3.00  2.00  0.00
##              kurtosis   se
## Sepal.Length    -0.78 0.01
## Sepal.Width      0.02 0.02
## Petal.Length    -1.42 0.14
## Petal.Width     -1.38 0.06
## Species*        -1.52 0.07
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
head(predict(dummies,newdata=etitanic))
##   pclass.1st pclass.2nd pclass.3rd sex.female sex.male     age sibsp parch
## 1          1          0          0          1        0 29.0000     0     0
## 2          1          0          0          0        1  0.9167     1     2
## 3          1          0          0          1        0  2.0000     1     2
## 4          1          0          0          0        1 30.0000     1     2
## 5          1          0          0          1        0 25.0000     1     2
## 6          1          0          0          0        1 48.0000     0     0
data("airquality")
colSums(is.na(airquality))
##   Ozone Solar.R    Wind    Temp   Month     Day 
##      37       7       0       0       0       0
library(RANN)
head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6
#Scatter Plots("mlbench")
library(mlbench)
data("BostonHousing")
x=BostonHousing[,c("age","lstat","tax")]
y=BostonHousing$medv
featurePlot(x,y=BostonHousing$medv,
            plot="scatter",layout=c(3,1))

data("PimaIndiansDiabetes")
glimpse(PimaIndiansDiabetes)
## Rows: 768
## Columns: 9
## $ pregnant <dbl> 6, 1, 8, 1, 0, 5, 3, 10, 2, 8, 4, 10, 10, 1, 5, 7, 0, 7, 1, 1…
## $ glucose  <dbl> 148, 85, 183, 89, 137, 116, 78, 115, 197, 125, 110, 168, 139,…
## $ pressure <dbl> 72, 66, 64, 66, 40, 74, 50, 0, 70, 96, 92, 74, 80, 60, 72, 0,…
## $ triceps  <dbl> 35, 29, 0, 23, 35, 0, 32, 0, 45, 0, 0, 0, 0, 23, 19, 0, 47, 0…
## $ insulin  <dbl> 0, 0, 0, 94, 168, 0, 88, 0, 543, 0, 0, 0, 0, 846, 175, 0, 230…
## $ mass     <dbl> 33.6, 26.6, 23.3, 28.1, 43.1, 25.6, 31.0, 35.3, 30.5, 0.0, 37…
## $ pedigree <dbl> 0.627, 0.351, 0.672, 0.167, 2.288, 0.201, 0.248, 0.134, 0.158…
## $ age      <dbl> 50, 31, 32, 21, 33, 30, 26, 29, 53, 54, 30, 34, 57, 59, 51, 3…
## $ diabetes <fct> pos, neg, pos, neg, pos, neg, pos, neg, pos, pos, neg, pos, n…
head(PimaIndiansDiabetes,1)
##   pregnant glucose pressure triceps insulin mass pedigree age diabetes
## 1        6     148       72      35       0 33.6    0.627  50      pos
summary(PimaIndiansDiabetes)
##     pregnant         glucose         pressure         triceps     
##  Min.   : 0.000   Min.   :  0.0   Min.   :  0.00   Min.   : 0.00  
##  1st Qu.: 1.000   1st Qu.: 99.0   1st Qu.: 62.00   1st Qu.: 0.00  
##  Median : 3.000   Median :117.0   Median : 72.00   Median :23.00  
##  Mean   : 3.845   Mean   :120.9   Mean   : 69.11   Mean   :20.54  
##  3rd Qu.: 6.000   3rd Qu.:140.2   3rd Qu.: 80.00   3rd Qu.:32.00  
##  Max.   :17.000   Max.   :199.0   Max.   :122.00   Max.   :99.00  
##     insulin           mass          pedigree           age        diabetes 
##  Min.   :  0.0   Min.   : 0.00   Min.   :0.0780   Min.   :21.00   neg:500  
##  1st Qu.:  0.0   1st Qu.:27.30   1st Qu.:0.2437   1st Qu.:24.00   pos:268  
##  Median : 30.5   Median :32.00   Median :0.3725   Median :29.00            
##  Mean   : 79.8   Mean   :31.99   Mean   :0.4719   Mean   :33.24            
##  3rd Qu.:127.2   3rd Qu.:36.60   3rd Qu.:0.6262   3rd Qu.:41.00            
##  Max.   :846.0   Max.   :67.10   Max.   :2.4200   Max.   :81.00
control<-trainControl(method="repeatedcv",number=5)
model<-train(diabetes~.,data=PimaIndiansDiabetes,method="knn",
             preProcess=c("center","scale"),trControl=control)
library(caret)
library(mlbench)
data(Sonar)
set.seed(998)
inTraining<-createDataPartition(y=Sonar$Class,p=.70,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 
## 
## 146 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## Pre-processing: centered (60), scaled (60) 
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 146, 146, 146, 146, 146, 146, ... 
## Resampling results across tuning parameters:
## 
##   mtry  Accuracy   Kappa    
##    2    0.7875678  0.5700164
##   31    0.7792146  0.5541451
##   60    0.7705804  0.5363789
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 2.
fitControl<-trainControl(method="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: 0x0000016b1cc9d480>
## <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)
customGrid<-expand.grid(mtry=1:10)
rffit2<-train(Class~.,data=training,method="rf",trControl=fitControl,tuneGrid=customGrid,metric="ROC")
rffit2
## Random Forest 
## 
## 146 samples
##  60 predictor
##   2 classes: 'M', 'R' 
## 
## No pre-processing
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 117, 118, 116, 117, 116 
## Resampling results across tuning parameters:
## 
##   mtry  ROC        Sens       Spec     
##    1    0.9378159  0.9225000  0.7065934
##    2    0.9365842  0.9100000  0.7054945
##    3    0.9353297  0.9225000  0.7340659
##    4    0.9311493  0.8841667  0.7208791
##    5    0.9329487  0.8975000  0.7197802
##    6    0.9364240  0.8841667  0.7043956
##    7    0.9255266  0.8716667  0.7197802
##    8    0.9218956  0.8466667  0.7054945
##    9    0.9254121  0.8716667  0.7054945
##   10    0.9225893  0.8333333  0.7351648
## 
## ROC was used to select the optimal model using the largest value.
## The final value used for the model was mtry = 1.
library(lubridate)
## 
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(nycflights13)
library(dplyr)
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…
head(flights,3)
## # A tibble: 3 × 19
##    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
##   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
## 1  2013     1     1      517            515         2      830            819
## 2  2013     1     1      533            529         4      850            830
## 3  2013     1     1      542            540         2      923            850
## # ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## #   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## #   hour <dbl>, minute <dbl>, time_hour <dttm>
today()
## [1] "2024-01-31"
now()
## [1] "2024-01-31 16:50:49 KST"
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<='2018-01-31') 
##   show_id    type                                        title        director
## 1   s5058 TV Show                                  Retribution                
## 2   s5075   Movie                          Bad Day for the Cut     Chris Baugh
## 3   s5098 TV Show                                     Lovesick                
## 4   s7399   Movie   Manolo: The Boy Who Made Shoes for Lizards Michael Roberts
## 5   s8358 TV Show                             The Inbetweeners                
## 6   s8620   Movie Treasures from the Wreck of the Unbelievable   Sam Hobkinson
##                                                                                                                                                                            cast
## 1                         Georgina Campbell, Joe Dempsie, Adrian Edmondson, Steve Evets, Laura Fraser, Julie Graham, John Lynch, Gary Lewis, Juliet Stevenson, Joanna Vanderham
## 2                        Nigel O'Neill, Susan Lynch, J?zef Pawlowski, Stuart Graham, David Pearse, Brian Milligan, Anna Prochniak, Stella McCusker, Ian McElhinney, Lalor Roddy
## 3                                                                  Johnny Flynn, Antonia Thomas, Daniel Ings, Hannah Britland, Joshua McGuire, Richard Thomson, Jessica Ellerby
## 4 Manolo Blahnik, Anna Wintour, Andr? Leon Talley, Paloma Picasso, Candace Bushnell, Iman, Rihanna, Naomi Campbell, Isaac Mizrahi, Rupert Everett, Sofia Coppola, John Galliano
## 5                     Simon Bird, James Buckley, Blake Harrison, Joe Thomas, Greg Davies, Emily Head, Martin Trenaman, Belinda Stewart-Wilson, Robin Weaver, Henry Lloyd-Hughes
## 6                                                                                                                                                                              
##          country       date_added release_year rating  duration
## 1 United Kingdom        30-Jan-18         2016  TV-MA  1 Season
## 2 United Kingdom        18-Jan-18         2017  TV-MA    99 min
## 3 United Kingdom        01-Jan-18         2018  TV-MA 3 Seasons
## 4 United Kingdom        15-Jan-18         2017  TV-MA    89 min
## 5 United Kingdom  January 1, 2018         2010  TV-MA 3 Seasons
## 6 United Kingdom        01-Jan-18         2017   TV-G    82 min
##                                                     listed_in         y2
## 1    British TV Shows, Crime TV Shows, International TV Shows 2018-01-30
## 2         Independent Movies, International Movies, Thrillers 2018-01-18
## 3 British TV Shows, International TV Shows, Romantic TV Shows 2018-01-01
## 4                         Documentaries, International Movies 2018-01-15
## 5                               British TV Shows, TV Comedies 2018-01-01
## 6                 Documentaries, Dramas, International Movies 2018-01-01
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## 다음의 패키지를 부착합니다: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
library(caret)
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, …
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
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, …
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
set.seed(1)
idx<-createDataPartition(df$Gender,p=0.8)
table(df[idx$Resample1,"Gender"])
## 
## female   male 
##    909    632
table(df[-idx$Resample1,"Gender"])
## 
## female   male 
##    227    157
idx<-createDataPartition(df$Gender,p=0.8,list=FALSE)
train<-df[idx,]
test<-df[-idx,]
nrow(train)
## [1] 1541
nrow(test)
## [1] 384
sca_df<-preProcess(df,method="range")
sca_df
## Created from 1925 samples and 27 variables
## 
## Pre-processing:
##   - ignored (1)
##   - re-scaling to [0, 1] (26)
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, …
sca_df_p<-predict(sca_df,df)
head(sca_df_p)
##              X   Q1   Q2   Q3   Q4   Q5   Q6   Q7   Q8   Q9  Q10  Q11  Q12  Q13
## 1 0.0000000000 0.75 0.75 0.25 0.50 0.75 0.25 0.25 0.75 0.75 0.75 0.75 0.75 0.75
## 2 0.0005197505 0.75 0.75 0.75 0.75 0.75 0.50 0.25 0.75 0.75 0.75 0.75 0.75 0.75
## 3 0.0010395010 0.75 0.75 0.75 0.75 0.25 0.75 0.75 0.75 0.75 0.25 0.75 0.75 0.75
## 4 0.0015592516 1.00 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
## 5 0.0020790021 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.25 0.75 0.75 0.75 0.75
## 6 0.0025987526 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75 0.75
##    Q14  Q15  Q16  Q17  Q18  Q19  Q20 Gender       EDU   BF   BM Happiness
## 1 0.75 0.75 0.75 0.75 0.75 0.75 0.75 female 0.0000000 0.60 0.55 0.7222222
## 2 0.75 0.75 0.75 0.50 0.75 0.25 0.00 female 0.0000000 0.75 0.60 0.7222222
## 3 0.75 0.50 0.75 0.75 0.75 0.75 0.50 female 0.3333333 0.65 0.65 0.6666667
## 4 0.75 0.75 0.75 0.75 0.75 0.75 0.75 female 0.0000000 0.80 0.75 0.7222222
## 5 0.75 0.75 0.75 0.75 0.75 0.75 0.75 female 0.3333333 0.75 0.65 0.7222222
## 6 0.75 0.75 0.75 0.75 0.75 0.75 0.75 female 0.0000000 0.75 0.75 0.7222222
##       Peace
## 1 0.7368421
## 2 0.4210526
## 3 0.6842105
## 4 0.7368421
## 5 0.7368421
## 6 0.7368421
library(psych)
describe(sca_df_p)
##           vars    n mean   sd median trimmed  mad min max range  skew kurtosis
## X            1 1925 0.50 0.29   0.50    0.50 0.37   0   1     1  0.00    -1.20
## Q1           2 1925 0.63 0.24   0.75    0.65 0.00   0   1     1 -0.75    -0.04
## Q2           3 1925 0.57 0.23   0.50    0.58 0.37   0   1     1 -0.36    -0.57
## Q3           4 1925 0.48 0.25   0.50    0.48 0.37   0   1     1  0.06    -1.10
## Q4           5 1925 0.52 0.24   0.50    0.52 0.37   0   1     1 -0.11    -1.08
## Q5           6 1925 0.51 0.25   0.50    0.51 0.37   0   1     1 -0.06    -1.09
## Q6           7 1925 0.45 0.28   0.50    0.46 0.37   0   1     1  0.12    -1.16
## Q7           8 1925 0.52 0.27   0.50    0.52 0.37   0   1     1 -0.14    -1.11
## Q8           9 1925 0.51 0.27   0.50    0.52 0.37   0   1     1 -0.15    -1.18
## Q9          10 1925 0.52 0.28   0.50    0.52 0.37   0   1     1 -0.17    -1.21
## Q10         11 1925 0.47 0.24   0.50    0.47 0.37   0   1     1  0.15    -0.64
## Q11         12 1925 0.62 0.24   0.75    0.63 0.37   0   1     1 -0.67     0.05
## Q12         13 1925 0.61 0.25   0.75    0.61 0.37   0   1     1 -0.43    -0.64
## Q13         14 1925 0.65 0.22   0.75    0.66 0.00   0   1     1 -0.65    -0.15
## Q14         15 1925 0.68 0.22   0.75    0.70 0.00   0   1     1 -0.77     0.19
## Q15         16 1925 0.64 0.23   0.75    0.64 0.37   0   1     1 -0.48    -0.26
## Q16         17 1925 0.70 0.21   0.75    0.72 0.00   0   1     1 -0.87     0.84
## Q17         18 1925 0.63 0.25   0.75    0.64 0.00   0   1     1 -0.57    -0.50
## Q18         19 1925 0.70 0.24   0.75    0.73 0.00   0   1     1 -1.01     0.79
## Q19         20 1925 0.59 0.23   0.50    0.59 0.37   0   1     1 -0.28    -0.48
## Q20         21 1925 0.59 0.23   0.50    0.59 0.37   0   1     1 -0.27    -0.68
## Gender*     22 1925 1.41 0.49   1.00    1.39 0.00   1   2     1  0.37    -1.87
## EDU         23 1925 0.54 0.28   0.67    0.55 0.00   0   1     1 -0.47    -0.34
## BF          24 1925 0.54 0.19   0.55    0.55 0.22   0   1     1 -0.12    -0.52
## BM          25 1925 0.49 0.19   0.50    0.49 0.22   0   1     1  0.02    -0.41
## Happiness   26 1925 0.60 0.21   0.61    0.60 0.16   0   1     1 -0.37    -0.20
## Peace       27 1925 0.62 0.17   0.63    0.63 0.16   0   1     1 -0.46     0.14
##             se
## X         0.01
## Q1        0.01
## Q2        0.01
## Q3        0.01
## Q4        0.01
## Q5        0.01
## Q6        0.01
## Q7        0.01
## Q8        0.01
## Q9        0.01
## Q10       0.01
## Q11       0.01
## Q12       0.01
## Q13       0.01
## Q14       0.01
## Q15       0.01
## Q16       0.00
## Q17       0.01
## Q18       0.01
## Q19       0.01
## Q20       0.01
## Gender*   0.01
## EDU       0.01
## BF        0.00
## BM        0.00
## Happiness 0.00
## Peace     0.00
featurePlot(x=df[,1:21],y=df$Gender,plot="density")

# 데이터 불러오기
data <- read.csv("data1.csv")

# 데이터 전처리: 범주형 변수인 Gender를 factor로 변환
data$Gender <- as.factor(data$Gender)

# 데이터 분할: 학습용과 테스트용으로 나누기
library(caTools)
set.seed(123)
split <- sample.split(data$Gender, SplitRatio = 0.7)
train_data <- subset(data, split == TRUE)
test_data <- subset(data, split == FALSE)

# 로지스틱 회귀 모델 학습
model <- glm(Gender ~ ., data = train_data, family = binomial)

# 테스트 데이터에 대한 예측
predicted <- predict(model, newdata = test_data, type = "response")

# 예측값을 이진 분류로 변환 (임계값: 0.5)
threshold <- 0.5
predicted_class <- ifelse(predicted > threshold, 1, 0)

# 필요한 패키지 로드
library(ggplot2)

# 혼동 행렬 생성
confusion_matrix <- table(Actual = test_data$Gender, Predicted = predicted_class)

# 혼동 행렬을 데이터프레임으로 변환
confusion_matrix_df <- as.data.frame.matrix(confusion_matrix)

# 시각화를 위한 데이터프레임 생성
confusion_matrix_df$Actual <- rownames(confusion_matrix_df)
confusion_matrix_df_long <- tidyr::gather(confusion_matrix_df, key = "Predicted", value = "Count", -Actual)

# 시각화
ggplot(confusion_matrix_df_long, aes(x = Actual, y = Predicted, fill = Count)) +
  geom_tile(color = "white") +
  scale_fill_gradient(low = "lightblue", high = "darkblue") +
  geom_text(aes(label = Count)) +
  labs(x = "Actual", y = "Predicted", title = "Confusion Matrix") +
  theme_minimal()