#Chapitre 3

#Analyse en composantes principales
#page 131
#q1
d_macdo<-read.csv("https://tinyurl.com/y3qobgsd")

#q2
str(d_macdo)
## 'data.frame':    260 obs. of  24 variables:
##  $ Category                     : Factor w/ 9 levels "Beef & Pork",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ Item                         : Factor w/ 260 levels "1% Low Fat Milk Jug",..: 76 77 228 229 230 245 12 11 14 13 ...
##  $ Serving.Size                 : Factor w/ 107 levels "1 carton (236 ml)",..: 55 54 42 69 69 83 63 72 65 73 ...
##  $ Calories                     : int  300 250 370 450 400 430 460 520 410 470 ...
##  $ Calories.from.Fat            : int  120 70 200 250 210 210 230 270 180 220 ...
##  $ Total.Fat                    : num  13 8 23 28 23 23 26 30 20 25 ...
##  $ Total.Fat....Daily.Value.    : int  20 12 35 43 35 36 40 47 32 38 ...
##  $ Saturated.Fat                : num  5 3 8 10 8 9 13 14 11 12 ...
##  $ Saturated.Fat....Daily.Value.: int  25 15 42 52 42 46 65 68 56 59 ...
##  $ Trans.Fat                    : num  0 0 0 0 0 1 0 0 0 0 ...
##  $ Cholesterol                  : int  260 25 45 285 50 300 250 250 35 35 ...
##  $ Cholesterol....Daily.Value.  : int  87 8 15 95 16 100 83 83 11 11 ...
##  $ Sodium                       : int  750 770 780 860 880 960 1300 1410 1300 1420 ...
##  $ Sodium....Daily.Value.       : int  31 32 33 36 37 40 54 59 54 59 ...
##  $ Carbohydrates                : int  31 30 29 30 30 31 38 43 36 42 ...
##  $ Carbohydrates....Daily.Value.: int  10 10 10 10 10 10 13 14 12 14 ...
##  $ Dietary.Fiber                : int  4 4 4 4 4 4 2 3 2 3 ...
##  $ Dietary.Fiber....Daily.Value.: int  17 17 17 17 17 18 7 12 7 12 ...
##  $ Sugars                       : int  3 3 2 2 2 3 3 4 3 4 ...
##  $ Protein                      : int  17 18 14 21 21 26 19 19 20 20 ...
##  $ Vitamin.A....Daily.Value.    : int  10 6 8 15 6 15 10 15 2 6 ...
##  $ Vitamin.C....Daily.Value.    : int  0 0 0 0 0 2 8 8 8 8 ...
##  $ Calcium....Daily.Value.      : int  25 25 25 30 25 30 15 20 15 15 ...
##  $ Iron....Daily.Value.         : int  15 8 10 15 10 20 15 20 10 15 ...
#q3
summary(d_macdo)
##                Category                                       Item    
##  Coffee & Tea      :95   1% Low Fat Milk Jug                    :  1  
##  Breakfast         :42   Apple Slices                           :  1  
##  Smoothies & Shakes:28   Bacon Buffalo Ranch McChicken          :  1  
##  Beverages         :27   Bacon Cheddar McChicken                :  1  
##  Chicken & Fish    :27   Bacon Clubhouse Burger                 :  1  
##  Beef & Pork       :15   Bacon Clubhouse Crispy Chicken Sandwich:  1  
##  (Other)           :26   (Other)                                :254  
##        Serving.Size    Calories      Calories.from.Fat   Total.Fat      
##  16 fl oz cup: 45   Min.   :   0.0   Min.   :   0.0    Min.   :  0.000  
##  12 fl oz cup: 38   1st Qu.: 210.0   1st Qu.:  20.0    1st Qu.:  2.375  
##  22 fl oz cup: 20   Median : 340.0   Median : 100.0    Median : 11.000  
##  20 fl oz cup: 16   Mean   : 368.3   Mean   : 127.1    Mean   : 14.165  
##  21 fl oz cup:  7   3rd Qu.: 500.0   3rd Qu.: 200.0    3rd Qu.: 22.250  
##  30 fl oz cup:  7   Max.   :1880.0   Max.   :1060.0    Max.   :118.000  
##  (Other)     :127                                                       
##  Total.Fat....Daily.Value. Saturated.Fat    Saturated.Fat....Daily.Value.
##  Min.   :  0.00            Min.   : 0.000   Min.   :  0.00               
##  1st Qu.:  3.75            1st Qu.: 1.000   1st Qu.:  4.75               
##  Median : 17.00            Median : 5.000   Median : 24.00               
##  Mean   : 21.82            Mean   : 6.008   Mean   : 29.97               
##  3rd Qu.: 35.00            3rd Qu.:10.000   3rd Qu.: 48.00               
##  Max.   :182.00            Max.   :20.000   Max.   :102.00               
##                                                                          
##    Trans.Fat       Cholesterol     Cholesterol....Daily.Value.
##  Min.   :0.0000   Min.   :  0.00   Min.   :  0.00             
##  1st Qu.:0.0000   1st Qu.:  5.00   1st Qu.:  2.00             
##  Median :0.0000   Median : 35.00   Median : 11.00             
##  Mean   :0.2038   Mean   : 54.94   Mean   : 18.39             
##  3rd Qu.:0.0000   3rd Qu.: 65.00   3rd Qu.: 21.25             
##  Max.   :2.5000   Max.   :575.00   Max.   :192.00             
##                                                               
##      Sodium       Sodium....Daily.Value. Carbohydrates   
##  Min.   :   0.0   Min.   :  0.00         Min.   :  0.00  
##  1st Qu.: 107.5   1st Qu.:  4.75         1st Qu.: 30.00  
##  Median : 190.0   Median :  8.00         Median : 44.00  
##  Mean   : 495.8   Mean   : 20.68         Mean   : 47.35  
##  3rd Qu.: 865.0   3rd Qu.: 36.25         3rd Qu.: 60.00  
##  Max.   :3600.0   Max.   :150.00         Max.   :141.00  
##                                                          
##  Carbohydrates....Daily.Value. Dietary.Fiber  
##  Min.   : 0.00                 Min.   :0.000  
##  1st Qu.:10.00                 1st Qu.:0.000  
##  Median :15.00                 Median :1.000  
##  Mean   :15.78                 Mean   :1.631  
##  3rd Qu.:20.00                 3rd Qu.:3.000  
##  Max.   :47.00                 Max.   :7.000  
##                                               
##  Dietary.Fiber....Daily.Value.     Sugars          Protein     
##  Min.   : 0.000                Min.   :  0.00   Min.   : 0.00  
##  1st Qu.: 0.000                1st Qu.:  5.75   1st Qu.: 4.00  
##  Median : 5.000                Median : 17.50   Median :12.00  
##  Mean   : 6.531                Mean   : 29.42   Mean   :13.34  
##  3rd Qu.:10.000                3rd Qu.: 48.00   3rd Qu.:19.00  
##  Max.   :28.000                Max.   :128.00   Max.   :87.00  
##                                                                
##  Vitamin.A....Daily.Value. Vitamin.C....Daily.Value.
##  Min.   :  0.00            Min.   :  0.000          
##  1st Qu.:  2.00            1st Qu.:  0.000          
##  Median :  8.00            Median :  0.000          
##  Mean   : 13.43            Mean   :  8.535          
##  3rd Qu.: 15.00            3rd Qu.:  4.000          
##  Max.   :170.00            Max.   :240.000          
##                                                     
##  Calcium....Daily.Value. Iron....Daily.Value.
##  Min.   : 0.00           Min.   : 0.000      
##  1st Qu.: 6.00           1st Qu.: 0.000      
##  Median :20.00           Median : 4.000      
##  Mean   :20.97           Mean   : 7.735      
##  3rd Qu.:30.00           3rd Qu.:15.000      
##  Max.   :70.00           Max.   :40.000      
## 
#page 132
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
ggparcoord(d_macdo, columns = 3:ncol(d_macdo), groupColumn=1,
           scale = "std", boxplot = TRUE, alphaLines = 0.5)+facet_grid(
           rows=vars(Category))+ theme(axis.text.x=element_text(angle=45,
           hjust=1,vjust=1),legend.position="top")

#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("ggiraphExtra")){install.packages("ggiraphExtra")}
library(ggiraphExtra)
ggRadar(d_macdo[,-c(2,3)],aes(facet=Category))

#page 134
#q4
cor(d_macdo[,-c(1,2,3)])[1:3,1:3]
##                    Calories Calories.from.Fat Total.Fat
## Calories          1.0000000         0.9045878 0.9044092
## Calories.from.Fat 0.9045878         1.0000000 0.9996635
## Total.Fat         0.9044092         0.9996635 1.0000000
#page 135
#q5
tmp_d_macdo <- d_macdo
p_ <- GGally::print_if_interactive
plotList <- list()
plotList[[1]] <- ggally_smooth_lm(tmp_d_macdo, mapping =
                    ggplot2::aes(x = Calories, y = Total.Fat))
plotList[[2]] <- ggally_smooth_loess(tmp_d_macdo, mapping =
                    ggplot2::aes(x = Calories, y = Total.Fat))
pm <- ggmatrix(plotList, 1, 2, c("Ajustement lin\u00e9aire",
                    "R\u00e9gression polynomiale locale"), byrow = TRUE)
p_(pm)

ggduo(tmp_d_macdo, c("Calories"), c("Total.Fat"), 
      types = list(continuous = "smooth_loess"))

#page 136
#Pour sauvegarder le graphique enlever les commentaires des quatres lignes ci-dessous
# pdf("ggduo.pdf")
# ggduo(tmp_d_macdo, c("Calories"), c("Total.Fat"), 
#       types = list(continuous = "smooth_loess"))
# dev.off()

#q6
library(MVN)
## sROC 0.1-2 loaded
result = mvn(data = d_macdo[,c("Calories","Total.Fat")],
               mvnTest = "mardia",
               univariateTest = "SW", univariatePlot = "histogram",
               multivariatePlot = "qq",
               multivariateOutlierMethod = "adj",
               showOutliers = TRUE, showNewData = TRUE)

result$multivariateNormality
##              Test        Statistic              p value Result
## 1 Mardia Skewness 224.229958917198 2.30464057341325e-47     NO
## 2 Mardia Kurtosis 22.0280366224199                    0     NO
## 3             MVN             <NA>                 <NA>     NO
#page 137
d_macdo[83,c("Item","Calories","Total.Fat")]
##                            Item Calories Total.Fat
## 83 Chicken McNuggets (40 piece)     1880       118
sort(d_macdo$Calories,decreasing = TRUE)[1:5]
## [1] 1880 1150 1090 1050  990
#page 149
sort(d_macdo$Total.Fat,decreasing = TRUE)[1:5]
## [1] 118  60  59  56  52
stem(d_macdo$Total.Fat)
## 
##   The decimal point is 1 digit(s) to the right of the |
## 
##    0 | 00000000000000000000000000000000000000000000000001111111111111122344+40
##    1 | 000000111111123333334444444555555666666666677778889999999
##    2 | 0000000011112222223333333333334444455566666677777889
##    3 | 00111111222233335778
##    4 | 01368
##    5 | 0269
##    6 | 0
##    7 | 
##    8 | 
##    9 | 
##   10 | 
##   11 | 8
d_macdo$Item[as.numeric(rownames(result$multivariateOutliers))]
##  [1] Chicken McNuggets (40 piece)                                
##  [2] Big Breakfast with Hotcakes (Large Biscuit)                 
##  [3] Chocolate Shake (Large)                                     
##  [4] Strawberry Shake (Large)                                    
##  [5] Big Breakfast with Hotcakes (Regular Biscuit)               
##  [6] Chicken McNuggets (20 piece)                                
##  [7] Vanilla Shake (Large)                                       
##  [8] Shamrock Shake (Large)                                      
##  [9] McFlurry with M&M's™ Candies (Medium)                       
## [10] Big Breakfast with Hotcakes and Egg Whites (Large Biscuit)  
## [11] Big Breakfast with Hotcakes and Egg Whites (Regular Biscuit)
## 260 Levels: 1% Low Fat Milk Jug ... Vanilla Shake (Small)
#page 139
library(GGally)
tmp_d_macdo <- d_macdo
tmp_d_macdo$indic_outlier <- as.factor(1:nrow(d_macdo) 
      %in% as.numeric(rownames(result$multivariateOutliers)))
colnumbers <- which(colnames(tmp_d_macdo) %in% 
                      c("Calories", "Total.Fat"))
ggscatmat(tmp_d_macdo, columns = colnumbers, color= 
            "indic_outlier")

#page 140
d_macdo1 <- d_macdo[-83,]
dim(d_macdo1)
## [1] 259  24
result1 = mvn(data = d_macdo1[,c("Calories","Total.Fat")], 
              mvnTest = "mardia", 
              univariateTest = "SW", univariatePlot = "histogram", 
              multivariatePlot = "qq", 
              multivariateOutlierMethod = "adj", 
              showOutliers = TRUE, showNewData = TRUE)

result1$multivariateNormality
##              Test        Statistic              p value Result
## 1 Mardia Skewness 62.7152531692116 7.78991049204941e-13     NO
## 2 Mardia Kurtosis 3.20642612029187  0.00134394810305571     NO
## 3             MVN             <NA>                 <NA>     NO
#q7
library(pspearman)
spearman.test(d_macdo[,"Calories"],d_macdo[,"Total.Fat"])
## Warning in spearman.test(d_macdo[, "Calories"], d_macdo[, "Total.Fat"]):
## Cannot compute exact p-values with ties
## 
##  Spearman's rank correlation rho
## 
## data:  d_macdo[, "Calories"] and d_macdo[, "Total.Fat"]
## S = 303800, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##       rho 
## 0.8962887
#page 141
library(coin)
## Loading required package: survival
set.seed(1133)
spearman_test(d_macdo[,"Calories"]~d_macdo[,"Total.Fat"], 
              distribution=approximate(999999))
## 
##  Approximative Spearman Correlation Test
## 
## data:  d_macdo[, "Calories"] by d_macdo[, "Total.Fat"]
## Z = 14.424, p-value < 1e-06
## alternative hypothesis: true rho is not equal to 0
cor.test(d_macdo[,"Calories"],d_macdo[,"Total.Fat"], 
         method = "kendall")
