Réponses aux exercices

Les outils R de base

Exercice 1 : sphère

sphere <- function(rayon = 1)
{
  aire <- 4 * pi * rayon^2
  volume <- (4 * pi * rayon^3)/3
  comp <- volume/aire
  df <- data.frame(rayon = rayon, aire = aire, volume = volume, compacité = comp)
  class(df) <- c("sphere", "data.frame")
  df
}

print.sphere <- function(x, ...)
{
  r <- paste(x$rayon, collapse = ", ")
  if (nrow(x) == 1)
    message(paste("Les caractéristiques d'une sphère de rayon", r, "sont :"))
  else message(paste("Les caractéristiques de sphères de rayons", r, "sont :"))
  print(as.data.frame(x))
}

sphere(4)
sphere(1:5)
toto <- sphere(1:5)
toto$volume / toto$aire

Exercice 2 : ANOVA

### Moyennes
moyG <- mean(iris$Petal.Width)
by(iris$Petal.Width, iris$Species, mean)
iris$SpMean <- rep(by(iris$Petal.Width, iris$Species, mean), each = 50)

### SCE_T
SCEt <- sum((iris$Petal.Width - moyG)^2)

### SCE_M
SCEm <- sum((iris$SpMean - moyG)^2)

### SCE_R
SCEr <- sum((iris$Petal.Width - iris$SpMean)^2)

### Check
SCEm + SCEr

### F
F <- (SCEm/(3-1))/(SCEr/(150-3))
p <- 1 - pf(F, 3-1, 150-3)
print(p, digits = 22)

### eta²
(SCEm/150)/(SCEt/150)

Graphiques et visualisation de données

Exercice 1 : Boîtes à moustaches

ggplot(um18,  aes(x = bac_regroup, y = prem_insc_univ_fr, color = bac_regroup)) +
  geom_jitter(alpha = 0.05) +
  geom_boxplot(alpha = 0, color = "black") +
  ylim(1980, 2020) +
  theme_classic() +
  labs(title = "Quelle est l'année de première inscription à l'université, selon la filière du bac ?",
       subtitle = "(données pour les inscriptions à l'Université de Montpellier 2018–2019)",
       x = "Type de baccalauréat",
       y = "Année (tronquée à partir de 1980)") +
  theme(legend.position = "none",
        axis.text.x = element_text(angle = -45, vjust = 0.1, hjust = 0.1))

Exercice 2 : Facettage

ggplot(um18[!is.na(um18$dom_abr),], aes(bac_abr, fill = bac_abr)) +
  geom_bar() +
  facet_grid(cols = vars(dom_abr), rows = vars(inscription_premiere), scales = "free") +
  theme_light() +
  theme(legend.position = "none") +
  labs(title = "Origine des étudiants inscrits par domaine disciplinaire",
       subtitle = "(selon s'il s'agit d'une inscription principale ou secondaire)",
       x = "Type de bac",
       y = "Nombre d'étudiants")

Exercice 3 : ggsave

ggsave("images/ex.png", scale = 2, dpi = 150)

Importer des fichiers externes de données

Exercice 1 : ins_univ_18_ano.csv

(ins18_univ <- read_csv2("data/ins_univ_18_ano.csv", na = c("", "."), col_types =
   cols(
     ACAETA = col_character(), # col_double(),
     COMPOS = col_character(),
     CONV = col_character(), # col_logical(),
     CURPAR = col_character(),
     CURSUS_LMD = col_character(),
     CYCLE = col_double(),
     DEGETU = col_character(), # col_double(),
     DEPETA = col_character(),
     DIPDER = col_character(),
     DIPLOM = col_character(), # col_double(),
     DISCIPLI = col_character(),
     EFFECTIF = col_character(), # col_double(),
     ETABLI = col_character(), # col_character(),
     ETABLI_DIFFUSION = col_character(),
     flag_meef = col_double(),
     GROUPE = col_character(),
     IDETU = col_character(),
     NBACH = col_double(),
     NET = col_double(),
     NIVEAU = col_character(),
     NUMED = col_character(), # col_logical(),
     NUMINS = col_character(), # col_double(),
     PARIPA = col_character(), # col_double(),
     REGIME = col_double(),
     SECTDIS = col_character(),
     SITUPRE = col_character(),
     SPECIA = col_character(), # col_logical(),
     SPECIB = col_character(), # col_logical(),
     TYPREPA = col_character(), # col_logical(),
     TYP_DIPL = col_character(),
     UNIV = col_character(),
     VOIE = col_double()
   )))

Manipuler et traîter les données façon bases de données

Exercice 2 : barplots en proportions

um18 %>% 
  filter(!is.na(domaine_disciplinaire)) %>% 
  mutate(inscription_premiere = recode(inscription_premiere,
                                       "oui" = "Principale",
                                       "non" = "Secondaire")) %>% 
  count(dom_abr, inscription_premiere, bac_abr) %>% 
  group_by(dom_abr, inscription_premiere) %>% 
  mutate(total = sum(n)) %>% 
  mutate(prop = n / total) %>% 
  ggplot(aes(bac_abr, prop, fill = bac_abr)) +
  geom_bar(stat="identity") +
  geom_label(aes(x = 3.5, y = Inf, vjust = 1.5, label = total), fill = "white") +
  facet_grid(cols = vars(dom_abr), rows = vars(inscription_premiere), scales = "free") +
  theme_light() +
  theme(legend.position = "none") +
  labs(title = "Origine des étudiants inscrits par domaine disciplinaire",
       subtitle = "(selon s'il s'agit d'une inscription principale ou secondaire)",
       x = "Type de bac",
       y = "Proportion d'étudiants")