|
| 1 | +--- |
| 2 | +title: "Validation croisée" |
| 3 | +author: "Guyliann Engels & Philippe Grosjean" |
| 4 | +description: "**SDD III Module 2** La validation croisée" |
| 5 | +tutorial: |
| 6 | + id: "C02La_cv" |
| 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 | +set.seed(42) |
| 21 | +# exercice preparation -------- |
| 22 | +read("biometry", package = "BioDataScience") %>.% |
| 23 | + select(., gender, weight, height, wrist) %>.% |
| 24 | + drop_na(.) -> bio |
| 25 | +
|
| 26 | +## Creation d'un modèle lda |
| 27 | +bio_lda <- mlLda(data = bio, gender ~ .) |
| 28 | +
|
| 29 | +## Confusion |
| 30 | +bio_conf <- confusion(cvpredict(bio_lda, cv.k = 10), bio$gender) |
| 31 | +conf_tab <- summary(bio_conf) |
| 32 | +``` |
| 33 | + |
| 34 | +```{r, echo=FALSE} |
| 35 | +BioDataScience3::learnr_banner() |
| 36 | +``` |
| 37 | + |
| 38 | +```{r, context="server"} |
| 39 | +BioDataScience3::learnr_server(input, output, session) |
| 40 | +``` |
| 41 | + |
| 42 | +---- |
| 43 | + |
| 44 | +## Objectifs |
| 45 | + |
| 46 | +- Réalisez une analyse discriminante linéaire avec la validation croisée. |
| 47 | + |
| 48 | +## Création de votre modèle |
| 49 | + |
| 50 | +Vous avez à votre disposition le jeu de données `bio` dont un résumé est proposé ci-dessous. |
| 51 | + |
| 52 | +```{r, echo = TRUE} |
| 53 | +skimr::skim(bio) |
| 54 | +``` |
| 55 | + |
| 56 | +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 : |
| 57 | + |
| 58 | +- weight : la masse en kg |
| 59 | +- height : la taille en cm |
| 60 | +- wrist : la circonférence du poignet en mm |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +Réalisez un modèle avec `bio`. Prédisez la variable `gender` à l'aide des 3 variables numériques. |
| 65 | + |
| 66 | +```{r lda1_h2, exercise = TRUE} |
| 67 | +bio_lda <- mlLda(data = ___, ___ ~ ___) |
| 68 | +summary(bio_lda) |
| 69 | +``` |
| 70 | + |
| 71 | +```{r lda1_h2-hint-1} |
| 72 | +bio_lda <- mlLda(data = bio, ___ ~ ___) |
| 73 | +summary(bio_lda) |
| 74 | +
|
| 75 | +## Attention, le prochain indice est la solution ## |
| 76 | +``` |
| 77 | + |
| 78 | +```{r lda1_h2-solution} |
| 79 | +## Solution ## |
| 80 | +bio_lda <- mlLda(data = bio, gender ~ .) |
| 81 | +summary(bio_lda) |
| 82 | +``` |
| 83 | + |
| 84 | +```{r lda1_h2-check} |
| 85 | +grade_code("Votre modèle est une réussite.") |
| 86 | +``` |
| 87 | + |
| 88 | +*La formule doit être écrite sous sa forme condensée* |
| 89 | + |
| 90 | +## Performance de votre modèle. |
| 91 | + |
| 92 | +Vous venez de créer votre outils de classification qui se nomme `bio_lda`. Vous devez maintenant tester les performances de votre modèle avec une validation croisée dix fois |
| 93 | + |
| 94 | +```{r lda2_h2, exercise = TRUE} |
| 95 | +set.seed(42) |
| 96 | +# prédiction sur le set de test |
| 97 | +bio_pred <- cvpredict(___, cv.k = ___) |
| 98 | +# matrice de confusion |
| 99 | +bio_conf <- confusion(___, ___$___) |
| 100 | +# analyse du résultat |
| 101 | +bio_conf |
| 102 | +summary(bio_conf) |
| 103 | +``` |
| 104 | + |
| 105 | +```{r lda2_h2-hint-1} |
| 106 | +set.seed(42) |
| 107 | +# prédiction sur le set de test |
| 108 | +bio_pred <- cvpredict(bio_lda, cv.k = 10) |
| 109 | +# matrice de confusion |
| 110 | +bio_conf <- confusion(bio_pred, ___$___) |
| 111 | +# analyse du résultat |
| 112 | +bio_conf |
| 113 | +summary(bio_conf) |
| 114 | +``` |
| 115 | + |
| 116 | +```{r lda2_h2-solution} |
| 117 | +set.seed(42) |
| 118 | +# prédiction sur le set de test |
| 119 | +bio_pred <- cvpredict(bio_lda, cv.k = 10) |
| 120 | +# matrice de confusion |
| 121 | +bio_conf <- confusion(bio_pred, bio$gender) |
| 122 | +# analyse du résultat |
| 123 | +bio_conf |
| 124 | +summary(bio_conf) |
| 125 | +``` |
| 126 | + |
| 127 | +```{r lda2_h2-check} |
| 128 | +grade_code("Vous venez de réaliser vos premiers tests de performance.") |
| 129 | +``` |
| 130 | + |
| 131 | +Analysez vos premiers tests de performance |
| 132 | + |
| 133 | +```{r lda_qu} |
| 134 | +quiz( |
| 135 | + question("Combien d'items sont correctement classés ?", |
| 136 | + answer(sprintf("%1.f", sum(conf_tab$TP)), correct = TRUE), |
| 137 | + answer(sprintf("%1.f", sum(conf_tab$Auto))), |
| 138 | + answer(sprintf("%1.f", conf_tab$Manu[1])), |
| 139 | + answer(sprintf("%1.f", conf_tab$TN[2])), |
| 140 | + answer("Aucune des réponses proposées"), |
| 141 | + allow_retry = TRUE, |
| 142 | + incorrect = "Mauvaise réponse. Recommencez afin de trouver la bonne réponse", |
| 143 | + correct = "Bravo, c'est correct !" |
| 144 | + ), |
| 145 | + question("Quel est le taux d'erreur global (en %) ?", |
| 146 | + answer(sprintf("%.1f", 100*(1-(sum(conf_tab$TP)/sum(conf_tab$Auto)))), correct = TRUE), |
| 147 | + answer(sprintf("%.1f", 100*(sum(conf_tab$TP)/sum(conf_tab$Auto)))), |
| 148 | + answer(sprintf("%3.f", sum(conf_tab$Auto)-sum(conf_tab$TP))), |
| 149 | + answer(sprintf("%3.f", conf_tab$TN[2])), |
| 150 | + answer("Aucune des réponses proposées"), |
| 151 | + allow_retry = TRUE, |
| 152 | + incorrect = "Mauvaise réponse. Recommencez afin de trouver la bonne réponse", |
| 153 | + correct = "Bravo, c'est correct !" |
| 154 | + ), |
| 155 | + question("Quel est la spécificité pour les femmes (F) ?", |
| 156 | + answer(sprintf("%.3f", conf_tab[row.names(conf_tab) == "W", ]$Specificity), correct = TRUE), |
| 157 | + answer(sprintf("%.3f", conf_tab[row.names(conf_tab) == "M", ]$Fscore)), |
| 158 | + answer(sprintf("%3.f", sum(conf_tab$Auto)-sum(conf_tab$TP))), |
| 159 | + answer(sprintf("%.3f", conf_tab[row.names(conf_tab) == "W", ]$Recall)), |
| 160 | + answer("Aucune des réponses proposées"), |
| 161 | + allow_retry = TRUE, |
| 162 | + incorrect = "Mauvaise réponse. Recommencez afin de trouver la bonne réponse", |
| 163 | + correct = "Bravo, c'est correct !" |
| 164 | + ) |
| 165 | + ) |
| 166 | +``` |
| 167 | + |
| 168 | +## Conclusion |
| 169 | + |
| 170 | +```{r comm_noscore, echo=FALSE} |
| 171 | +question_text( |
| 172 | + "Laissez-nous vos impressions sur cet outil pédagogique", |
| 173 | + answer("", TRUE, message = "Pas de commentaires... C'est bien aussi."), |
| 174 | + incorrect = "Vos commentaires sont enregistrés.", |
| 175 | + placeholder = "Entrez vos commentaires ici...", |
| 176 | + allow_retry = TRUE |
| 177 | +) |
| 178 | +``` |
0 commit comments