## 
##  Kendall's rank correlation tau
## 
## data:  d_macdo[, "Calories"] and d_macdo[, "Total.Fat"]
## z = 17.091, p-value < 2.2e-16
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
##       tau 
## 0.7339707
#page 142
#q8
library(jmuOutlier)
set.seed(1133)
perm.cor.test(d_macdo[,"Calories"],d_macdo[,"Total.Fat"])
## [[1]]
## [1] "Permutation correlation test.  Method is pearson"
## 
## [[2]]
## [1] "p-value was estimated based on 20000 simulations."
## 
## $alternative
## [1] "two.sided"
## 
## $p.value
## [1] 0
#q9
library(ModStatR)
set.seed(1133)
r_c_mdo <- perm.cor.mtest(d_macdo[,c("Calories","Total.Fat", 
                                     "Cholesterol","Sodium","Sugars","Protein")], num.sim = 50000)
lapply(r_c_mdo, round, 4)
## $p
##      [,1]   [,2]  [,3] [,4]   [,5]  [,6]
## [1,]    0 0.0000 0.000    0 0.0000 0.000
## [2,]    0 0.0000 0.000    0 0.0618 0.000
## [3,]    0 0.0000 0.000    0 0.0280 0.000
## [4,]    0 0.0000 0.000    0 0.0000 0.000
## [5,]    0 0.0618 0.028    0 0.0000 0.004
## [6,]    0 0.0000 0.000    0 0.0040 0.000
## 
## $cor
##        [,1]    [,2]    [,3]    [,4]    [,5]    [,6]
## [1,] 1.0000  0.9044  0.5964  0.7123  0.2596  0.7878
## [2,] 0.9044  1.0000  0.6805  0.8462 -0.1154  0.8078
## [3,] 0.5964  0.6805  1.0000  0.6244 -0.1355  0.5616
## [4,] 0.7123  0.8462  0.6244  1.0000 -0.4265  0.8698
## [5,] 0.2596 -0.1154 -0.1355 -0.4265  1.0000 -0.1799
## [6,] 0.7878  0.8078  0.5616  0.8698 -0.1799  1.0000
#page 143
r_c_mdo$p < .05/(6*5/2)
##      [,1]  [,2]  [,3] [,4]  [,5]  [,6]
## [1,] TRUE  TRUE  TRUE TRUE  TRUE  TRUE
## [2,] TRUE  TRUE  TRUE TRUE FALSE  TRUE
## [3,] TRUE  TRUE  TRUE TRUE FALSE  TRUE
## [4,] TRUE  TRUE  TRUE TRUE  TRUE  TRUE
## [5,] TRUE FALSE FALSE TRUE  TRUE FALSE
## [6,] TRUE  TRUE  TRUE TRUE FALSE  TRUE
source("http://www.sthda.com/upload/rquery_cormat.r")
rquery.cormat(d_macdo[,c("Calories","Total.Fat", 
                         "Cholesterol","Sodium","Sugars","Protein")])$r
## corrplot 0.84 loaded
##             Sugars Cholesterol Calories Total.Fat Sodium Protein
## Sugars           1                                              
## Cholesterol  -0.14           1                                  
## Calories      0.26         0.6        1                         
## Total.Fat    -0.12        0.68      0.9         1               
## Sodium       -0.43        0.62     0.71      0.85      1        
## Protein      -0.18        0.56     0.79      0.81   0.87       1
#page 144
#q10
library(rgl)

#q11
plot3d(d_macdo$Calories,d_macdo$Total.Fat, d_macdo$Cholesterol,type="s")

#page 145
#q12
list <- c("Calories", "Total.Fat", "Cholesterol")
d_macdo.cr <- scale(d_macdo[, list])
lims <- c(min(d_macdo.cr),max(d_macdo.cr))
plot3d(d_macdo.cr, type = "s", xlim = lims, 
       ylim = lims,zlim = lims)

#page 146
#q13
d_macdo.cr_df <- as.data.frame(d_macdo.cr)
plot3d(d_macdo.cr, type = "s", xlim = lims, 
       ylim = lims, zlim = lims)
plot3d(ellipse3d(cor(cbind(d_macdo.cr_df$Calories, 
       d_macdo.cr_df$Total.Fat,d_macdo.cr_df$Cholesterol))), col="grey",add=TRUE)

#page 148
#q 16
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("ade4")){install.packages("ade4")}
library(ade4)
## 
## Attaching package: 'ade4'
## The following object is masked from 'package:jmuOutlier':
## 
##     score
list <- setdiff(colnames(d_macdo), c("Category", "Item", 
                                     "Serving.Size"))
macdo.acp <- dudi.pca(d_macdo[, list], center=TRUE, 
                      scale=TRUE, scannf = FALSE, nf = 3)
names(macdo.acp)
##  [1] "tab"  "cw"   "lw"   "eig"  "rank" "nf"   "c1"   "li"   "co"   "l1"  
## [11] "call" "cent" "norm"
#q17
macdo.acp$cw
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
head(macdo.acp$lw)
## [1] 0.003846154 0.003846154 0.003846154 0.003846154 0.003846154 0.003846154
#page 149
#q18
round(macdo.acp$eig,3)
##  [1] 11.027  3.796  1.667  1.038  0.925  0.821  0.557  0.461  0.360  0.197
## [11]  0.067  0.050  0.020  0.012  0.001  0.000  0.000  0.000  0.000  0.000
## [21]  0.000
sum(macdo.acp$eig)
## [1] 21
#page 150
#q19
round(pve <- 100*macdo.acp$eig/sum(macdo.acp$eig),3)
##  [1] 52.508 18.075  7.938  4.943  4.406  3.911  2.651  2.197  1.715  0.936
## [11]  0.319  0.237  0.095  0.058  0.003  0.002  0.002  0.001  0.001  0.001
## [21]  0.000
round(cumsum(pve),2)
##  [1]  52.51  70.58  78.52  83.46  87.87  91.78  94.43  96.63  98.34  99.28
## [11]  99.60  99.84  99.93  99.99  99.99 100.00 100.00 100.00 100.00 100.00
## [21] 100.00
round(cumsum(pve),2)[3:4]
## [1] 78.52 83.46
#page 151
#q20
screeplot(macdo.acp)

#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("factoextra")){install.packages("factoextra")}
library("factoextra")
## Welcome! Related Books: `Practical Guide To Cluster Analysis in R` at https://goo.gl/13EFCZ
fviz_eig(macdo.acp)

inertia.dudi(macdo.acp)
## Inertia information:
## Call: inertia.dudi(x = macdo.acp)
## 
## Decomposition of total inertia:
##        inertia     cum  cum(%)
## Ax1  1.103e+01   11.03   52.51
## Ax2  3.796e+00   14.82   70.58
## Ax3  1.667e+00   16.49   78.52
## Ax4  1.038e+00   17.53   83.46
## Ax5  9.252e-01   18.45   87.87
## Ax6  8.214e-01   19.27   91.78
## Ax7  5.567e-01   19.83   94.43
## Ax8  4.615e-01   20.29   96.63
## Ax9  3.602e-01   20.65   98.34
## Ax10 1.965e-01   20.85   99.28
## Ax11 6.700e-02   20.92   99.60
## Ax12 4.984e-02   20.97   99.84
## Ax13 1.997e-02   20.99   99.93
## Ax14 1.222e-02   21.00   99.99
## Ax15 6.954e-04   21.00   99.99
## Ax16 4.619e-04   21.00  100.00
## Ax17 3.265e-04   21.00  100.00
## Ax18 2.343e-04   21.00  100.00
## Ax19 2.047e-04   21.00  100.00
## Ax20 1.345e-04   21.00  100.00
## Ax21 6.221e-05   21.00  100.00
#page 152
#q21
s.corcircle(macdo.acp$co, xax=1, yax=2)

#q22
round(inertia.dudi(macdo.acp, col.inertia = TRUE)$col.abs,4)
##                                Axis1   Axis2   Axis3
## Calories                      7.9287  2.2004  0.1857
## Calories.from.Fat             8.2801  0.0660  1.2916
## Total.Fat                     8.2694  0.0652  1.3079
## Total.Fat....Daily.Value.     8.2736  0.0663  1.3263
## Saturated.Fat                 6.6702  2.4498  3.4404
## Saturated.Fat....Daily.Value. 6.7151  2.3949  3.3777
## Trans.Fat                     2.0901  5.6506  1.3713
## Cholesterol                   5.1209  0.4376  3.4235
## Cholesterol....Daily.Value.   5.1064  0.4430  3.4392
## Sodium                        6.7521  4.5018  0.0277
## Sodium....Daily.Value.        6.7620  4.4607  0.0281
## Carbohydrates                 2.7755 14.2704  3.3196
## Carbohydrates....Daily.Value. 2.7774 14.2729  3.2834
## Dietary.Fiber                 4.1774  3.9423 16.4047
## Dietary.Fiber....Daily.Value. 4.1797  3.7937 17.4811
## Sugars                        0.0110 24.1047  1.8295
## Protein                       6.8119  0.6065  0.8637
## Vitamin.A....Daily.Value.     0.2270  0.0139 23.3497
## Vitamin.C....Daily.Value.     0.0534  0.7587 12.0094
## Calcium....Daily.Value.       0.7818 12.0210  1.9947
## Iron....Daily.Value.          6.2362  3.4796  0.2448
#page 153
round(inertia.dudi(macdo.acp, col.inertia = TRUE)$col.rel, 4)
##                                  Axis1    Axis2   Axis3
## Calories                      -87.4265  -8.3525  0.3096
## Calories.from.Fat             -91.3017   0.2505 -2.1532
## Total.Fat                     -91.1831   0.2475 -2.1803
## Total.Fat....Daily.Value.     -91.2301   0.2516 -2.2111
## Saturated.Fat                 -73.5500  -9.2990 -5.7353
## Saturated.Fat....Daily.Value. -74.0444  -9.0907 -5.6308
## Trans.Fat                     -23.0466 -21.4487 -2.2861
## Cholesterol                   -56.4656   1.6612 -5.7071
## Cholesterol....Daily.Value.   -56.3065   1.6816 -5.7332
## Sodium                        -74.4522  17.0880 -0.0462
## Sodium....Daily.Value.        -74.5618  16.9321 -0.0469
## Carbohydrates                 -30.6039 -54.1679  5.5340
## Carbohydrates....Daily.Value. -30.6251 -54.1775  5.4735
## Dietary.Fiber                 -46.0627  14.9642 27.3472
## Dietary.Fiber....Daily.Value. -46.0882  14.4001 29.1415
## Sugars                          0.1214 -91.4974  3.0498
## Protein                       -75.1122   2.3020  1.4397
## Vitamin.A....Daily.Value.      -2.5031   0.0527 38.9247
## Vitamin.C....Daily.Value.       0.5894   2.8801 20.0201
## Calcium....Daily.Value.        -8.6207 -45.6297  3.3252
## Iron....Daily.Value.          -68.7639  13.2080  0.4080
#page 154
round(macdo.acp$co,4)
##                                 Comp1   Comp2   Comp3
## Calories                      -0.9350 -0.2890  0.0556
## Calories.from.Fat             -0.9555  0.0500 -0.1467
## Total.Fat                     -0.9549  0.0497 -0.1477
## Total.Fat....Daily.Value.     -0.9551  0.0502 -0.1487
## Saturated.Fat                 -0.8576 -0.3049 -0.2395
## Saturated.Fat....Daily.Value. -0.8605 -0.3015 -0.2373
## Trans.Fat                     -0.4801 -0.4631 -0.1512
## Cholesterol                   -0.7514  0.1289 -0.2389
## Cholesterol....Daily.Value.   -0.7504  0.1297 -0.2394
## Sodium                        -0.8629  0.4134 -0.0215
## Sodium....Daily.Value.        -0.8635  0.4115 -0.0216
## Carbohydrates                 -0.5532 -0.7360  0.2352
## Carbohydrates....Daily.Value. -0.5534 -0.7361  0.2340
## Dietary.Fiber                 -0.6787  0.3868  0.5229
## Dietary.Fiber....Daily.Value. -0.6789  0.3795  0.5398
## Sugars                         0.0348 -0.9565  0.1746
## Protein                       -0.8667  0.1517  0.1200
## Vitamin.A....Daily.Value.     -0.1582  0.0230  0.6239
## Vitamin.C....Daily.Value.      0.0768  0.1697  0.4474
## Calcium....Daily.Value.       -0.2936 -0.6755  0.1824
## Iron....Daily.Value.          -0.8292  0.3634  0.0639
#page 155
#q25
round(get_pca_var(macdo.acp)$cos2, 4)
##                                Dim.1  Dim.2  Dim.3
## Calories                      0.8743 0.0835 0.0031
## Calories.from.Fat             0.9130 0.0025 0.0215
## Total.Fat                     0.9118 0.0025 0.0218
## Total.Fat....Daily.Value.     0.9123 0.0025 0.0221
## Saturated.Fat                 0.7355 0.0930 0.0574
## Saturated.Fat....Daily.Value. 0.7404 0.0909 0.0563
## Trans.Fat                     0.2305 0.2145 0.0229
## Cholesterol                   0.5647 0.0166 0.0571
## Cholesterol....Daily.Value.   0.5631 0.0168 0.0573
## Sodium                        0.7445 0.1709 0.0005
## Sodium....Daily.Value.        0.7456 0.1693 0.0005
## Carbohydrates                 0.3060 0.5417 0.0553
## Carbohydrates....Daily.Value. 0.3063 0.5418 0.0547
## Dietary.Fiber                 0.4606 0.1496 0.2735
## Dietary.Fiber....Daily.Value. 0.4609 0.1440 0.2914
## Sugars                        0.0012 0.9150 0.0305
## Protein                       0.7511 0.0230 0.0144
## Vitamin.A....Daily.Value.     0.0250 0.0005 0.3892
## Vitamin.C....Daily.Value.     0.0059 0.0288 0.2002
## Calcium....Daily.Value.       0.0862 0.4563 0.0333
## Iron....Daily.Value.          0.6876 0.1321 0.0041
#q26
round(sort(rowSums(get_pca_var(macdo.acp)$cos2[,1:2])), 4)
##     Vitamin.A....Daily.Value.     Vitamin.C....Daily.Value. 
##                        0.0256                        0.0347 
##                     Trans.Fat       Calcium....Daily.Value. 
##                        0.4450                        0.5425 
##   Cholesterol....Daily.Value.                   Cholesterol 
##                        0.5799                        0.5813 
## Dietary.Fiber....Daily.Value.                 Dietary.Fiber 
##                        0.6049                        0.6103 
##                       Protein          Iron....Daily.Value. 
##                        0.7741                        0.8197 
##                 Saturated.Fat Saturated.Fat....Daily.Value. 
##                        0.8285                        0.8314 
##                 Carbohydrates Carbohydrates....Daily.Value. 
##                        0.8477                        0.8480 
##                     Total.Fat     Total.Fat....Daily.Value. 
##                        0.9143                        0.9148 
##        Sodium....Daily.Value.                        Sodium 
##                        0.9149                        0.9154 
##             Calories.from.Fat                        Sugars 
##                        0.9155                        0.9162 
##                      Calories 
##                        0.9578
#page 156
fviz_pca_var(macdo.acp, col.var="contrib", gradient.cols= 
               c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE)

