Réponses aux exercices
Les outils R de base
Exercice 1 : sphère
<- function(rayon = 1)
sphere
{<- 4 * pi * rayon^2
aire <- (4 * pi * rayon^3)/3
volume <- volume/aire
comp <- data.frame(rayon = rayon, aire = aire, volume = volume, compacité = comp)
df class(df) <- c("sphere", "data.frame")
df
}
<- function(x, ...)
print.sphere
{<- paste(x$rayon, collapse = ", ")
r 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)
<- sphere(1:5)
toto $volume / toto$aire toto
Exercice 2 : ANOVA
### Moyennes
<- mean(iris$Petal.Width)
moyG by(iris$Petal.Width, iris$Species, mean)
$SpMean <- rep(by(iris$Petal.Width, iris$Species, mean), each = 50)
iris
### SCE_T
<- sum((iris$Petal.Width - moyG)^2)
SCEt
### SCE_M
<- sum((iris$SpMean - moyG)^2)
SCEm
### SCE_R
<- sum((iris$Petal.Width - iris$SpMean)^2)
SCEr
### Check
+ SCEr
SCEm
### F
<- (SCEm/(3-1))/(SCEr/(150-3))
F <- 1 - pf(F, 3-1, 150-3)
p print(p, digits = 22)
### eta²
/150)/(SCEt/150) (SCEm
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
<- read_csv2("data/ins_univ_18_ano.csv", na = c("", "."), col_types =
(ins18_univ 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")