-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.html
More file actions
1344 lines (1270 loc) · 91.5 KB
/
Copy pathanalysis.html
File metadata and controls
1344 lines (1270 loc) · 91.5 KB
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="quarto-1.8.25">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>regPoisson_ozone</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
html { -webkit-text-size-adjust: 100%; }
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="regPoisson_ozone_files/libs/clipboard/clipboard.min.js"></script>
<script src="regPoisson_ozone_files/libs/quarto-html/quarto.js" type="module"></script>
<script src="regPoisson_ozone_files/libs/quarto-html/tabsets/tabsets.js" type="module"></script>
<script src="regPoisson_ozone_files/libs/quarto-html/axe/axe-check.js" type="module"></script>
<script src="regPoisson_ozone_files/libs/quarto-html/popper.min.js"></script>
<script src="regPoisson_ozone_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="regPoisson_ozone_files/libs/quarto-html/anchor.min.js"></script>
<link href="regPoisson_ozone_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="regPoisson_ozone_files/libs/quarto-html/quarto-syntax-highlighting-7b89279ff1a6dce999919e0e67d4d9ec.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="regPoisson_ozone_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="regPoisson_ozone_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="regPoisson_ozone_files/libs/bootstrap/bootstrap-9e3ffae467580fdb927a41352e75a2e0.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<script type="text/javascript">
const typesetMath = (el) => {
if (window.MathJax) {
// MathJax Typeset
window.MathJax.typeset([el]);
} else if (window.katex) {
// KaTeX Render
var mathElements = el.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN" && texText && texText.data) {
window.katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}
}
}
}
window.Quarto = {
typesetMath
};
</script>
</head>
<body class="quarto-light">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article toc-left">
<div id="quarto-sidebar-toc-left" class="sidebar toc-left">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#régression-de-poisson-prédiction-de-lozone" id="toc-régression-de-poisson-prédiction-de-lozone" class="nav-link active" data-scroll-target="#régression-de-poisson-prédiction-de-lozone">Régression de Poisson : prédiction de l’ozone</a>
<ul class="collapse">
<li><a href="#introduction" id="toc-introduction" class="nav-link" data-scroll-target="#introduction">Introduction</a>
<ul class="collapse">
<li><a href="#contexte-et-justification" id="toc-contexte-et-justification" class="nav-link" data-scroll-target="#contexte-et-justification">Contexte et justification</a></li>
<li><a href="#choix-des-données" id="toc-choix-des-données" class="nav-link" data-scroll-target="#choix-des-données">Choix des données</a></li>
</ul></li>
<li><a href="#modélisation" id="toc-modélisation" class="nav-link" data-scroll-target="#modélisation">Modélisation</a>
<ul class="collapse">
<li><a href="#traitement-des-données-et-analyse-exploratoire" id="toc-traitement-des-données-et-analyse-exploratoire" class="nav-link" data-scroll-target="#traitement-des-données-et-analyse-exploratoire">Traitement des données et Analyse exploratoire</a></li>
<li><a href="#modèle-de-poisson" id="toc-modèle-de-poisson" class="nav-link" data-scroll-target="#modèle-de-poisson">Modèle de Poisson</a></li>
<li><a href="#estimation-des-coefficients-significativité-globale-et-individuelle-et-interprétation" id="toc-estimation-des-coefficients-significativité-globale-et-individuelle-et-interprétation" class="nav-link" data-scroll-target="#estimation-des-coefficients-significativité-globale-et-individuelle-et-interprétation">Estimation des coefficients, significativité globale et individuelle et interprétation</a></li>
<li><a href="#validation-du-modèle" id="toc-validation-du-modèle" class="nav-link" data-scroll-target="#validation-du-modèle">Validation du modèle</a></li>
<li><a href="#conclusion" id="toc-conclusion" class="nav-link" data-scroll-target="#conclusion">Conclusion</a></li>
</ul></li>
</ul></li>
</ul>
</nav>
</div>
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar zindex-bottom">
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">regPoisson_ozone</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<section id="régression-de-poisson-prédiction-de-lozone" class="level1">
<h1>Régression de Poisson : prédiction de l’ozone</h1>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<section id="contexte-et-justification" class="level3">
<h3 class="anchored" data-anchor-id="contexte-et-justification">Contexte et justification</h3>
<p>Le réchauffement climatique est aujourd’hui l’un des plus grands problèmes auxquels fait face la planète Terre, et est la juste conséquence de la mauvaise exploitation des ressources par l’homme. Et l’un des effets les plus critiques est la destruction de la couche d’ozone responsable de l’effet de serre. Pour lutter contre un tel phénomène, il est important de comprendre l’évolution des différentes entités environnementales en jeu, et l’ozone en est un parfait exemple. Comprendre comment modéliser les transformations qu’elle subit en fonction de plusieurs facteurs est impératif si l’on veut prendre des mesures radicales et avisées. Ce travail consistera donc à modéliser le niveau de l’ozone, en se basant sur le dataset <code>airquality</code> qui mesure la qualité de l’air à New York, de façon journalière, de mai à septempbre 1973.</p>
</section>
<section id="choix-des-données" class="level3">
<h3 class="anchored" data-anchor-id="choix-des-données">Choix des données</h3>
<p>Le niveau d’ozone est en général mesuré en <code>ppb</code> ou <em>parts per billion</em> et est une unité de mesure de concentration représentant une partie de soluté pour un milliard <span class="math inline">\(10^9\)</span> de parties de solution. Utilisé pour de très faibles concentrations (contaminants, air, eau), <span class="math inline">\(1 \text{ ppb} = 1 \mu g/L \text{ ou } 1 \mu g / kg\)</span>. Par définition donc, c’est une valeur qui doit être <strong>strictement non-négative</strong>. De plus, les analyseurs d’ozone (photométrie UV) ont en général une résolution de <code>1 pb</code>, ce qui en fait des mesures toutes en <strong>nombres entiers</strong>. Ces conditions sont toutes respectées dans le dataset <code>airquality</code> et constituent les raisons pour lesquelles nous avons choisi ces données.</p>
<hr>
</section>
</section>
<section id="modélisation" class="level2">
<h2 class="anchored" data-anchor-id="modélisation">Modélisation</h2>
<section id="traitement-des-données-et-analyse-exploratoire" class="level3">
<h3 class="anchored" data-anchor-id="traitement-des-données-et-analyse-exploratoire">Traitement des données et Analyse exploratoire</h3>
<p>Chargeons tous les packages que nous aurons à utiliser dans cette étude.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">#install.packages("ggplot2")</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co">#install.packages("gridExtra")</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gridExtra)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co">#install.packages("caTools") </span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(caTools) <span class="co"># pour la séparation de dataset en parties train et test</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co">#install.packages("MASS") </span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(MASS) <span class="co"># pour le modèle binomial négatif</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="co">#install.packages("ResourceSelection") </span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ResourceSelection) <span class="co"># pour le test de Hosmer-Lemeshow</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stderr">
<pre><code>ResourceSelection 0.3-6 2023-06-27</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co">#install.packages("performance") </span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(performance) <span class="co"># pour la performance</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
<p>Chargement des données et exploration.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(<span class="st">"airquality"</span>)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="co">#?airquality</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(airquality)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame': 153 obs. of 6 variables:
$ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
$ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
$ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
$ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
$ Month : int 5 5 5 5 5 5 5 5 5 5 ...
$ Day : int 1 2 3 4 5 6 7 8 9 10 ...</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(airquality)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> Ozone Solar.R Wind Temp
Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00
1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00
Median : 31.50 Median :205.0 Median : 9.700 Median :79.00
Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88
3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00
Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00
NA's :37 NA's :7
Month Day
Min. :5.000 Min. : 1.0
1st Qu.:6.000 1st Qu.: 8.0
Median :7.000 Median :16.0
Mean :6.993 Mean :15.8
3rd Qu.:8.000 3rd Qu.:23.0
Max. :9.000 Max. :31.0
</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(airquality)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6</code></pre>
</div>
</div>
<p>Nous allons séparer le dataset en parties d’entraînement et de test, grâce au package <code>SPlit</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>data_split <span class="ot"><-</span> <span class="fu">sample.split</span>(airquality, <span class="at">SplitRatio =</span> <span class="fl">0.7</span>)</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="co"># on ne sélectionnera que les colonnes qui nous intéressent</span></span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>train <span class="ot"><-</span> <span class="fu">subset</span>(airquality, data_split <span class="sc">==</span> <span class="cn">TRUE</span>, <span class="at">select =</span> <span class="fu">c</span>(<span class="st">"Ozone"</span>, <span class="st">"Solar.R"</span>, <span class="st">"Wind"</span>, <span class="st">"Temp"</span>))</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a>test <span class="ot"><-</span> <span class="fu">subset</span>(airquality, data_split <span class="sc">==</span> <span class="cn">FALSE</span>, <span class="at">select =</span> <span class="fu">c</span>(<span class="st">"Ozone"</span>, <span class="st">"Solar.R"</span>, <span class="st">"Wind"</span>, <span class="st">"Temp"</span>))</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(train)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame': 101 obs. of 4 variables:
$ Ozone : int 41 18 NA 28 23 NA 7 16 11 14 ...
$ Solar.R: int 190 313 NA NA 299 194 NA 256 290 334 ...
$ Wind : num 7.4 11.5 14.3 14.9 8.6 8.6 6.9 9.7 9.2 11.5 ...
$ Temp : int 67 62 56 66 65 69 74 69 66 64 ...</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(test)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame': 52 obs. of 4 variables:
$ Ozone : int 36 12 19 8 14 18 11 1 NA NA ...
$ Solar.R: int 118 149 99 19 274 65 44 8 266 NA ...
$ Wind : num 8 12.6 13.8 20.1 10.9 13.2 9.7 9.7 14.9 8 ...
$ Temp : int 72 74 59 61 68 58 62 59 58 57 ...</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(train)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> Ozone Solar.R Wind Temp
Min. : 4.0 Min. : 7.0 Min. : 1.70 Min. :56.0
1st Qu.: 16.0 1st Qu.:109.2 1st Qu.: 7.40 1st Qu.:72.0
Median : 32.0 Median :210.0 Median : 9.70 Median :79.0
Mean : 40.9 Mean :188.8 Mean :10.01 Mean :77.9
3rd Qu.: 64.0 3rd Qu.:259.0 3rd Qu.:12.00 3rd Qu.:84.0
Max. :118.0 Max. :334.0 Max. :20.70 Max. :97.0
NA's :28 NA's :5 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(test)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> Ozone Solar.R Wind Temp
Min. : 1.00 Min. : 8.0 Min. : 3.400 Min. :57.00
1st Qu.: 18.00 1st Qu.:118.5 1st Qu.: 7.850 1st Qu.:73.75
Median : 31.00 Median :190.0 Median : 9.700 Median :80.50
Mean : 44.21 Mean :180.3 Mean : 9.852 Mean :77.85
3rd Qu.: 61.00 3rd Qu.:254.8 3rd Qu.:11.500 3rd Qu.:85.00
Max. :168.00 Max. :332.0 Max. :20.100 Max. :96.00
NA's :9 NA's :2 </code></pre>
</div>
</div>
<p>Notons que ces données contiennent toujours des valleurs manquantes, nous allons donc les enlever.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>trainCleaned <span class="ot"><-</span> <span class="fu">na.omit</span>(train)</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>testCleaned <span class="ot"><-</span> <span class="fu">na.omit</span>(test)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(trainCleaned)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame': 69 obs. of 4 variables:
$ Ozone : int 41 18 23 16 11 14 34 6 30 11 ...
$ Solar.R: int 190 313 299 256 290 334 307 78 322 320 ...
$ Wind : num 7.4 11.5 8.6 9.7 9.2 11.5 12 18.4 11.5 16.6 ...
$ Temp : int 67 62 65 69 66 64 66 57 68 73 ...
- attr(*, "na.action")= 'omit' Named int [1:32] 3 4 6 7 17 22 23 24 25 28 ...
..- attr(*, "names")= chr [1:32] "5" "6" "10" "11" ...</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cor</span>(trainCleaned)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> Ozone Solar.R Wind Temp
Ozone 1.0000000 0.23165444 -0.57777021 0.7551062
Solar.R 0.2316544 1.00000000 -0.05746952 0.1833149
Wind -0.5777702 -0.05746952 1.00000000 -0.4734974
Temp 0.7551062 0.18331495 -0.47349744 1.0000000</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">var</span>(trainCleaned<span class="sc">$</span>Ozone)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 909.5269</code></pre>
</div>
</div>
<p>Visualisations des relations</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>(trainCleaned) <span class="sc">+</span> </span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">x =</span> Solar.R, <span class="at">y =</span> Ozone)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Ozone VS Solar.R"</span>)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>(trainCleaned) <span class="sc">+</span> </span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">x =</span> Wind, <span class="at">y =</span> Ozone)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Ozone VS Wind"</span>)</span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a>p3 <span class="ot"><-</span> <span class="fu">ggplot</span>(trainCleaned) <span class="sc">+</span> </span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="fu">aes</span>(<span class="at">x =</span> Temp, <span class="at">y =</span> Ozone)) <span class="sc">+</span> <span class="fu">ggtitle</span>(<span class="st">"Ozone VS Temp"</span>)</span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-10"><a href="#cb25-10" aria-hidden="true" tabindex="-1"></a><span class="fu">grid.arrange</span>(p1, p2, p3, <span class="at">ncol =</span> <span class="dv">3</span>, <span class="at">nrow =</span> <span class="dv">1</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="regPoisson_ozone_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="modèle-de-poisson" class="level3">
<h3 class="anchored" data-anchor-id="modèle-de-poisson">Modèle de Poisson</h3>
<p>Le modèle de Poisson est réalisé grâce à la fonction <code>glm()</code> avec l’argument <code>family = "poisson"</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>pois.model <span class="ot"><-</span> <span class="fu">glm</span>(Ozone <span class="sc">~</span> Solar.R <span class="sc">+</span> Wind <span class="sc">+</span> Temp, <span class="at">data =</span> trainCleaned, <span class="at">family =</span> <span class="st">"poisson"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
</section>
<section id="estimation-des-coefficients-significativité-globale-et-individuelle-et-interprétation" class="level3">
<h3 class="anchored" data-anchor-id="estimation-des-coefficients-significativité-globale-et-individuelle-et-interprétation">Estimation des coefficients, significativité globale et individuelle et interprétation</h3>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(pois.model)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = Ozone ~ Solar.R + Wind + Temp, family = "poisson",
data = trainCleaned)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.1556014 0.2380097 0.654 0.513
Solar.R 0.0013950 0.0002509 5.560 2.69e-08 ***
Wind -0.0599402 0.0064425 -9.304 < 2e-16 ***
Temp 0.0477071 0.0024846 19.201 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 1437.77 on 68 degrees of freedom
Residual deviance: 439.33 on 65 degrees of freedom
AIC: 811.01
Number of Fisher Scoring iterations: 5</code></pre>
</div>
</div>
<p>L’expression du modèle est donc : <span class="math display">\[\ln{\lambda_{Ozone}} = \beta_0 + \beta_1 \cdot \text{Solar.R} + \beta_2 \cdot \text{Wind} + \beta_3 \cdot \text{Temp}\]</span></p>
<p>avec <span class="math inline">\(\beta_0 = 1.457\)</span>, <span class="math inline">\(\beta_1 = 0.0025\)</span>, <span class="math inline">\(\beta_2 = -1.004\)</span>, <span class="math inline">\(\beta_3 = 0.0334\)</span> .</p>
<p>Comme on peut le remarquer, tous les coefficients du modèle sont tous très significatifs.</p>
<section id="interprétation-des-coefficients" class="level4">
<h4 class="anchored" data-anchor-id="interprétation-des-coefficients">Interprétation des coefficients</h4>
<p>On dira alors qu’une augmentation d’une unité de <code>Solar.R</code> (rayonnement solaire) entraîne une augmentation de 0.0025 ppb de la concentration moyenne d’<code>Ozone</code>. La même logique est utilisée pour les autres coefficients.</p>
<p>Une meilleure interprétation se fait en calculant les ratios de taux d’incidence (<strong>IRR</strong>) via <span class="math inline">\(\exp(\hat{\beta}_j)\)</span> pour chaque coefficient. Ainsi, on a :</p>
<ul>
<li><p><strong>Solar.R :</strong> <code>exp(0.0025131) = 1.002516</code>. Une augmentation d’une unité du rayonnement solaire augmente la concentration d’ozone de <strong>0.25%</strong>, toutes choses étant égales par ailleurs</p></li>
<li><p><strong>Wind</strong> : <code>exp(-0.1003999) = 0.9044756</code>. Pour chaque augmentation de 1 unité de la vitesse du vent, la concentration attendue d’ozone <strong>diminue de 9,55%</strong> (1-0.904).</p></li>
<li><p><strong>Temp</strong> : <code>exp(0.0334104) = 1.033975</code>. Pour chaque augmentation de 1 degré de température, la concentration attendue d’ozone <strong>augmente de 3,4%</strong>.</p></li>
</ul>
</section>
<section id="déviance" class="level4">
<h4 class="anchored" data-anchor-id="déviance">Déviance</h4>
<p>On remarque que la sortie <code>summary(pois.model)</code> a donné des coefficients tous <strong>très significatifs individuellement</strong>.</p>
<p>On analyse en plus la <strong>déviance</strong>, pour analyser l’influence de chacune des variables sur l’Ozone.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">anova</span>(pois.model, <span class="at">test=</span><span class="st">"Chisq"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Analysis of Deviance Table
Model: poisson, link: log
Response: Ozone
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev Pr(>Chi)
NULL 68 1437.77
Solar.R 1 84.13 67 1353.64 < 2.2e-16 ***
Wind 1 528.37 66 825.27 < 2.2e-16 ***
Temp 1 385.95 65 439.33 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>On remarque que toutes les p-valeurs sont très significatives; par conséquent, l’influence de chacune de ces valeurs (<code>Solar.R</code>, <code>Wind</code> et <code>Temp</code>) est hautement significative sur <code>Ozone</code>.</p>
</section>
<section id="rapport-de-vraisemblance-lr" class="level4">
<h4 class="anchored" data-anchor-id="rapport-de-vraisemblance-lr">Rapport de vraisemblance LR</h4>
<p>L’objectif est d’analyser la <strong>significativité globale</strong> des coefficients :</p>
<ul>
<li><p>on crée le modèle nul</p></li>
<li><p>on fait une anamyse anova entre le modèle complet et le modèle nul</p></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>pois.model_null <span class="ot"><-</span> <span class="fu">glm</span>(Ozone <span class="sc">~</span> <span class="dv">1</span>, <span class="at">data =</span> trainCleaned, <span class="at">family =</span> <span class="st">"poisson"</span>)</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a><span class="fu">anova</span>(pois.model_null, pois.model, <span class="at">test =</span> <span class="st">"Chisq"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Analysis of Deviance Table
Model 1: Ozone ~ 1
Model 2: Ozone ~ Solar.R + Wind + Temp
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 68 1437.77
2 65 439.33 3 998.44 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<p>On remarque que la p-value est hautement significative, alors le modèle est globalement significatif (il y a au moins un coefficient non nul). On dit que <em>les prédicteurs améliorent significativement le modèle par rapport au modèle nul.</em></p>
<p>Remarque : ces résultats sont déjà visibles dans la dernière partie du <code>summary(pois.model)</code> :</p>
<pre><code> Null deviance: 1844.07 on 74 degrees of freedom
Residual deviance: 489.11 on 71 degrees of freedom
AIC: 892.59</code></pre>
<p>Cependant, la déviance résiduelle (489.11) est <strong>beaucoup plus grande que ses degrés de liberté (71)</strong>. Le rapport <code>489.11 / 71 ≈ 6.89</code> est bien supérieur à 1, ce qui <strong>indique une très forte surdispersion</strong>.</p>
<hr>
</section>
</section>
<section id="validation-du-modèle" class="level3">
<h3 class="anchored" data-anchor-id="validation-du-modèle">Validation du modèle</h3>
<section id="vérification-des-hypothèses" class="level4">
<h4 class="anchored" data-anchor-id="vérification-des-hypothèses">Vérification des hypothèses</h4>
<ol type="1">
<li><p><strong>Indépendance des réponses (Ozone)</strong></p>
<p>On considère en général que cette hypothèse est satisfaite.</p></li>
<li><p><strong>Distribution de Poisson pour les réponses (Ozone)</strong></p>
<p>Pour cela, on va comparer la distribution des valeurs observées avec la distribution théorique de Poisson de paramètre la moyenne observée.</p></li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>lambda_obs <span class="ot"><-</span> <span class="fu">mean</span>(trainCleaned<span class="sc">$</span>Ozone)</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a><span class="co"># simulation des comptages</span></span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a>theoretic_oz <span class="ot"><-</span> <span class="fu">rpois</span>(<span class="fu">length</span>(trainCleaned<span class="sc">$</span>Ozone), <span class="at">lambda =</span> lambda_obs)</span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a><span class="co"># affichage simultané</span></span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(trainCleaned) <span class="sc">+</span> </span>
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="fu">aes</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="at">fill =</span> <span class="st">"#0000FF"</span>)) <span class="sc">+</span></span>
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="fu">aes</span>(theoretic_oz, <span class="at">fill =</span> <span class="st">"#FF2100"</span>)) <span class="sc">+</span></span>
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggtitle</span>(<span class="st">"Ozone vs Distribution de Poisson"</span>) <span class="sc">+</span> </span>
<span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">"Légende"</span>, <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"Ozone"</span>, <span class="st">"Poisson théorique"</span>))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="regPoisson_ozone_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>On peut voir que les deux distributions sont assez différentes. La variable <code>Ozone</code> ne suit donc pas une distribution de poisson de paramètre 42.78. De plus, les valeurs sont très dispersées sur l’axe des abscisses, ce qui peut être un effet de la <strong>surdispersion</strong>.</p>
<ol start="3" type="1">
<li><p><strong>Surdispersion</strong></p>
<p>La surdispersion a déjà été remarquée précédemment, donc nous allons nous contenter de faire un test pour la confirmer, celui du <strong>Z-Score.</strong></p>
<p><span class="math display">\[
Z_i = \frac{(y_i - \lambda_i)^2 - y_i}{\lambda_i \sqrt{2}} \sim t_{1,\alpha}
\]</span></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>lamda <span class="ot"><-</span> <span class="fu">predict</span>(pois.model, <span class="at">type =</span> <span class="st">"response"</span>) <span class="co">#estimations du paramètre lambda</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a>z <span class="ot"><-</span> ((trainCleaned<span class="sc">$</span>Ozone <span class="sc">-</span> lamda)<span class="sc">^</span><span class="dv">2</span> <span class="sc">-</span> trainCleaned<span class="sc">$</span>Ozone) <span class="sc">/</span> (lamda <span class="sc">*</span> <span class="fu">sqrt</span>(<span class="dv">2</span>))</span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a>zscore <span class="ot"><-</span> <span class="fu">lm</span>(z <span class="sc">~</span> <span class="dv">1</span>)</span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(zscore)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = z ~ 1)
Residuals:
Min 1Q Median 3Q Max
-4.741 -4.016 -2.140 0.062 56.859
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.024 1.011 3.981 0.000169 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 8.396 on 68 degrees of freedom</code></pre>
</div>
</div></li>
</ol>
<p>Le <strong>z-score</strong> est donc significatif, ce qui, d’après <strong>J.M. Hilbe</strong>, indique la présence de <strong>surdispersion</strong>, ce qui confirme notre analyse précédente.</p>
<p>On constate que deux hypothèses sont violées (distribution de Poisson et surdispersion), alors nous allons passer à l’utilisation d’autres structures d’erreurs : les modèles <strong>quasi-poisson</strong> et <strong>binomial négatif.</strong></p>
<ul>
<li><p><strong>Modèle Quasi-Poisson</strong></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>quasipois.model <span class="ot"><-</span> <span class="fu">glm</span>(Ozone <span class="sc">~</span> Solar.R <span class="sc">+</span> Wind <span class="sc">+</span> Temp, <span class="at">data =</span> trainCleaned, <span class="at">family =</span> <span class="st">"quasipoisson"</span>)</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(quasipois.model)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = Ozone ~ Solar.R + Wind + Temp, family = "quasipoisson",
data = trainCleaned)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.1556014 0.6345631 0.245 0.807066
Solar.R 0.0013950 0.0006689 2.086 0.040946 *
Wind -0.0599402 0.0171764 -3.490 0.000874 ***
Temp 0.0477071 0.0066242 7.202 7.62e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for quasipoisson family taken to be 7.108215)
Null deviance: 1437.77 on 68 degrees of freedom
Residual deviance: 439.33 on 65 degrees of freedom
AIC: NA
Number of Fisher Scoring iterations: 5</code></pre>
</div>
</div>
<p>On remarque que <span class="math display">\[463.84/72 \lt 6.442222 \text{ (paramètre de dispersion)}\]</span>, par conséquent, la surdispersion a été prise en compte par ce modèle et l’a corrigé.</p>
<p><strong>NB :</strong> L’utilisation d’une structure d’erreur « quasi poisson » à pour conséquence d’<strong>augmenter l’erreur standard des paramètres</strong>. On peut le remarquer par exemple avec celui de l’<code>intercept</code> qui était de 0.24 environ pour le modèle de poisson standard, contrairement à celui actuel de 0.66 environ. <strong>En dehors de ça les coefficients ne changent pas</strong>.</p></li>
<li><p><strong>Modèle Binomial Négatif</strong></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>nb2_model <span class="ot"><-</span> MASS<span class="sc">::</span><span class="fu">glm.nb</span>(Ozone <span class="sc">~</span> Solar.R <span class="sc">+</span> Wind <span class="sc">+</span> Temp, <span class="at">data =</span> trainCleaned)</span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(nb2_model)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
MASS::glm.nb(formula = Ozone ~ Solar.R + Wind + Temp, data = trainCleaned,
init.theta = 6.098764353, link = log)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.3344106 0.6049308 0.553 0.580395
Solar.R 0.0014930 0.0005897 2.532 0.011351 *
Wind -0.0612403 0.0172783 -3.544 0.000394 ***
Temp 0.0453497 0.0065236 6.952 3.61e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(6.0988) family taken to be 1)
Null deviance: 203.290 on 68 degrees of freedom
Residual deviance: 72.602 on 65 degrees of freedom
AIC: 575.11
Number of Fisher Scoring iterations: 1
Theta: 6.10
Std. Err.: 1.27
2 x log-likelihood: -565.107 </code></pre>
</div>
</div>
<p>De même, pour ce modèle, on a : <span class="math inline">\(80.684 / 71 = 1.136394\)</span>, ce qui indique une surdispersion modérée : on dit que ce modèle a absorbé la surdispersion induite par le modèle de poisson simple.</p>
<p>Nous pouvons tester aussi la régression de Poisson avec des poids, comme l’a proposé Matthias Döring :</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>get.weights <span class="ot"><-</span> <span class="cf">function</span>(trainCleaned) {</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a> z.scores <span class="ot"><-</span> (trainCleaned<span class="sc">$</span>Ozone <span class="sc">-</span> <span class="fu">mean</span>(trainCleaned<span class="sc">$</span>Ozone)) <span class="sc">/</span> <span class="fu">sd</span>(trainCleaned<span class="sc">$</span>Ozone)</span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a> weights <span class="ot"><-</span> <span class="fu">exp</span>(z.scores)</span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a> weights <span class="ot"><-</span> weights <span class="sc">/</span> <span class="fu">mean</span>(weights) <span class="co"># normalisation</span></span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(weights)</span>
<span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a>weights <span class="ot"><-</span> <span class="fu">get.weights</span>(trainCleaned)</span>
<span id="cb41-9"><a href="#cb41-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-10"><a href="#cb41-10" aria-hidden="true" tabindex="-1"></a>weighted_pois.model <span class="ot"><-</span> <span class="fu">glm</span>(Ozone <span class="sc">~</span> Solar.R <span class="sc">+</span> Wind <span class="sc">+</span> Temp, <span class="at">data =</span> trainCleaned, <span class="at">family =</span> <span class="st">"poisson"</span>, <span class="at">weights =</span> weights)</span>
<span id="cb41-11"><a href="#cb41-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-12"><a href="#cb41-12" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(weighted_pois.model)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = Ozone ~ Solar.R + Wind + Temp, family = "poisson",
data = trainCleaned, weights = weights)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.1396431 0.2165112 9.882 < 2e-16 ***
Solar.R 0.0009834 0.0002756 3.568 0.000359 ***
Wind -0.0562996 0.0050862 -11.069 < 2e-16 ***
Temp 0.0277461 0.0022238 12.477 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 1114.06 on 68 degrees of freedom
Residual deviance: 503.38 on 65 degrees of freedom
AIC: 928.87
Number of Fisher Scoring iterations: 4</code></pre>
</div>
</div>
<ul>
<li>On remarque ici que le modèle a une constante très significative, qui n’était pas le cas dans le modèle de poisson standard.</li>
</ul>
<p>Faisons aussi une régression pesée de quasi-poisson</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>weighted_quasipois.model <span class="ot"><-</span> <span class="fu">glm</span>(Ozone <span class="sc">~</span> Solar.R <span class="sc">+</span> Wind <span class="sc">+</span> Temp, <span class="at">data =</span> trainCleaned, <span class="at">family =</span> <span class="st">"quasipoisson"</span>, <span class="at">weights =</span> weights)</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(weighted_quasipois.model)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = Ozone ~ Solar.R + Wind + Temp, family = "quasipoisson",
data = trainCleaned, weights = weights)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.1396431 0.5974751 3.581 0.000654 ***
Solar.R 0.0009834 0.0007605 1.293 0.200551
Wind -0.0562996 0.0140356 -4.011 0.000159 ***
Temp 0.0277461 0.0061368 4.521 2.67e-05 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for quasipoisson family taken to be 7.615155)
Null deviance: 1114.06 on 68 degrees of freedom
Residual deviance: 503.38 on 65 degrees of freedom
AIC: NA
Number of Fisher Scoring iterations: 4</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>weighted_nb2_model <span class="ot"><-</span> MASS<span class="sc">::</span><span class="fu">glm.nb</span>(Ozone <span class="sc">~</span> Solar.R <span class="sc">+</span> Wind <span class="sc">+</span> Temp, <span class="at">data =</span> trainCleaned, <span class="at">weights =</span> weights)</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(weighted_nb2_model)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
MASS::glm.nb(formula = Ozone ~ Solar.R + Wind + Temp, data = trainCleaned,
weights = weights, init.theta = 9.735403346, link = log)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 1.714931 0.570861 3.004 0.00266 **
Solar.R 0.001259 0.000714 1.764 0.07775 .
Wind -0.065921 0.014589 -4.519 6.22e-06 ***
Temp 0.032750 0.005944 5.509 3.60e-08 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for Negative Binomial(9.7354) family taken to be 1)
Null deviance: 160.356 on 68 degrees of freedom
Residual deviance: 73.531 on 65 degrees of freedom
AIC: 646.71
Number of Fisher Scoring iterations: 1
Theta: 9.74
Std. Err.: 1.97
2 x log-likelihood: -636.713 </code></pre>
</div>
</div>
<p>Ici, l’intercept devient significatif face à celui du quasipoisson sans poids, mais la variable <code>Temp</code> devient non significative.</p>
<p>Pour le modèle binomial négatif, la régression avec poids a rendu la constante du modèle significative, et le coefficient de dispersion est sensiblement le même.</p></li>
</ul>
</section>
<section id="qualité-et-pertinence" class="level4">
<h4 class="anchored" data-anchor-id="qualité-et-pertinence">Qualité et Pertinence</h4>
<p>Jusque-là, nous avons 6 modèles : poisson normal et avec poids, quasi-poisson avec poids et binomial négatif, et avec poids.</p>
<p>Pour analyser la pertinence de chacun de ces modèles, nous allons faire les tests de :</p>
<ul>
<li><p>Test de Hosmer-Lemeshow</p></li>
<li><p>Test des résidus de Pearson</p></li>
</ul>
<p>Quant à la qualité, nous allons utiliser</p>
<ul>
<li><p>le score R²</p></li>
<li><p>la précision des prédiction</p></li>
<li><p>les mesures d’erreurs : RMSE, MSE et RSE etc</p></li>
</ul>
<ol type="1">
<li><p><strong>Pertinence</strong></p>
<p>Hosmer-Lemeshow</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hoslem.test</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="fu">fitted</span>(pois.model))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Hosmer and Lemeshow goodness of fit (GOF) test
data: trainCleaned$Ozone, fitted(pois.model)
X-squared = -4.0481, df = 8, p-value = 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hoslem.test</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="fu">fitted</span>(weighted_pois.model))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Hosmer and Lemeshow goodness of fit (GOF) test
data: trainCleaned$Ozone, fitted(weighted_pois.model)
X-squared = -9.0879, df = 8, p-value = 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb51"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hoslem.test</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="fu">fitted</span>(quasipois.model))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Hosmer and Lemeshow goodness of fit (GOF) test
data: trainCleaned$Ozone, fitted(quasipois.model)
X-squared = -4.0481, df = 8, p-value = 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hoslem.test</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="fu">fitted</span>(weighted_quasipois.model))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Hosmer and Lemeshow goodness of fit (GOF) test
data: trainCleaned$Ozone, fitted(weighted_quasipois.model)
X-squared = -9.0879, df = 8, p-value = 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hoslem.test</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="fu">fitted</span>(nb2_model))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Hosmer and Lemeshow goodness of fit (GOF) test
data: trainCleaned$Ozone, fitted(nb2_model)
X-squared = -4.6601, df = 8, p-value = 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="fu">hoslem.test</span>(trainCleaned<span class="sc">$</span>Ozone, <span class="fu">fitted</span>(weighted_nb2_model))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Hosmer and Lemeshow goodness of fit (GOF) test
data: trainCleaned$Ozone, fitted(weighted_nb2_model)
X-squared = -6.7266, df = 8, p-value = 1</code></pre>
</div>
</div>
<p>Ici, aucune p-valeur n’est inférieure à 5%, et sont égales à 1 ! Autrement dit, aucun modèle ne serait assez adapté aux données. Néanmoins, le modèle binomial négatif avec poids a une statitistique plus grande que celle des autres.</p>
<p>Résidus de Pearson</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb59"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">residuals</span>(pois.model,<span class="at">type=</span><span class="st">"pearson"</span>)<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a>ddl <span class="ot"><-</span> <span class="fu">df.residual</span>(pois.model)</span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a>pv <span class="ot"><-</span> <span class="dv">1</span><span class="sc">-</span><span class="fu">pchisq</span>(s,ddl)</span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">Modèle Poisson standard : "</span>, pv)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Modèle Poisson standard : 0</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb61"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">residuals</span>(weighted_pois.model,<span class="at">type=</span><span class="st">"pearson"</span>)<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a>ddl <span class="ot"><-</span> <span class="fu">df.residual</span>(weighted_pois.model)</span>
<span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a>pv <span class="ot"><-</span> <span class="dv">1</span><span class="sc">-</span><span class="fu">pchisq</span>(s,ddl)</span>
<span id="cb61-4"><a href="#cb61-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">Modèle Poisson avec poids : "</span>, pv)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Modèle Poisson avec poids : 0</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb63"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">residuals</span>(quasipois.model,<span class="at">type=</span><span class="st">"pearson"</span>)<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a>ddl <span class="ot"><-</span> <span class="fu">df.residual</span>(quasipois.model)</span>
<span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a>pv <span class="ot"><-</span> <span class="dv">1</span><span class="sc">-</span><span class="fu">pchisq</span>(s,ddl)</span>
<span id="cb63-4"><a href="#cb63-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">Modèle QuasiPoisson : "</span>, pv)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Modèle QuasiPoisson : 0</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb65"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">residuals</span>(weighted_quasipois.model,<span class="at">type=</span><span class="st">"pearson"</span>)<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a>ddl <span class="ot"><-</span> <span class="fu">df.residual</span>(weighted_quasipois.model)</span>
<span id="cb65-3"><a href="#cb65-3" aria-hidden="true" tabindex="-1"></a>pv <span class="ot"><-</span> <span class="dv">1</span><span class="sc">-</span><span class="fu">pchisq</span>(s,ddl)</span>
<span id="cb65-4"><a href="#cb65-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">Modèle QuasiPoisson avec poids: "</span>, pv)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Modèle QuasiPoisson avec poids: 0</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb67"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">residuals</span>(nb2_model,<span class="at">type=</span><span class="st">"pearson"</span>)<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a>ddl <span class="ot"><-</span> <span class="fu">df.residual</span>(nb2_model)</span>
<span id="cb67-3"><a href="#cb67-3" aria-hidden="true" tabindex="-1"></a>pv <span class="ot"><-</span> <span class="dv">1</span><span class="sc">-</span><span class="fu">pchisq</span>(s,ddl)</span>
<span id="cb67-4"><a href="#cb67-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">Modèle binomial négatif : "</span>, pv)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Modèle binomial négatif : 0.1730304</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb69"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span> <span class="fu">sum</span>(<span class="fu">residuals</span>(weighted_nb2_model,<span class="at">type=</span><span class="st">"pearson"</span>)<span class="sc">^</span><span class="dv">2</span>)</span>
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a>ddl <span class="ot"><-</span> <span class="fu">df.residual</span>(weighted_nb2_model)</span>
<span id="cb69-3"><a href="#cb69-3" aria-hidden="true" tabindex="-1"></a>pv <span class="ot"><-</span> <span class="dv">1</span><span class="sc">-</span><span class="fu">pchisq</span>(s,ddl)</span>
<span id="cb69-4"><a href="#cb69-4" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">Modèle binomial négatif avec poids: "</span>, pv)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Modèle binomial négatif avec poids: 0.3335389</code></pre>
</div>
</div>
<p>Aucune p-value n’est supérieure à 5%, alors aucun modèle ne serait bien adapté aux données. Par contre le modèle binomial négatif semble avoir une meilleure p-value.</p>
<p>Par contre, celle du modèle binomial négatif avec poids respecte la condition : c’est le plus pertinent.</p>
<p>En conclusion pour la pertinence, seul le modèle binomial négatif serait adapté aux données.</p>
<ol start="2" type="1">
<li><p><strong>Qualité</strong></p>
<p>Nous pouvons tout vérifier grâce à <code>compare_performance()</code> du package <code>performance</code>.</p></li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb71"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="fu">compare_performance</span>(pois.model, weighted_pois.model, quasipois.model, weighted_quasipois.model, nb2_model, weighted_nb2_model, <span class="at">metrics =</span> <span class="st">"all"</span>, <span class="at">rank =</span> <span class="cn">TRUE</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stderr">
<pre><code>Following indices with missing values are not used for ranking: AIC_wt,
AICc_wt, BIC_wt</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code># Comparison of Model Performance Indices
Name | Model | Nagelkerke's R2 | RMSE | Sigma
--------------------------------------------------------------------
nb2_model | negbin | 0.897 | 16.380 | 1.000
pois.model | glm | 1.000 | 16.363 | 1.000
weighted_nb2_model | negbin | 0.794 | 20.339 | 1.000
quasipois.model | glm | 1.000 | 16.363 | 2.666
weighted_pois.model | glm | 1.000 | 21.790 | 1.000
weighted_quasipois.model | glm | 1.000 | 21.790 | 2.760
Name | Score_log | Score_spherical | Performance-Score
--------------------------------------------------------------------------
nb2_model | -4.161 | 0.098 | 86.40%
pois.model | -5.819 | 0.076 | 77.05%
weighted_nb2_model | -4.309 | 0.104 | 64.67%
quasipois.model | -5.819 | 0.076 | 58.11%
weighted_pois.model | -8.538 | 0.068 | 39.99%
weighted_quasipois.model | -8.538 | 0.068 | 19.99%</code></pre>
</div>
</div></li>
</ol>
<p>Résumé en graphiques</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb74"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a>pl1 <span class="ot"><-</span> <span class="fu">plot_pred_accuracy</span>(pois.model, testCleaned[<span class="sc">-</span><span class="dv">1</span>], testCleaned<span class="sc">$</span>Ozone, <span class="at">plot_title =</span> <span class="st">"Poisson Standard"</span>)</span>
<span id="cb74-2"><a href="#cb74-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-3"><a href="#cb74-3" aria-hidden="true" tabindex="-1"></a>pl2 <span class="ot"><-</span> <span class="fu">plot_pred_accuracy</span>(weighted_pois.model, testCleaned[<span class="sc">-</span><span class="dv">1</span>], testCleaned<span class="sc">$</span>Ozone, <span class="at">plot_title =</span> <span class="st">"Poisson Pondéré"</span>)</span>
<span id="cb74-4"><a href="#cb74-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-5"><a href="#cb74-5" aria-hidden="true" tabindex="-1"></a>pl3 <span class="ot"><-</span> <span class="fu">plot_pred_accuracy</span>(quasipois.model, testCleaned[<span class="sc">-</span><span class="dv">1</span>], testCleaned<span class="sc">$</span>Ozone, <span class="at">plot_title =</span> <span class="st">"QuasiPoisson Standard"</span>)</span>
<span id="cb74-6"><a href="#cb74-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-7"><a href="#cb74-7" aria-hidden="true" tabindex="-1"></a>pl4 <span class="ot"><-</span> <span class="fu">plot_pred_accuracy</span>(weighted_quasipois.model, testCleaned[<span class="sc">-</span><span class="dv">1</span>], testCleaned<span class="sc">$</span>Ozone, <span class="at">plot_title =</span> <span class="st">"QuasiPoisson Pondéré"</span>)</span>
<span id="cb74-8"><a href="#cb74-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-9"><a href="#cb74-9" aria-hidden="true" tabindex="-1"></a>pl5 <span class="ot"><-</span> <span class="fu">plot_pred_accuracy</span>(nb2_model, testCleaned[<span class="sc">-</span><span class="dv">1</span>], testCleaned<span class="sc">$</span>Ozone, <span class="at">plot_title =</span> <span class="st">"Binomial Négatif Standard"</span>)</span>
<span id="cb74-10"><a href="#cb74-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-11"><a href="#cb74-11" aria-hidden="true" tabindex="-1"></a>pl6 <span class="ot"><-</span> <span class="fu">plot_pred_accuracy</span>(weighted_nb2_model, testCleaned[<span class="sc">-</span><span class="dv">1</span>], testCleaned<span class="sc">$</span>Ozone, <span class="at">plot_title =</span> <span class="st">"Binomial Négatif Pondéré"</span>)</span>
<span id="cb74-12"><a href="#cb74-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-13"><a href="#cb74-13" aria-hidden="true" tabindex="-1"></a><span class="co">#pfinal <- grid.arrange(pl1, pl2, pl3, pl4, pl5, pl6, ncol = 2, nrow = 3)</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb75"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grid.arrange</span>(pl1, pl2, <span class="at">nrow =</span> <span class="dv">1</span>, <span class="at">ncol =</span> <span class="dv">2</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="regPoisson_ozone_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb76"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grid.arrange</span>(pl3, pl4, <span class="at">nrow =</span> <span class="dv">1</span>, <span class="at">ncol =</span> <span class="dv">2</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="regPoisson_ozone_files/figure-html/unnamed-chunk-27-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb77"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="fu">grid.arrange</span>(pl5, pl6, <span class="at">nrow =</span> <span class="dv">1</span>, <span class="at">ncol =</span> <span class="dv">2</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="regPoisson_ozone_files/figure-html/unnamed-chunk-28-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb78"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a>AIC <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb78-2"><a href="#cb78-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">AIC</span>(pois.model),</span>
<span id="cb78-3"><a href="#cb78-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">AIC</span>(weighted_pois.model),</span>
<span id="cb78-4"><a href="#cb78-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">AIC</span>(quasipois.model),</span>
<span id="cb78-5"><a href="#cb78-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">AIC</span>(weighted_quasipois.model),</span>
<span id="cb78-6"><a href="#cb78-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">AIC</span>(nb2_model),</span>
<span id="cb78-7"><a href="#cb78-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">AIC</span>(weighted_nb2_model))</span>
<span id="cb78-8"><a href="#cb78-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb78-9"><a href="#cb78-9" aria-hidden="true" tabindex="-1"></a>BIC <span class="ot"><-</span> <span class="fu">c</span>(</span>
<span id="cb78-10"><a href="#cb78-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">BIC</span>(pois.model),</span>
<span id="cb78-11"><a href="#cb78-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">BIC</span>(weighted_pois.model),</span>
<span id="cb78-12"><a href="#cb78-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">BIC</span>(quasipois.model),</span>
<span id="cb78-13"><a href="#cb78-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">BIC</span>(weighted_quasipois.model),</span>
<span id="cb78-14"><a href="#cb78-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">BIC</span>(nb2_model),</span>
<span id="cb78-15"><a href="#cb78-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">BIC</span>(weighted_nb2_model))</span>
<span id="cb78-16"><a href="#cb78-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb78-17"><a href="#cb78-17" aria-hidden="true" tabindex="-1"></a>models <span class="ot">=</span> <span class="fu">c</span>(<span class="st">"std poisson"</span>,</span>
<span id="cb78-18"><a href="#cb78-18" aria-hidden="true" tabindex="-1"></a> <span class="st">"wt poisson"</span>,</span>
<span id="cb78-19"><a href="#cb78-19" aria-hidden="true" tabindex="-1"></a> <span class="st">"std quasipoisson"</span>,</span>
<span id="cb78-20"><a href="#cb78-20" aria-hidden="true" tabindex="-1"></a> <span class="st">"wt quasipoisson"</span>,</span>
<span id="cb78-21"><a href="#cb78-21" aria-hidden="true" tabindex="-1"></a> <span class="st">"std nb2"</span>,</span>
<span id="cb78-22"><a href="#cb78-22" aria-hidden="true" tabindex="-1"></a> <span class="st">"wt nb2"</span>)</span>
<span id="cb78-23"><a href="#cb78-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb78-24"><a href="#cb78-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb78-25"><a href="#cb78-25" aria-hidden="true" tabindex="-1"></a>info_df <span class="ot"><-</span> <span class="fu">data.frame</span>(models, AIC, BIC)</span>
<span id="cb78-26"><a href="#cb78-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb78-27"><a href="#cb78-27" aria-hidden="true" tabindex="-1"></a>info_df</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code> models AIC BIC
1 std poisson 811.0066 819.9430
2 wt poisson 928.8735 937.8099
3 std quasipoisson NA NA
4 wt quasipoisson NA NA
5 std nb2 575.1073 586.2778
6 wt nb2 646.7125 657.8830</code></pre>
</div>
</div>
<hr>
</section>
</section>
<section id="conclusion" class="level3">
<h3 class="anchored" data-anchor-id="conclusion">Conclusion</h3>
<p>En conclusion, l’un des problèmes les plus féquents en modélisation par la régression de Poisson est la surdispersion, et les moyens les plus courants pour la résoudre sont la modélisation de quasipoisson et le modèle binomial négatif. Cependant, pour les données étudiées, le modèle quasipoisson n’a aucun avantage sur la modélisation de poisson si ce n’est de prendre en compte la surdispersion. Le modèle binomial négatif semble être en termes de pertinence être le plus adapté à nos données étudiées. La pondération, comme constatée par Matthias Döring prend mieux en compte les outliers, mais diminue un peu la qualité du modèle.</p>
</section>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const onCopySuccess = function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
}
const getTextToCopy = function(trigger) {
const outerScaffold = trigger.parentElement.cloneNode(true);
const codeEl = outerScaffold.querySelector('code');
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
text: getTextToCopy