#page 157
fviz_pca_var(macdo.acp, col.var = "cos2", gradient.cols = 
               c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE)

#page 158
fviz_pca_var(macdo.acp, axes = c(1, 3), col.var="contrib", 
             gradient.cols=c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE)
fviz_pca_var(macdo.acp, axes = c(1, 3), col.var = "cos2", 
             gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = TRUE)

#q27
s.label(macdo.acp$li, xax = 1, yax = 2)

#page 159
s.label(macdo.acp$li, label=as.character(d_macdo$Item), clabel=0.5)

library(yarrr)
## Loading required package: jpeg
## Loading required package: BayesFactor
## Loading required package: coda
## Loading required package: Matrix
## ************
## Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey (richarddmorey@gmail.com).
## 
## Type BFManual() to open the manual.
## ************
## Loading required package: circlize
## ========================================
## circlize version 0.4.8
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: http://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization 
##   in R. Bioinformatics 2014.
## ========================================
## yarrr v0.1.6. Citation info at citation('yarrr'). Package guide at yarrr.guide()
## Email me at Nathaniel.D.Phillips.is@gmail.com
## 
## Attaching package: 'yarrr'
## The following object is masked from 'package:ggplot2':
## 
##     diamonds
cont_ind <- get_pca_ind(macdo.acp)$contrib
pirateplot(data=cont_ind,point.o=.75, ylab="Contribution",xlab="")

#page 160
rownames(cont_ind) <- tmp_d_macdo$Item
round(sort(cont_ind[,1],decreasing=TRUE)[1:20], 4)
##                                 Chicken McNuggets (40 piece) 
##                                                      10.9178 
##                  Big Breakfast with Hotcakes (Large Biscuit) 
##                                                       5.9160 
##                Big Breakfast with Hotcakes (Regular Biscuit) 
##                                                       5.1581 
##                                Big Breakfast (Large Biscuit) 
##                                                       3.0149 
##   Big Breakfast with Hotcakes and Egg Whites (Large Biscuit) 
##                                                       2.7354 
##                              Big Breakfast (Regular Biscuit) 
##                                                       2.3888 
## Big Breakfast with Hotcakes and Egg Whites (Regular Biscuit) 
##                                                       2.2587 
##                           Double Quarter Pounder with Cheese 
##                                                       1.8953 
##                                 Chicken McNuggets (20 piece) 
##                                                       1.4595 
##                                    Steak, Egg & Cheese Bagel 
##                                                       1.3944 
##                                       Bacon Clubhouse Burger 
##                                                       1.3389 
##                        Steak & Egg Biscuit (Regular Biscuit) 
##                                                       0.9837 
##                      Bacon Clubhouse Crispy Chicken Sandwich 
##                                                       0.9618 
##                                    Bacon, Egg & Cheese Bagel 
##                                                       0.9220 
##                          Quarter Pounder with Bacon & Cheese 
##                                                       0.8487 
##                        McFlurry with M&M's™ Candies (Medium) 
##                                                       0.8480 
##                Big Breakfast with Egg Whites (Large Biscuit) 
##                                                       0.8376 
##                    Quarter Pounder with Bacon Habanero Ranch 
##                                                       0.8328 
##                     Sausage Biscuit with Egg (Large Biscuit) 
##                                                       0.8053 
##            Premium McWrap Southwest Chicken (Crispy Chicken) 
##                                                       0.7307
round(sort(cont_ind[,2],decreasing=TRUE)[1:20], 4)
##                          Strawberry Shake (Large) 
##                                            4.9639 
##             McFlurry with M&M's™ Candies (Medium) 
##                                            4.4992 
##                            Shamrock Shake (Large) 
##                                            4.2622 
##                             Vanilla Shake (Large) 
##                                            3.9524 
##                           Chocolate Shake (Large) 
##                                            3.4575 
##                         Strawberry Shake (Medium) 
##                                            2.9121 
##                     Frappé Chocolate Chip (Large) 
##                                            2.7804 
##                           Shamrock Shake (Medium) 
##                                            2.5820 
## McFlurry with Reese's Peanut Butter Cups (Medium) 
##                                            2.4738 
##                            Vanilla Shake (Medium) 
##                                            2.3793 
##                            Frappé Caramel (Large) 
##                                            2.3544 
##                          Chocolate Shake (Medium) 
##                                            2.1075 
##                              Frappé Mocha (Large) 
##                                            1.8893 
##               McFlurry with Oreo Cookies (Medium) 
##                                            1.7987 
##                          Strawberry Shake (Small) 
##                                            1.6320 
##              McFlurry with M&M's™ Candies (Small) 
##                                            1.5816 
##                    Frappé Chocolate Chip (Medium) 
##                                            1.5243 
##                             Vanilla Shake (Small) 
##                                            1.3347 
##                           Frappé Caramel (Medium) 
##                                            1.2753 
##                           Chocolate Shake (Small) 
##                                            1.2314
round(sort(cont_ind[,3],decreasing=TRUE)[1:20], 4)
##               Premium Southwest Salad with Grilled Chicken 
##                                                     9.1948 
##                Premium Southwest Salad with Crispy Chicken 
##                                                     8.6640 
##                  Premium Southwest Salad (without Chicken) 
##                                                     7.1022 
##                                      Fruit & Maple Oatmeal 
##                                                     3.5078 
##                Premium Bacon Ranch Salad (without Chicken) 
##                                                     3.4989 
##                  Fruit & Maple Oatmeal without Brown Sugar 
##                                                     3.4661 
##                           Minute Maid Orange Juice (Large) 
##                                                     2.2139 
##         Premium McWrap Southwest Chicken (Grilled Chicken) 
##                                                     2.0114 
##                            Big Breakfast (Regular Biscuit) 
##                                                     1.9446 
##                         Strawberry Banana Smoothie (Large) 
##                                                     1.8023 
##                     Blueberry Pomegranate Smoothie (Large) 
##                                                     1.7410 
##                                         Large French Fries 
##                                                     1.5562 
##                           Mango Pineapple Smoothie (Large) 
##                                                     1.3807 
##                              Big Breakfast (Large Biscuit) 
##                                                     1.2955 
##          Premium McWrap Southwest Chicken (Crispy Chicken) 
##                                                     1.2893 
##                      Steak & Egg Biscuit (Regular Biscuit) 
##                                                     1.2480 
##                 Sausage Biscuit with Egg (Regular Biscuit) 
##                                                     1.1705 
## Big Breakfast with Hotcakes and Egg Whites (Large Biscuit) 
##                                                     1.1081 
##             Premium Bacon Ranch Salad with Grilled Chicken 
##                                                     1.0662 
##       Premium McWrap Chicken Sweet Chili (Grilled Chicken) 
##                                                     1.0027
names(sort(cont_ind[cont_ind[,1]>100/260*5,1],decreasing=TRUE))
## [1] "Chicken McNuggets (40 piece)"                                
## [2] "Big Breakfast with Hotcakes (Large Biscuit)"                 
## [3] "Big Breakfast with Hotcakes (Regular Biscuit)"               
## [4] "Big Breakfast (Large Biscuit)"                               
## [5] "Big Breakfast with Hotcakes and Egg Whites (Large Biscuit)"  
## [6] "Big Breakfast (Regular Biscuit)"                             
## [7] "Big Breakfast with Hotcakes and Egg Whites (Regular Biscuit)"
#page 161
names(sort(cont_ind[cont_ind[,2]>100/260*5,2],decreasing=TRUE))
##  [1] "Strawberry Shake (Large)"                         
##  [2] "McFlurry with M&M's™ Candies (Medium)"            
##  [3] "Shamrock Shake (Large)"                           
##  [4] "Vanilla Shake (Large)"                            
##  [5] "Chocolate Shake (Large)"                          
##  [6] "Strawberry Shake (Medium)"                        
##  [7] "Frappé Chocolate Chip (Large)"                    
##  [8] "Shamrock Shake (Medium)"                          
##  [9] "McFlurry with Reese's Peanut Butter Cups (Medium)"
## [10] "Vanilla Shake (Medium)"                           
## [11] "Frappé Caramel (Large)"                           
## [12] "Chocolate Shake (Medium)"
names(sort(cont_ind[cont_ind[,3]>100/260*5,3],decreasing=TRUE))
## [1] "Premium Southwest Salad with Grilled Chicken"      
## [2] "Premium Southwest Salad with Crispy Chicken"       
## [3] "Premium Southwest Salad (without Chicken)"         
## [4] "Fruit & Maple Oatmeal"                             
## [5] "Premium Bacon Ranch Salad (without Chicken)"       
## [6] "Fruit & Maple Oatmeal without Brown Sugar"         
## [7] "Minute Maid Orange Juice (Large)"                  
## [8] "Premium McWrap Southwest Chicken (Grilled Chicken)"
## [9] "Big Breakfast (Regular Biscuit)"
fviz_pca_ind(macdo.acp, geom = c("point"), col.ind = 
               "contrib", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"))

fviz_pca_ind(macdo.acp, geom = c("point"), col.ind = 
               "cos2", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"))

#page 163
fviz_pca_ind(macdo.acp, geom = c("point"), alpha.ind = "contrib")

fviz_pca_ind(macdo.acp, geom = c("point"), alpha.ind = "cos2")

#q(28)
scatter(macdo.acp)

#q29
gcol <- hcl.colors(9, palette = "Dynamic")
s.class(dfxy = macdo.acp$li, fac = d_macdo$Category, 
        col = gcol, xax = 1, yax = 2)

#page 165
#q30
fviz_pca_biplot(macdo.acp, col.ind ="contrib", col.var ="contrib")

fviz_pca_biplot(macdo.acp, col.ind = "cos2", col.var = "cos2")

#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("adegraphics")){install.packages("adegraphics")}
library(adegraphics)
## Registered S3 methods overwritten by 'adegraphics':
##   method         from
##   biplot.dudi    ade4
##   kplot.foucart  ade4
##   kplot.mcoa     ade4
##   kplot.mfa      ade4
##   kplot.pta      ade4
##   kplot.sepan    ade4
##   kplot.statis   ade4
##   scatter.coa    ade4
##   scatter.dudi   ade4
##   scatter.nipals ade4
##   scatter.pco    ade4
##   score.acm      ade4
##   score.mix      ade4
##   score.pca      ade4
##   screeplot.dudi ade4
## 
## Attaching package: 'adegraphics'
## The following objects are masked from 'package:ade4':
## 
##     kplotsepan.coa, s.arrow, s.class, s.corcircle, s.distri,
##     s.image, s.label, s.logo, s.match, s.traject, s.value,
##     table.value, triangle.class
## The following object is masked from 'package:jmuOutlier':
## 
##     score
list <- setdiff(colnames(d_macdo), c("Category", "Item", 
                                     "Serving.Size"))
tmp_macdo=list()
tmp_macdo$Category=d_macdo[, "Category"]
tmp_macdo$tab=d_macdo[, list]
macdo.acp.dudi <- dudi.pca(tmp_macdo$tab, center = TRUE, scale = 
                             FALSE, scan = FALSE, nf = 3)

#page 166
macdo.acp.dudi1 <- dudi.pca(tmp_macdo$tab, center = TRUE, scale = 
                              TRUE, scan = FALSE, nf = 3)
g1 <- s.class(macdo.acp.dudi$li, tmp_macdo$Category, plot = FALSE)
g2 <- s.arrow(macdo.acp.dudi$c1, lab = names(macdo.acp$tab), 
              plot = FALSE)
g3 <- s.class(macdo.acp.dudi1$li, tmp_macdo$Category, plot = FALSE)
g4 <- s.corcircle(macdo.acp.dudi1$co, lab = names(macdo.acp$tab), 
                  full = FALSE, plot = FALSE)
G1 <- rbindADEg(cbindADEg(g1, g2, plot = FALSE), cbindADEg(g3, g4, 
                  plot = FALSE), plot = TRUE)

#page 167
head(sort(apply(d_macdo[,-(1:3)],2,var),decreasing=TRUE))
##                      Sodium                    Calories 
##                 332959.3774                  57729.6184 
##           Calories.from.Fat                 Cholesterol 
##                  16352.2494                   7615.9233 
## Cholesterol....Daily.Value.                      Sugars 
##                    846.3243                    822.5307
#Analyse factorielle des correspondances
#page 167
d_vac<-read.csv2("https://tinyurl.com/y3emuylu", row.names = 1)
rownames(d_vac)
## [1] "Agriculteurs"  "Patrons"       "Cadres.sup"    "Cadre.moy"    
## [5] "Employes"      "Ouvriers"      "Autres.actifs" "Inactifs"
#page 168
str(d_vac)
## 'data.frame':    8 obs. of  8 variables:
##  $ Hotel     : int  195 700 961 572 441 783 142 741
##  $ Location  : int  62 354 471 537 404 1114 103 332
##  $ Res.Second: int  1 229 633 279 166 387 210 327
##  $ Parents   : int  499 959 1580 1689 1079 4052 1133 1789
##  $ Amis      : int  44 185 305 206 178 497 132 311
##  $ Camping   : int  141 292 360 748 434 1464 181 236
##  $ Sej.org   : int  49 119 162 155 178 525 46 102
##  $ Autres    : int  65 140 148 112 92 387 59 102
barplot(t(d_vac),beside=TRUE, col = hcl.colors(8, palette = 
        "Dynamic"), legend.text = colnames(d_vac), args.legend = 
        list(bg = "white", x = "topleft"))

#page 169
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(ggpubr)){install.packages("ggpubr")}
library(ggpubr)
## Loading required package: magrittr
my_cols <- c("#0D0887FF", "#6A00A8FF", "#B12A90FF", 
             "#E16462FF", "#FCA636FF", "#F0F921FF")
ggballoonplot(d_vac, fill = "value") + scale_fill_gradientn( 
  colors = my_cols)

library(ade4)
table.cont(d_vac)

#page 170
sum(d_vac)
## [1] 31079
khi.test.d_vac <- chisq.test(d_vac)
round(khi.test.d_vac$expected, 2)
##                 Hotel Location Res.Second Parents   Amis Camping Sej.org
## Agriculteurs   154.09   114.74      75.84  434.24  63.13  131.02   45.39
## Patrons        434.55   323.59     213.87 1224.58 178.03  369.48  128.02
## Cadres.sup     674.14   502.00     331.79 1899.79 276.20  573.21  198.60
## Cadre.moy      627.16   467.01     308.67 1767.38 256.95  533.26  184.76
## Employes       433.67   322.93     213.44 1222.12 177.68  368.74  127.76
## Ouvriers      1343.76  1000.64     661.36 3786.83 550.54 1142.57  395.87
## Autres.actifs  292.71   217.97     144.06  824.89 119.92  248.89   86.23
## Inactifs       574.92   428.11     282.96 1620.17 235.55  488.84  169.37
##               Autres
## Agriculteurs   37.55
## Patrons       105.88
## Cadres.sup    164.26
## Cadre.moy     152.81
## Employes      105.67
## Ouvriers      327.42
## Autres.actifs  71.32
## Inactifs      140.08
khi.test.d_vac
## 
##  Pearson's Chi-squared test
## 
## data:  d_vac
## X-squared = 2292.1, df = 49, p-value < 2.2e-16
#page 171
set.seed(1133)
chisq.test(d_vac, simulate.p.value = TRUE, B=50000)
## 
##  Pearson's Chi-squared test with simulated p-value (based on 50000
##  replicates)
## 
## data:  d_vac
## X-squared = 2292.1, df = NA, p-value = 2e-05
mosaicplot(d_vac, type = "pearson", shade = TRUE, las = 2, 
           main = "Associations et r\u00e9sidus du test du chi2")

mosaicplot(t(d_vac), type = "pearson", shade = TRUE, las = 2, 
           main = "Associations et r\u00e9sidus du test du chi2")

#page 172
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("vcd")){install.packages("vcd")}
library(vcd)
## Loading required package: grid
d_vactable <- as.table(as.matrix(d_vac))
assoc(d_vactable, shade=TRUE, las=2, varnames=F, rot_labels=90, 
main="Associations et r\u00e9sidus du test du test du chi2", 
labeling_args=list(abbreviate=c(A=TRUE)))

