% Options for packages loaded elsewhere \PassOptionsToPackage{unicode}{hyperref} \PassOptionsToPackage{hyphens}{url} % \documentclass[ ]{article} \usepackage{amsmath,amssymb} \usepackage{iftex} \ifPDFTeX \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{textcomp} % provide euro and other symbols \else % if luatex or xetex \usepackage{unicode-math} % this also loads fontspec \defaultfontfeatures{Scale=MatchLowercase} \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1} \fi \usepackage{lmodern} \ifPDFTeX\else % xetex/luatex font selection \fi % Use upquote if available, for straight quotes in verbatim environments \IfFileExists{upquote.sty}{\usepackage{upquote}}{} \IfFileExists{microtype.sty}{% use microtype if available \usepackage[]{microtype} \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts }{} \makeatletter \@ifundefined{KOMAClassName}{% if non-KOMA class \IfFileExists{parskip.sty}{% \usepackage{parskip} }{% else \setlength{\parindent}{0pt} \setlength{\parskip}{6pt plus 2pt minus 1pt}} }{% if KOMA class \KOMAoptions{parskip=half}} \makeatother \usepackage{xcolor} \usepackage[margin=1in]{geometry} \usepackage{color} \usepackage{fancyvrb} \newcommand{\VerbBar}{|} \newcommand{\VERB}{\Verb[commandchars=\\\{\}]} \DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}} % Add ',fontsize=\small' for more characters per line \usepackage{framed} \definecolor{shadecolor}{RGB}{248,248,248} \newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}} \newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}} \newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}} \newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} \newcommand{\BuiltInTok}[1]{#1} \newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}} \newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}} \newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} \newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}} \newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} \newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}} \newcommand{\ExtensionTok}[1]{#1} \newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}} \newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} \newcommand{\ImportTok}[1]{#1} \newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}} \newcommand{\NormalTok}[1]{#1} \newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}} \newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}} \newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}} \newcommand{\RegionMarkerTok}[1]{#1} \newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}} \newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}} \newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}} \newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}} \usepackage{longtable,booktabs,array} \usepackage{calc} % for calculating minipage widths % Correct order of tables after \paragraph or \subparagraph \usepackage{etoolbox} \makeatletter \patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{} \makeatother % Allow footnotes in longtable head/foot \IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}} \makesavenoteenv{longtable} \usepackage{graphicx} \makeatletter \def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi} \def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi} \makeatother % Scale images if necessary, so that they will not overflow the page % margins by default, and it is still possible to overwrite the defaults % using explicit options in \includegraphics[width, height, ...]{} \setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio} % Set default figure placement to htbp \makeatletter \def\fps@figure{htbp} \makeatother \setlength{\emergencystretch}{3em} % prevent overfull lines \providecommand{\tightlist}{% \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} \setcounter{secnumdepth}{-\maxdimen} % remove section numbering \ifLuaTeX \usepackage{selnolig} % disable illegal ligatures \fi \usepackage{bookmark} \IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available \urlstyle{same} \hypersetup{ pdftitle={TP2 - Statistique Descriptive}, hidelinks, pdfcreator={LaTeX via pandoc}} \title{TP2 - Statistique Descriptive} \author{} \date{\vspace{-2.5em}3MIC - 2024-2025} \begin{document} \maketitle { \setcounter{tocdepth}{4} \tableofcontents } Ce TP a pour objectif de mener l'étude descriptive uni- et bi-dimensionnelle du jeu de données \texttt{wine} disponible sous Moodle. Vous rédigerez les réponses et vos observations dans le script .Rmd du TP au fur et à mesure. Vous pourrez compiler votre script à la fin du TP pour obtenir un compte-rendu du TP. \emph{Remarque : pensez à enlever les ``eval=F'' au fur et à mesure de l'avancement dans le TP pour avoir les résultats dans votre compte-rendu final de TP.} \section{Données}\label{donnuxe9es} On va étudier le jeu de données \textbf{wine} disponible sur la page moodle du cours. Commencez par récupérer ce jeu de données et sauvegardez le fichier dans votre dossier de travail. Le jeu de données \textbf{wine } comprend des mesures physico-chimiques réalisées sur un échantillon de \emph{\(600\) }vins (rouges et blancs) du Portugal. Ces mesures sont complétées par une évaluation sensorielle de la qualité par un ensemble d'experts. Chaque vin est décrit par les variables suivantes : \begin{itemize} \tightlist \item \emph{Qualite} : son évaluation sensorielle par les experts (``bad'',``medium'',``good''), \item \emph{Type} : son type (1 pour un vin rouge, 0 pour un vin blanc), \item \emph{AcidVol} : la teneur en acide volatile (en g/dm3 d'acide acétique), \item \emph{AcidCitr} : la teneur en acide citrique (en g/dm3), \item \emph{SO2lbr} : le dosage du dioxyde de soufre libre (en mg/dm3), \item \emph{SO2tot} : le dosage du dioxyde de soufre total (en mg/dm3), \item \emph{Densite} : la densité (en g/cm3), \item \emph{Alcool} : le degré d'alcool (en \% Vol.). \end{itemize} Dans un premier temps, commencez par charger le jeu de données à l'aide de la fonction \texttt{read.table()}. \begin{Shaded} \begin{Highlighting}[] \NormalTok{wine }\OtherTok{\textless{}{-}} \FunctionTok{read.table}\NormalTok{(}\StringTok{"wine.txt"}\NormalTok{,}\AttributeTok{header=}\ConstantTok{TRUE}\NormalTok{)} \end{Highlighting} \end{Shaded} Vous pouvez voir les premières lignes du jeu de données à l'aide de la fonction \texttt{head()} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{head}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} Qualite Type AcidVol AcidCitr SO2lbr SO2tot Densite Alcool 1352 medium 1 0.62 0.01 8 46 0.99332 11.8 5493 medium 0 0.34 0.10 17 63 0.99370 9.2 5153 medium 0 0.22 0.22 39 110 0.99855 9.0 5308 medium 0 0.35 0.46 61 183 0.99786 9.0 3866 medium 0 0.42 0.32 20 167 0.99479 10.6 694 medium 1 0.48 0.32 21 122 0.99840 9.4 \end{verbatim} \textbf{Question :} Contrôlez la dimension du jeu de données (c'est-à-dire le nombre d'individus et le nombre de variables) ? Vous pouvez vous aider des fonctions \texttt{dim()}, \texttt{nrow()}et \texttt{ncol()}. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{dim}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 600 8 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{nrow}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 600 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ncol}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 8 \end{verbatim} \textbf{Question :} Contrôlez que l'objet \texttt{wine} est bien de type data.frame (commande \texttt{is.data.frame()}) ? Remarquons que l'on peut obtenir les noms des variables grâce à la commande \texttt{names(wine)} ou \texttt{colnames(wine)}. Plus largement, on peut utiliser la commande \texttt{attributes()}. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{is.data.frame}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] TRUE \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{names}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] "Qualite" "Type" "AcidVol" "AcidCitr" "SO2lbr" "SO2tot" "Densite" [8] "Alcool" \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{colnames}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] "Qualite" "Type" "AcidVol" "AcidCitr" "SO2lbr" "SO2tot" "Densite" [8] "Alcool" \end{verbatim} \textbf{Question :} Quelle est la nature de chaque variable ? Vous pouvez vous aider de la commande \texttt{str()}. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{str}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} 'data.frame': 600 obs. of 8 variables: $ Qualite : chr "medium" "medium" "medium" "medium" ... $ Type : int 1 0 0 0 0 1 0 0 0 0 ... $ AcidVol : num 0.62 0.34 0.22 0.35 0.42 0.48 0.21 0.28 0.3 0.4 ... $ AcidCitr: num 0.01 0.1 0.22 0.46 0.32 0.32 0.32 0.14 0.25 0.42 ... $ SO2lbr : num 8 17 39 61 20 21 39 64 21 41 ... $ SO2tot : int 46 63 110 183 167 122 113 159 124 176 ... $ Densite : num 0.993 0.994 0.999 0.998 0.995 ... $ Alcool : num 11.8 9.2 9 9 10.6 9.4 10.2 10 10.8 9.4 ... \end{verbatim} Attention, il faut bien préciser à R les variables qui doivent être considérées comme qualitatives. On utilise donc la fonction \texttt{as.factor()} sur les variables \emph{Qualite} et \emph{Type}. On va aussi en profiter pour renommer les modalités de la variable \emph{Type} en \texttt{blanc}et \texttt{rouge}. \begin{Shaded} \begin{Highlighting}[] \NormalTok{wine}\SpecialCharTok{$}\NormalTok{Qualite}\OtherTok{\textless{}{-}}\FunctionTok{as.factor}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite)} \NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type}\OtherTok{\textless{}{-}}\FunctionTok{factor}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Type,}\AttributeTok{labels=}\FunctionTok{c}\NormalTok{(}\StringTok{"blanc"}\NormalTok{,}\StringTok{"rouge"}\NormalTok{))} \FunctionTok{head}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} Qualite Type AcidVol AcidCitr SO2lbr SO2tot Densite Alcool 1352 medium rouge 0.62 0.01 8 46 0.99332 11.8 5493 medium blanc 0.34 0.10 17 63 0.99370 9.2 5153 medium blanc 0.22 0.22 39 110 0.99855 9.0 5308 medium blanc 0.35 0.46 61 183 0.99786 9.0 3866 medium blanc 0.42 0.32 20 167 0.99479 10.6 694 medium rouge 0.48 0.32 21 122 0.99840 9.4 \end{verbatim} On peut obtenir un résumé rapide du jeu de données à l'aide de la fonction \texttt{summary()} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{summary}\NormalTok{(wine)} \end{Highlighting} \end{Shaded} \begin{verbatim} Qualite Type AcidVol AcidCitr SO2lbr bad : 19 blanc:425 Min. :0.1000 Min. :0.0000 Min. : 2.00 good :110 rouge:175 1st Qu.:0.2400 1st Qu.:0.2400 1st Qu.: 15.75 medium:471 Median :0.3000 Median :0.3000 Median : 27.00 Mean :0.3512 Mean :0.3141 Mean : 29.41 3rd Qu.:0.4300 3rd Qu.:0.3900 3rd Qu.: 41.00 Max. :1.0400 Max. :1.0000 Max. :112.00 SO2tot Densite Alcool Min. : 7.0 Min. :0.9875 Min. : 8.00 1st Qu.: 68.0 1st Qu.:0.9925 1st Qu.: 9.50 Median :114.5 Median :0.9949 Median :10.40 Mean :111.2 Mean :0.9947 Mean :10.49 3rd Qu.:154.0 3rd Qu.:0.9970 3rd Qu.:11.30 Max. :278.0 Max. :1.0030 Max. :14.00 \end{verbatim} \section{\texorpdfstring{Présentation de \texttt{ggplot2}}{Présentation de ggplot2}}\label{pruxe9sentation-de-ggplot2} Avant de commencer l'analyse uni- et bi-dimensionnelle du jeu de données, on va ici donner quelques notions sur les graphiques avec \texttt{ggplot2} pour comprendre la suite. \href{https://ggplot2.tidyverse.org/}{\textbf{ggplot2}} est une extension dédiée aux graphiques. Elle est basée sur une syntaxe particulière (il faut oublier les graphiques de base de R) mais robuste et efficace pour la construction de graphiques complexes. Elle part du principe (comme tout package de tidyverse) que les données relatives au graphique sont stockées dans un tableau (data.frame ou tibble). Un graphique en \texttt{ggplot2} s'initialise avec la fonction \texttt{ggplot()} qui prend en argument le jeu de données étudié. Par exemple, \begin{Shaded} \begin{Highlighting}[] \NormalTok{gEx }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine)} \end{Highlighting} \end{Shaded} Pour l'instant, on ne voit rien mais pourtant l'objet \texttt{gEx} contient des informations: \begin{Shaded} \begin{Highlighting}[] \FunctionTok{summary}\NormalTok{(gEx)} \end{Highlighting} \end{Shaded} \begin{verbatim} data: Qualite, Type, AcidVol, AcidCitr, SO2lbr, SO2tot, Densite, Alcool [600x8] faceting: compute_layout: function draw_back: function draw_front: function draw_labels: function draw_panels: function finish_data: function init_scales: function map_data: function params: list setup_data: function setup_params: function shrink: TRUE train_scales: function vars: function super: \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{names}\NormalTok{(gEx)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] "data" "layers" "scales" "guides" "mapping" [6] "theme" "coordinates" "facet" "plot_env" "layout" [11] "labels" \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \NormalTok{gEx}\SpecialCharTok{$}\NormalTok{layers} \end{Highlighting} \end{Shaded} \begin{verbatim} list() \end{verbatim} mais pour l'instant son \texttt{layers} est vide. On ajoute ensuite des éléments graphiques, appelés \texttt{geom}, à l'objet graphique grâce à l'opérateur \texttt{+} et on précise à l'aide de la fonction \texttt{aes()} les données à utiliser en argument du \texttt{geom}. Par exemple, si on veut tracer le nuage de points entre les variables \texttt{Densite} et \texttt{Alcool} : \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Densite,}\AttributeTok{y=}\NormalTok{Alcool))}\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{()} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-12-1.pdf} Parmi les \texttt{geom} les plus utilisés, on peut citer \begin{itemize} \tightlist \item \texttt{geom\_point()} (nuage de points) \item \texttt{geom\_line()}(trace des lignes) \item \texttt{geom\_bar()} (représentation en bâtons) \item \texttt{geom\_boxplot()} (boxplot) \item \texttt{geom\_violin()} (violin plot) \item \texttt{geom\_histogram()} (histogramme) \item \texttt{geom\_density()} (densité) \item \texttt{geom\_smooth()} \end{itemize} On peut ajouter un \textbf{mappage}, c'est à dire une mise en relation entre un attribut graphique d'un \texttt{geom} et une variable du tableau de données. Par exemple, on peut mettre des couleurs à un nuage de points en fonction d'une variable qualitative, \ldots{} Il faut bien distinguer un mappage d'une modification d'un attribut graphique sans lien avec une variable. Une règle importante à retenir est donc \begin{itemize} \tightlist \item pour définir un mappage (relation entre les valeurs d'une variable et un attribut graphique), on le déclare dans \texttt{aes()} \item pour modifier un attribut graphique de la même manière pour tous les points, on le définit en-dehors de la fonction \texttt{aes()}. \end{itemize} Par exemple, \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine)}\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Densite,}\AttributeTok{y=}\NormalTok{Alcool,}\AttributeTok{color=}\NormalTok{Type))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-13-1.pdf} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine)}\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Densite,}\AttributeTok{y=}\NormalTok{Alcool),}\AttributeTok{color=}\StringTok{"blue"}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-14-1.pdf} On peut représenter plusieurs \texttt{geom} sur un même graphique simultanément, il suffit d'utiliser l'opérateur \texttt{+} pour les ajouter au fur et à mesure. Par exemple, \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine)}\SpecialCharTok{+} \FunctionTok{geom\_violin}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Qualite,}\AttributeTok{y=}\NormalTok{Alcool))}\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Qualite,}\AttributeTok{y=}\NormalTok{Alcool), }\AttributeTok{col =} \StringTok{"blue"}\NormalTok{, }\AttributeTok{alpha =} \FloatTok{0.2}\NormalTok{,}\AttributeTok{position=}\StringTok{"jitter"}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-15-1.pdf} Les \emph{scales} permettent de modifier la façon dont un attribut graphique est lié aux valeurs d'une variable. Pour les modifier, on ajoute un nouvel élément de la forme \texttt{scale\_\textless{}attribut\textgreater{}\_\textless{}type\textgreater{}} à l'objet graphique ggplot2 avec \texttt{+}. Les fonctions \emph{scale} les plus connues sont : \begin{itemize} \tightlist \item \texttt{scale\_size()} (avec son argument \texttt{range}) pour modifier les tailles minimales, maximales, \ldots{} \item \texttt{scale\_x()} et \texttt{scale\_y()} pour modifier l'axe \texttt{x}et l'axe \texttt{y} respectivement \item \texttt{scale\_x\_continuous()} (resp. \texttt{scale\_x\_discrete()}) pour modifier l'axe des \texttt{x} dans le cas d'une variable quantitative (resp. une variable qualitative) \item \texttt{scale\_y\_continuous()} (resp. \texttt{scale\_y\_discrete()}) pour modifier l'axe des \texttt{y} dans le cas d'une variable quantitative (resp. une variable qualitative) \item \texttt{scale\_color()} pour modifier les couleurs de dessin \item \texttt{scale\_fill()} pour modifier les couleurs de remplissage \end{itemize} Pour les couleurs, on peut spécifier un gradient de couleur sur une variable quantitative avec \texttt{scale\_color\_gradient()}, modifier manuellement la palette de couleur avec \texttt{scale\_color\_manual()}, faire appel à une palette de couleur prédéfinie (par exemple en utilisant le package \href{\%22https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html\%22}{viridis} avec \texttt{scale\_color\_viridis}, en s'appuyant sur les palettes disponibles dans \href{https://colorbrewer2.org/\#type=sequential&scheme=BuGn&n=3}{ColorBrewer} avec \texttt{scale\_color\_brewer()}), \ldots{} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine)}\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Alcool,}\AttributeTok{y=}\NormalTok{Densite,}\AttributeTok{size=}\NormalTok{AcidVol,}\AttributeTok{color=}\NormalTok{Type))}\SpecialCharTok{+} \FunctionTok{scale\_size}\NormalTok{(}\StringTok{"Acide vol."}\NormalTok{, }\AttributeTok{range =} \FunctionTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\FloatTok{1.5}\NormalTok{),}\AttributeTok{breaks=}\FunctionTok{seq}\NormalTok{(}\DecValTok{0}\NormalTok{,}\FloatTok{1.5}\NormalTok{,}\FloatTok{0.2}\NormalTok{)) }\SpecialCharTok{+} \FunctionTok{scale\_x\_continuous}\NormalTok{(}\StringTok{"Alcool"}\NormalTok{,}\AttributeTok{limits=}\FunctionTok{c}\NormalTok{(}\DecValTok{8}\NormalTok{,}\DecValTok{16}\NormalTok{)) }\SpecialCharTok{+} \FunctionTok{scale\_y\_continuous}\NormalTok{(}\StringTok{"Densité"}\NormalTok{,}\AttributeTok{limits=}\FunctionTok{c}\NormalTok{(}\FloatTok{0.985}\NormalTok{,}\FloatTok{1.01}\NormalTok{))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-16-1.pdf} Remarque : on peut modifier certains éléments avec une commande spécifique comme \begin{itemize} \tightlist \item \texttt{ggtitle()} pour modifier le titre \item \texttt{xlab()}et \texttt{ylab()} pour modifier l'intituler de l'axe des abscisses et des ordonnées respectivement \end{itemize} En bref, les possibles avec \texttt{ggplot2} sont infinis car de nombreux éléments sont personnalisables. Voici quelques références pour les graphiques en \texttt{ggplot2} parmi les nombreuses ressources disponibles sur le web : \begin{itemize} \tightlist \item Le livre \emph{Ggplot2: Elegant Graphics for Data Analysis} de H. Wickham. {[}@ggplot2{]} \item Le \href{\%22https://ggplot2.tidyverse.org/\%22}{site web de ggplot2} \item \href{https://www.r-graph-gallery.com/ggplot2-package.html}{The R Graph Gallery} \item \href{https://thinkr.fr/pdf/ggplot2-french-cheatsheet.pdf}{The Cheat Sheet de ggplot 2} \end{itemize} \section{Etude statistique unidimensionnelle}\label{etude-statistique-unidimensionnelle} \subsection{Pour une variable qualitative}\label{pour-une-variable-qualitative} Nous nous intéressons dans cette partie aux deux variables qualitatives \emph{Type} et \emph{Qualite}. \textbf{Question :} A l'aide des commandes \texttt{table()}, \texttt{summary()} et \texttt{levels()}, donnez un résumé de la variable \texttt{Type}. Quels sont les modalités du facteur ? \begin{Shaded} \begin{Highlighting}[] \CommentTok{\# A COMPLETER} \FunctionTok{summary}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} blanc rouge 425 175 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{levels}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Type} \NormalTok{ )} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] "blanc" "rouge" \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} blanc rouge 425 175 \end{verbatim} \textbf{Question :} A l'aide du code suivant, résumez graphiquement la variable \texttt{Type} par une représentation par secteurs ou un diagramme en barres. Interprétez les résultats. \begin{Shaded} \begin{Highlighting}[] \NormalTok{g1}\OtherTok{\textless{}{-}}\FunctionTok{ggplot}\NormalTok{(wine, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Type))}\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{()}\SpecialCharTok{+} \FunctionTok{ylab}\NormalTok{(}\StringTok{""}\NormalTok{)}\SpecialCharTok{+}\FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Effectifs"}\NormalTok{)} \NormalTok{g2}\OtherTok{\textless{}{-}}\FunctionTok{ggplot}\NormalTok{(wine, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ Type)) }\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{y =}\NormalTok{ (..count..)}\SpecialCharTok{/}\FunctionTok{sum}\NormalTok{(..count..)))}\SpecialCharTok{+}\FunctionTok{ylab}\NormalTok{(}\StringTok{""}\NormalTok{)}\SpecialCharTok{+}\FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Frequences"}\NormalTok{)} \NormalTok{df }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}\AttributeTok{group =} \FunctionTok{levels}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Type),} \AttributeTok{value =} \FunctionTok{as.vector}\NormalTok{(}\FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Type))}\SpecialCharTok{/}\FunctionTok{nrow}\NormalTok{(wine))} \NormalTok{g3}\OtherTok{\textless{}{-}}\FunctionTok{ggplot}\NormalTok{(df, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\StringTok{""}\NormalTok{, }\AttributeTok{y=}\NormalTok{value, }\AttributeTok{fill=}\NormalTok{group))}\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{(}\AttributeTok{width =} \DecValTok{1}\NormalTok{, }\AttributeTok{stat =} \StringTok{"identity"}\NormalTok{)}\SpecialCharTok{+} \FunctionTok{coord\_polar}\NormalTok{(}\StringTok{"y"}\NormalTok{, }\AttributeTok{start=}\DecValTok{0}\NormalTok{)}\SpecialCharTok{+} \FunctionTok{theme}\NormalTok{(}\AttributeTok{legend.position=}\StringTok{"bottom"}\NormalTok{)} \FunctionTok{grid.arrange}\NormalTok{(g3,g1,g2,}\AttributeTok{ncol=}\DecValTok{3}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-18-1.pdf} \textbf{Question :} La variable \emph{Qualite} est une variable qualitative ordinale. On peut donc utiliser les effectifs cumulés et les fréquences cumulées. Pour tracer les fréquences cumulées, il faut que l'ordre des modalités soit bien compris par R. Ici, on commence par réorganiser les modalités en \texttt{bad}, \texttt{medium} et \texttt{good}. A l'aide des codes suivants, étudiez la variable \emph{Qualite}. \begin{Shaded} \begin{Highlighting}[] \NormalTok{wine}\SpecialCharTok{$}\NormalTok{Qualite }\OtherTok{\textless{}{-}} \FunctionTok{fct\_relevel}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite,}\StringTok{"bad"}\NormalTok{,}\StringTok{"medium"}\NormalTok{,}\StringTok{"good"}\NormalTok{)} \NormalTok{EffQual}\OtherTok{=}\FunctionTok{as.vector}\NormalTok{(}\FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite))} \NormalTok{FreqQual}\OtherTok{=} \FunctionTok{data.frame}\NormalTok{(}\AttributeTok{Eff =}\NormalTok{ EffQual, }\AttributeTok{Freq =}\NormalTok{ EffQual}\SpecialCharTok{/}\FunctionTok{length}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite), }\AttributeTok{FreqCumul=}\FunctionTok{cumsum}\NormalTok{(EffQual)}\SpecialCharTok{/}\FunctionTok{length}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite))} \FunctionTok{rownames}\NormalTok{(FreqQual)}\OtherTok{=}\FunctionTok{levels}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite)} \NormalTok{knitr}\SpecialCharTok{::}\FunctionTok{kable}\NormalTok{(FreqQual, }\AttributeTok{caption =} \StringTok{\textquotesingle{}Description de la variable Qualite\textquotesingle{}}\NormalTok{,}\AttributeTok{booktabs =} \ConstantTok{TRUE}\NormalTok{,}\AttributeTok{digits=}\DecValTok{3}\NormalTok{)} \end{Highlighting} \end{Shaded} \begin{longtable}[]{@{}lrrr@{}} \caption{Description de la variable Qualite}\tabularnewline \toprule\noalign{} & Eff & Freq & FreqCumul \\ \midrule\noalign{} \endfirsthead \toprule\noalign{} & Eff & Freq & FreqCumul \\ \midrule\noalign{} \endhead \bottomrule\noalign{} \endlastfoot bad & 19 & 0.032 & 0.032 \\ medium & 471 & 0.785 & 0.817 \\ good & 110 & 0.183 & 1.000 \\ \end{longtable} \begin{Shaded} \begin{Highlighting}[] \NormalTok{df }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(}\AttributeTok{Qualite =} \FunctionTok{levels}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite), }\AttributeTok{value =} \FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite),} \AttributeTok{valuecumul =} \DecValTok{100} \SpecialCharTok{*} \FunctionTok{cumsum}\NormalTok{(}\FunctionTok{prop.table}\NormalTok{(}\FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite))))} \NormalTok{df}\SpecialCharTok{$}\NormalTok{Qualite }\OtherTok{\textless{}{-}} \FunctionTok{fct\_relevel}\NormalTok{(df}\SpecialCharTok{$}\NormalTok{Qualite, }\StringTok{"bad"}\NormalTok{, }\StringTok{"medium"}\NormalTok{, }\StringTok{"good"}\NormalTok{)} \NormalTok{df }\OtherTok{\textless{}{-}} \FunctionTok{data.frame}\NormalTok{(df, }\AttributeTok{freq =}\NormalTok{ df}\SpecialCharTok{$}\NormalTok{value.Freq}\SpecialCharTok{/}\FunctionTok{nrow}\NormalTok{(wine))} \NormalTok{g1 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(wine) }\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ Qualite)) }\SpecialCharTok{+} \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Effectifs"}\NormalTok{)}\SpecialCharTok{+}\FunctionTok{xlab}\NormalTok{(}\StringTok{"Qualite"}\NormalTok{)} \NormalTok{g2 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(wine) }\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ Qualite, }\AttributeTok{y =}\NormalTok{ ..prop.., }\AttributeTok{group =} \DecValTok{1}\NormalTok{)) }\SpecialCharTok{+} \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Frequences"}\NormalTok{)}\SpecialCharTok{+}\FunctionTok{xlab}\NormalTok{(}\StringTok{"Qualite"}\NormalTok{)} \NormalTok{g3 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(df, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ Qualite, }\AttributeTok{y =}\NormalTok{ valuecumul)) }\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{(}\AttributeTok{stat =} \StringTok{"identity"}\NormalTok{) }\SpecialCharTok{+} \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Fréquences cumulées"}\NormalTok{)} \NormalTok{g4 }\OtherTok{\textless{}{-}} \FunctionTok{ggplot}\NormalTok{(df, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =} \StringTok{""}\NormalTok{, }\AttributeTok{y =}\NormalTok{ freq, }\AttributeTok{fill =}\NormalTok{ Qualite)) }\SpecialCharTok{+} \FunctionTok{geom\_bar}\NormalTok{(}\AttributeTok{width =} \DecValTok{1}\NormalTok{, }\AttributeTok{stat =} \StringTok{"identity"}\NormalTok{) }\SpecialCharTok{+} \FunctionTok{coord\_polar}\NormalTok{(}\StringTok{"y"}\NormalTok{, }\AttributeTok{start =} \DecValTok{0}\NormalTok{)} \FunctionTok{grid.arrange}\NormalTok{(g1, g2, g3, g4, }\AttributeTok{ncol =} \DecValTok{2}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-20-1.pdf} \subsection{Pour une variable quantitative}\label{pour-une-variable-quantitative} Pour cette section, on se focalise sur la variable \emph{Alcool}. \subsubsection{Indicateurs statistiques}\label{indicateurs-statistiques} \textbf{Question :} Que calculent les commandes \texttt{mean()}, \texttt{median()}, \texttt{var()}, \texttt{sd()}, \texttt{range()} ? Calculez l'étendue des données. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{mean}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 10.48592 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{median}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 10.4 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{var}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 1.316059 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{sd}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 1.147196 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{range}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 8 14 \end{verbatim} \textbf{Question :} Etudiez les sorties des commandes \texttt{summary(wine\$Alcool)} et \texttt{quantile(wine\$Alcool)}. Donnez également l'écart interquartile et les valeurs adjacentes. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{summary}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} Min. 1st Qu. Median Mean 3rd Qu. Max. 8.00 9.50 10.40 10.49 11.30 14.00 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{quantile}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool,}\FloatTok{0.75}\NormalTok{,}\AttributeTok{names=}\ConstantTok{FALSE}\NormalTok{)}\SpecialCharTok{{-}}\FunctionTok{quantile}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool,}\FloatTok{0.25}\NormalTok{,}\AttributeTok{names =} \ConstantTok{FALSE}\NormalTok{)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 1.8 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{quantile}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \begin{verbatim} 0% 25% 50% 75% 100% 8.0 9.5 10.4 11.3 14.0 \end{verbatim} \subsubsection{Représentations graphiques}\label{repruxe9sentations-graphiques} Le but de la statistique exploratoire est de synthétiser, résumer et structurer l'information contenue dans des données. On utilise pour cela des représentations de données sous forme de graphiques. \textbf{Question :} On peut utiliser la représentation par histogramme. Tapez \texttt{H\textless{}-hist(wine\$Alcool)} et commentez les différents attributs de \texttt{H}. \begin{Shaded} \begin{Highlighting}[] \NormalTok{H}\OtherTok{\textless{}{-}}\FunctionTok{hist}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-23-1.pdf} Pour tracer un histogramme avec ggplot, vous pouvez utiliser le code suivant. Commentez. \begin{Shaded} \begin{Highlighting}[] \NormalTok{g1}\OtherTok{\textless{}{-}}\FunctionTok{ggplot}\NormalTok{(wine,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Alcool))}\SpecialCharTok{+} \FunctionTok{geom\_histogram}\NormalTok{(}\AttributeTok{bins=}\DecValTok{15}\NormalTok{,}\AttributeTok{color=}\StringTok{"black"}\NormalTok{,}\AttributeTok{fill=}\StringTok{"white"}\NormalTok{)}\SpecialCharTok{+} \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Histo. des effectifs"}\NormalTok{)}\SpecialCharTok{+} \FunctionTok{ylab}\NormalTok{(}\StringTok{"Frequency"}\NormalTok{)}\SpecialCharTok{+}\FunctionTok{xlab}\NormalTok{(}\StringTok{"Alcool"}\NormalTok{)} \NormalTok{g2}\OtherTok{\textless{}{-}}\FunctionTok{ggplot}\NormalTok{(wine,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Alcool))}\SpecialCharTok{+} \FunctionTok{geom\_histogram}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{y=}\NormalTok{..density..),}\AttributeTok{bins=}\DecValTok{15}\NormalTok{,}\AttributeTok{color=}\StringTok{"black"}\NormalTok{, }\AttributeTok{fill=}\StringTok{"white"}\NormalTok{)}\SpecialCharTok{+} \FunctionTok{ggtitle}\NormalTok{(}\StringTok{"Histo. des frequences"}\NormalTok{)}\SpecialCharTok{+} \FunctionTok{ylab}\NormalTok{(}\StringTok{"Density"}\NormalTok{)}\SpecialCharTok{+}\FunctionTok{xlab}\NormalTok{(}\StringTok{"Alcool"}\NormalTok{)} \FunctionTok{grid.arrange}\NormalTok{(g1,g2,}\AttributeTok{ncol=}\DecValTok{2}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/hist-1.pdf} \textbf{Question :} A l'aide du code suivant, représentez un boxplot de la variable ``Alcool''. Que remarquez-vous ? Comparez avec les valeurs adjacentes calculées précédemment. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(wine,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{y=}\NormalTok{Alcool))}\SpecialCharTok{+}\FunctionTok{geom\_boxplot}\NormalTok{()} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-24-1.pdf} Pour tracer les boxplots de toutes les variables quantitatives en même temps, il faut réorganiser en amont le jeu de données. \begin{Shaded} \begin{Highlighting}[] \NormalTok{wineaux}\OtherTok{\textless{}{-}}\FunctionTok{melt}\NormalTok{(wine[,}\SpecialCharTok{{-}}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{)])} \FunctionTok{ggplot}\NormalTok{(wineaux,}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{variable,}\AttributeTok{y=}\NormalTok{value))}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{()} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/boxplot-1.pdf} \textbf{Question :} En vous aidant de l'aide de R et des résultats de la section précédente, tapez \texttt{B\textless{}-boxplot(wine\$Alcool)} et commentez les différents attributs de \texttt{B}. \begin{Shaded} \begin{Highlighting}[] \NormalTok{B}\OtherTok{\textless{}{-}}\FunctionTok{boxplot}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Alcool)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-25-1.pdf} \section{Statistiques descriptives bidimensionnelles}\label{statistiques-descriptives-bidimensionnelles} \subsection{Entre 2 variables quantitatives}\label{entre-2-variables-quantitatives} \textbf{Question :} Calculez la matrice de corrélation des variables quantitatives (commande \texttt{cor()}) ou la matrice de variance-covariance (commandes \texttt{var()} ou \texttt{cov()}). Représentez graphiquement les corrélations à l'aide de la fonction \texttt{corrplot()} de la librairie \texttt{corrplot}. Vous pourrez utiliser l'option \texttt{method="ellipse"} pour une meilleure lisibilité. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{print}\NormalTok{(}\StringTok{\textquotesingle{}Correlation\textquotesingle{}}\NormalTok{)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] "Correlation" \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{cor}\NormalTok{(wine[,}\SpecialCharTok{{-}}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{)])} \end{Highlighting} \end{Shaded} \begin{verbatim} AcidVol AcidCitr SO2lbr SO2tot Densite Alcool AcidVol 1.00000000 -0.4107821 -0.37071293 -0.41542773 0.29814510 -0.02208799 AcidCitr -0.41078214 1.0000000 0.14764139 0.20724080 0.12360061 -0.03949990 SO2lbr -0.37071293 0.1476414 1.00000000 0.71394673 -0.03516798 -0.14830238 SO2tot -0.41542773 0.2072408 0.71394673 1.00000000 -0.03003947 -0.24679350 Densite 0.29814510 0.1236006 -0.03516798 -0.03003947 1.00000000 -0.67624598 Alcool -0.02208799 -0.0394999 -0.14830238 -0.24679350 -0.67624598 1.00000000 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{print}\NormalTok{(}\StringTok{\textquotesingle{}Covariance\textquotesingle{}}\NormalTok{)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] "Covariance" \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{cov}\NormalTok{(wine[,}\SpecialCharTok{{-}}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{)])} \end{Highlighting} \end{Shaded} \begin{verbatim} AcidVol AcidCitr SO2lbr SO2tot Densite AcidVol 0.0268978296 -1.063582e-02 -1.077438119 -3.827641e+00 1.429567e-04 AcidCitr -0.0106358172 2.492306e-02 0.413052087 1.838032e+00 5.704787e-05 SO2lbr -1.0774381191 4.130521e-01 314.045063996 7.107860e+02 -1.822055e-03 SO2tot -3.8276406511 1.838032e+00 710.785976628 3.156124e+03 -4.933866e-03 Densite 0.0001429567 5.704787e-05 -0.001822055 -4.933866e-03 8.547444e-06 Alcool -0.0041557797 -7.153760e-03 -3.014961677 -1.590554e+01 -2.268091e-03 Alcool AcidVol -0.004155780 AcidCitr -0.007153760 SO2lbr -3.014961677 SO2tot -15.905542015 Densite -0.002268091 Alcool 1.316059100 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{help}\NormalTok{(corrplot)} \FunctionTok{corrplot}\NormalTok{(}\FunctionTok{cor}\NormalTok{(wine[,}\SpecialCharTok{{-}}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{)]),}\AttributeTok{method =} \StringTok{"ellipse"}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-26-1.pdf} \textbf{Question :} Interprétez les résultats. =\textgreater{} il y a une forte correlation negative entre l'Alcool et la densié \textbf{Question :} Représentez graphiquement la \emph{Densite} en fonction de l'\textbf{Alcool} à l'aide de la commande \texttt{geom\_point()}. Au vu de ce graphique, les variables sont-elles corrélées ? Vous pourrez ajouter la commande \texttt{+geom\_smooth(method="lm")} pour tracer la droite de régression linéaire. Est-ce cohérent avec la corrélation calculée ci-dessus ? \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(}\AttributeTok{data=}\NormalTok{wine)}\SpecialCharTok{+} \FunctionTok{geom\_point}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Alcool,}\AttributeTok{y=}\NormalTok{Densite))}\SpecialCharTok{+} \FunctionTok{geom\_smooth}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Alcool,}\AttributeTok{y=}\NormalTok{Densite),}\AttributeTok{method=}\StringTok{"lm"}\NormalTok{)} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-27-1.pdf} \subsection{Entre une variable quantitative et une variable qualitative}\label{entre-une-variable-quantitative-et-une-variable-qualitative} \textbf{Question :} Représentez le boxplot de la variable \texttt{Alcool} pour chaque modalité de la variable qualitative \emph{Type}. Même question avec \emph{Qualite}. Interprétez les résultats. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(wine)}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Type,}\AttributeTok{y=}\NormalTok{Alcool))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-28-1.pdf} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(wine)}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Qualite,}\AttributeTok{y=}\NormalTok{Alcool))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-28-2.pdf} \textbf{Question :} Explorez les autres combinaisons de variables quantitatives avec les variables qualitatives afin de déterminer les variables fortement liées. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(wine)}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Type,}\AttributeTok{y=}\NormalTok{AcidVol))}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Qualite,}\AttributeTok{y=}\NormalTok{AcidVol))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-29-1.pdf} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(wine)}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Type,}\AttributeTok{y=}\NormalTok{Densite))}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Qualite,}\AttributeTok{y=}\NormalTok{Densite))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-29-2.pdf} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{ggplot}\NormalTok{(wine)}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Type,}\AttributeTok{y=}\NormalTok{AcidCitr))}\SpecialCharTok{+} \FunctionTok{geom\_boxplot}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{x=}\NormalTok{Qualite,}\AttributeTok{y=}\NormalTok{AcidCitr))} \end{Highlighting} \end{Shaded} \includegraphics{TP2-StatDesc_files/figure-latex/unnamed-chunk-29-3.pdf} \textbf{Question :} A l'aide de la fonction \texttt{eta2()} de la librairie \texttt{BioStatR}, calculez le rapport de corrélation \(\eta^2\) entre chaque variable quantitative et la variable \emph{Type}. Commentez. Faites de même avec la variable \emph{Qualité}. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{eta2}\NormalTok{(}\AttributeTok{x=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{AcidVol,}\AttributeTok{y=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 0.4481639 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{eta2}\NormalTok{(}\AttributeTok{x=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{AcidCitr,}\AttributeTok{y=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 0.03693554 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{eta2}\NormalTok{(}\AttributeTok{x=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{SO2lbr,}\AttributeTok{y=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 0.2153399 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{eta2}\NormalTok{(}\AttributeTok{x=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{SO2tot,}\AttributeTok{y=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 0.4580193 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{eta2}\NormalTok{(}\AttributeTok{x=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Alcool,}\AttributeTok{y=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 0.003022674 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{eta2}\NormalTok{(}\AttributeTok{x=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Densite,}\AttributeTok{y=}\NormalTok{wine}\SpecialCharTok{$}\NormalTok{Type)} \end{Highlighting} \end{Shaded} \begin{verbatim} [1] 0.214444 \end{verbatim} \subsection{Entre deux variables qualitatives}\label{entre-deux-variables-qualitatives} \textbf{Question :} Analysez la table de contingence entre les deux variables qualitatives \emph{Type} et \emph{Qualite} avec la commande \texttt{table()}. Ajoutez les effectifs marginaux avec la commande \texttt{addmargins()}. \begin{Shaded} \begin{Highlighting}[] \NormalTok{a}\OtherTok{=}\FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite,wine}\SpecialCharTok{$}\NormalTok{Type)} \FunctionTok{addmargins}\NormalTok{(a)} \end{Highlighting} \end{Shaded} \begin{verbatim} blanc rouge Sum bad 17 2 19 medium 319 152 471 good 89 21 110 Sum 425 175 600 \end{verbatim} \textbf{Question :} Calculez et représentez les profils-lignes à l'aide des fonctions \texttt{prop.table(...,\ margin\ =\ ...)}et \texttt{mosaicplot}. Interprétez les résultats. Faites de même avec les profils-colonnes. \begin{Shaded} \begin{Highlighting}[] \FunctionTok{prop.table}\NormalTok{(}\FunctionTok{table}\NormalTok{(wine}\SpecialCharTok{$}\NormalTok{Qualite,wine}\SpecialCharTok{$}\NormalTok{Type), }\AttributeTok{margin =} \DecValTok{1}\NormalTok{)} \end{Highlighting} \end{Shaded} \begin{verbatim} blanc rouge bad 0.8947368 0.1052632 medium 0.6772824 0.3227176 good 0.8090909 0.1909091 \end{verbatim} \begin{Shaded} \begin{Highlighting}[] \FunctionTok{help}\NormalTok{(}\StringTok{"prop.table"}\NormalTok{)} \end{Highlighting} \end{Shaded} \end{document}