Skip to content

Commit bd981ce

Browse files
edit C01Lb_lda
1 parent 0ecd2dc commit bd981ce

File tree

2 files changed

+898
-0
lines changed

2 files changed

+898
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
---
2+
title: "Analyse discriminante linéaire"
3+
author: "Guyliann Engels & Philippe Grosjean"
4+
description: "**SDD III** Exercices sur l'ADL"
5+
tutorial:
6+
id: "C01Lb_lda"
7+
version: 1.0.0/5
8+
output:
9+
learnr::tutorial:
10+
progressive: true
11+
allow_skip: true
12+
runtime: shiny_prerendered
13+
---
14+
15+
```{r setup, include=FALSE}
16+
BioDataScience3::learnr_setup()
17+
SciViews::R()
18+
library(mlearning)
19+
20+
read("biometry", package = "BioDataScience") %>.%
21+
select(., gender, weight, height, wrist) %>.%
22+
drop_na(.) -> bio
23+
24+
# Prepare learn test and set test
25+
n <- nrow(bio)
26+
n_learning <- round(n * 2/3)
27+
set.seed(164)
28+
learning <- sample(1:n, n_learning)
29+
30+
bio_learn <- bio[learning, ]
31+
bio_test <- bio[-learning, ]
32+
33+
bio_lda <- mlLda(formula = gender ~ ., data = bio_learn)
34+
```
35+
36+
```{r, echo=FALSE}
37+
BioDataScience3::learnr_banner()
38+
```
39+
40+
```{r, context="server"}
41+
BioDataScience3::learnr_server(input, output, session)
42+
```
43+
44+
----
45+
46+
## Objectifs
47+
48+
- Réalisez une analyse discriminante linéaire.
49+
50+
```{r, message=FALSE, warning=FALSE}
51+
# exercice 1 : résolution
52+
read("biometry", package = "BioDataScience") %>.%
53+
select(., gender, weight, height, wrist) %>.%
54+
drop_na(.) -> bio
55+
56+
# Prepare learn test and set test
57+
n <- nrow(bio)
58+
n_learning <- round(n * 2/3)
59+
set.seed(164)
60+
learning <- sample(1:n, n_learning)
61+
62+
bio_learn <- bio[learning, ]
63+
bio_test <- bio[-learning, ]
64+
65+
bio_lda <- mlLda(formula = gender ~ ., data = bio_learn)
66+
bio_conf <- confusion(predict(bio_lda, bio_test), bio_test$gender)
67+
conf_tab <- summary(bio_conf)
68+
```
69+
70+
## Création de votre modèle
71+
72+
73+
Vous avez à votre disposition le jeu de données `bio` dont un résumé est proposé ci-dessous.
74+
75+
```{r, echo = TRUE}
76+
skimr::skim(bio)
77+
```
78+
79+
Ce tableau comprend `r ncol(bio)` variables. La variable `gender` est une variable facteur avec 2 niveaux : M (men), W (Women). Il y a également 3 variables numérique qui sont les attributs sur les items :
80+
81+
- weight : la masse en kg
82+
- height : la taille en cm
83+
- wrist : la circonférence du poignet en mm
84+
85+
Cet tableau est divisé en un set d'apprentissage et un set de test. Le set d'apprentissage comprend 2/3 du tableau principal. Ce tableau nommé `bio_learn` comprend `r nrow(bio_learn)`
86+
87+
```{r, echo=TRUE}
88+
table(bio_learn$gender)
89+
```
90+
91+
Le tableau de test se nomme `bio_test` et comprend `r nrow(bio_test)`.
92+
93+
```{r}
94+
table(bio_test$gender)
95+
```
96+
97+
Réalisez un modèle avec le set d'apprentissage. Prédisez la variable `gender` à l'aide des 3 variables numériques.
98+
99+
```{r lda1_h2, exercise = TRUE}
100+
bio_lda <- mlLda(formula = ___ ~ ___, data = ___)
101+
summary(bio_lda)
102+
```
103+
104+
```{r lda1_h2-hint-1}
105+
bio_lda <- mlLda(formula = ___ ~ ___, data = bio_learn)
106+
summary(bio_lda)
107+
```
108+
109+
```{r lda1_h2-solution}
110+
bio_lda <- mlLda(formula = gender ~ ., data = bio_learn)
111+
summary(bio_lda)
112+
```
113+
114+
```{r lda1_h2-check}
115+
grade_code("Votre premier modèle est une réussite.")
116+
```
117+
118+
*La formule doit être écrite sous sa forme condensée*
119+
120+
## Performance de votre modèle.
121+
122+
Vous venez de créer votre outils de classification qui se nomme `bio_lda`. Vous devez maintenant tester les performances de votre modèle.
123+
124+
```{r lda2_h2, exercise = TRUE}
125+
# prédiction sur le set de test
126+
bio_pred <- predict(___, ___)
127+
# matrice de confusion
128+
bio_conf <- confusion(___, ___$___)
129+
# analyse du résultat
130+
bio_conf
131+
summary(bio_conf)
132+
```
133+
134+
```{r lda2_h2-hint-1}
135+
# prédiction sur le set de test
136+
bio_pred <- predict(bio_lda, bio_test)
137+
# matrice de confusion
138+
bio_conf <- confusion(bio_pred, ___$___)
139+
# analyse du résultat
140+
bio_conf
141+
summary(bio_conf)
142+
```
143+
144+
```{r lda2_h2-solution}
145+
# prédiction sur le set de test
146+
bio_pred <- predict(bio_lda, bio_test)
147+
# matrice de confusion
148+
bio_conf <- confusion(bio_pred, bio_test$gender)
149+
# analyse du résultat
150+
bio_conf
151+
summary(bio_conf)
152+
```
153+
154+
```{r lda2_h2-check}
155+
grade_code("Vous venez de réaliser vos premiers tests de performance.")
156+
```
157+
158+
Analysez vos premiers tests de performance
159+
160+
```{r lda_qu}
161+
quiz(
162+
question("Combien d'items du set de test sont correctement classé ?",
163+
answer(sprintf("%1.f", sum(conf_tab$TP)), correct = TRUE),
164+
answer(sprintf("%1.f", sum(conf_tab$Auto))),
165+
answer(sprintf("%1.f", conf_tab$Manu[1])),
166+
answer(sprintf("%1.f", conf_tab$TN[2])),
167+
answer("Aucune des réponses proposées"),
168+
allow_retry = TRUE,
169+
incorrect = "Mauvaise réponse. Recommencez afin de trouver la bonne réponse",
170+
correct = "Bravo, c'est correct !"
171+
),
172+
question("Quel est le taux d'erreur global (en %) ?",
173+
answer(sprintf("%.1f", 100*(1-(sum(conf_tab$TP)/sum(conf_tab$Auto)))), correct = TRUE),
174+
answer(sprintf("%.1f", 100*(sum(conf_tab$TP)/sum(conf_tab$Auto)))),
175+
answer(sprintf("%3.f", sum(conf_tab$Auto)-sum(conf_tab$TP))),
176+
answer(sprintf("%3.f", conf_tab$TN[2])),
177+
answer("Aucune des réponses proposées"),
178+
allow_retry = TRUE,
179+
incorrect = "Mauvaise réponse. Recommencez afin de trouver la bonne réponse",
180+
correct = "Bravo, c'est correct !"
181+
),
182+
question("Quel est le taux de vrai positif pour les hommes (M) ?",
183+
answer(sprintf("%.3f", conf_tab[row.names(conf_tab) == "M", ]$Recall), correct = TRUE),
184+
answer(sprintf("%.3f", conf_tab[row.names(conf_tab) == "M", ]$Fscore)),
185+
answer(sprintf("%3.f", sum(conf_tab$Auto)-sum(conf_tab$TP))),
186+
answer(sprintf("%.3f", conf_tab[row.names(conf_tab) == "W", ]$Recall)),
187+
answer("Aucune des réponses proposées"),
188+
allow_retry = TRUE,
189+
incorrect = "Mauvaise réponse. Recommencez afin de trouver la bonne réponse",
190+
correct = "Bravo, c'est correct !"
191+
)
192+
)
193+
```
194+
195+
## Conclusion
196+
197+
```{r comm_noscore, echo=FALSE}
198+
question_text(
199+
"Laissez-nous vos impressions sur cet outil pédagogique",
200+
answer("", TRUE, message = "Pas de commentaires... C'est bien aussi."),
201+
incorrect = "Vos commentaires sont enregistrés.",
202+
placeholder = "Entrez vos commentaires ici...",
203+
allow_retry = TRUE
204+
)
205+
```

0 commit comments

Comments
 (0)