#page 173
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(FactoMineR)){install.packages("FactoMineR")}
library(FactoMineR)
## 
## Attaching package: 'FactoMineR'
## The following object is masked from 'package:ade4':
## 
##     reconst
(res.ca.d_vac<-CA(d_vac, ncp=4, graph=FALSE))
## **Results of the Correspondence Analysis (CA)**
## The row variable has  8  categories; the column variable has 8 categories
## The chi square of independence between the two variables is equal to 2292.148 (p-value =  0 ).
## *The results are available in the following objects:
## 
##    name              description                   
## 1  "$eig"            "eigenvalues"                 
## 2  "$col"            "results for the columns"     
## 3  "$col$coord"      "coord. for the columns"      
## 4  "$col$cos2"       "cos2 for the columns"        
## 5  "$col$contrib"    "contributions of the columns"
## 6  "$row"            "results for the rows"        
## 7  "$row$coord"      "coord. for the rows"         
## 8  "$row$cos2"       "cos2 for the rows"           
## 9  "$row$contrib"    "contributions of the rows"   
## 10 "$call"           "summary called parameters"   
## 11 "$call$marge.col" "weights of the columns"      
## 12 "$call$marge.row" "weights of the rows"
#page 174
library(factoextra)
round(eig.val <- get_eigenvalue(res.ca.d_vac), 3)
##       eigenvalue variance.percent cumulative.variance.percent
## Dim.1      0.044           59.977                      59.977
## Dim.2      0.018           23.997                      83.973
## Dim.3      0.008           10.375                      94.349
## Dim.4      0.002            3.038                      97.386
## Dim.5      0.002            2.283                      99.669
## Dim.6      0.000            0.330                      99.999
## Dim.7      0.000            0.001                     100.000
#page 175
as.numeric(colSums(eig.val)[1])
## [1] 0.07375229
sqrt(as.numeric(khi.test.d_vac$statistic)/sum(d_vac)/ 
       (min(nrow(d_vac),ncol(d_vac))-1))
## [1] 0.1026452
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(questionr)){install.packages("questionr")}
library(questionr)
cramer.v(d_vac)
## [1] 0.1026452
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(DescTools)){install.packages("DescTools")}
library(DescTools)
CramerV(d_vac)
## [1] 0.1026452
Phi(d_vac)
## [1] 0.2715737
#page 176
CramerV(d_vac, conf.level = .95)
##  Cramer V    lwr.ci    upr.ci 
## 0.1026452 0.0973395 0.1057883
d_vac.tab <- as.table(as.matrix(d_vac))
d_vac.frm <- Untable(d_vac.tab)
head(d_vac.frm)
##           Var1  Var2
## 1 Agriculteurs Hotel
## 2 Agriculteurs Hotel
## 3 Agriculteurs Hotel
## 4 Agriculteurs Hotel
## 5 Agriculteurs Hotel
## 6 Agriculteurs Hotel
n <- 10000
set.seed(1133)
idx <- matrix(sample(nrow(d_vac.frm), size=nrow(d_vac.frm) * n, 
                     replace=TRUE), ncol=n, byrow=FALSE)
v <- apply(idx, 2, function(x) CramerV(d_vac.frm[x,1], 
                                       d_vac.frm[x,2]))
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(lattice)){install.packages("lattice")}
library(lattice)
lattice::bwplot(v)

quantile(v, probs=c(0.025,0.975))
##       2.5%      97.5% 
## 0.09976719 0.10772114
#page 178
set.seed(1133)
idx.perm <- replicate(n,sample(nrow(d_vac.frm), replace=FALSE))
v.perm <- apply(idx.perm, 2, function(x) CramerV(d_vac.frm[,1], 
                                                 d_vac.frm[x,2]))
hist(v.perm, xlim=c(0,CramerV(d_vac)))
abline(v=CramerV(d_vac), lwd=2, col="red")

mean(v.perm>=CramerV(d_vac))
## [1] 0
library(factoextra)
fviz_eig(res.ca.d_vac)

#page 179
mean(res.ca.d_vac$eig[,1])
## [1] 0.01053604
res.ca.d_vac$eig[,2]>1/(ncol(d_vac)-1)*100
## dim 1 dim 2 dim 3 dim 4 dim 5 dim 6 dim 7 
##  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
fviz_ca_row(res.ca.d_vac)

fviz_ca_col(res.ca.d_vac) 

fviz_ca_biplot(res.ca.d_vac)

#page 180
fviz_ca_row(res.ca.d_vac)

fviz_ca_row(res.ca.d_vac, col.row="contrib")

fviz_ca_row(res.ca.d_vac, col.row="cos2")

fviz_ca_row(res.ca.d_vac, alpha.row="contrib")

fviz_ca_row(res.ca.d_vac, alpha.row="cos2")

fviz_ca_col(res.ca.d_vac)

fviz_ca_col(res.ca.d_vac, col.col="contrib")

fviz_ca_col(res.ca.d_vac, col.col="cos2")

fviz_ca_col(res.ca.d_vac, alpha.col="contrib")

fviz_ca_col(res.ca.d_vac, alpha.col="cos2")

fviz_ca_biplot(res.ca.d_vac)

fviz_ca_biplot(res.ca.d_vac, col.row="contrib", col.col="contrib")

fviz_ca_biplot(res.ca.d_vac, col.row="cos2", col.col="cos2")

#page 181
rowpr <- fviz_ca_biplot(res.ca.d_vac, map="rowprincipal", arrow = 
                          c(TRUE, TRUE), repel=TRUE)
colpr <- fviz_ca_biplot(res.ca.d_vac, map="colprincipal", arrow = 
                          c(TRUE, TRUE), repel=TRUE)
library(GGally)
ggmatrix(list(rowpr, colpr),1,2)

#Analyse non symetrique des correspondances
#page 182
d_TM<-read.csv2("https://tinyurl.com/y55e3k9y", row.names = 1)
rownames(d_TM)
##  [1] "Laundry"    "Main_meal"  "Dinner"     "Breakfeast" "Tidying"   
##  [6] "Dishes"     "Shopping"   "Official"   "Driving"    "Finances"  
## [11] "Insurance"  "Repairs"    "Holidays"
str(d_TM)
## 'data.frame':    13 obs. of  4 variables:
##  $ Wife       : int  156 124 77 82 53 32 33 12 10 13 ...
##  $ Alternating: int  14 20 11 36 11 24 23 46 51 13 ...
##  $ Husband    : int  2 5 7 15 1 4 9 23 75 21 ...
##  $ Jointly    : int  4 4 13 7 57 53 55 15 3 66 ...
#page 183
barplot(t(d_TM), beside=TRUE, names=d_TM$Task, col = c("red", 
        "green","blue","purple"), legend.text = colnames(d_TM), 
        args.legend = list(bg = "white", x = "top"))

#page 184
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(ggpubr)){install.packages("ggpubr")}
library(ggpubr)
my_cols <- c("#0D0887FF", "#6A00A8FF", "#B12A90FF", 
             "#E16462FF", "#FCA636FF", "#F0F921FF")
ggballoonplot(d_TM, fill = "value") + scale_fill_gradientn(
  colors = my_cols)

library(ade4)
table.cont(d_TM)

sum(d_TM)
## [1] 1744
#page 185
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(DescTools)){install.packages("DescTools")}
library(DescTools)
Lambda(d_TM)
## [1] 0.3410767
Lambda(d_TM, conf.level=0.95)
##    lambda    lwr.ci    upr.ci 
## 0.3410767 0.3170578 0.3650956
#page 186
Lambda(d_TM, direction="row", conf.level=0.95)
##    lambda    lwr.ci    upr.ci 
## 0.2193878 0.1977464 0.2410292
Lambda(d_TM, direction="column", conf.level=0.95)
##    lambda    lwr.ci    upr.ci 
## 0.5078671 0.4717505 0.5439838
d_TM.tab <- as.table(as.matrix(d_TM))
d_TM.frm <- Untable(d_TM.tab)
head(d_TM.frm)
##      Var1 Var2
## 1 Laundry Wife
## 2 Laundry Wife
## 3 Laundry Wife
## 4 Laundry Wife
## 5 Laundry Wife
## 6 Laundry Wife
n <- 10000
set.seed(1133)
idx <- matrix(sample(nrow(d_TM.frm), size=nrow(d_TM.frm) * n, 
                     replace=TRUE), ncol=n, byrow=FALSE)
lR.d_TM <- apply(idx, 2, function(x) Lambda(d_TM.frm[x,1], 
                            d_TM.frm[x,2],direction = "row"))
lC.d_TM <- apply(idx, 2, function(x) Lambda(d_TM.frm[x,1], 
                            d_TM.frm[x,2],direction = "column"))
l.d_TM <- apply(idx, 2, function(x) Lambda(d_TM.frm[x,1], 
                            d_TM.frm[x,2]))
lattice::bwplot(lR.d_TM)

lattice::bwplot(lC.d_TM)

lattice::bwplot(l.d_TM)

#page 187
quantile(lR.d_TM, probs=c(0.025,0.975))
##      2.5%     97.5% 
## 0.1985655 0.2379738
quantile(lC.d_TM, probs=c(0.025,0.975))
##      2.5%     97.5% 
## 0.4756842 0.5450645
quantile(l.d_TM, probs=c(0.025,0.975))
##      2.5%     97.5% 
## 0.3184851 0.3642997
#page 187-188
n <- 10000; set.seed(1133)
idx.perm <- replicate(n,sample(nrow(d_TM.frm), replace=FALSE))
lR.perm.d_TM <- apply(idx.perm, 2, function(x) Lambda(d_TM.frm[,1], 
                      d_TM.frm[x,2], direction = "row"))
lC.perm.d_TM <- apply(idx.perm, 2, function(x) Lambda(d_TM.frm[,1], 
                      d_TM.frm[x,2], direction = "column"))
l.perm.d_TM <- apply(idx.perm, 2, function(x) Lambda(d_TM.frm[,1], 
                      d_TM.frm[x,2]))
hist(lR.perm.d_TM, xlim=c(0,Lambda(d_TM, direction = "row")))
abline(v=Lambda(d_TM, direction = "row"), lwd=2, col="red")

hist(lC.perm.d_TM, xlim=c(0,Lambda(d_TM, direction = "column")))
abline(v=Lambda(d_TM, direction = "column"), lwd=2, col="red")

hist(l.perm.d_TM, xlim=c(0,Lambda(d_TM)))
abline(v=Lambda(d_TM), lwd=2, col="red")

mean(lR.perm.d_TM>=Lambda(d_TM))
## [1] 0
mean(lC.perm.d_TM>=Lambda(d_TM))
## [1] 0
mean(l.perm.d_TM>=Lambda(d_TM))
## [1] 0
GoodmanKruskalTau(d_TM.tab, direction="column", conf.level=0.95)
##      tauA    lwr.ci    upr.ci 
## 0.4067139 0.3796921 0.4337358
#page 189
GoodmanKruskalTau(d_TM.tab, direction="row", conf.level=0.95)
##       tauA     lwr.ci     upr.ci 
## 0.10465652 0.09471543 0.11459762
library(ade4)
(res.nsc.d_TM <- dudi.nsc(d_TM, scan = FALSE))
## Duality diagramm
## class: nsc dudi
## $call: dudi.nsc(df = d_TM, scannf = FALSE)
## 
## $nf: 2 axis-components saved
## $rank: 3
## eigen values: 0.6322 0.4662 0.08517
##   vector length mode    content       
## 1 $cw    4      numeric column weights
## 2 $lw    13     numeric row weights   
## 3 $eig   3      numeric eigen values  
## 
##   data.frame nrow ncol content             
## 1 $tab       13   4    modified array      
## 2 $li        13   2    row coordinates     
## 3 $l1        13   2    row normed scores   
## 4 $co        4    2    column coordinates  
## 5 $c1        4    2    column normed scores
## other elements: N
#page 190
library(adegraphics)
g1 <- s.label(res.nsc.d_TM$c1, plab.cex = 1.25)
g2 <- s.arrow(res.nsc.d_TM$li, add = TRUE, plab.cex = 0.75)

