You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
2.0KB

  1. library(tidyverse)
  2. library(gapminder)
  3. library(lubridate)
  4. # Preliminaire ----
  5. # pipe : %>%
  6. # Permet de chaîner les instructions
  7. # x %>% f <=> f(x)
  8. # x %>% f(.) <=> f(x)
  9. # x %>% f(y = 10) <=> f(x, y = 10)
  10. # EDA ----
  11. ## Diamonds ----
  12. # Charger les données
  13. data(diamonds)
  14. # Affichage des premières lignes
  15. diamonds
  16. # Structure des données
  17. diamonds %>%
  18. str
  19. # Résumé stat des données
  20. diamonds %>%
  21. summary
  22. # Écart type pour les variables numériques
  23. diamonds %>%
  24. keep(is.numeric) %>%
  25. map(sd)
  26. # Distribution des valeurs de carats (continu)
  27. diamonds %>%
  28. ggplot() +
  29. aes(x = carat) +
  30. geom_histogram(binwidth = .05)
  31. # Distribution des valeurs de coupe (catégoriel)
  32. diamonds %>%
  33. ggplot() +
  34. aes(x = cut) +
  35. geom_bar()
  36. # …
  37. # Fonction pour afficher une distribution
  38. plot_desc <- function(x, var, ...)
  39. {
  40. x %>%
  41. ggplot() +
  42. aes_string(x = var) -> g
  43. if (x[[var]] %>% is.numeric)
  44. g + geom_histogram(...)
  45. else
  46. g + geom_bar(...)
  47. }
  48. # Application
  49. diamonds %>%
  50. plot_desc("carat", binwidth = .01)
  51. # Application en masse
  52. diamonds %>%
  53. keep(is.numeric) %>%
  54. names %>%
  55. map(~plot_desc(diamonds, ., binwidth = .5)) %>%
  56. set_names(diamonds %>% keep(is.numeric) %>% names) -> plots
  57. diamonds %>%
  58. keep(is.factor) %>%
  59. names %>%
  60. map(~plot_desc(diamonds, .)) %>%
  61. set_names(diamonds %>% keep(is.factor) %>% names) -> plots
  62. # Matrice de relations entre les variables
  63. diamonds %>%
  64. sample_n(1000) %>%
  65. plot
  66. # Association entre deux variables (continue/catégorielle)
  67. diamonds %>%
  68. ggplot() +
  69. aes(x = carat, fill = clarity) +
  70. geom_histogram(position = "fill")
  71. # Association entre deux variables continues
  72. diamonds %>%
  73. ggplot() +
  74. aes(x = x, y = y) +
  75. geom_point()
  76. # Association entre 3 variables
  77. diamonds %>%
  78. ggplot() +
  79. aes(x = carat, y = price, color = color) +
  80. geom_point()
  81. ## Exercice gapminder ----
  82. data(gapminder)
  83. # Feature engineering ----
  84. read_csv2("commits.csv")
  85. # Commits par jour ? En fonction de l'heure ? Par jour et par heure ? boulot/perso ? en semaine/week-end ?