|
- library(tidyverse)
- library(gapminder)
- library(lubridate)
-
- # Preliminaire ----
-
- # pipe : %>%
- # Permet de chaîner les instructions
- # x %>% f <=> f(x)
- # x %>% f(.) <=> f(x)
- # x %>% f(y = 10) <=> f(x, y = 10)
-
- # EDA ----
-
- ## Diamonds ----
-
- # Charger les données
- data(diamonds)
-
- # Affichage des premières lignes
- diamonds
-
- # Structure des données
- diamonds %>%
- str
-
- # Résumé stat des données
- diamonds %>%
- summary
-
- # Écart type pour les variables numériques
- diamonds %>%
- keep(is.numeric) %>%
- map(sd)
-
- # Distribution des valeurs de carats (continu)
- diamonds %>%
- ggplot() +
- aes(x = carat) +
- geom_histogram(binwidth = .05)
-
- # Distribution des valeurs de coupe (catégoriel)
- diamonds %>%
- ggplot() +
- aes(x = cut) +
- geom_bar()
-
- # …
-
- # Fonction pour afficher une distribution
- plot_desc <- function(x, var, ...)
- {
- x %>%
- ggplot() +
- aes_string(x = var) -> g
-
- if (x[[var]] %>% is.numeric)
- g + geom_histogram(...)
- else
- g + geom_bar(...)
- }
-
- # Application
- diamonds %>%
- plot_desc("carat", binwidth = .01)
-
- # Application en masse
- diamonds %>%
- keep(is.numeric) %>%
- names %>%
- map(~plot_desc(diamonds, ., binwidth = .5)) %>%
- set_names(diamonds %>% keep(is.numeric) %>% names) -> plots
-
- diamonds %>%
- keep(is.factor) %>%
- names %>%
- map(~plot_desc(diamonds, .)) %>%
- set_names(diamonds %>% keep(is.factor) %>% names) -> plots
-
- # Matrice de relations entre les variables
- diamonds %>%
- sample_n(1000) %>%
- plot
-
- # Association entre deux variables (continue/catégorielle)
- diamonds %>%
- ggplot() +
- aes(x = carat, fill = clarity) +
- geom_histogram(position = "fill")
-
- # Association entre deux variables continues
- diamonds %>%
- ggplot() +
- aes(x = x, y = y) +
- geom_point()
-
- # Association entre 3 variables
- diamonds %>%
- ggplot() +
- aes(x = carat, y = price, color = color) +
- geom_point()
-
- ## Exercice gapminder ----
-
- data(gapminder)
-
- # Feature engineering ----
-
- read_csv2("commits.csv")
-
- # Commits par jour ? En fonction de l'heure ? Par jour et par heure ? boulot/perso ? en semaine/week-end ?
|