#Analyse des correspondances multiples
#page 191
poke<-read.csv("https://tinyurl.com/y4y6a86m",na.strings= c("","NA"))
poke<-as.data.frame(poke)
poke$Generation<-as.factor(poke$Generation)
summary(poke)
##        X.                             Name         Type.1   
##  Min.   :  1.0   Abomasnow              :  1   Water  :112  
##  1st Qu.:184.8   AbomasnowMega Abomasnow:  1   Normal : 98  
##  Median :364.5   Abra                   :  1   Grass  : 70  
##  Mean   :362.8   Absol                  :  1   Bug    : 69  
##  3rd Qu.:539.2   AbsolMega Absol        :  1   Psychic: 57  
##  Max.   :721.0   Accelgor               :  1   Fire   : 52  
##                  (Other)                :794   (Other):342  
##       Type.2        Total             HP             Attack   
##  Flying  : 97   Min.   :180.0   Min.   :  1.00   Min.   :  5  
##  Ground  : 35   1st Qu.:330.0   1st Qu.: 50.00   1st Qu.: 55  
##  Poison  : 34   Median :450.0   Median : 65.00   Median : 75  
##  Psychic : 33   Mean   :435.1   Mean   : 69.26   Mean   : 79  
##  Fighting: 26   3rd Qu.:515.0   3rd Qu.: 80.00   3rd Qu.:100  
##  (Other) :189   Max.   :780.0   Max.   :255.00   Max.   :190  
##  NA's    :386                                                 
##     Defense          Sp..Atk          Sp..Def          Speed       
##  Min.   :  5.00   Min.   : 10.00   Min.   : 20.0   Min.   :  5.00  
##  1st Qu.: 50.00   1st Qu.: 49.75   1st Qu.: 50.0   1st Qu.: 45.00  
##  Median : 70.00   Median : 65.00   Median : 70.0   Median : 65.00  
##  Mean   : 73.84   Mean   : 72.82   Mean   : 71.9   Mean   : 68.28  
##  3rd Qu.: 90.00   3rd Qu.: 95.00   3rd Qu.: 90.0   3rd Qu.: 90.00  
##  Max.   :230.00   Max.   :194.00   Max.   :230.0   Max.   :180.00  
##                                                                    
##  Generation Legendary  
##  1:166      False:735  
##  2:106      True : 65  
##  3:160                 
##  4:121                 
##  5:165                 
##  6: 82                 
## 
poke.x<-poke[,c(3,12,13)]

#page 192
library(ade4); library(adegraphics)
res.acm.poke<-dudi.acm(poke.x,scannf=FALSE)

min(nrow(poke.x) - 1, nlevels(poke$Type.1) + nlevels(
  poke$Generation) + nlevels(poke$Legendary) - ncol(poke.x))
## [1] 23
fviz_screeplot(res.acm.poke)

get_eig(res.acm.poke)
##        eigenvalue variance.percent cumulative.variance.percent
## Dim.1   0.4780316         6.235195                    6.235195
## Dim.2   0.4365931         5.694692                   11.929888
## Dim.3   0.4003682         5.222194                   17.152081
## Dim.4   0.3831707         4.997879                   22.149961
## Dim.5   0.3750273         4.891660                   27.041621
## Dim.6   0.3670084         4.787066                   31.828686
## Dim.7   0.3333333         4.347826                   36.176512
## Dim.8   0.3333333         4.347826                   40.524338
## Dim.9   0.3333333         4.347826                   44.872165
## Dim.10  0.3333333         4.347826                   49.219991
## Dim.11  0.3333333         4.347826                   53.567817
## Dim.12  0.3333333         4.347826                   57.915643
## Dim.13  0.3333333         4.347826                   62.263469
## Dim.14  0.3333333         4.347826                   66.611295
## Dim.15  0.3333333         4.347826                   70.959121
## Dim.16  0.3333333         4.347826                   75.306947
## Dim.17  0.3333333         4.347826                   79.654773
## Dim.18  0.2961106         3.862313                   83.517086
## Dim.19  0.2853067         3.721391                   87.238477
## Dim.20  0.2794082         3.644455                   90.882932
## Dim.21  0.2550264         3.326432                   94.209364
## Dim.22  0.2311516         3.015021                   97.224384
## Dim.23  0.2127972         2.775616                  100.000000
#page 193
res.acm.poke$cr
##                  RS1        RS2
## Type.1     0.6285080 0.65023195
## Generation 0.3154106 0.63418729
## Legendary  0.4901764 0.02536001
#page 194
score(res.acm.poke, xax=1)

score(res.acm.poke, xax=1, type = "boxplot")

boxplot(res.acm.poke)

ade4::s.corcircle(res.acm.poke$co, clabel = 0.7)

#page 195
library(devtools)
## Loading required package: usethis
if(!require(JLutils)){install_github("larmarange/JLutils")}
## Loading required package: JLutils
## Loading required package: plyr
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:ggpubr':
## 
##     mutate
## 
## Attaching package: 'JLutils'
## The following object is masked from 'package:ggpubr':
## 
##     get_legend
## The following object is masked from 'package:coin':
## 
##     pvalue
library(JLutils)
s.freq(res.acm.poke$li)

library(factoextra)
fviz_mca_biplot(res.acm.poke)

scatter(res.acm.poke)

#Analyse factorielle des donnees mixtes
#page 197
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("PCAmixdata")){install.packages("PCAmixdata")}
library(PCAmixdata)

round(cor(poke[,c(7,8,9,10,11)]),2)
##         Attack Defense Sp..Atk Sp..Def Speed
## Attack    1.00    0.44    0.40    0.26  0.38
## Defense   0.44    1.00    0.22    0.51  0.02
## Sp..Atk   0.40    0.22    1.00    0.51  0.47
## Sp..Def   0.26    0.51    0.51    1.00  0.26
## Speed     0.38    0.02    0.47    0.26  1.00
mix.poke<-PCAmix(subset(poke,select=7:11),subset(poke,select=3))

#page 198
round(mix.poke$eig, 2)
##        Eigenvalue Proportion Cumulative
## dim 1        2.57      11.69      11.69
## dim 2        1.54       7.00      18.68
## dim 3        1.23       5.58      24.26
## dim 4        1.04       4.71      28.97
## dim 5        1.02       4.64      33.61
## dim 6        1.00       4.55      38.16
## dim 7        1.00       4.55      42.70
## dim 8        1.00       4.55      47.25
## dim 9        1.00       4.55      51.79
## dim 10       1.00       4.55      56.34
## dim 11       1.00       4.55      60.88
## dim 12       1.00       4.55      65.43
## dim 13       1.00       4.55      69.97
## dim 14       1.00       4.55      74.52
## dim 15       1.00       4.55      79.06
## dim 16       1.00       4.55      83.61
## dim 17       1.00       4.55      88.16
## dim 18       0.85       3.88      92.04
## dim 19       0.67       3.04      95.08
## dim 20       0.47       2.12      97.20
## dim 21       0.36       1.65      98.85
## dim 22       0.25       1.15     100.00
round(mix.poke$categ.coord, 2)
##          dim 1 dim 2 dim 3 dim 4 dim 5
## Bug      -0.64  0.36  0.07 -0.67  0.54
## Dark      0.17 -0.35  1.04  0.14 -0.36
## Dragon    1.44 -0.08  1.13  1.12  1.39
## Electric  0.26 -1.24 -0.35 -1.64 -1.04
## Fairy    -0.28  0.12 -2.62  2.48  2.64
## Fighting -0.31  0.31  1.96  1.50  1.36
## Fire      0.33 -0.69  0.27  1.27 -1.47
## Flying    0.66 -1.83  0.99 -3.27 -1.38
## Ghost     0.15  0.25 -0.97 -0.16 -0.27
## Grass    -0.15 -0.06 -0.65  1.09 -1.07
## Ground   -0.09  1.00  1.59  0.23 -0.98
## Ice      -0.03 -0.07 -0.90  0.68  0.55
## Normal   -0.61 -0.39  0.79 -0.83  0.98
## Poison   -0.49  0.13  0.26  0.03 -0.25
## Psychic   0.61 -1.17 -1.17 -0.63  1.19
## Rock      0.29  1.76  0.08  0.05  0.32
## Steel     0.75  2.68 -0.62 -2.04 -0.55
## Water    -0.10 -0.05 -0.38  0.14 -0.71
#Classification ascendante hierarchique et methode des K-moyennes
#page 200
data_event <- read.csv("https://tinyurl.com/y2k7mwbr")
head(data_event)
##     X   id_odsp  id_event sort_order time
## 1 321 00nmICd9/ 00nmICd91          1    3
## 2 322 00nmICd9/ 00nmICd92          2    3
## 3 323 00nmICd9/ 00nmICd93          3    7
## 4 324 00nmICd9/ 00nmICd94          4    7
## 5 325 00nmICd9/ 00nmICd95          5    7
## 6 326 00nmICd9/ 00nmICd96          6    7
##                                                                 text
## 1                                Foul by Juan Manuel FalcA³n (Metz).
## 2 TiemouA© Bakayoko (Monaco) wins a free kick in the defensive half.
## 3                                  Foul by Anthony Martial (Monaco).
## 4     Sylvain Marchal (Metz) wins a free kick in the defensive half.
## 5                                    Foul by Cheick Doukoure (Metz).
## 6 TiemouA© Bakayoko (Monaco) wins a free kick in the defensive half.
##   event_type event_type2 side event_team  opponent             player
## 1          3          NA    1       Metz AS Monaco juan manuel falcon
## 2          8          NA    2  AS Monaco      Metz   tiemoue bakayoko
## 3          3          NA    2  AS Monaco      Metz    anthony martial
## 4          8          NA    1       Metz AS Monaco    sylvain marchal
## 5          3          NA    1       Metz AS Monaco    cheick doukoure
## 6          8          NA    2  AS Monaco      Metz   tiemoue bakayoko
##   player2 player_in player_out shot_place shot_outcome is_goal location
## 1    <NA>      <NA>       <NA>         NA           NA       0       NA
## 2    <NA>      <NA>       <NA>         NA           NA       0        2
## 3    <NA>      <NA>       <NA>         NA           NA       0       NA
## 4    <NA>      <NA>       <NA>         NA           NA       0        2
## 5    <NA>      <NA>       <NA>         NA           NA       0       NA
## 6    <NA>      <NA>       <NA>         NA           NA       0        2
##   bodypart assist_method situation fast_break
## 1       NA             0        NA          0
## 2       NA             0        NA          0
## 3       NA             0        NA          0
## 4       NA             0        NA          0
## 5       NA             0        NA          0
## 6       NA             0        NA          0
##                                                link_odsp adv_stats
## 1 /soccer/france/ligue-1-2014-2015/metz-monaco-00nmICd9/      TRUE
## 2 /soccer/france/ligue-1-2014-2015/metz-monaco-00nmICd9/      TRUE
## 3 /soccer/france/ligue-1-2014-2015/metz-monaco-00nmICd9/      TRUE
## 4 /soccer/france/ligue-1-2014-2015/metz-monaco-00nmICd9/      TRUE
## 5 /soccer/france/ligue-1-2014-2015/metz-monaco-00nmICd9/      TRUE
## 6 /soccer/france/ligue-1-2014-2015/metz-monaco-00nmICd9/      TRUE
##         date league season country   ht        at fthg ftag odd_h odd_d
## 1 2014-12-20     F1   2015  france Metz AS Monaco    0    1  4.45  3.55
## 2 2014-12-20     F1   2015  france Metz AS Monaco    0    1  4.45  3.55
## 3 2014-12-20     F1   2015  france Metz AS Monaco    0    1  4.45  3.55
## 4 2014-12-20     F1   2015  france Metz AS Monaco    0    1  4.45  3.55
## 5 2014-12-20     F1   2015  france Metz AS Monaco    0    1  4.45  3.55
## 6 2014-12-20     F1   2015  france Metz AS Monaco    0    1  4.45  3.55
##   odd_a odd_over odd_under odd_bts odd_bts_n
## 1  2.14       NA        NA      NA        NA
## 2  2.14       NA        NA      NA        NA
## 3  2.14       NA        NA      NA        NA
## 4  2.14       NA        NA      NA        NA
## 5  2.14       NA        NA      NA        NA
## 6  2.14       NA        NA      NA        NA
#page 201
list_col <- c("sort_order","time","event_team","fthg","odd_h")
data_event.x <- data_event[,list_col]

#page 202
data_event.c<-aggregate(.~event_team,data=data_event.x,FUN=mean)
str(data_event.c)
## 'data.frame':    30 obs. of  5 variables:
##  $ event_team: Factor w/ 30 levels "AC Ajaccio","AJ Auxerre",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ sort_order: num  55.4 53 47.9 51.8 52.8 ...
##  $ time      : num  49 49.9 49.8 49.7 49.7 ...
##  $ fthg      : num  1.48 1.62 1.18 1.48 1.3 ...
##  $ odd_h     : num  2.81 2.56 2.58 2.88 2.49 ...
rownames(data_event.c) <- data_event.c$event_team
data_event.c$event_team <- NULL
head(data_event.c)
##                   sort_order     time     fthg    odd_h
## AC Ajaccio          55.41732 49.02205 1.476640 2.809825
## AJ Auxerre          53.02772 49.92238 1.620968 2.556764
## Angers              47.88800 49.79695 1.181333 2.579836
## AS Monaco           51.81358 49.68000 1.477080 2.880829
## AS Nancy Lorraine   52.83515 49.70655 1.298690 2.494692
## Bastia              51.18294 49.12756 1.489122 2.687525
library(GGally)
ggpairs(data_event.c)

#page 204
d.data_event <- dist(data_event.c)
cah.ward <- hclust(d.data_event, method="ward.D2")
plot(cah.ward, xlab="\u00e9quipe de football", ylab="", 
     main="Dendrogramme", sub="", axes=TRUE, cex=0.5)

#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require(ggdendro)){install.packages("ggdendro")}
library(ggdendro)
ggdendrogram(cah.ward, rotate = FALSE, size = 2)

#page 205
library(JLutils)
best.cutree(cah.ward, min = 3, graph = TRUE, xlab = 
              "Nombre de classes", ylab = "Inertie relative")

## [1] 3
#page 206
(groupes.cah <- cutree(cah.ward,k=5))
##            AC Ajaccio            AJ Auxerre                Angers 
##                     1                     2                     3 
##             AS Monaco     AS Nancy Lorraine                Bastia 
##                     4                     2                     4 
##              Bordeaux                 Brest                  Caen 
##                     4                     2                     5 
##             Dijon FCO Evian Thonon Gaillard           GFC Ajaccio 
##                     1                     4                     3 
##              Guingamp                  Lens                 Lille 
##                     2                     5                     2 
##               Lorient                  Lyon             Marseille 
##                     3                     2                     2 
##                  Metz           Montpellier                Nantes 
##                     4                     2                     3 
##                  Nice   Paris Saint-Germain               Sochaux 
##                     4                     5                     4 
##            St Etienne        Stade de Reims          Stade Rennes 
##                     5                     5                     3 
##              Toulouse                Troyes          Valenciennes 
##                     2                     5                     2
table(groupes.cah)
## groupes.cah
##  1  2  3  4  5 
##  2 10  5  7  6
plot(cah.ward, xlab="\u00e9quipe de football", ylab="", 
     main="Dendrogramme", sub="", axes=TRUE, cex=0.5)
