class: center, middle, inverse, title-slide .title[ # Statistique descriptive ] .author[ ### Traitement des données ] .date[ ### INTECHMER - CT2 GEM/PVRM ] --- ## Premier problème : décrire les données On parle de **Statistique descriptive** ou **exploratoire** ### Objectifs - Résumer l'information contenue dans les données - Faire ressortir des éléments intéressants - Poser des hypothèses sur des phénomènes potentiellement existant dans les données ### Outils - Description numérique (moyenne, occurrences, corrélation...) - Description graphique (histogramme, diagramme en barres, nuage de points...) --- ## Données exemple - `tips` ``` r library(tidyverse) data = read_delim("tips.csv", delim = ",") ```
--- class: center, middle, inverse ## Variable quantitative --- ## Variable quantitative - Moyenne `\(\bar{x}\)` $$ \bar{x} = \frac{1}{n} \sum_{i=1}^n x_i $$ - Variance $$ \sigma^2(x) = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x})^2 $$ - Ecart-type `\(\sigma(x) = \sqrt{\sigma^2(x)}\)` --- ## Variable quantitative - Médiane `\(med(x)\)` : valeur permettant de séparer les observations ordonnées prises par `\(x\)` en 2 groupes de même taille $$ med(x) = m | P(x \le m) = .5 $$ - si `\(n\)` est impair : `\(med(x) = x_{(n + 1) / 2}\)` - si `\(n\)` est pair : `\(med(x) = \frac{x_{n/2} + x_{n/2 + 1}}{2}\)` - Quantile `\(q_p(x)\)` : valeur pour laquelle une proportion `\(p\)` d'observations sont inférieures $$ q_p(x) = q | P(x \le q) = p $$ - Quartiles `\(Q1\)` et `\(Q3\)` : respectivement 25% et 75% (utilisés dans les boîtes à moustaches) - Quantiles usuels : `\(.01\)` (1%), `\(.1\)` (10%), `\(.9\)` (90%) et `\(.99\)` (99%) --- ## Variable quantitative Exemple : montant payé par table ### Représentation numérique <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Statistique </th> <th style="text-align:right;"> Valeur Stat </th> <th style="text-align:left;"> Quantile </th> <th style="text-align:right;"> Valeur Quantile </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Moyenne </td> <td style="text-align:right;"> 19.79 </td> <td style="text-align:left;"> 1 % </td> <td style="text-align:right;"> 7.25 </td> </tr> <tr> <td style="text-align:left;"> Ecart-Type </td> <td style="text-align:right;"> 8.90 </td> <td style="text-align:left;"> 5 % </td> <td style="text-align:right;"> 9.56 </td> </tr> <tr> <td style="text-align:left;"> Variance </td> <td style="text-align:right;"> 79.25 </td> <td style="text-align:left;"> 10 % </td> <td style="text-align:right;"> 10.34 </td> </tr> <tr> <td style="text-align:left;"> Médiane </td> <td style="text-align:right;"> 17.80 </td> <td style="text-align:left;"> 90 % </td> <td style="text-align:right;"> 32.24 </td> </tr> <tr> <td style="text-align:left;"> Minimum </td> <td style="text-align:right;"> 3.07 </td> <td style="text-align:left;"> 95 % </td> <td style="text-align:right;"> 38.06 </td> </tr> <tr> <td style="text-align:left;"> Maximum </td> <td style="text-align:right;"> 50.81 </td> <td style="text-align:left;"> 99 % </td> <td style="text-align:right;"> 48.23 </td> </tr> </tbody> </table> ### A regarder aussi : - Si divergence moyenne et médiane, valeurs extrêmes présentes - Déséquilibre de la répartition des valeurs - Présence de valeurs aberrantes (nommés **outliers**) --- ## Variable quantitative ### Représentation graphique Histogramme <img src="seance2--stat-desc_files/figure-html/qt-graph-hist-1.png" style="display: block; margin: auto;" /> --- ## Variable quantitative ### Représentation graphique Boîte à moustaches <img src="seance2--stat-desc_files/figure-html/qt-graph-box-1.png" style="display: block; margin: auto;" /> --- ## Variable quantitative - description numérique avec R ``` r mean(data$total_bill) # Moyenne ``` ``` ## [1] 19.78594 ``` ``` r var(data$total_bill) # Variance ``` ``` ## [1] 79.25294 ``` ``` r sd(data$total_bill) # Ecart-type ``` ``` ## [1] 8.902412 ``` ``` r min(data$total_bill) # Minimum ``` ``` ## [1] 3.07 ``` ``` r max(data$total_bill) # Maximum ``` ``` ## [1] 50.81 ``` --- ## Variable quantitative - description numérique avec R ``` r range(data$total_bill) # Min et Max en même temps ``` ``` ## [1] 3.07 50.81 ``` ``` r median(data$total_bill) # Médiane ``` ``` ## [1] 17.795 ``` ``` r summary(data$total_bill) # Plusieurs choses en même temps ``` ``` ## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 3.07 13.35 17.80 19.79 24.13 50.81 ``` ``` r quantile(data$total_bill, c(.25, .75)) # Q1 et Q3 ``` ``` ## 25% 75% ## 13.3475 24.1275 ``` ``` r quantile(data$total_bill, c(.01, .05, .10, .90, .95, .99)) # Quantiles usuels ``` ``` ## 1% 5% 10% 90% 95% 99% ## 7.2500 9.5575 10.3400 32.2350 38.0610 48.2270 ``` --- ## Variable quantitative - description numérique avec R `summary()` applicable à plusieurs variables en même temps (intéressant dans une phase d'analyse rapide) ``` r data %>% select(total_bill, tip, size) %>% summary() ``` ``` ## total_bill tip size ## Min. : 3.07 Min. : 1.000 Min. :1.00 ## 1st Qu.:13.35 1st Qu.: 2.000 1st Qu.:2.00 ## Median :17.80 Median : 2.900 Median :2.00 ## Mean :19.79 Mean : 2.998 Mean :2.57 ## 3rd Qu.:24.13 3rd Qu.: 3.562 3rd Qu.:3.00 ## Max. :50.81 Max. :10.000 Max. :6.00 ``` Moyenne calculable sur plusieurs variables en même temps ``` r data %>% select(total_bill, tip, size) %>% summarise_all(mean) ``` ``` ## # A tibble: 1 × 3 ## total_bill tip size ## <dbl> <dbl> <dbl> ## 1 19.8 3.00 2.57 ``` --- ## Variable quantitative -- histogramme avec R ``` r ggplot(data, aes(x = total_bill)) + geom_histogram() ``` <img src="seance2--stat-desc_files/figure-html/uni-qt-hist-base-1.png" style="display: block; margin: auto;" /> --- ## Variable quantitative -- histogramme avec R - Trop (ou pas assez) d'intervalles peut biaiser l'analyse - Modification avec le paramètre `bins` ``` r ggplot(data, aes(x = total_bill)) + geom_histogram(bins = 10) ``` <img src="seance2--stat-desc_files/figure-html/uni-qt-hist-bins-1.png" style="display: block; margin: auto;" /> --- ## Variable quantitative -- histogramme avec R - Densité estimée par noyau - Intéressant car permet de plus facilement voir la tendance globale ``` r ggplot(data, aes(x = total_bill)) + geom_density() ``` <img src="seance2--stat-desc_files/figure-html/uni-qt-density-1.png" style="display: block; margin: auto;" /> --- ## Variable quantitative -- boîte à moustaches avec R ``` r ggplot(data, aes(x = total_bill)) + geom_boxplot() ``` <img src="seance2--stat-desc_files/figure-html/uni-qt-boxplot-1.png" style="display: block; margin: auto;" /> --- class: middle, center, inverse ## Variable qualitative --- ## Variable qualitative ### Nominale - Modalités de la variable `\(x\)` : `\(m_j\)` (avec `\(j=1,...,p\)`) - Effectif (ou occurrences) d'une modalité `\(n_j\)` : nombre d'individus ayant la modalité `\(m_j\)` - Fréquence d'une modalité `\(f_j\)` $$ f_j = \frac{n_j}{n} $$ ### Ordinale - Effectif cumulé `\(n_j^{cum}\)` : nombre d'individus ayant une modalité entre `\(n_1\)` et `\(n_j\)` - Fréquence cumulée `$$n_j^{cum} = \sum_{k=1}^j n_k \mbox{ and } f_j^{cum} = \sum_{k=1}^j f_k$$` --- ## Variable qualitative Exemple : Jour de la semaine (*ordinale* de plus) ### Représentation numérique <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Modalités </th> <th style="text-align:right;"> Effectifs </th> <th style="text-align:right;"> Eff. cum. </th> <th style="text-align:right;"> Fréquences </th> <th style="text-align:right;"> Fréq. cum. </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Fri </td> <td style="text-align:right;"> 19 </td> <td style="text-align:right;"> 19 </td> <td style="text-align:right;"> 0.08 </td> <td style="text-align:right;"> 0.08 </td> </tr> <tr> <td style="text-align:left;"> Sat </td> <td style="text-align:right;"> 87 </td> <td style="text-align:right;"> 106 </td> <td style="text-align:right;"> 0.36 </td> <td style="text-align:right;"> 0.43 </td> </tr> <tr> <td style="text-align:left;"> Sun </td> <td style="text-align:right;"> 76 </td> <td style="text-align:right;"> 182 </td> <td style="text-align:right;"> 0.31 </td> <td style="text-align:right;"> 0.75 </td> </tr> <tr> <td style="text-align:left;"> Thur </td> <td style="text-align:right;"> 62 </td> <td style="text-align:right;"> 244 </td> <td style="text-align:right;"> 0.25 </td> <td style="text-align:right;"> 1.00 </td> </tr> </tbody> </table> ### A regarder aussi : - Différence entre les proportions - Si modalités peu fréquentes, regroupement de modalités à envisager --- ## Variable qualitative ### Représentation graphique Diagramme en barres <img src="seance2--stat-desc_files/figure-html/ql-graph-bar-1.png" style="display: block; margin: auto;" /> --- ## Variable qualitative - description numérique avec R ``` r data %>% group_by(day) %>% count() ``` ``` ## # A tibble: 4 × 2 ## # Groups: day [4] ## day n ## <chr> <int> ## 1 Fri 19 ## 2 Sat 87 ## 3 Sun 76 ## 4 Thur 62 ``` ``` r data %>% group_by(day) %>% count() %>% ungroup() %>% mutate(n_cum = cumsum(n), freq = n / sum(n), freq_cum = n_cum / sum(n)) ``` ``` ## # A tibble: 4 × 5 ## day n n_cum freq freq_cum ## <chr> <int> <int> <dbl> <dbl> ## 1 Fri 19 19 0.0779 0.0779 ## 2 Sat 87 106 0.357 0.434 ## 3 Sun 76 182 0.311 0.746 ## 4 Thur 62 244 0.254 1 ``` --- ## Variable qualitative - diagramme en barres avec R ``` r ggplot(data, aes(x = day)) + geom_bar() ``` <img src="seance2--stat-desc_files/figure-html/uni-ql-bar-1.png" style="display: block; margin: auto;" /> --- ## Variable qualitative - diagramme en barres avec R - Si beaucoup de modalités ou noms des modalités longs, intéressant en mode horizontale ``` r ggplot(data, aes(y = day)) + geom_bar() ``` <img src="seance2--stat-desc_files/figure-html/uni-ql-bar-horiz-1.png" style="display: block; margin: auto;" /> --- ## Variable qualitative - diagramme en barres avec R - Modification de l'ordre des modalités possible ``` r ggplot(data, aes(x = day)) + geom_bar() + scale_x_discrete(limits = c("Thur", "Fri", "Sat", "Sun")) ``` <img src="seance2--stat-desc_files/figure-html/uni-ql-bar-ordre-1.png" style="display: block; margin: auto;" /> --- ## Variable qualitative - diagramme en barres empilées avec R - Empilement intéressant (surtout quand on va comparer) - Noter la fonction `fct_relevel()` pour changer l'ordre d'affichage des modalités ``` r ggplot(data, aes(x = "", fill = fct_relevel(day, c("Thur", "Fri", "Sat", "Sun")))) + geom_bar() ``` <img src="seance2--stat-desc_files/figure-html/uni-ql-bar-stack-1.png" style="display: block; margin: auto;" /> --- ## Variable qualitative - diagramme circulaire avec R - **ATTENTION** : à ne pas utiliser avec variable ordinale et avec trop de modalités ``` r ggplot(data, aes(x = "", fill = sex)) + geom_bar(width = 1) + coord_polar(theta = "y") ``` <img src="seance2--stat-desc_files/figure-html/uni-ql-bar-pie-1.png" style="display: block; margin: auto;" /> --- class: middle, center, inverse ## Lien entre deux variables quantitatives --- ## Quantitative vs quantitative ### Covariance $$ cov(x,y) = \frac{1}{n} \sum_{i=1}^n (x_i - \bar{x}) (y_i - \bar{y}) $$ - Problème : non bornée et donc non exploitable ### Coefficient de corrélation linéaire (de *Pearson*) $$ \rho(x,y) = \frac{cov(x,y)}{\sigma^2(x) \sigma^2(y)} $$ - Covariance des variables normalisées - Valeurs comprises entre -1 et 1 - `\(0\)` : pas de lien linéaire (autre type de lien possible) - `\(1\)` : lien positif fort (si `\(x\)` augmente, `\(y\)` augmente) - `\(-1\)` : lien négatif fort (si `\(x\)` augmente, `\(y\)` diminue) --- ## Quantitative vs quantitative Exemple : Montant de la table et Pourboire ### Représentation numérique <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Statistique </th> <th style="text-align:right;"> Valeur </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Covariance </td> <td style="text-align:right;"> 8.32 </td> </tr> <tr> <td style="text-align:left;"> Corrélation </td> <td style="text-align:right;"> 0.68 </td> </tr> </tbody> </table> ### A regarder aussi : - Présence d'**outliers** avec un comportement atypique --- ## Quantitative vs quantitative ### Représentation graphique Nuage de points <img src="seance2--stat-desc_files/figure-html/qtqt-graph-1.png" style="display: block; margin: auto;" /> --- ## Anscombe La visualisation est aussi importante (voire plus) que la représentation numérique ! Entre ces quatre séries : - même moyenne et même variance pour `\(x\)` et `\(y\)` - même coefficient de corrélation entre les deux <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> 1 </th> <th style="text-align:right;"> 2 </th> <th style="text-align:right;"> 3 </th> <th style="text-align:right;"> 4 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Moyenne(x) </td> <td style="text-align:right;"> 9.00 </td> <td style="text-align:right;"> 9.00 </td> <td style="text-align:right;"> 9.00 </td> <td style="text-align:right;"> 9.00 </td> </tr> <tr> <td style="text-align:left;"> Moyenne(y) </td> <td style="text-align:right;"> 7.50 </td> <td style="text-align:right;"> 7.50 </td> <td style="text-align:right;"> 7.50 </td> <td style="text-align:right;"> 7.50 </td> </tr> <tr> <td style="text-align:left;"> Ecart-type(x) </td> <td style="text-align:right;"> 3.32 </td> <td style="text-align:right;"> 3.32 </td> <td style="text-align:right;"> 3.32 </td> <td style="text-align:right;"> 3.32 </td> </tr> <tr> <td style="text-align:left;"> Ecart-type(y) </td> <td style="text-align:right;"> 2.03 </td> <td style="text-align:right;"> 2.03 </td> <td style="text-align:right;"> 2.03 </td> <td style="text-align:right;"> 2.03 </td> </tr> <tr> <td style="text-align:left;"> Covariance </td> <td style="text-align:right;"> 5.50 </td> <td style="text-align:right;"> 5.50 </td> <td style="text-align:right;"> 5.50 </td> <td style="text-align:right;"> 5.50 </td> </tr> <tr> <td style="text-align:left;"> Corrélation </td> <td style="text-align:right;"> 0.82 </td> <td style="text-align:right;"> 0.82 </td> <td style="text-align:right;"> 0.82 </td> <td style="text-align:right;"> 0.82 </td> </tr> </tbody> </table> --- ## Anscombe <img src="seance2--stat-desc_files/figure-html/anscombe-graph-1.png" style="display: block; margin: auto;" /> --- ## Quantitative vs quantitative - description numérique avec R ``` r cov(data$total_bill, data$tip) ``` ``` ## [1] 8.323502 ``` ``` r cor(data$total_bill, data$tip) ``` ``` ## [1] 0.6757341 ``` ``` r cor(data %>% select(total_bill, tip, size)) ``` ``` ## total_bill tip size ## total_bill 1.0000000 0.6757341 0.5983151 ## tip 0.6757341 1.0000000 0.4892988 ## size 0.5983151 0.4892988 1.0000000 ``` --- ## Quantitative vs quantitative - nuage de points avec R ``` r ggplot(data, aes(x = total_bill, y = tip)) + geom_point() ``` <img src="seance2--stat-desc_files/figure-html/bi-qtqt-scatter-1.png" style="display: block; margin: auto;" /> --- class: middle, center, inverse ## Lien entre deux variables qualitatives --- ## Qualitative vs qualitative ### Table de contingence - Croisement des 2 ensembles de modalités, avec le nombre d'individus ayant chaque couple de modalités - `\(n_{ij}\)` : Nombre d'observations ayant la modalité `\(i\)` pour `\(x\)` et `\(j\)` pour `\(y\)` - `\(n_{i.}\)` : Effectif marginal (nombre d'observations ayant la modalité `\(i\)` pour `\(x\)`) - `\(n_{.j}\)` : Effectif marginal (nombre d'observations ayant la modalité `\(j\)` pour `\(y\)`) - `\(n_{..}\)` : Effectif total
--- ## Qualitative vs qualitative ### Profils lignes et colonnes - Distribution d'une variable conditionnellement aux modalités de l'autre ### Profil ligne - Pour une ligne `\(i\)` : `\(\frac{n_{ij}}{n_{i.}}\)` - Somme des valeurs en lignes = 100% ### Profil colonne - Pour une colonne `\(j\)` : `\(\frac{n_{ij}}{n_{.j}}\)` - Somme des valeurs en colonnes = 100% --- ## Qualitative vs qualitative Exemple : Jour de la semaine et Présence de fumeur ### Représentation numérique <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> day </th> <th style="text-align:right;"> No </th> <th style="text-align:right;"> Yes </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Fri </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 15 </td> </tr> <tr> <td style="text-align:left;"> Sat </td> <td style="text-align:right;"> 45 </td> <td style="text-align:right;"> 42 </td> </tr> <tr> <td style="text-align:left;"> Sun </td> <td style="text-align:right;"> 57 </td> <td style="text-align:right;"> 19 </td> </tr> <tr> <td style="text-align:left;"> Thur </td> <td style="text-align:right;"> 45 </td> <td style="text-align:right;"> 17 </td> </tr> </tbody> </table> ### A regarder aussi : - Couple de modalités très peu pris - Ici aussi, regroupement de modalités à envisager éventuellement --- ## Qualitative vs qualitative ### Représentation graphique <img src="seance2--stat-desc_files/figure-html/qtql-graph-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs qualitative ### Représentation numérique Profils colonnes ici (sommes en colonnes = 100%) <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> day </th> <th style="text-align:right;"> No </th> <th style="text-align:right;"> Yes </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Fri </td> <td style="text-align:right;"> 0.03 </td> <td style="text-align:right;"> 0.16 </td> </tr> <tr> <td style="text-align:left;"> Sat </td> <td style="text-align:right;"> 0.30 </td> <td style="text-align:right;"> 0.45 </td> </tr> <tr> <td style="text-align:left;"> Sun </td> <td style="text-align:right;"> 0.38 </td> <td style="text-align:right;"> 0.20 </td> </tr> <tr> <td style="text-align:left;"> Thur </td> <td style="text-align:right;"> 0.30 </td> <td style="text-align:right;"> 0.18 </td> </tr> </tbody> </table> --- ## Qualitative vs qualitative ### Représentation graphique Profils colonnes <img src="seance2--stat-desc_files/figure-html/qtql-graph-2-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs qualitative ### Représentation numérique Profils lignes ici (sommes en lignes = 100%) <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> day </th> <th style="text-align:right;"> No </th> <th style="text-align:right;"> Yes </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Fri </td> <td style="text-align:right;"> 0.21 </td> <td style="text-align:right;"> 0.79 </td> </tr> <tr> <td style="text-align:left;"> Sat </td> <td style="text-align:right;"> 0.52 </td> <td style="text-align:right;"> 0.48 </td> </tr> <tr> <td style="text-align:left;"> Sun </td> <td style="text-align:right;"> 0.75 </td> <td style="text-align:right;"> 0.25 </td> </tr> <tr> <td style="text-align:left;"> Thur </td> <td style="text-align:right;"> 0.73 </td> <td style="text-align:right;"> 0.27 </td> </tr> </tbody> </table> --- ## Qualitative vs qualitative ### Représentation graphique Profils lignes <img src="seance2--stat-desc_files/figure-html/qtql-graph-3-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs qualitative - description numérique avec R Table de contingence ``` r table(data$day, data$smoker) ``` ``` ## ## No Yes ## Fri 4 15 ## Sat 45 42 ## Sun 57 19 ## Thur 45 17 ``` Fréquence globale ``` r prop.table(table(data$day, data$smoker)) ``` ``` ## ## No Yes ## Fri 0.01639344 0.06147541 ## Sat 0.18442623 0.17213115 ## Sun 0.23360656 0.07786885 ## Thur 0.18442623 0.06967213 ``` --- ## Qualitative vs qualitative - description numérique avec R Profils lignes ``` r prop.table(table(data$day, data$smoker), margin = 1) ``` ``` ## ## No Yes ## Fri 0.2105263 0.7894737 ## Sat 0.5172414 0.4827586 ## Sun 0.7500000 0.2500000 ## Thur 0.7258065 0.2741935 ``` Profils colonnes ``` r prop.table(table(data$day, data$smoker), margin = 2) ``` ``` ## ## No Yes ## Fri 0.02649007 0.16129032 ## Sat 0.29801325 0.45161290 ## Sun 0.37748344 0.20430108 ## Thur 0.29801325 0.18279570 ``` --- ## Qualitative vs qualitative - diagramme en barres avec R Empilées par défaut, mais pas avec une somme à 100% ``` r ggplot(data, aes(x = day, fill = smoker)) + geom_bar() ``` <img src="seance2--stat-desc_files/figure-html/bi-qlql-bar-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs qualitative - diagramme en barres avec R On peut les séparer, mais attention si une des modalités est beaucoup trop présente (risque d'avoir un graphique illisible) ``` r ggplot(data, aes(x = day, fill = smoker)) + geom_bar(position = "dodge") ``` <img src="seance2--stat-desc_files/figure-html/bi-qlql-dodge-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs qualitative - diagramme en barres avec R - Empilées avec une somme à 100% (donc avec des vrais profils) - `x` et `fill` à inverser si on veut voir les autres profils ``` r ggplot(data, aes(x = day, fill = smoker)) + geom_bar(position = "fill") ``` <img src="seance2--stat-desc_files/figure-html/bi-qlql-profil-1.png" style="display: block; margin: auto;" /> --- class: middle, center, inverse ## Lien entre une variable quantitative et une variable qualitative --- ## Qualitative vs quantitative - Soit `\(Y\)` la variable qualitative à `\(m\)` modalités, et `\(X\)` la variable quantitative - Sous-populations déterminées par les modalités de `\(Y\)` - Indicateurs calculés pour chaque modalité k `$$\bar{x_j} = \frac{1}{n_j} \sum_{i | y_i = j} x_i$$` `$$\sigma^2(x_j) = \frac{1}{n_j} \sum {}_{i | y_i = j} (x_i - \bar{x_j})^2$$` --- ## Qualitative vs quantitative Exemple : Montant payé et Jour de la semaine ### Représentation numérique <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> day </th> <th style="text-align:right;"> Moyenne </th> <th style="text-align:right;"> Ecart-type </th> <th style="text-align:right;"> Médiane </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Fri </td> <td style="text-align:right;"> 17.15 </td> <td style="text-align:right;"> 8.30 </td> <td style="text-align:right;"> 15.38 </td> </tr> <tr> <td style="text-align:left;"> Sat </td> <td style="text-align:right;"> 20.44 </td> <td style="text-align:right;"> 9.48 </td> <td style="text-align:right;"> 18.24 </td> </tr> <tr> <td style="text-align:left;"> Sun </td> <td style="text-align:right;"> 21.41 </td> <td style="text-align:right;"> 8.83 </td> <td style="text-align:right;"> 19.63 </td> </tr> <tr> <td style="text-align:left;"> Thur </td> <td style="text-align:right;"> 17.68 </td> <td style="text-align:right;"> 7.89 </td> <td style="text-align:right;"> 16.20 </td> </tr> </tbody> </table> ### A regarder aussi : - Outliers --- ## Qualitative vs quantitative ### Représentation graphique Boîte à moustaches <img src="seance2--stat-desc_files/figure-html/qlqt-graph-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs quantitative - description numérique avec R ``` r data %>% group_by(day) %>% summarise(Moyenne = mean(total_bill), EcartType = sd(total_bill)) ``` ``` ## # A tibble: 4 × 3 ## day Moyenne EcartType ## <chr> <dbl> <dbl> ## 1 Fri 17.2 8.30 ## 2 Sat 20.4 9.48 ## 3 Sun 21.4 8.83 ## 4 Thur 17.7 7.89 ``` --- ## Qualitative vs quantitative - densité avec R ``` r ggplot(data, aes(x = total_bill, color = day)) + geom_density() ``` <img src="seance2--stat-desc_files/figure-html/bi-qlqt-densite-1.png" style="display: block; margin: auto;" /> --- ## Qualitative vs quantitative - boîtes à moustaches avec R Inversion de `x` et `y` possible ``` r ggplot(data, aes(x = total_bill, y = day)) + geom_boxplot() ``` <img src="seance2--stat-desc_files/figure-html/bi-qlqt-box-1.png" style="display: block; margin: auto;" /> --- class: middle, center, inverse ## Quelques compléments --- ## Personnalisation des graphiques possible dans `ggplot()` ``` r ggplot(data, aes(x = total_bill, y = fct_relevel(day, c("Thur", "Fri", "Sat", "Sun")), fill = fct_relevel(day, c("Thur", "Fri", "Sat", "Sun")))) + geom_boxplot() + scale_fill_manual(values = c("orchid", "orange", "steelblue", "limegreen")) + scale_y_discrete(limits = rev) + theme_minimal() + # d'autres thèmes existent labs(x = "Montant facture", y = "", fill = "Jour de la\nsemaine") ``` <img src="seance2--stat-desc_files/figure-html/ggplot-theme-1.png" style="display: block; margin: auto;" /> --- ## Transformation de variable ### Quantitative en qualitative - Courant de transformer une variable **quantitative** en variable **qualitative ordinale** - Ex : Catégorie d'âge, Nombre d'enfants du foyer, ... - Combien de modalités (*intervalles* ici) ? - Taille identique des intervalles ou variable (*amplitude*) ? - Seuils des intervalles ? - *Plus simple* Transformer en variable binaire : présence / absence - peut-être *trop* simple --- ## Transformation de variable ### Standardisation ou normalisation d'une variable quantitative - Obligatoire pour l'utilisation de certaines méthodes statistiques - 2 opérations sont réalisées : - Centrage : on retire la moyenne à chaque valeur - Réduction : on divise par la variance $$ x_{norm} = \frac{x - \bar{x}}{\sigma^2} $$ --- ## Transformation de variable ### Beaucoup d'autres envisageables - Division par le total ou par le maximum - Normalisation pour que la somme en ligne soit égale à 1 - Utiliser les rangs - Transformation de Hellinger $$ y\prime_i^j = \sqrt{\frac{y_i^j}{\sum_k y_i^k}} $$ - Calcul de `\(\log(x)\)` voire `\(\log(1+x)\)` (si valeur 0 présente par exemple) - Plusieurs autres possibles...