diff --git a/module3/exo2/exercice_python_en.org b/module3/exo2/exercice_python_en.org index 5782f493934678ba782fb65634a4d86e5f3adefc..c5ecf7adc3c0c5acc123e398e87290d77541186d 100644 --- a/module3/exo2/exercice_python_en.org +++ b/module3/exo2/exercice_python_en.org @@ -1,9 +1,8 @@ -#+TITLE: Your title -#+AUTHOR: Your name -#+DATE: Today's date -#+LANGUAGE: en -# #+PROPERTY: header-args :eval never-export +#+TITLE: Incidence du syndrôme varicelle +#+LANGUAGE: fr +#+OPTIONS: *:nil num:1 toc:t +# #+HTML_HEAD: #+HTML_HEAD: #+HTML_HEAD: #+HTML_HEAD: @@ -11,84 +10,230 @@ #+HTML_HEAD: #+HTML_HEAD: -* Some explanations - -This is an org-mode document with code examples in R. Once opened in -Emacs, this document can easily be exported to HTML, PDF, and Office -formats. For more information on org-mode, see -https://orgmode.org/guide/. - -When you type the shortcut =C-c C-e h o=, this document will be -exported as HTML. All the code in it will be re-executed, and the -results will be retrieved and included into the exported document. If -you do not want to re-execute all code each time, you can delete the # -and the space before ~#+PROPERTY:~ in the header of this document. - -Like we showed in the video, Python code is included as follows (and -is exxecuted by typing ~C-c C-c~): - -#+begin_src python :results output :exports both -print("Hello world!") -#+end_src - -#+RESULTS: -: Hello world! - -And now the same but in an Python session. With a session, Python's -state, i.e. the values of all the variables, remains persistent from -one code block to the next. The code is still executed using ~C-c -C-c~. - -#+begin_src python :results output :session :exports both -import numpy -x=numpy.linspace(-15,15) -print(x) -#+end_src - -#+RESULTS: -#+begin_example -[-15. -14.3877551 -13.7755102 -13.16326531 -12.55102041 - -11.93877551 -11.32653061 -10.71428571 -10.10204082 -9.48979592 - -8.87755102 -8.26530612 -7.65306122 -7.04081633 -6.42857143 - -5.81632653 -5.20408163 -4.59183673 -3.97959184 -3.36734694 - -2.75510204 -2.14285714 -1.53061224 -0.91836735 -0.30612245 - 0.30612245 0.91836735 1.53061224 2.14285714 2.75510204 - 3.36734694 3.97959184 4.59183673 5.20408163 5.81632653 - 6.42857143 7.04081633 7.65306122 8.26530612 8.87755102 - 9.48979592 10.10204082 10.71428571 11.32653061 11.93877551 - 12.55102041 13.16326531 13.7755102 14.3877551 15. ] -#+end_example - -Finally, an example for graphical output: -#+begin_src python :results output file :session :var matplot_lib_filename="./cosxsx.png" :exports results -import matplotlib.pyplot as plt - -plt.figure(figsize=(10,5)) -plt.plot(x,numpy.cos(x)/x) -plt.tight_layout() - -plt.savefig(matplot_lib_filename) -print(matplot_lib_filename) -#+end_src - -#+RESULTS: -[[file:./cosxsx.png]] - -Note the parameter ~:exports results~, which indicates that the code -will not appear in the exported document. We recommend that in the -context of this MOOC, you always leave this parameter setting as -~:exports both~, because we want your analyses to be perfectly -transparent and reproducible. - -Watch out: the figure generated by the code block is /not/ stored in -the org document. It's a plain file, here named ~cosxsx.png~. You have -to commit it explicitly if you want your analysis to be legible and -understandable on GitLab. - -Finally, don't forget that we provide in the resource section of this -MOOC a configuration with a few keyboard shortcuts that allow you to -quickly create code blocks in Python by typing ~
R +Nous passons au langage R pour inspecter nos données, parce que l'analyse et la préparation de graphiques sont plus concises en R, sans nécessiter aucune bibliothèque supplémentaire. + +Nous utilisons le mécanisme d'échange de données proposé par org-mode, ce qui nécessite un peu de code Python pour transformer les données dans le bon format. +#+NAME: data-for-R +#+BEGIN_SRC python :results silent :exports both +[('date', 'inc'), None] + [(str(date), inc) for date, inc in data] +#+END_SRC + +En R, les données arrivent sous forme d'un data frame, mais il faut encore convertir les dates, qui arrivent comme chaînes de caractères. +#+BEGIN_SRC R :results output :var data=data-for-R :exports both +data$date <- as.Date(data$date) +summary(data) +#+END_SRC + +** Inspection +Regardons enfin à quoi ressemblent nos données ! +#+BEGIN_SRC R :results output graphics :file inc-plot.png :exports both +plot(data, type="l", xlab="Date", ylab="Incidence hebdomadaire") +#+END_SRC + +Un zoom sur les dernières années montre mieux la situation des pics en hiver. Le creux des incidences se trouve en été. +#+BEGIN_SRC R :results output graphics :file inc-plot-zoom.png :exports both +plot(tail(data, 200), type="l", xlab="Date", ylab="Incidence hebdomadaire") +#+END_SRC + +* Étude de l'incidence annuelle + +** Calcul de l'incidence annuelle +Étant donné que le pic de l'épidémie se situe en hiver, à cheval entre deux années civiles, nous définissons la période de référence entre deux minima de l'incidence, du 1er août de l'année /N/ au 1er août de l'année /N+1/. Nous mettons l'année /N+1/ comme étiquette sur cette année décalée, car le pic de l'épidémie est toujours au début de l'année /N+1/. Comme l'incidence du syndrome varicelle est très faible en été, cette modification ne risque pas de fausser nos conclusions. + +Voici une fonction qui calcule l'incidence annuelle en appliquant ces conventions. +#+BEGIN_SRC R :results silent :exports both +pic_annuel = function(annee) { + debut = paste0(annee-1,"-09-01") + fin = paste0(annee,"-09-01") + semaines = data$date > debut & data$date <= fin + sum(data$inc[semaines], na.rm=TRUE) + } +#+END_SRC + +Nous devons aussi faire attention aux premières et dernières années de notre jeux de données. Les données commencent en octobre 1984, ce qui ne permet pas de quantifier complètement le pic attribué à l'année 1985. Nous le supprimons donc de notre analyse. Par contre, les données se terminent après le 1er août 2018 (pour une exécution après cette date bien sûr), ce qui nous permet d'inclure cette année dans l'analyse. +#+BEGIN_SRC R :results silent :exports both +annees <- 1986:2018 +#+END_SRC + +#+BEGIN_SRC R :results value :exports both +inc_annuelle = data.frame(annee = annees, + incidence = sapply(annees, pic_annuel)) +head(inc_annuelle) +#+END_SRC + +** Inspection +Voici les incidences annuelles en graphique. +#+BEGIN_SRC R :results output graphics :file annual-inc-plot.png :exports both +plot(inc_annuelle, type="p", xlab="Année", ylab="Incidence annuelle") +#+END_SRC + +** Identification des épidémies les plus fortes +Une liste triée par ordre décroissant d'incidence annuelle permet de plus facilement repérer les valeurs les plus élevées: +#+BEGIN_SRC R :results output :exports both +head(inc_annuelle[order(-inc_annuelle$incidence),]) +#+END_SRC + +Enfin, un histogramme montre bien que les épidémies fortes, qui touchent environ 10% de la population française, sont assez rares: il y en eu trois au cours des 35 dernières années. +#+BEGIN_SRC R :results output graphics :file annual-inc-hist.png :exports both +hist(inc_annuelle$incidence, breaks=10, xlab="Incidence annuelle", ylab="Nb d'observations", main="") +#+END_SRC