rect.hclust(cah.ward, 5)

#page 207
library(factoextra)
hc.cut <- hcut(d.data_event, k = 5, hc_method = "complete")
fviz_dend(hc.cut, show_labels = TRUE, rect = TRUE)

fviz_cluster(hc.cut, ellipse.type = "convex",data=d.data_event)

#page 209
#Si le package n'est pas installe, enlever le commentaire
#puis executer la commande ci-dessous.
#if(!require("vegan")){install.packages("vegan")}
library(vegan)
## Loading required package: permute
## 
## Attaching package: 'permute'
## The following object is masked from 'package:devtools':
## 
##     check
## Registered S3 method overwritten by 'vegan':
##   method     from      
##   rev.hclust dendextend
## This is vegan 2.5-6
k.event.cal <- cascadeKM(data_event.c, 3, 10, iter = 100, 
                         criterion = "calinski")
plot(k.event.cal)

#page 210
set.seed(1133)
groupes.kmeans <- kmeans(data_event.c,centers=5,nstart=1000)
print(groupes.kmeans)
## K-means clustering with 5 clusters of sizes 3, 2, 8, 6, 11
## 
## Cluster means:
##   sort_order     time     fthg    odd_h
## 1   48.68203 49.89800 1.192835 2.596994
## 2   55.61308 49.33683 1.543583 2.700434
## 3   51.56216 49.74589 1.467143 2.669775
## 4   50.16731 49.53553 1.457661 2.881200
## 5   53.44651 49.67571 1.462649 2.570104
## 
## Clustering vector:
##            AC Ajaccio            AJ Auxerre                Angers 
##                     2                     5                     1 
##             AS Monaco     AS Nancy Lorraine                Bastia 
##                     3                     5                     3 
##              Bordeaux                 Brest                  Caen 
##                     3                     5                     4 
##             Dijon FCO Evian Thonon Gaillard           GFC Ajaccio 
##                     2                     3                     4 
##              Guingamp                  Lens                 Lille 
##                     5                     4                     5 
##               Lorient                  Lyon             Marseille 
##                     4                     5                     5 
##                  Metz           Montpellier                Nantes 
##                     3                     5                     1 
##                  Nice   Paris Saint-Germain               Sochaux 
##                     3                     4                     5 
##            St Etienne        Stade de Reims          Stade Rennes 
##                     4                     3                     1 
##              Toulouse                Troyes          Valenciennes 
##                     5                     3                     5 
## 
## Within cluster sum of squares by cluster:
## [1] 1.1951004 0.3077104 2.7979322 3.8380955 5.2867042
##  (between_SS / total_SS =  88.7 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"    
## [5] "tot.withinss" "betweenss"    "size"         "iter"        
## [9] "ifault"
#page 211
print(table(groupes.cah,groupes.kmeans=groupes.kmeans$cluster))
##            groupes.kmeans
## groupes.cah  1  2  3  4  5
##           1  0  2  0  0  0
##           2  0  0  0  0 10
##           3  3  0  0  2  0
##           4  0  0  6  0  1
##           5  0  0  2  4  0
#page 212
library(FactoMineR)
res.pca <- PCA(data_event.c, ncp = 3, graph = FALSE)
get_eig(res.pca)
##       eigenvalue variance.percent cumulative.variance.percent
## Dim.1  1.4274181         35.68545                    35.68545
## Dim.2  1.1521241         28.80310                    64.48856
## Dim.3  0.9807879         24.51970                    89.00825
## Dim.4  0.4396699         10.99175                   100.00000
fviz_screeplot(res.pca)

res.hcpc <- HCPC(res.pca, graph = FALSE)
fviz_dend(res.hcpc, cex = 0.7, palette = "jco", rect = TRUE, 
          rect_fill = TRUE, rect_border = "jco", labels_track_height = 0.8)

#page 213
fviz_cluster(res.hcpc, repel = TRUE, show.clust.cent = TRUE, 
             palette = "jco", ggtheme = theme_minimal(), main = "Factor map")

plot(res.hcpc, choice = "3D.map")

