#Chapitre 3 : solution des exercices

#Complement en ligne
#Exercice 3.1
d_hotels <- read.csv("https://tinyurl.com/y3rxbxoo")
head(d_hotels)
##              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
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
ggparcoord(d_hotels, columns = 3:ncol(d_hotels), groupColumn=2, 
           scale = "std", boxplot = TRUE, alphaLines = 0.5)+ facet_grid(
             rows=vars(PAYS))+ theme(axis.text.x=element_text(angle=45,hjust=1,
                                                              vjust=1),legend.position="")

#Exercice 3.2
d_pres2002<-read.csv("https://tinyurl.com/yyoowvkl",row.names=1)
head(d_pres2002[,1:3])
##               Arlette.Laguiller Dominique.Voynet François.Bayrou
## MIDI PYRENEES             18613            25267          341641
## ALSACE                    13821            20382          214339
## BRETAGNE                  25663            39026          451986
## RHONE ALPES               38095            62997          689671
## PACA                      26407            38339          419162
## NORD PDC                  52668            31382          340694
mosaicplot(d_pres2002, type = "pearson", shade = TRUE, las = 2, 
           main = "Associations et r\u00e9sidus du test du chi2")

d_pres2007 <- read.csv("https://tinyurl.com/yyolq665",row.names=1)
head(d_pres2007[,1:8])
##           Sarkozy Bayrou  Royal Le.Pen Besanc. Villiers Voynet Laguiller
## Alsace     362391 214259 171282 135730   33310    22492  20382     13821
## Aquitaine  532127 417546 557300 168664   78230    34028  28285     22046
## Auvergne   238152 169395 225477  78704   41522    18730  12090     12936
## Bourgogne  297544 175213 241094 119041   42246    24971  13690     14440
## Bretagne   557507 451988 564100 143926   94205    41212  39026     25662
## Centre     460425 278175 345352 168912   65347    45720  22655     22279
mosaicplot(d_pres2007, type = "pearson", shade = TRUE, las = 2, 
           main = "Associations et r\u00e9sidus du test du chi2")

#q1
data(UCBAdmissions)
str(UCBAdmissions)
##  'table' num [1:2, 1:2, 1:6] 512 313 89 19 353 207 17 8 120 205 ...
##  - attr(*, "dimnames")=List of 3
##   ..$ Admit : chr [1:2] "Admitted" "Rejected"
##   ..$ Gender: chr [1:2] "Male" "Female"
##   ..$ Dept  : chr [1:6] "A" "B" "C" "D" ...
mosaicplot(UCBAdmissions)

library(vcd)
## Loading required package: grid
assoc(UCBAdmissions)

#q2
library(FactoMineR) 
try(MCA(UCBAdmissions))
## Error in dimnames(res) <- list(attributes(tab)$row.names, listModa) : 
##   la longueur de 'dimnames' [2] n'est pas égale à l'étendue du tableau
?MCA

#q3
library(DescTools)
UCBA.df <- Untable(UCBAdmissions)
head(UCBA.df)
##      Admit Gender Dept
## 1 Admitted   Male    A
## 2 Admitted   Male    A
## 3 Admitted   Male    A
## 4 Admitted   Male    A
## 5 Admitted   Male    A
## 6 Admitted   Male    A
str(UCBA.df)
## 'data.frame':    4526 obs. of  3 variables:
##  $ Admit : Factor w/ 2 levels "Admitted","Rejected": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Gender: Factor w/ 2 levels "Male","Female": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Dept  : Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...
##  - attr(*, "out.attrs")=List of 2
##   ..$ dim     : Named int  2 2 6
##   .. ..- attr(*, "names")= chr  "Admit" "Gender" "Dept"
##   ..$ dimnames:List of 3
##   .. ..$ Admit : chr  "Admit=Admitted" "Admit=Rejected"
##   .. ..$ Gender: chr  "Gender=Male" "Gender=Female"
##   .. ..$ Dept  : chr  "Dept=A" "Dept=B" "Dept=C" "Dept=D" ...
MCA(UCBA.df, graph=FALSE)
## **Results of the Multiple Correspondence Analysis (MCA)**
## The analysis was performed on 4526 individuals, described by 3 variables
## *The results are available in the following objects:
## 
##    name              description                       
## 1  "$eig"            "eigenvalues"                     
## 2  "$var"            "results for the variables"       
## 3  "$var$coord"      "coord. of the categories"        
## 4  "$var$cos2"       "cos2 for the categories"         
## 5  "$var$contrib"    "contributions of the categories" 
## 6  "$var$v.test"     "v-test for the categories"       
## 7  "$ind"            "results for the individuals"     
## 8  "$ind$coord"      "coord. for the individuals"      
## 9  "$ind$cos2"       "cos2 for the individuals"        
## 10 "$ind$contrib"    "contributions of the individuals"
## 11 "$call"           "intermediate results"            
## 12 "$call$marge.col" "weights of columns"              
## 13 "$call$marge.li"  "weights of rows"
#Exercice 3.4
d_wow <- read.csv("https://tinyurl.com/y5gffvsb", row.names =1)
head(d_wow[,1:3])
##                             Continent             Area
## Durotar                      Kalimdor Central Kalimdor
## The Barrens                  Kalimdor Central Kalimdor
## Silverpine Forest    Eastern Kingdoms        Lordaeron
## Stonetalon Mountains         Kalimdor Central Kalimdor
## Thunder Bluff                Kalimdor Central Kalimdor
## Dustwallow Marsh             Kalimdor Central Kalimdor
##                                      Zone
## Durotar                           Durotar
## The Barrens                   The Barrens
## Silverpine Forest       Silverpine Forest
## Stonetalon Mountains Stonetalon Mountains
## Thunder Bluff               Thunder Bluff
## Dustwallow Marsh         Dustwallow Marsh
d.d_wow <- dist(d_wow[,8:12])
wow.cah.ward <- hclust(d.d_wow, method="ward.D2")
library(ggdendro)
ggdendrogram(wow.cah.ward, labels = FALSE)

#Exercice 3.5
d_hotels <- read.csv("https://tinyurl.com/y3rxbxoo", row.names=1)
head(d_hotels)
##                 PAYS ETOILE CONFORT CHAMBRE CUISINE SPORT PLAGE PRIX
## Appolpon       Grèce      1       4      56       2     0     8  390
## Caravel        Grèce      4       7     471       7     6     5  468
## Christina      Grèce      2       7      93       3     0     5  427
## Economy        Grèce      1       3      56       1     0     8  369
## Eden Beach     Grèce      1       4     286       3     4     7  499
## Hanikian Beach Grèce      3       6     282       5    10    10  526
d.d_hotels <- dist(d_hotels[,2:7])
hotels.cah.ward <- hclust(d.d_hotels, method="ward.D2")
ggdendrogram(hotels.cah.ward)