#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")