#page 214
#Exercice 3.1
read.csv("https://tinyurl.com/y3rxbxoo")
##               NOM     PAYS ETOILE CONFORT CHAMBRE CUISINE SPORT PLAGE PRIX
## 1        Appolpon    Grèce      1       4      56       2     0     8  390
## 2         Caravel    Grèce      4       7     471       7     6     5  468
## 3       Christina    Grèce      2       7      93       3     0     5  427
## 4         Economy    Grèce      1       3      56       1     0     8  369
## 5      Eden Beach    Grèce      1       4     286       3     4     7  499
## 6  Hanikian Beach    Grèce      3       6     282       5    10    10  526
## 7    Marina Beach    Grèce      3       6     310       7     7    10  587
## 8           Xenia    Grèce      3       4     300       6    10     8  534
## 9           Agdal    Maroc      4       5     146       5     1     0  447
## 10      Almohades    Maroc      4       6     250       8     0     3  482
## 11          Atlas    Maroc      4       5     196       9     6     6  511
## 12 Atlas Arnadi I    Maroc      4       7     324      10     6     5  532
## 13          Chems    Maroc      4       5     138       3     2     0  450
## 14     Dunes d'or    Maroc      3       4     400      10    10    10  569
## 15   La Résidence    Maroc      0       5     366       7     4    10  419
## 16          M'Diq    Maroc      0       3     300       5     7    10  421
## 17       Omayades    Maroc      4       6     144       7     4     8  579
## 18         Sahara    Maroc      5       7     330      10     5     5  598
## 19      Sud Bahia    Maroc      4       5     260       5     2     6  495
## 20    Tour Hassan    Maroc      5       7     170      10     1    10  730
## 21       Alfa Mar Portugal      4       6     254       7    10     8  646
## 22     App Do Mar Portugal      4       8     140       7     6    10  652
## 23       De Lagos Portugal      4       6     273      10    10    10  802
## 24        Madeira Portugal      5       7     260      10     8    10  761
## 25         Reid's Portugal      5       7     169      10    10    10 1101
## 26   Abou Sofiane  Tunisie      3       4     225       5    10    10  434
## 27       Asdrubal  Tunisie      4       4     225       7     6    10  489
## 28       Colombes  Tunisie      3       5     250       9    10     8  436
## 29       Darnaour  Tunisie      2       3     550       6     9     8  399
## 30         Djerba  Tunisie      3       6     800      10    10    10  477
## 31         Mimosa  Tunisie      2       4     150       5     6     4  375
## 32         Omarit  Tunisie      3       5     425       7     7     8  486
## 33       Orangers  Tunisie      3       4     366       5     8     8  447
## 34       Penelope  Tunisie      0       5     200       5    10     7  473
## 35         Ulysse  Tunisie      4       4     130       8     7    10  495
## 36     Dauphin Or  Turquie      3       4     500       8    10    10  617
## 37     Hippocampe  Turquie      1       2      50       1     5    10  489
## 38  Kuriat Palace  Turquie      4       9     232      10    10    10  520
## 39         Sultan  Turquie      0       3     110       7     6     8  534
#Exercice 3.2
read.csv("https://tinyurl.com/yyoowvkl")
##               X Arlette.Laguiller Dominique.Voynet François.Bayrou
## 1 MIDI PYRENEES             18613            25267          341641
## 2        ALSACE             13821            20382          214339
## 3      BRETAGNE             25663            39026          451986
## 4   RHONE ALPES             38095            62997          689671
## 5          PACA             26407            38339          419162
## 6      NORD PDC             52668            31382          340694
## 7     AQUITAINE             22016            28285          417546
## 8    LA REUNION              4288             4608           47574
##   Frédéric.Nihous Gérard.Schivardi Jean.Marie.Le.Pen José.Bové
## 1           25280             8778            154767     33687
## 2            5142             2522            135732     13758
## 3           21207             5169            143926     28485
## 4           32615            10963            360297     51420
## 5           26534             9935            377830     36963
## 6           43598             6324            335856     24593
## 7           35300             7572            168665     27941
## 8            1061              695             17469      4847
##   Marie.George.Buffet Nicolas.Sarkozy Olivier.Besancenot
## 1               34076          458092              69177
## 2                6100          362384              33310
## 3               31860          557505              94205
## 4               57624         1120930             123711
## 5               61968         1010235              88331
## 6               74021          639293             127893
## 7               41791          532158              78230
## 8               10645           89800               9419
##   Philippe.de.Villiers Ségolène.Royal
## 1                30850         542039
## 2                22492         171283
## 3                41212         564090
## 4                75904         806682
## 5                54851         579036
## 6                40682         573021
## 7                34028         557300
## 8                 2095         165464
read.csv("https://tinyurl.com/yyolq665")
##             X Sarkozy  Bayrou   Royal Le.Pen Besanc. Villiers Voynet
## 1      Alsace  362391  214259  171282 135730   33310    22492  20382
## 2   Aquitaine  532127  417546  557300 168664   78230    34028  28285
## 3    Auvergne  238152  169395  225477  78704   41522    18730  12090
## 4   Bourgogne  297544  175213  241094 119041   42246    24971  13690
## 5    Bretagne  557507  451988  564100 143926   94205    41212  39026
## 6      Centre  460425  278175  345352 168912   65347    45720  22655
## 7    Cham-Ard  246680  122642  160280 114527   33424    20455  10727
## 8       Corse   56819   18979   33493  23432    5941     1908   2119
## 9    Fr-Comte  212358  114148  165243  94212   30672    16361  12879
## 10  Ile de Fr 1931429 1143081 1593033 430553  181247    89498  89885
## 11  Lang-Rous  470017  234739  395509 214468   62597    28166  20787
## 12   Limousin  123870   82445  142237  38525   24040    10789   6144
## 13   Lorraine  403919  250195  315596 196696   70940    29183  21562
## 14   Midi-Pyr  458093  341651  542038 154777   69177    30850  25267
## 15   Nord-PdC  639390  340679  573071 335855  127881    40702  31388
## 16  Basse-Nor  280914  184256  209308  88569   43997    25722  14389
## 17  Haute-Nor  309924  184615  257664 126795   58312    25692  15707
## 18 Pays-Loire  636934  457560  552280 158844   93685   107895  38952
## 19   Picardie  331053  161236  251862 168699   57769    26731  14219
## 20  Poit.-Cha  304493  194126  322212  88138   45638    38735  16333
## 21       PACA 1010234  419161  579036 377831   88331    54851  38339
## 22  Rhone-Alp 1121615  689984  807220 360646  123776    75959  63032
## 23   Outremer  337711  103933  398110  36714   22159     5139  12383
##    Laguiller  Bove Buffet Nihous Schivardi
## 1      13821 13758   6100   5142      2522
## 2      22046 27941  41791  35300      7572
## 3      12936 13532  21920  12474      4207
## 4      14440 12296  18154  12079      3608
## 5      25662 28484  31860  21207      5169
## 6      22279 17395  30003  20567      5696
## 7      13560  7414  12465   9016      2270
## 8       1346  1659   5163   2260       450
## 9      10880 10365   9204   7814      2446
## 10     52965 57453 110967  19890     12386
## 11     17175 27412  38590  21356     11436
## 12      6566  6629  15695   8029      2284
## 13     25574 16094  19229  10378      4211
## 14     18623 33687  34076  25280      8778
## 15     52695 24591  74027  43595      6348
## 16     14652 13180  11211  21449      3090
## 17     20048 12563  26376  13717      3604
## 18     28481 25811  26737  26674      6481
## 19     23982 11619  22334  20439      4189
## 20     13949 13934  15901  21571      3882
## 21     26467 36963  61968  26538      9935
## 22     38114 51441  57654  32624     10969
## 23     10234 14904  14062   2698      1772
#page 215
#Exercice 3.4
read.csv("https://tinyurl.com/y5gffvsb")
##                            Zone_Name        Continent               Area
## 1                            Durotar         Kalimdor   Central Kalimdor
## 2                        The Barrens         Kalimdor   Central Kalimdor
## 3                  Silverpine Forest Eastern Kingdoms          Lordaeron
## 4               Stonetalon Mountains         Kalimdor   Central Kalimdor
## 5                      Thunder Bluff         Kalimdor   Central Kalimdor
## 6                   Dustwallow Marsh         Kalimdor   Central Kalimdor
## 7                          Orgrimmar         Kalimdor   Central Kalimdor
## 8                          Undercity Eastern Kingdoms          Lordaeron
## 9                          Ashenvale         Kalimdor  Northern Kalimdor
## 10                Stranglethorn Vale Eastern Kingdoms            Azeroth
## 11                   Wailing Caverns         Kalimdor   Central Kalimdor
## 12                           Tanaris         Kalimdor  Southern Kalimdor
## 13                          Maraudon         Kalimdor   Central Kalimdor
## 14                   The Hinterlands Eastern Kingdoms          Lordaeron
## 15                    Un'Goro Crater         Kalimdor  Southern Kalimdor
## 16                           Felwood         Kalimdor  Northern Kalimdor
## 17         The Temple of Atal'Hakkar Eastern Kingdoms            Azeroth
## 18                  Blackrock Depths Eastern Kingdoms         Khaz Modan
## 19                      Winterspring         Kalimdor  Northern Kalimdor
## 20               Eastern Plaguelands Eastern Kingdoms          Lordaeron
## 21                           Azshara         Kalimdor  Northern Kalimdor
## 22                       Scholomance Eastern Kingdoms          Lordaeron
## 23                      Arathi Basin Eastern Kingdoms          Lordaeron
## 24                   Blackrock Spire Eastern Kingdoms         Khaz Modan
## 25                Blackrock Mountain Eastern Kingdoms         Khaz Modan
## 26                         Moonglade         Kalimdor  Northern Kalimdor
## 27                           Mulgore         Kalimdor   Central Kalimdor
## 28                  Thousand Needles         Kalimdor  Southern Kalimdor
## 29               Hillsbrad Foothills Eastern Kingdoms          Lordaeron
## 30                 Blackfathom Deeps         Kalimdor  Northern Kalimdor
## 31                          Desolace         Kalimdor   Central Kalimdor
## 32                         Darkshore         Kalimdor  Northern Kalimdor
## 33                 Alterac Mountains Eastern Kingdoms          Lordaeron
## 34                  Swamp of Sorrows Eastern Kingdoms            Azeroth
## 35                   Tirisfal Glades Eastern Kingdoms          Lordaeron
## 36                     Searing Gorge Eastern Kingdoms         Khaz Modan
## 37                   Burning Steppes Eastern Kingdoms         Khaz Modan
## 38                        Zul'Farrak         Kalimdor  Southern Kalimdor
## 39                           Feralas         Kalimdor  Southern Kalimdor
## 40                  Arathi Highlands Eastern Kingdoms          Lordaeron
## 41                     Blasted Lands Eastern Kingdoms            Azeroth
## 42               Western Plaguelands Eastern Kingdoms          Lordaeron
## 43                        Stratholme         Kalimdor          Lordaeron
## 44                 Scarlet Monastery Eastern Kingdoms          Lordaeron
## 45                   Shadowfang Keep Eastern Kingdoms          Lordaeron
## 46                          Badlands Eastern Kingdoms         Khaz Modan
## 47                          Wetlands Eastern Kingdoms         Khaz Modan
## 48                Redridge Mountains Eastern Kingdoms            Azeroth
## 49                   Hall of Legends         Kalimdor   Central Kalimdor
## 50                        Gnomeregan Eastern Kingdoms         Khaz Modan
## 51                    Razorfen Downs         Kalimdor  Southern Kalimdor
## 52                          Duskwood Eastern Kingdoms            Azeroth
## 53                        Loch Modan Eastern Kingdoms         Khaz Modan
## 54                          Silithus         Kalimdor  Southern Kalimdor
## 55                     Deadwind Pass Eastern Kingdoms            Azeroth
## 56                    Razorfen Kraul         Kalimdor   Central Kalimdor
## 57                    Ragefire Chasm         Kalimdor   Central Kalimdor
## 58                         Dire Maul         Kalimdor  Southern Kalimdor
## 59                           Uldaman Eastern Kingdoms         Khaz Modan
## 60                        Dun Morogh Eastern Kingdoms         Khaz Modan
## 61                     Elwynn Forest Eastern Kingdoms            Azeroth
## 62                         Zul'Gurub Eastern Kingdoms            Azeroth
## 63                        Teldrassil         Kalimdor  Northern Kalimdor
## 64                          Westfall Eastern Kingdoms            Azeroth
## 65                    Stormwind City Eastern Kingdoms            Azeroth
## 66                       Molten Core Eastern Kingdoms         Khaz Modan
## 67                     Onyxia's Lair         Kalimdor   Central Kalimdor
## 68                     Warsong Gulch         Kalimdor  Northern Kalimdor
## 69                    Blackwing Lair Eastern Kingdoms         Khaz Modan
## 70                         Deadmines Eastern Kingdoms            Azeroth
## 71                Gates of Ahn'Qiraj         Kalimdor  Southern Kalimdor
## 72                    Alterac Valley Eastern Kingdoms          Lordaeron
## 73                         Ironforge Eastern Kingdoms         Khaz Modan
## 74                             Hyjal         Kalimdor  Northern Kalimdor
## 75                      Deeprun Tram Eastern Kingdoms                   
## 76                Ruins of Ahn'Qiraj         Kalimdor  Southern Kalimdor
## 77                         Ahn'Qiraj         Kalimdor  Southern Kalimdor
## 78                         Naxxramas        Northrend          Northrend
## 79                         GM Island         Kalimdor  Northern Kalimdor
## 80                     The Great Sea    The Great Sea                   
## 81                     Nagrand Arena           Ouland            Outland
## 82                Blade's Edge Arena          Outland            Outland
## 83                       Quel'thalas Eastern Kingdoms        Quel'Thalas
## 84                Hellfire Peninsula          Outland            Outland
## 85                        Ghostlands Eastern Kingdoms        Quel'Thalas
## 86                   Silvermoon City Eastern Kingdoms        Quel'Thalas
## 87                       Zangarmarsh          Outland            Outland
## 88                   Terokkar Forest          Outland            Outland
## 89                    Shattrath City          Outland            Outland
## 90                 Hellfire Ramparts          Outland            Outland
## 91                    Eversong Woods Eastern Kingdoms        Quel'Thalas
## 92                           Nagrand          Outland            Outland
## 93            Blade's Edge Mountains          Outland            Outland
## 94                       Netherstorm          Outland            Outland
## 95                 The Blood Furnace          Outland            Outland
## 96                 Shadowmoon Valley          Outland            Outland
## 97          Coilfang: The Slave Pens          Outland            Outland
## 98                    The Steamvault          Outland            Outland
## 99                  Eye of the Storm          Outland            Outland
## 100                   Azuremyst Isle         Kalimdor  Northern Kalimdor
## 101                   Bloodmyst Isle         Kalimdor  Northern Kalimdor
## 102                       Mana-Tombs          Outland            Outland
## 103          Old Hillsbrad Foothills         Kalimdor  Southern Kalimdor
## 104                  Auchenai Crypts          Outland            Outland
## 105                    Sethekk Halls          Outland            Outland
## 106                  Twisting Nether            Other                   
## 107                 The Black Morass         Kalimdor  Southern Kalimdor
## 108                   The Veiled Sea         Kalimdor     The Veiled Sea
## 109              The Shattered Halls          Outland            Outland
## 110     Auchindoun: Shadow Labyrinth          Outland            Outland
## 111                     The Botanica          Outland            Outland
## 112                     The Arcatraz          Outland            Outland
## 113                     Gruul's Lair          Outland            Outland
## 114                         Karazhan Eastern Kingdoms            Azeroth
## 115               Magtheridon's Lair          Outland            Outland
## 116                       The Exodar         Kalimdor  Northern Kalimdor
## 117               The Forbidding Sea Eastern Kingdoms The Forbidding Sea
## 118               Ruins of Lordaeron Eastern Kingdoms          Lordaeron
## 119             Serpentshrine Cavern          Outland            Outland
## 120                     Tempest Keep          Outland            Outland
## 121                     The Underbog          Outland            Outland
## 122                     The Mechanar          Outland            Outland
## 123                     Black Temple          Outland            Outland
## 124               Magisters' Terrace Eastern Kingdoms        Quel'Thalas
## 125               Isle of Quel'Danas Eastern Kingdoms        Quel'Thalas
## 126                  Sunwell Plateau Eastern Kingdoms        Quel'Thalas
## 127                    The North Sea Eastern Kingdoms        Quel'Thalas
## 128                The Ring of Valor         Kalimdor   Central Kalimdor
## 129  Dalaran<U+7AF6><U+6280><U+5834>        Northrend          Northrend
## 130 Plaguelands: The Scarlet Enclave Eastern Kingdoms          Lordaeron
## 131                    Borean Tundra        Northrend          Northrend
## 132                     Dragonblight        Northrend          Northrend
## 133                    Howling Fjord        Northrend          Northrend
## 134                        The Nexus        Northrend          Northrend
## 135                    Grizzly Hills        Northrend          Northrend
## 136                   Sholazar Basin        Northrend          Northrend
## 137               Crystalsong Forest        Northrend          Northrend
## 138                   The Frozen Sea        Northrend          Northrend
## 139                     Utgarde Keep        Northrend          Northrend
## 140                      Azjol-Nerub        Northrend          Northrend
## 141                  The Storm Peaks        Northrend          Northrend
## 142                         Icecrown        Northrend          Northrend
## 143                          Dalaran        Northrend          Northrend
## 144       Ahn'kahet: The Old Kingdom        Northrend          Northrend
## 145                         Zul'Drak        Northrend          Northrend
## 146                  The Violet Hold        Northrend          Northrend
## 147                      Wintergrasp        Northrend          Northrend
## 148           Strand of the Ancients        Northrend          Northrend
## 149                 Drak'Tharon Keep        Northrend          Northrend
## 150                          Gundrak        Northrend          Northrend
## 151        The Culling of Stratholme         Kalimdor  Southern Kalimdor
## 152                       The Oculus        Northrend          Northrend
## 153               Halls of Lightning        Northrend          Northrend
## 154                   Halls of Stone        Northrend          Northrend
## 155                 Utgarde Pinnacle        Northrend          Northrend
## 156                Vault of Archavon        Northrend          Northrend
## 157              The Eye of Eternity        Northrend          Northrend
## 158             The Obsidian Sanctum        Northrend          Northrend
## 159 <U+9F8D><U+9AA8><U+8352><U+91CE>        Northrend          Northrend
## 160 <U+5317><U+65B9><U+6D77><U+5CB8> Eastern Kingdoms          Lordaeron
##                       Zone               Subzone         Type Size
## 1                  Durotar                               Zone   NA
## 2              The Barrens                               Zone   NA
## 3        Silverpine Forest                               Zone   NA
## 4     Stonetalon Mountains                               Zone   NA
## 5            Thunder Bluff                               City   NA
## 6         Dustwallow Marsh                               Zone   NA
## 7                  Durotar             Orgrimmar         City   NA
## 8          Tirisfal Glades             Undercity         City   NA
## 9                Ashenvale                               Zone   NA
## 10      Stranglethorn Vale                               Zone   NA
## 11             The Barrens       Lushwater Oasis      Dungeon    5
## 12                 Tanaris                               Zone   NA
## 13                Desolace              Maraudon      Dungeon    5
## 14         The Hinterlands                               Zone   NA
## 15          Un'Goro Crater                               Zone   NA
## 16                 Felwood                               Zone   NA
## 17        Swamp of Sorrows         Sunken Temple      Dungeon    5
## 18      Blackrock Mountain          The Masonary      Dungeon    5
## 19            Winterspring                               Zone   NA
## 20     Eastern Plaguelands                               Zone   NA
## 21                 Azshara                               Zone   NA
## 22     Western Plaguelands           Caer Darrow      Dungeon    5
## 23        Arathi Highlands                       Battleground   15
## 24      Blackrock Mountain Lower Blackrock Spire      Dungeon    5
## 25      Blackrock Mountain                               Zone   NA
## 26               Moonglade                               Zone   NA
## 27                 Mulgore                               Zone   NA
## 28        Thousand Needles                               Zone   NA
## 29     Hillsbrad Foothills                               Zone   NA
## 30               Ashenvale      The Zoram Strand      Dungeon    5
## 31                Desolace                               Zone   NA
## 32               Darkshore                               Zone   NA
## 33     Hillsbrad Foothills     Alterac Mountains         Zone   NA
## 34        Swamp of Sorrows                               Zone   NA
## 35         Tirisfal Glades                               Zone   NA
## 36           Searing Gorge                               Zone   NA
## 37         Burning Steppes                               Zone   NA
## 38                 Tanaris      Sandsorrow Watch      Dungeon    5
## 39                 Feralas                               Zone   NA
## 40        Arathi Highlands                               Zone   NA
## 41           Blasted Lands                               Zone   NA
## 42     Western Plaguelands                               Zone   NA
## 43     Eastern Plaguelands            Plaguewood      Dungeon    5
## 44         Tirisfal Glades    Whispering Gardens      Dungeon    5
## 45       Silverpine Forest                            Dungeon    5
## 46                Badlands                               Zone   NA
## 47                Wetlands                               Zone   NA
## 48      Redridge Mountains                               Zone   NA
## 49                 Durotar             Orgrimmar         City   NA
## 50              Dun Morogh                            Dungeon    5
## 51         Thosand Needles                            Dungeon    5
## 52                Duskwood                               Zone   NA
## 53              Loch Modan                               Zone   NA
## 54                Silithus                               Zone   NA
## 55           Deadwind Pass                               Zone   NA
## 56        Southern Barrens                            Dungeon    5
## 57               Orgrimmar       Cleft of Shadow      Dungeon    5
## 58                 Feralas       High Wilderness      Dungeon    5
## 59                Badlands   The Maker's Terrace      Dungeon    5
## 60              Dun Morogh                               Zone   NA
## 61           Elwynn Forest                               Zone   NA
## 62      Stranglethorn Vale                            Dungeon   20
## 63              Teldrassil                               Zone   NA
## 64                Westfall                               Zone   NA
## 65          Stormwind City                               Zone   NA
## 66      Blackrock Mountain                            Dungeon   40
## 67        Dustwallow Marsh                            Dungeon   40
## 68               Ashenvale                       Battleground   10
## 69      Blackrock Mountain                            Dungeon   40
## 70                Westfall             Moonbrook      Dungeon    5
## 71               Ahn'Qiraj                              Event   NA
## 72       Alterac Mountains   Hillsbrad Foothills Battleground   40
## 73               Ironforge                               Zone   NA
## 74             Mount Hyjal          Hyjal Summit      Dungeon   25
## 75                                                    Transit   NA
## 76                Silithus                            Dungeon   20
## 77                Silithus             Ahn'Qiraj         Zone   NA
## 78            Dragonblight                            Dungeon   40
## 79              Teldrassil             GM Island         Zone   NA
## 80                                                        Sea   NA
## 81                 Nagrand  Ring of Trials Arena        Arena   NA
## 82  Blade's Edge Mountains Circle of Blood Arena        Arena   NA
## 83                                                       Zone   NA
## 84      Hellfire Peninsula                               Zone   NA
## 85              Ghostlands                               Zone   NA
## 86         Silvermoon City                               Zone   NA
## 87             Zangarmarsh                               Zone   NA
## 88         Terokkar Forest                               Zone   NA
## 89          Shattrath City                               Zone   NA
## 90      Hellfire Peninsula                            Dungeon    5
## 91          Eversong Woods                               Zone   NA
## 92                 Nagrand                               Zone   NA
## 93  Blade's Edge Mountains                               Zone   NA
## 94             Netherstorm                               Zone   NA
## 95      Hellfire Peninsula                            Dungeon    5
## 96       Shadowmoon Valley                               Zone   NA
## 97             Zangarmarsh    Coilfang Reservoir      Dungeon    5
## 98             Zangarmarsh    Coilfang Reservoir      Dungeon    5
## 99             Netherstorm                       Battleground   15
## 100         Azuremyst Isle                               Zone   NA
## 101         Bloodmyst Isle                               Zone   NA
## 102        Terokkar Forest            Auchindoun      Dungeon    5
## 103                Tanaris       Caverns of Time      Dungeon    5
## 104        Terokkar Forest            Auchindoun      Dungeon    5
## 105        Terokkar Forest            Auchindoun      Dungeon    5
## 106                                                      Zone   NA
## 107                Tanaris       Caverns of Time      Dungeon    5
## 108                                                       Sea   NA
## 109     Hellfire Peninsula                            Dungeon    5
## 110        Terokkar Forest            Auchindoun      Dungeon    5
## 111            Netherstorm          Tempest Keep      Dungeon    5
## 112            Netherstorm          Tempest Keep      Dungeon    5
## 113 Blade's Edge Mountains                            Dungeon   25
## 114          Deadwind Pass                            Dungeon   10
## 115     Hellfire Peninsula                            Dungeon   25
## 116             The Exodar                               Zone   NA
## 117                                                       Sea   NA
## 118        Tirisfal Glades             Undercity        Arena   NA
## 119            Zangarmarsh    Coilfang Reservoir      Dungeon   25
## 120            Netherstorm               The eye      Dungeon   25
## 121            Zangarmarsh    Coilfang Reservoir      Dungeon    5
## 122            Netherstorm          Tempest Keep      Dungeon    5
## 123      Shadowmoon Valley                            Dungeon   25
## 124     Isle of Quel'Danas                            Dungeon    5
## 125     Isle of Quel'Danas                               Zone   NA
## 126     Isle of Quel'Danas                            Dungeon   25
## 127                                                       Sea   NA
## 128              Orgrimmar       Orgrimmar Arena        Arena   NA
## 129                Dalaran                              Arena   NA
## 130    Eastern Plaguelands                               Zone   NA
## 131          Borean Tundra                               Zone   NA
## 132           Dragonblight                               Zone   NA
## 133          Howling Fjord                               Zone   NA
## 134          Borean Tundra              Coldarra      Dungeon    5
## 135          Grizzly Hills                               Zone   NA
## 136         Sholazar Basin                               Zone   NA
## 137     Crystalsong Forest                               Zone   NA
## 138         The Frozen Sea                                Sea   NA
## 139           Utgarde Keep                            Dungeon    5
## 140           Dragonblight                            Dungeon    5
## 141        The Storm Peaks                               Zone   NA
## 142               Icecrown                               Zone   NA
## 143                Dalaran                               Zone   NA
## 144           Dragonblight         Pit of Narjun      Dungeon    5
## 145               Zul'Drak                               Zone   NA
## 146           Broken Isles               Dalaran      Dungeon    5
## 147            Wintergrasp                               Zone   NA
## 148           Dragonblight                       Battleground   15
## 149          Grizzly Hills                            Dungeon    5
## 150               Zul'Drak                            Dungeon    5
## 151                Tanaris       Caverns of Time      Dungeon    5
## 152          Borean Tundra              Coldarra      Dungeon    5
## 153            Storm Peaks                Ulduar      Dungeon    5
## 154            Storm Peaks                Ulduar      Dungeon    5
## 155           Utgarde Keep                            Dungeon    5
## 156            Wintergrasp  Wintergrasp Fortress      Dungeon   25
## 157          Borean Tundra              Coldarra      Dungeon   25
## 158           Dragonblight    Chamber of Aspects      Dungeon   25
## 159           Dragonblight          Dragonblight         Zone   NA
## 160        Tirisfal Glades           North Coast         Zone   NA
##     Controlled Min_req_level Min_rec_level Max_rec_level Min_bot_level
## 1        Horde             1             1            10             1
## 2    Contested             1            10            35            10
## 3        Horde             1            10            20            10
## 4    Contested             1            25            30            25
## 5        Horde             1             1           100             1
## 6    Contested             1            35            40            35
## 7        Horde             1             1           100             1
## 8        Horde             1             1           100             1
## 9    Contested             1            20            25            20
## 10   Contested             1            30            50            30
## 11       Horde            10            15            25            15
## 12   Contested             1            45            50            45
## 13   Contested            25            32            44            36
## 14   Contested             1            30            35            30
## 15   Contested             1            50            55            50
## 16   Contested             1            45            50            45
## 17   Contested            45            50            60            40
## 18   Contested            42            47            61            53
## 19   Contested             1            50            55            50
## 20   Contested             1            40            45            40
## 21       Horde             1            10            20            10
## 22   Contested            33            38            48            41
## 23         PvP            10            10           100            10
## 24   Contested            48            55            65            51
## 25   Contested             1            50            60            50
## 26   Contested             1            55            60            55
## 27       Horde             1             1            10             1
## 28   Contested             1            40            45            40
## 29       Horde             1            20            25            20
## 30   Contested            15            20            30            18
## 31   Contested             1            30            40            30
## 32    Alliance             1            10            20            10
## 33   Contested             1            30            40            30
## 34   Contested             1            51            55            51
## 35       Horde             1             1            10             1
## 36   Contested             1            47            51            47
## 37   Contested             1            49            52            49
## 38   Contested            39            44            54            30
## 39   Contested             1            35            40            35
## 40   Contested             1            25            30            25
## 41   Contested             1            54            60            54
## 42   Contested             1            35            40            35
## 43   Contested            37            42            56            48
## 44   Contested            23            28            38            30
## 45       Horde            11            16            26            13
## 46   Contested             1            44            48            44
## 47    Alliance             1            20            25            20
## 48    Alliance             1            15            20            15
## 49       Horde             1             1           100             1
## 50    Alliance            19            24            34            26
## 51   Contested            35            40            50            34
## 52    Alliance             1            20            25            20
## 53    Alliance             1            10            20            10
## 54   Contested             1            55            60            55
## 55   Contested             1            50            60            55
## 56       Horde            25            30            40            21
## 57       Horde            10            15            21             9
## 58   Contested            31            36            52            44
## 59   Contested            30            35            45            35
## 60    Alliance             1             1            10             1
## 61    Alliance             1             1            10             1
## 62   Contested            50            56            70            56
## 63    Alliance             1             1            10             1
## 64    Alliance             1            10            15            10
## 65    Alliance             1             1           100             1
## 66   Contested            50            60            70            60
## 67   Contested            60            60            70            60
## 68         PvP            10            10           100            10
## 69   Contested            50            60            70            60
## 70    Alliance            10            15            21             9
## 71   Contested             1             1            60             1
## 72         PvP            20            20           100            20
## 73    Alliance             1             1           100             1
## 74   Contested            68            70            80            70
## 75    Alliance             1             1           100             1
## 76   Contested            50            60            70            60
## 77   Contested             1             1            60             1
## 78   Contested            51            60            70            60
## 79   Contested             1             1           100             1
## 80   Contested             1             1           100             1
## 81         PvP             1            10            90            10
## 82         PvP             1            10            90            10
## 83   Contested             1             1           100             1
## 84   Contested             1            58            70            58
## 85       Horde             1            10            20            10
## 86       Horde             1             1            90             1
## 87   Contested             1            60            64            60
## 88   Contested             1            62            70            62
## 89   Sanctuary             1             1           100             1
## 90   Contested            57            57            67            57
## 91       Horde             1             1            10             1
## 92   Contested             1            64            70            64
## 93   Contested             1            65            70            65
## 94   Contested             1            67            70            66
## 95   Contested            58            59            68            58
## 96   Contested             1            NA            NA            67
## 97   Contested            59            60            69            61
## 98   Contested            65            67            75            69
## 99         PvP            15            15           100            15
## 100   Alliance             1             1            10             1
## 101   Alliance             1            10            20            10
## 102  Contested            61            62            71            61
## 103  Contested            63            64            73            65
## 104  Contested            62            63            72            64
## 105  Contested            63            65            73            65
## 106  Contested             1             0             0            NA
## 107  Contested            65            68            75            60
## 108  Contested             1             0             0            NA
## 109  Contested            65            67            75            67
## 110  Contested            65            67            75            69
## 111  Contested            67            67            75            68
## 112  Contested            65            68            75            68
## 113  Contested            70            70            80            70
## 114  Contested            68            70            80            70
## 115  Contested            70            70            80            70
## 116   Alliance             1             1           100             1
## 117  Contested             1             1           100             1
## 118        PvP             1            10            90            10
## 119  Contested            70            70            80            70
## 120  Contested            70            70            80            70
## 121  Contested            60            61            70            62
## 122  Contested            65            67            75            69
## 123  Contested            70            70            80            70
## 124  Contested            65            68            75            68
## 125  Contested             1            70             0            70
## 126  Contested            70            70            80            70
## 127  Contested             1             1           100             1
## 128        PvP             1            10            90            10
## 129        PvP             1            10            90            10
## 130  Contested             1            55            58            55
## 131  Contested             1            68            72            68
## 132  Contested             1            71            80            71
## 133  Contested             1            68            72            68
## 134  Contested            68            71            73            71
## 135  Contested             1            73            75            73
## 136  Contested             1            76            80            75
## 137  Contested             1            77            80            77
## 138  Contested             1             1           100             1
## 139  Contested            67            67            68            70
## 140  Contested            69            72            74            72
## 141  Contested             1            77            80            77
## 142  Contested             1            77            80            77
## 143  Sanctuary             1             1           100             1
## 144  Contested            70            73            75            73
## 145  Contested             1            74            76            74
## 146  Contested            72            75            77            75
## 147  Contested             1            77            80            77
## 148        PvP            65            65            89            65
## 149  Contested            71            74            76            74
## 150  Contested            73            76            78            76
## 151  Contested            75            80            80            80
## 152  Contested            75            80            80            80
## 153  Contested            75            80            80            80
## 154  Contested            74            77            79            77
## 155  Contested            75            80            80            80
## 156  Contested            80            80            80            80
## 157  Contested            80            80            80            80
## 158  Contested            80            80            80            80
## 159  Contested             1            72            75            72
## 160  Contested             1             1           100             1
##     Max_bot_level
## 1              10
## 2              35
## 3              20
## 4              30
## 5             100
## 6              40
## 7             100
## 8             100
## 9              25
## 10             50
## 11             20
## 12             50
## 13             39
## 14             35
## 15             55
## 16             50
## 17             55
## 18             56
## 19             55
## 20             45
## 21             20
## 22             43
## 23            100
## 24             62
## 25             60
## 26             60
## 27             10
## 28             45
## 29             25
## 30             25
## 31             40
## 32             20
## 33             40
## 34             55
## 35             10
## 36             51
## 37             52
## 38             49
## 39             40
## 40             30
## 41             60
## 42             40
## 43             51
## 44             33
## 45             21
## 46             48
## 47             25
## 48             20
## 49            100
## 50             29
## 51             45
## 52             25
## 53             20
## 54             60
## 55             56
## 56             35
## 57             17
## 58             47
## 59             40
## 60             10
## 61             10
## 62             70
## 63             10
## 64             15
## 65            100
## 66             70
## 67             70
## 68            100
## 69             70
## 70             20
## 71             60
## 72            100
## 73            100
## 74             80
## 75            100
## 76             70
## 77             60
## 78             70
## 79            100
## 80            100
## 81             90
## 82             90
## 83            100
## 84             63
## 85             20
## 86             90
## 87             64
## 88             65
## 89            100
## 90             72
## 91             10
## 92             67
## 93             68
## 94             70
## 95             72
## 96             70
## 97             72
## 98             70
## 99            100
## 100            10
## 101            20
## 102            72
## 103            72
## 104            72
## 105            72
## 106            NA
## 107            72
## 108            NA
## 109            71
## 110            70
## 111            72
## 112            72
## 113            80
## 114            80
## 115            80
## 116           100
## 117           100
## 118            90
## 119            80
## 120            80
## 121            72
## 122            72
## 123            80
## 124            72
## 125             0
## 126            80
## 127           100
## 128            90
## 129            90
## 130            58
## 131            72
## 132            75
## 133            72
## 134            73
## 135            75
## 136            78
## 137            80
## 138           100
## 139            72
## 140            74
## 141            80
## 142            80
## 143           100
## 144            75
## 145            76
## 146            77
## 147            80
## 148            89
## 149            76
## 150            78
## 151            80
## 152            80
## 153            80
## 154            79
## 155            80
## 156            80
## 157            80
## 158            80
## 159            75
## 160           100
#Exercice 3.5
read.csv("https://tinyurl.com/y3rxbxoo")
##               NOM     PAYS ETOILE CONFORT CHAMBRE CUISINE SPORT PLAGE PRIX
## 1        Appolpon    Grèce      1       4      56       2     0     8  390
## 2         Caravel    Grèce      4       7     471       7     6     5  468
## 3       Christina    Grèce      2       7      93       3     0     5  427
## 4         Economy    Grèce      1       3      56       1     0     8  369
## 5      Eden Beach    Grèce      1       4     286       3     4     7  499
## 6  Hanikian Beach    Grèce      3       6     282       5    10    10  526
## 7    Marina Beach    Grèce      3       6     310       7     7    10  587
## 8           Xenia    Grèce      3       4     300       6    10     8  534
## 9           Agdal    Maroc      4       5     146       5     1     0  447
## 10      Almohades    Maroc      4       6     250       8     0     3  482
## 11          Atlas    Maroc      4       5     196       9     6     6  511
## 12 Atlas Arnadi I    Maroc      4       7     324      10     6     5  532
## 13          Chems    Maroc      4       5     138       3     2     0  450
## 14     Dunes d'or    Maroc      3       4     400      10    10    10  569
## 15   La Résidence    Maroc      0       5     366       7     4    10  419
## 16          M'Diq    Maroc      0       3     300       5     7    10  421
## 17       Omayades    Maroc      4       6     144       7     4     8  579
## 18         Sahara    Maroc      5       7     330      10     5     5  598
## 19      Sud Bahia    Maroc      4       5     260       5     2     6  495
## 20    Tour Hassan    Maroc      5       7     170      10     1    10  730
## 21       Alfa Mar Portugal      4       6     254       7    10     8  646
## 22     App Do Mar Portugal      4       8     140       7     6    10  652
## 23       De Lagos Portugal      4       6     273      10    10    10  802
## 24        Madeira Portugal      5       7     260      10     8    10  761
## 25         Reid's Portugal      5       7     169      10    10    10 1101
## 26   Abou Sofiane  Tunisie      3       4     225       5    10    10  434
## 27       Asdrubal  Tunisie      4       4     225       7     6    10  489
## 28       Colombes  Tunisie      3       5     250       9    10     8  436
## 29       Darnaour  Tunisie      2       3     550       6     9     8  399
## 30         Djerba  Tunisie      3       6     800      10    10    10  477
## 31         Mimosa  Tunisie      2       4     150       5     6     4  375
## 32         Omarit  Tunisie      3       5     425       7     7     8  486
## 33       Orangers  Tunisie      3       4     366       5     8     8  447
## 34       Penelope  Tunisie      0       5     200       5    10     7  473
## 35         Ulysse  Tunisie      4       4     130       8     7    10  495
## 36     Dauphin Or  Turquie      3       4     500       8    10    10  617
## 37     Hippocampe  Turquie      1       2      50       1     5    10  489
## 38  Kuriat Palace  Turquie      4       9     232      10    10    10  520
## 39         Sultan  Turquie      0       3     110       7     6     8  534