forked from flodebarre/covid_vaccination
-
Notifications
You must be signed in to change notification settings - Fork 0
/
communes.html
977 lines (799 loc) · 37.9 KB
/
communes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="FD" />
<title>communes</title>
<script src="communes_files/header-attrs-2.7/header-attrs.js"></script>
<script src="communes_files/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="communes_files/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="communes_files/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="communes_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="communes_files/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="communes_files/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="communes_files/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="communes_files/tocify-1.9.1/jquery.tocify.js"></script>
<script src="communes_files/navigation-1.1/tabsets.js"></script>
<script src="communes_files/navigation-1.1/codefolding.js"></script>
<link href="communes_files/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="communes_files/highlightjs-9.12.0/highlight.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
pre code {
padding: 0;
}
</style>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
.code-folding-btn { margin-bottom: 4px; }
</style>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-sm-12 col-md-4 col-lg-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-sm-12 col-md-8 col-lg-9">
<div id="header">
<div class="btn-group pull-right float-right">
<button type="button" class="btn btn-default btn-xs btn-secondary btn-sm dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span>Code</span> <span class="caret"></span></button>
<ul class="dropdown-menu dropdown-menu-right" style="min-width: 50px;">
<li><a id="rmd-show-all-code" href="#">Show All Code</a></li>
<li><a id="rmd-hide-all-code" href="#">Hide All Code</a></li>
</ul>
</div>
<h1 class="title toc-ignore">communes</h1>
<h4 class="author">FD</h4>
</div>
<div id="initializations" class="section level1">
<h1>Initializations</h1>
<div id="load-data" class="section level2">
<h2>Load data</h2>
<div id="niveau-de-vie-par-commune-2014" class="section level3">
<h3>Niveau de vie par commune (2014)</h3>
<p>Source : <a href="https://www.data.gouv.fr/fr/datasets/niveau-de-vie-des-francais-par-commune/" class="uri">https://www.data.gouv.fr/fr/datasets/niveau-de-vie-des-francais-par-commune/</a></p>
<blockquote>
<p>L’Insee a publié les niveaux de vie des ménages par commune pour l’année 2014. Le dispositif d’analyse, appelé Filosofi, permet de détailler où se situent les zones de pauvreté en France. Date de mise à jour : 31 octobre 2017</p>
</blockquote>
<pre class="r"><code>URL <- "https://www.data.gouv.fr/fr/datasets/r/16fce6ae-1907-442d-99b2-9ddcf6c41b03"
dataFile <- paste0("data/niveauVie.csv") # name file with today's date
if(dlData){
download.file(URL, dataFile) # download file from repo
}
dat.niveauVie2014 <- read.csv(dataFile, sep = ";", stringsAsFactors = FALSE, dec = ",")
head(dat.niveauVie2014)</code></pre>
</div>
<div id="niveau-de-vie-par-commune-2018" class="section level3">
<h3>Niveau de vie par commune 2018</h3>
<p>Source : <a href="https://www.insee.fr/fr/statistiques/5009236?sommaire=5009255#consulter" class="uri">https://www.insee.fr/fr/statistiques/5009236?sommaire=5009255#consulter</a></p>
<!--
Liste des variables
Code géographique ;
Libellé géographique ;
Nombre de ménages fiscaux ;
Nombre de personnes dans les ménages fiscaux ;
Médiane du niveau de vie (€) ;
Part des ménages fiscaux imposés (%) ;
Taux de pauvreté-Ensemble (%) ;
Taux de pauvreté des ménages dont le référent fiscal a moins de 30 ans (%) ;
Taux de pauvreté des ménages dont le référent fiscal a de 30 à 39 ans (%) ;
Taux de pauvreté des ménages dont le référent fiscal a de 40 à 49 ans (%) ;
Taux de pauvreté des ménages dont le référent fiscal a de 50 à 59 ans (%) ;
Taux de pauvreté des ménages dont le référent fiscal a de 60 à 74 ans (%) ;
Taux de pauvreté des ménages dont le référent fiscal a 75 ans ou plus (%) ;
Taux de pauvreté des ménages propriétaires de leur logement (%) ;
Taux de pauvreté des ménages locataires de leur logement (%) ;
Part des revenus d'activité (%) ;
dont part des salaires, traitements (%) ;
dont part des indemnités de chômage (%) ;
dont part des revenus des activités non salariées (%) ;
Part des pensions, retraites et rentes (%) ;
Part des revenus du patrimoine et des autres revenus (%) ;
Part de l'ensemble des prestations sociales (%) ;
dont part des prestations familiales (%) ;
dont part des minima sociaux (%) ;
dont part des prestations logement (%) ;
Part des impôts (%) ;
Rapport interdécile 9e décile/1er decile ;
1er décile du niveau de vie (€) ;
9e décile du niveau de vie (€).
-->
<pre class="r"><code>dat.niveauVie2018 <- read.csv("data/cc_filosofi_2018_COM-geo2021.csv", sep = ";")
head(dat.niveauVie2018)</code></pre>
</div>
<div id="vaccination-par-commune" class="section level3">
<h3>Vaccination par commune</h3>
<pre class="r"><code>URL <- "https://datavaccin-covid.ameli.fr/explore/dataset/donnees-de-vaccination-par-commune/download/?format=csv&timezone=Europe/Berlin&lang=fr&use_labels_for_header=true&csv_separator=%3B"
dataFile <- paste0("data/vacciCommunes.csv") # name file with today's date
if(dlData){
download.file(URL, dataFile) # download file from repo
}
dat.vaccin <- read.csv(dataFile, sep = ";", stringsAsFactors = FALSE, dec = ",")
head(dat.vaccin)</code></pre>
</div>
</div>
<div id="clean-data" class="section level2">
<h2>Clean data</h2>
<pre class="r"><code># Final week in the data
maxWeek <- max(unique(dat.vaccin$semaine_injection))
# Select only data for the final week
dat.vaccin.final <- dat.vaccin[which(dat.vaccin$semaine_injection == maxWeek), ]</code></pre>
<p>Merge the two datasets</p>
<pre class="r"><code>nrow(dat.vaccin.final)
names(dat.niveauVie2014)
names(dat.niveauVie2014) <- c("commune_residence", "libelle_commune", "med14_2014")
nrow(dat.niveauVie2014)
names(dat.niveauVie2018)[c(1, 4)] <- c("commune_residence", "med14_2018")
dat.all <- merge(dat.niveauVie2014, dat.vaccin.final, by = "commune_residence")
nrow(dat.all)
dat.all <- merge(dat.all, dat.niveauVie2018, by = "commune_residence")
nrow(dat.all)
names(dat.all)</code></pre>
<p>Add region information</p>
<pre class="r"><code>dat.all$dep <- substr(dat.all$commune_residence, start = 1, stop = 2)
unique(dat.all$dep)
# Initialize region data
dat.all$region <- NA
# Get region from departement information
dat.all[base::is.element(dat.all$dep, c("13", "83", "84")), "region"] <- "PACA"
dat.all[base::is.element(dat.all$dep, c("75", "91", "92", "93", "94", "95")), "region"] <- "IDF"
dat.all[base::is.element(dat.all$dep, c("69")), "region"] <- "ARA"</code></pre>
<p>Remove NAs</p>
<pre class="r"><code>dat <- dat.all[which(!is.na(dat.all$effectif_cumu_1_inj) & !is.na(dat.all$effectif_cumu_termine) & !is.na(dat.all$population_carto)), ]
nrow(dat)</code></pre>
<p>Dictionary of age classes</p>
<pre class="r"><code>dic.ages <- as.character(unique(dat$classe_age))
names(dic.ages) <- unique(dat$libelle_classe_age)
dic.ages</code></pre>
</div>
<div id="plot-settings" class="section level2">
<h2>Plot settings</h2>
<pre class="r"><code>library(RColorBrewer)</code></pre>
<p>Region colors and pch</p>
<pre class="r"><code>colRegion <- brewer.pal(name = "Set2", n = 3)
names(colRegion) <- c("IDF", "ARA", "PACA")
pchRegion <- 16:18
names(pchRegion) <- names(colRegion)</code></pre>
<p>Age colors</p>
<pre class="r"><code>ages <- unique(dat$libelle_classe_age)
colAge <- brewer.pal(name = "Dark2", n = length(ages))
names(colAge) <- ages</code></pre>
</div>
</div>
<div id="model-and-plot" class="section level1">
<h1>Model and plot</h1>
<div id="models" class="section level2">
<h2>Models</h2>
<pre class="r"><code>mdl1D <- glm(formula = cbind(effectif_cumu_1_inj, population_carto - effectif_cumu_1_inj) ~ med14_2018 + classe_age + classe_age*med14_2018 + region, data = dat, family = binomial(link = "logit"))
mdl1D.noregion <- glm(formula = cbind(effectif_cumu_1_inj, population_carto - effectif_cumu_1_inj) ~ med14_2018 + classe_age, data = dat, family = binomial(link = "logit"))
mdlFin <- glm(formula = cbind(effectif_cumu_termine, population_carto - effectif_cumu_termine) ~ med14_2018 + classe_age + classe_age*med14_2018 + region, data = dat, family = binomial(link = "logit"))
mdlFin.noregion <- glm(formula = cbind(effectif_cumu_termine, population_carto - effectif_cumu_termine) ~ med14_2018 + classe_age, data = dat, family = binomial(link = "logit"))
summary(mdl1D)</code></pre>
<pre><code>##
## Call:
## glm(formula = cbind(effectif_cumu_1_inj, population_carto - effectif_cumu_1_inj) ~
## med14_2018 + classe_age + classe_age * med14_2018 + region,
## family = binomial(link = "logit"), data = dat)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -64.735 -3.479 0.140 2.902 63.076
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -4.446e+00 1.120e-02 -396.875 < 2e-16 ***
## med14_2018 6.666e-05 4.111e-07 162.152 < 2e-16 ***
## classe_age20-39 2.482e+00 1.226e-02 202.402 < 2e-16 ***
## classe_age40-54 3.216e+00 1.263e-02 254.720 < 2e-16 ***
## classe_age55-64 4.070e+00 1.377e-02 295.518 < 2e-16 ***
## classe_age65-74 4.582e+00 1.495e-02 306.457 < 2e-16 ***
## classe_age75 et + 4.746e+00 1.516e-02 313.040 < 2e-16 ***
## classe_ageTOUT_AGE 2.995e+00 1.145e-02 261.509 < 2e-16 ***
## regionIDF -5.041e-02 1.440e-03 -35.007 < 2e-16 ***
## regionPACA -2.180e-01 1.718e-03 -126.868 < 2e-16 ***
## med14_2018:classe_age20-39 5.386e-06 4.580e-07 11.759 < 2e-16 ***
## med14_2018:classe_age40-54 -3.588e-06 4.760e-07 -7.539 4.74e-14 ***
## med14_2018:classe_age55-64 -1.695e-05 5.281e-07 -32.100 < 2e-16 ***
## med14_2018:classe_age65-74 -2.413e-05 5.784e-07 -41.712 < 2e-16 ***
## med14_2018:classe_age75 et + -2.838e-05 5.805e-07 -48.883 < 2e-16 ***
## med14_2018:classe_ageTOUT_AGE -1.074e-05 4.236e-07 -25.354 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 3590876 on 2199 degrees of freedom
## Residual deviance: 152711 on 2184 degrees of freedom
## AIC: 170884
##
## Number of Fisher Scoring iterations: 4</code></pre>
<pre class="r"><code>summary(mdlFin)</code></pre>
<pre><code>##
## Call:
## glm(formula = cbind(effectif_cumu_termine, population_carto -
## effectif_cumu_termine) ~ med14_2018 + classe_age + classe_age *
## med14_2018 + region, family = binomial(link = "logit"), data = dat)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -47.096 -2.917 -0.117 2.439 53.165
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -6.143e+00 2.322e-02 -264.549 < 2e-16 ***
## med14_2018 6.751e-05 8.416e-07 80.216 < 2e-16 ***
## classe_age20-39 3.345e+00 2.397e-02 139.539 < 2e-16 ***
## classe_age40-54 4.101e+00 2.397e-02 171.083 < 2e-16 ***
## classe_age55-64 5.088e+00 2.434e-02 209.026 < 2e-16 ***
## classe_age65-74 5.619e+00 2.489e-02 225.748 < 2e-16 ***
## classe_age75 et + 5.922e+00 2.508e-02 236.161 < 2e-16 ***
## classe_ageTOUT_AGE 4.107e+00 2.335e-02 175.907 < 2e-16 ***
## regionIDF -5.207e-03 1.587e-03 -3.281 0.00103 **
## regionPACA 7.167e-02 1.879e-03 38.136 < 2e-16 ***
## med14_2018:classe_age20-39 -1.440e-05 8.724e-07 -16.507 < 2e-16 ***
## med14_2018:classe_age40-54 -1.621e-05 8.738e-07 -18.556 < 2e-16 ***
## med14_2018:classe_age55-64 -2.275e-05 8.918e-07 -25.514 < 2e-16 ***
## med14_2018:classe_age65-74 -2.088e-05 9.173e-07 -22.760 < 2e-16 ***
## med14_2018:classe_age75 et + -2.455e-05 9.228e-07 -26.609 < 2e-16 ***
## med14_2018:classe_ageTOUT_AGE -1.972e-05 8.480e-07 -23.250 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 3297582 on 2199 degrees of freedom
## Residual deviance: 90062 on 2184 degrees of freedom
## AIC: 107746
##
## Number of Fisher Scoring iterations: 4</code></pre>
<pre class="r"><code># Fit
newdata <- expand.grid(med14_2018 = seq(min(dat$med14_2018), max(dat$med14_2018), length.out = 100), classe_age = sort(unique(dat$classe_age)), region = unique(dat$region))
nrow(newdata)
newdata.noregion <- expand.grid(med14_2018 = seq(min(dat$med14_2018), max(dat$med14_2018), length.out = 100), classe_age = sort(unique(dat$classe_age)))
ndt <- newdata
ndt$prd1D <- predict(mdl1D, newdata = newdata, type = "response")
ndt$prdFin <- predict(mdlFin, newdata = newdata, type = "response")
ndt.noregion <- newdata.noregion
ndt.noregion$prd1D <- predict(mdl1D.noregion, newdata = newdata.noregion, type = "response")
ndt.noregion$prdFin <- predict(mdlFin.noregion, newdata = newdata.noregion, type = "response")</code></pre>
</div>
<div id="plot" class="section level2">
<h2>Plot</h2>
<pre class="r"><code>marWithDetails <- c(6, 4.5, 3, 8)
textDetails <- "Figure @flodebarre, données vaccination trouvées par @humeursdevictor
Code: https://github.com/flodebarre/covid_vaccination/blob/main/communes.Rmd
Données vaccination : https://datavaccin-covid.ameli.fr/explore/dataset/donnees-de-vaccination-par-commune
Données revenus : https://www.insee.fr/fr/statistiques/5009236?sommaire=5009255#consulter"</code></pre>
<pre class="r"><code>cexA <- 0.7 # Relative size of the tick labels
thetck <- -0.015 # tick size
themgp <- c(1.8, 0.4, 0) # mgp
legx <- 1.08*max(dat$med14_2018) # x position of the legend
legy <- 0.5
cexleg <- 0.7
axpos <- 0.99*min(dat$med14_2018) # xposition of the left axis</code></pre>
<div id="tous-ages" class="section level3">
<h3>Tous ages</h3>
<pre class="r"><code>cla <- "Tout âge"
par(mar = marWithDetails, las = 1, mgp = themgp, tck = thetck)
subdat <- dat[which(dat$libelle_classe_age == cla), ]
# initialize plot
plot(subdat$med14_2018, subdat$taux_cumu_1_inj,
ylim = c(0, 1),
xlab = "Revenu médian dans la commune (2018)", ylab = "Proportion de la population
ayant reçu au moins une injection",
yaxs = "i",
frame.plot = FALSE, axes = FALSE,
type = "n")
axis(1, cex.axis = cexA)
axis(2, cex.axis = cexA)
axis(4, cex.axis = cexA)
title(main = "Au moins une injection, tous âges")
mtext(textDetails, side = 1, line = 4.5, cex = 0.6, adj = 0, col = gray(0.6))
par(xpd = FALSE)
for(i in seq(0, 1, by = 0.1)){
abline(h = i, lwd = 0.5, col = gray(0.8))
}
points(subdat$med14_2018, subdat$taux_cumu_1_inj,
col = adjustcolor(colRegion[subdat$region], alpha.f = 0.5),
pch = pchRegion[subdat$region],
)
par(xpd = TRUE)
legend(x = legx, y = legy, legend = names(colRegion), col = colRegion, pch = pchRegion[names(colRegion)], bty = "n", title = "Regions", xjust = 0, yjust = 0.5, cex = cexleg, pt.cex = 1)
# Add legends
x1 <- max(dat$med14_2018)
d1 <- dat[which(dat$med14_2018 == x1), ]
x2 <- min(dat$med14_2018)
d2 <- dat[which(dat$med14_2018 == x2), ]
d3 <- dat[which(dat$med14_2018 >36000 & dat$med14_2018 < 36500), ]
x3 <- unique(d3$med14_2018)
dy <- 0.01
text(x = c(x1, x2, x3),
y = c(as.numeric(d1[d1$classe_age == "TOUT_AGE", "taux_cumu_1_inj"]), as.numeric(d2[d2$classe_age == "TOUT_AGE", "taux_cumu_1_inj"]), as.numeric(d3[d3$classe_age == "TOUT_AGE", "taux_cumu_1_inj"])) + dy,
labels = c(d1[1, "libelle_commune.x"], d2[1, "libelle_commune.x"], d3[1, "libelle_commune.x"]),
adj = c(0, 0.5), srt = 90, cex = 0.5)</code></pre>
<p><img src="communes_files/figure-html/unnamed-chunk-17-1.png" width="672" /></p>
<pre class="r"><code>par(xpd = FALSE) </code></pre>
</div>
<div id="au-moins-une-injection" class="section level3">
<h3>Au moins une injection</h3>
<p>(Pour faire les choses proprement il faudrait une boucle)</p>
<pre class="r"><code>par(mar = marWithDetails, las = 1, mgp = themgp, tck = thetck)
alf <- 0.4
lwd.pred <- 1.5
# Remove all ages
tmp <- dat[which(dat$classe_age != "TOUT_AGE"),]
plot(tmp$med14_2018, tmp$taux_cumu_1_inj,
xlab = "Revenu médian dans la commune (2018)", ylab = "Proportion de la classe d'âge
ayant reçu au moins une injection",
yaxs = "i", frame.plot = FALSE, ylim = c(0, 1),
type = "n", axes = FALSE
)
axis(1, cex.axis = cexA)
axis(2, cex.axis = cexA)
axis(4, cex.axis = cexA)
title(main = "Au moins une injection, par classe d'âge")
par(xpd = FALSE)
for(i in seq(0, 1, by = 0.1)){
abline(h = i, lwd = 0.5, col = gray(0.8))
}
# Legend communes
dx <- 400
dy <- 0.025
cexleg <- 0.6
colRec <- gray(0.925)
# max
xmax <- max(dat$med14_2018)
datmax <- dat[which(dat$med14_2018 == xmax), ]
rect(xleft = xmax - dx, xright = xmax + dx,
ybottom = as.numeric(min(datmax$taux_cumu_1_inj)) - dy, ytop = as.numeric(max(datmax$taux_cumu_1_inj)) + dy, col = colRec, border = gray(0, 0))
text(x = xmax - dx, y = (as.numeric(max(datmax$taux_cumu_1_inj)) + as.numeric(min(datmax$taux_cumu_1_inj)) )/2, labels = datmax[1, "libelle_commune.x"], cex = cexleg, adj = c(0.5, 0), srt = 90)
# min
xmin <- min(dat$med14_2018)
datmin <- dat[which(dat$med14_2018 == xmin), ]
rect(xleft = xmin - dx, xright = xmin + dx,
ybottom = as.numeric(min(datmin$taux_cumu_1_inj)) - dy, ytop = as.numeric(max(datmin$taux_cumu_1_inj)) + dy, col = colRec, border = gray(0, 0))
text(x = xmin - dx, y = (as.numeric(max(datmin$taux_cumu_1_inj)) + as.numeric(min(datmin$taux_cumu_1_inj)) )/2, labels = datmin[1, "libelle_commune.x"], cex = cexleg, adj = c(0.5, 0), srt = 90)
# Intermediate
dat3 <- dat[dat$med14_2018 == x3,]
rect(xleft = x3 - dx, xright = x3 + dx,
ybottom = as.numeric(min(dat3$taux_cumu_1_inj)) - dy, ytop = as.numeric(max(dat3$taux_cumu_1_inj)) + dy, col = colRec, border = gray(0, 0))
text(x = x3 - dx, y = (as.numeric(max(dat3$taux_cumu_1_inj)) + as.numeric(min(dat3$taux_cumu_1_inj)) )/2, labels = dat3[1, "libelle_commune.x"], cex = cexleg, adj = c(0.5, 0), srt = 90)
mtext(1, line = 4.5, text = textDetails, cex = 0.6, adj = 0, col = gray(0.6))
par(xpd = TRUE)
points(tmp$med14_2018, tmp$taux_cumu_1_inj,
col = adjustcolor(colAge[tmp$libelle_classe_age], alpha.f = alf),
pch = pchRegion[tmp$region])
# Get age classes and sort them by proportion vaccinated for the legend
mns <- aggregate(as.numeric(dat$taux_cumu_termine), by = list(dat$libelle_classe_age), FUN = mean, na.rm = TRUE)
mns2 <- mns[sort(mns$x, index.return = TRUE, decreasing = TRUE)$ix,]
mns2 <- mns2[mns2$Group.1 != "Tout âge", ]
# Legend
legend(x = legx, y = legx,
legend = c(mns2$Group.1, names(pchRegion)),
col = c(colAge[mns2$Group.1], rep(gray(0.5), 3)),
pch = c(rep(NA, nrow(mns2)), pchRegion),
lwd = c(rep(lwd.pred, nrow(mns2)), rep(0, length(pchRegion))),
bty = "n", cex = cexleg, pt.cex = 1, xjust = 0, yjust = 0.5)
for(age in ages[ages!="Tout âge"]){
agcl <- dic.ages[age] # Get corresponding age class
# Get predicted data for this age class (no region)
subd <- ndt.noregion[ndt.noregion$classe_age == agcl,]
lines(subd$med14_2018, subd$prd1D, col = colAge[age], lwd = lwd.pred)
}</code></pre>
<p><img src="communes_files/figure-html/unnamed-chunk-18-1.png" width="672" /></p>
</div>
<div id="vaccination-terminée" class="section level3">
<h3>Vaccination terminée</h3>
<pre class="r"><code>par(mar = marWithDetails, las = 1, mgp = themgp, tck = thetck)
plot(tmp$med14_2018, tmp$taux_cumu_termine,
xlab = "Revenu médian dans la commune (2018)", ylab = "Proportion de la classe d'âge
dont la vaccination est terminée",
yaxs = "i", frame.plot = FALSE, ylim = c(0, 1),
type = "n", axes = FALSE
)
axis(1, cex.axis = cexA)
axis(2, cex.axis = cexA)
axis(4, cex.axis = cexA)
par(xpd = FALSE)
title(main = "Vaccination terminée, par classe d'âge")
for(i in seq(0, 1, by = 0.1)){
abline(h = i, lwd = 0.5, col = gray(0.8))
}
# Legend communes
dx <- 400
dy <- 0.025
cexleg <- 0.6
colRec <- gray(0.925)
# max
xmax <- max(dat$med14_2018)
datmax <- dat[which(dat$med14_2018 == xmax), ]
rect(xleft = xmax - dx, xright = xmax + dx,
ybottom = as.numeric(min(datmax$taux_cumu_termine)) - dy, ytop = as.numeric(max(datmax$taux_cumu_termine)) + dy, col = colRec, border = gray(0, 0))
text(x = xmax - dx, y = (as.numeric(max(datmax$taux_cumu_termine)) + as.numeric(min(datmax$taux_cumu_termine)) )/2, labels = datmax[1, "libelle_commune.x"], cex = cexleg, adj = c(0.5, 0), srt = 90)
# min
xmin <- min(dat$med14_2018)
datmin <- dat[which(dat$med14_2018 == xmin), ]
rect(xleft = xmin - dx, xright = xmin + dx,
ybottom = as.numeric(min(datmin$taux_cumu_termine)) - dy, ytop = as.numeric(max(datmin$taux_cumu_termine)) + dy, col = colRec, border = gray(0, 0))
text(x = xmin - dx, y = (as.numeric(max(datmin$taux_cumu_termine)) + as.numeric(min(datmin$taux_cumu_termine)) )/2, labels = datmin[1, "libelle_commune.x"], cex = cexleg, adj = c(0.5, 0), srt = 90)
# Intermediate
dat3 <- dat[dat$med14_2018 == x3,]
rect(xleft = x3 - dx, xright = x3 + dx,
ybottom = as.numeric(min(dat3$taux_cumu_termine)) - dy, ytop = as.numeric(max(dat3$taux_cumu_termine)) + dy, col = colRec, border = gray(0, 0))
text(x = x3 - dx, y = (as.numeric(max(dat3$taux_cumu_termine)) + as.numeric(min(dat3$taux_cumu_termine)) )/2, labels = dat3[1, "libelle_commune.x"], cex = cexleg, adj = c(0.5, 0), srt = 90)
mtext(1, line = 4.5, text = textDetails, cex = 0.6, adj = 0, col = gray(0.6))
par(xpd = TRUE)
points(tmp$med14_2018, tmp$taux_cumu_termine,
col = adjustcolor(colAge[tmp$libelle_classe_age], alpha.f = alf),
pch = pchRegion[tmp$region])
# Get age classes and sort them by proportion vaccinated for the legend
mns <- aggregate(as.numeric(dat$taux_cumu_termine), by = list(dat$libelle_classe_age), FUN = mean, na.rm = TRUE)
mns2 <- mns[sort(mns$x, index.return = TRUE, decreasing = TRUE)$ix,]
mns2 <- mns2[mns2$Group.1 != "Tout âge", ]
legend(x = 1.08*max(tmp$med14_2018), y = 0.5,
legend = c(mns2$Group.1, names(pchRegion)),
col = c(colAge[mns2$Group.1], rep(gray(0.5), 3)),
pch = c(rep(NA, nrow(mns2)), pchRegion),
lwd = c(rep(lwd.pred, nrow(mns2)), rep(0, length(pchRegion))),
bty = "n", cex = cexleg, pt.cex = 1, xjust = 0, yjust = 0.5)
for(age in ages[ages!="Tout âge"]){
agcl <- dic.ages[age] # Get corresponding age class
# Get predicted data for this age class (no region)
subd <- ndt.noregion[ndt.noregion$classe_age == agcl,]
lines(subd$med14_2018, subd$prdFin, col = colAge[age], lwd = lwd.pred)
}</code></pre>
<p><img src="communes_files/figure-html/unnamed-chunk-19-1.png" width="672" /></p>
</div>
</div>
</div>
<div id="all-times" class="section level1">
<h1>All times</h1>
<pre class="r"><code># Extract departement
dat.vaccin$dep <- substr(dat.vaccin$commune_residence, start = 1, stop = 2)
# Add region
dat.vaccin$region <- NA
dat.vaccin[base::is.element(dat.vaccin$dep, c("13", "83", "84")), "region"] <- "PACA"
dat.vaccin[base::is.element(dat.vaccin$dep, c("75", "91", "92", "93", "94", "95")), "region"] <- "IDF"
dat.vaccin[base::is.element(dat.vaccin$dep, c("69")), "region"] <- "ARA"
dat.all.time <- merge(dat.niveauVie, dat.vaccin, by = "commune_residence")
nrow(dat.all.time)
dat.all.time$tx_cum_1d <- as.numeric(dat.all.time$taux_cumu_1_inj)
dat.all.time$tx_cum_fin <- as.numeric(dat.all.time$taux_cumu_termine)
# plot(as.Date(dat.all.time$date), dat.all.time$taux_cumu_1_inj)
# plot(as.Date(dat.all.time$date), dat.all.time$taux_cumu_termine)
dat.all.time$time <- as.numeric(as.Date(dat.all.time$date) - as.Date(min(dat.all.time$date)))
is.numeric(dat.all.time$time)
is.numeric(dat.all.time$taux_cumu_1_inj)
unique(dat.all.time$taux_cumu_1_inj)
dat$classe_age <- as.factor(dat$classe_age)
dat$region <- as.factor(dat$region)
mdl1D <- glm(formula = cbind(effectif_cumu_1_inj, population_carto - effectif_cumu_1_inj) ~ time + med14_2018 + classe_age + med14_2018*time + classe_age*time + region + region*time, data = dat, family = binomial(link = "logit"))
mdl1D.2 <- glm(formula = cbind(effectif_cumu_1_inj, population_carto - effectif_cumu_1_inj) ~ time + med14_2018 + classe_age + med14_2018*time + classe_age*time, data = dat, family = binomial(link = "logit"))
mdlFin <- glm(formula = cbind(effectif_cumu_termine, population_carto - effectif_cumu_termine) ~ time + med14_2018 + classe_age + med14_2018*time + classe_age*time + region + region*time, data = dat, family = binomial(link = "logit"))
mdlFin.2 <- glm(formula = cbind(effectif_cumu_termine, population_carto - effectif_cumu_termine) ~ time + med14_2018 + classe_age + med14_2018*time + classe_age*time, data = dat, family = binomial(link = "logit"))
summary(mdl1D)
summary(mdlFin)
length(predict(mdl))
prd <- predict(mdl1D, se.fit = TRUE)
dat$pred1D <- prd$fit
dat$se1D <- prd$se.fit
lgt <- function(x){ exp(x) / (1 + exp(x)) }
dat$pred1D.response <- lgt(dat$pred1D)
dat$pred1D.response.se.upper <- lgt(dat$pred1D + 1.96 * dat$se1D)
dat$pred1D.response.se.lower <- lgt(dat$pred1D - 1.96 * dat$se1D)
#with(preddat, lines(0:1000, exp(fit)/(1+exp(fit)), col="blue"))
#with(preddat, lines(0:1000, exp(fit+1.96*se.fit)/(1+exp(fit+1.96*se.fit)), lty=2))
#with(preddat, lines(0:1000, exp(fit-1.96*se.fit)/(1+exp(fit-1.96*se.fit)), lty=2))
newdata <- expand.grid(med14_2018 = seq(min(dat$med14_2018), max(dat$med14_2018), length.out = 100), classe_age = sort(unique(dat$classe_age)), time = seq(min(dat$time), max(dat$time), length.out = 11))
nrow(newdata)
prd1D <- predict(mdl1D.2, newdata = newdata, type = "response")
prdFin <- predict(mdlFin.2, newdata = newdata, type = "response")
ifinal <- which(newdata$time == max(newdata$time))
ndt <- newdata[ifinal, ]
ndt$pred1D <- prd1D[ifinal]
ndt$predFin <- prdFin[ifinal]
unique(dat$classe_age)
unique(dat$libelle_classe_age)</code></pre>
<pre class="r"><code>dat.final <- dat[which(dat$time == max(dat$time)),]
tmp <- dat.final[which(dat.final$classe_age != "TOUT_AGE"),]
plot(tmp$med14_2018, tmp$taux_cumu_1_inj)
mns <- aggregate(as.numeric(dat.final$taux_cumu_termine), by = list(dat.final$libelle_classe_age), FUN = mean, na.rm = TRUE)
mns2 <- mns[sort(mns$x, index.return = TRUE, decreasing = TRUE)$ix,]
mns2 <- mns2[mns2$Group.1 != "Tout âge", ]
par(mar = c(4, 3, 2, 6), las = 1)
alf <- 0.5
plot(tmp$med14_2018, tmp$taux_cumu_1_inj, col = adjustcolor(colAge[tmp$libelle_classe_age], alpha.f = alf),
xlab = xlb, ylab = "Proportion vaccination terminée", pch = 16, yaxs = "i", frame.plot = FALSE, ylim = c(0, 1))
par(xpd = TRUE)
legend(x = 1.03*max(tmp$med14_2018), y = 1, legend = mns2$Group.1, col = adjustcolor(colAge[mns2$Group.1], alpha.f = alf), pch = 16, bty = "n", cex = 0.7, pt.cex = 1, xjust = 0)
for(age in ages[ages!="Tout âge"]){
agcl <- dic.ages[age]
subd <- ndt[ndt$classe_age == agcl,]
lines(subd$med14_2018, subd$pred1D, col = colAge[age], lwd = 2)
}
tmp[1,]
prd1D</code></pre>
<pre class="r"><code>lang <- "EN"
if(lang == "EN"){
xlb <- "Median income"
ylb <- "Proportion 1st dose"
tt <- ""
detailsFig <- "Code: https://github.com/flodebarre/covid_vaccination/blob/main/communes.Rmd
Vaccination data: https://datavaccin-covid.ameli.fr/explore/dataset/donnees-de-vaccination-par-commune
Income data: https://www.data.gouv.fr/fr/datasets/niveau-de-vie-des-francais-par-commune/"
}else{
xlb <- "Revenu median"
ylb <- "Proportion 1ere dose"
tt <- ""
detailsFig <- "Code: https://github.com/flodebarre/covid_vaccination/blob/main/communes.Rmd
Données vaccination : https://datavaccin-covid.ameli.fr/explore/dataset/donnees-de-vaccination-par-commune
Données revenus : https://www.data.gouv.fr/fr/datasets/niveau-de-vie-des-francais-par-commune/"
}</code></pre>
<pre class="r"><code>par(mar = c(5, 4, 3, 3), las = 1, mgp = c(1.75, 0.4, 0), tck = -0.02)
for(cla in unique(dat.all$libelle_classe_age)){
subdat <- dat.all[which(dat.all$libelle_classe_age == cla), ]
plot(subdat$med14_2018, subdat$taux_cumu_1_inj,
ylim = c(0, 1),
xlab = "Revenu med14_2018", ylab = "Proportion 1ere injection",
yaxs = "i", pch = 16, col = adjustcolor(colRegion[subdat$region], alpha.f = 0.5), frame.plot = FALSE)
axis(4)
title(main = cla)
mtext("Code: https://github.com/flodebarre/covid_vaccination/blob/main/communes.Rmd
Données vaccination : https://datavaccin-covid.ameli.fr/explore/dataset/donnees-de-vaccination-par-commune
Données revenus : https://www.data.gouv.fr/fr/datasets/niveau-de-vie-des-francais-par-commune/", side = 1, line = 4, cex = 0.6, adj = 0, col = gray(0.4))
}</code></pre>
</div>
<div id="plotly-interactive-figures" class="section level1">
<h1>Plotly – interactive figures</h1>
<pre class="r"><code>#install.packages("plotly")
library(plotly)</code></pre>
<pre><code>## Loading required package: ggplot2</code></pre>
<pre><code>##
## Attaching package: 'plotly'</code></pre>
<pre><code>## The following object is masked from 'package:ggplot2':
##
## last_plot</code></pre>
<pre><code>## The following object is masked from 'package:stats':
##
## filter</code></pre>
<pre><code>## The following object is masked from 'package:graphics':
##
## layout</code></pre>
<p>Comparaison revenus 2014 et 2018</p>
<pre class="r"><code>dat.all$med14_relDiff <- (dat.all$med14_2018 - dat.all$med14_2014) / dat.all$med14_2014
hist(dat.all$med14_relDiff, xlab = "
Relative difference between
2018 and 2014 median income by commune", main = "income evolution")</code></pre>
<p><img src="communes_files/figure-html/unnamed-chunk-25-1.png" width="672" /></p>
<pre class="r"><code>plot_ly(dat.all, x = ~med14_2014, y = ~med14_relDiff, text = ~paste(commune_residence, libelle_commune.x), type = "scatter", mode = "markers")</code></pre>
<pre class="r"><code>cla <- "Tout âge"
subdat <- dat.all[which(dat.all$libelle_classe_age == cla), ]
fig <- plot_ly(subdat, x = ~med14_2018, y = ~as.numeric(taux_cumu_1_inj), type = 'scatter', mode = 'markers',
color = ~region,
symbol = ~region,
hoverinfo = 'text',
text = ~paste(commune_residence, libelle_commune.x)) %>% layout(yaxis = list(title = "Proportion 1ere injection"), xaxis = list(title = "Revenu median dans la commune (2018)"))
fig
fig2 <- fig %>% layout(yaxis = list(range = c(0, 0.75), title = "Proportion 1ere injection"))
fig2</code></pre>
<pre class="r"><code># Vaccination terminee
fig <- plot_ly(subdat, x = ~med14_2018, y = ~as.numeric(taux_cumu_termine), type = 'scatter', mode = 'markers',
color = ~region,
symbol = ~region,
hoverinfo = 'text',
text = ~paste(commune_residence, libelle_commune.x)) %>% layout(yaxis = list(title = "Proportion vaccination terminée"), xaxis = list(title = "Revenu median dans la commune (2018)"))
fig
fig2 <- fig %>% layout(yaxis = list(range = c(0, 0.75)))
fig2</code></pre>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
window.initializeCodeFolding("hide" === "show");
});
</script>
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>