-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-Julia-Geostats.html
1003 lines (951 loc) · 83.3 KB
/
R-Julia-Geostats.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
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 name="generator" content="quarto-1.1.189">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>R-Julia-Geostats</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 -1.6em;
vertical-align: middle;
}
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;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="terraR_files/libs/clipboard/clipboard.min.js"></script>
<script src="terraR_files/libs/quarto-html/quarto.js"></script>
<script src="terraR_files/libs/quarto-html/popper.min.js"></script>
<script src="terraR_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="terraR_files/libs/quarto-html/anchor.min.js"></script>
<link href="terraR_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="terraR_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="terraR_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="terraR_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="terraR_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#données" id="toc-données" class="nav-link active" data-scroll-target="#données">Données</a></li>
<li><a href="#le-package-terra" id="toc-le-package-terra" class="nav-link" data-scroll-target="#le-package-terra">0 - Le package terra</a></li>
<li><a href="#package-r-gstat" id="toc-package-r-gstat" class="nav-link" data-scroll-target="#package-r-gstat">1 - Package R gstat</a>
<ul class="collapse">
<li><a href="#transformation-au-format-sf" id="toc-transformation-au-format-sf" class="nav-link" data-scroll-target="#transformation-au-format-sf">Transformation au format sf</a></li>
<li><a href="#variogramme-empirique" id="toc-variogramme-empirique" class="nav-link" data-scroll-target="#variogramme-empirique">Variogramme empirique</a></li>
<li><a href="#le-type-vgm" id="toc-le-type-vgm" class="nav-link" data-scroll-target="#le-type-vgm">Le type vgm</a></li>
<li><a href="#estimation-du-variogramme-théorique" id="toc-estimation-du-variogramme-théorique" class="nav-link" data-scroll-target="#estimation-du-variogramme-théorique">Estimation du variogramme théorique</a></li>
<li><a href="#krigeage" id="toc-krigeage" class="nav-link" data-scroll-target="#krigeage">Krigeage</a></li>
</ul></li>
<li><a href="#package-r-geomodels" id="toc-package-r-geomodels" class="nav-link" data-scroll-target="#package-r-geomodels">2 - Package R GeoModels</a>
<ul class="collapse">
<li><a href="#variogramme-empirique-1" id="toc-variogramme-empirique-1" class="nav-link" data-scroll-target="#variogramme-empirique-1">Variogramme empirique</a></li>
<li><a href="#modèles-supportés" id="toc-modèles-supportés" class="nav-link" data-scroll-target="#modèles-supportés">Modèles supportés</a></li>
<li><a href="#estimation-du-variogramme-théorique-1" id="toc-estimation-du-variogramme-théorique-1" class="nav-link" data-scroll-target="#estimation-du-variogramme-théorique-1">Estimation du variogramme théorique</a></li>
<li><a href="#krigeage-1" id="toc-krigeage-1" class="nav-link" data-scroll-target="#krigeage-1">Krigeage</a></li>
</ul></li>
<li><a href="#package-julia-geostats" id="toc-package-julia-geostats" class="nav-link" data-scroll-target="#package-julia-geostats">3 - Package Julia GeoStats</a>
<ul class="collapse">
<li><a href="#bibliothèques-julia" id="toc-bibliothèques-julia" class="nav-link" data-scroll-target="#bibliothèques-julia">Bibliothèques Julia</a></li>
<li><a href="#importer-les-données" id="toc-importer-les-données" class="nav-link" data-scroll-target="#importer-les-données">Importer les données</a></li>
<li><a href="#transformation-en-données-spatiale" id="toc-transformation-en-données-spatiale" class="nav-link" data-scroll-target="#transformation-en-données-spatiale">Transformation en données spatiale</a></li>
<li><a href="#variogramme" id="toc-variogramme" class="nav-link" data-scroll-target="#variogramme">Variogramme</a></li>
<li><a href="#création-de-la-grille" id="toc-création-de-la-grille" class="nav-link" data-scroll-target="#création-de-la-grille">Création de la grille</a></li>
<li><a href="#krigeage-2" id="toc-krigeage-2" class="nav-link" data-scroll-target="#krigeage-2">Krigeage</a></li>
<li><a href="#affichage-du-krigeage" id="toc-affichage-du-krigeage" class="nav-link" data-scroll-target="#affichage-du-krigeage">Affichage du Krigeage</a></li>
</ul></li>
<li><a href="#comparaison" id="toc-comparaison" class="nav-link" data-scroll-target="#comparaison">4 - Comparaison</a></li>
</ul>
</nav>
</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">R-Julia-Geostats</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<p>L’objectif est de comparer des packages R, et Julia pour du krigeage.</p>
<p>On va utiliser :</p>
<p>- R : gstat</p>
<p>- R : GeoModels</p>
<p>- Julia : GeoStats</p>
<p>On compare les points suivant :</p>
<p>- simplicité des données d’entrée</p>
<p>- concision du code</p>
<p>- vitesse d’exécution</p>
<p>- type d’estimation</p>
<p>Pour chaque package, on calculera pour les mêmes données le variogramme empirique, puis on ajustera un variogramme théorique et on fera un krigeage.</p>
<section id="données" class="level2">
<h2 class="anchored" data-anchor-id="données">Données</h2>
<div class="cell">
<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="fu">library</span>(dplyr)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sp)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(terra)</span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(JuliaCall)</span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(meuse)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(meuse.grid)</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(gstat)</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(sf)</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>On utilise les données meuse du package sp. Celles-ci contiennent des données de concentration en métaux près de la meuse, avec des coordonnées cartésiennes en m.</p>
<p>Pour travailler sur ces concentrations, on passera tout au log afin de les normaliser.</p>
<p>Pour la visualisation en R, on utilise le package terra.</p>
</section>
<section id="le-package-terra" class="level1">
<h1>0 - Le package terra</h1>
<ul>
<li><p>dernière version : 1.7-39 du 22/06/2023</p></li>
<li><p>Travail des données spatiales sous 2 formes : raster et vector.</p>
<ul>
<li><p>raster = grilles rectangulaires, adapté aux données continues -> Classe SpatRaster</p></li>
<li><p>vector = points, lignes, polygones = données discrètes. -> classe SpatVector</p></li>
</ul></li>
<li><p>Début de la doc = récap des méthodes importantes (il faut bien, la doc fait 300 pages)</p></li>
<li><p>Sert à manipuler, visualiser des objets spatiaux, notamment après les calculs pour simplifier le travail sur les résultats.</p></li>
</ul>
<p>Méthode rast = créer un objet de classe SpatRaster. Le type de la 1ere entrée peut varier.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>meuseRast <span class="ot">=</span> <span class="fu">rast</span>(meuse.grid,<span class="at">type=</span><span class="st">"xyz"</span>,<span class="at">crs =</span> <span class="st">"EPSG:2154"</span>)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>meuseVect <span class="ot">=</span> <span class="fu">vect</span>(meuse,<span class="at">geom=</span>(<span class="fu">c</span>(<span class="st">'x'</span>,<span class="st">'y'</span>)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<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>meuseRastCd <span class="ot">=</span> <span class="fu">rast</span>(<span class="fu">select</span>(meuse,x,y,zinc),<span class="at">type=</span><span class="st">"xyz"</span>)</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">log</span>(meuseRastCd)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>class : SpatRaster
dimensions : 3898, 2786, 1 (nrow, ncol, nlyr)
resolution : 1, 1 (x, y)
extent : 178604.5, 181390.5, 329713.5, 333611.5 (xmin, xmax, ymin, ymax)
coord. ref. :
source(s) : memory
name : zinc
min value : 4.727388
max value : 7.516977 </code></pre>
</div>
<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>meuseRastCu <span class="ot">=</span> <span class="fu">rast</span>(<span class="fu">select</span>(meuse,x,y,copper),<span class="at">type=</span><span class="st">"xyz"</span>)</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>meuseRastCuCd <span class="ot">=</span> <span class="fu">c</span>(meuseRastCd,meuseRastCd) <span class="co">#methode c() = combinaison de layers</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="package-r-gstat" class="level1">
<h1>1 - Package R gstat</h1>
<p>gstat est disponible sur le CRAN : https://CRAN.R-project.org/package=gstat</p>
<p>On peut y trouver également un tutoriel sur le dataset “meuse” utilisé ici, qui présentent le type de données spatiales utilisées par le package (spatial dataframe, données grillées) et les méthodes permettant de travailler avec (méthodes de visualisation du package sp, ajustement de variogrammes, directionnels ou non, krigeage, simulation conditionnelle).</p>
<p>La version la plus ancienne sur les archives du CRAN date de 2003.</p>
<p>Au départ, les données spatiales supportées étaient celles du package sp, mais les données de type sf le sont également.</p>
<p>INSERER AU BON ENDROIT LES CELLULES</p>
<section id="transformation-au-format-sf" class="level2">
<h2 class="anchored" data-anchor-id="transformation-au-format-sf">Transformation au format sf</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">=</span> <span class="fu">select</span>(meuse,x,y,cadmium,zinc)</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>df_grid <span class="ot">=</span> <span class="fu">select</span>(meuse.grid,x,y)</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>df_sf <span class="ot"><-</span> df <span class="sc">%>%</span> </span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> sf<span class="sc">::</span><span class="fu">st_as_sf</span>( <span class="at">coords =</span> <span class="fu">c</span>(<span class="st">'x'</span>,<span class="st">'y'</span>))</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a>df_sf <span class="ot"><-</span> sf<span class="sc">::</span><span class="fu">st_make_valid</span>(df_sf)</span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>df_grid_sf <span class="ot">=</span> df_grid<span class="sc">%>%</span> sf<span class="sc">::</span><span class="fu">st_as_sf</span>(<span class="at">coords =</span> <span class="fu">c</span>(<span class="st">'x'</span>,<span class="st">'y'</span>))<span class="sc">%>%</span>sf<span class="sc">::</span><span class="fu">st_make_valid</span>()</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(df, <span class="fu">aes</span>(<span class="at">y=</span><span class="fu">log</span>(zinc))) <span class="sc">+</span> </span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">notch=</span><span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">y =</span> <span class="st">"Log(Zn concentration)"</span>) <span class="sc">+</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span> </span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.text.x =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.ticks =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.line =</span> <span class="fu">element_blank</span>()</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="variogramme-empirique" class="level2">
<h2 class="anchored" data-anchor-id="variogramme-empirique">Variogramme empirique</h2>
<p>Pour simplifier la comparaison avec le package Julia, on va se contenter d’une moyenne constante sans introduire de prédicteurs : on demande donc log(zinc)~1. Si on avait voulu, comme dans le tutoriel du package, expliquer la moyenne par la distance à la rivière, il faudrait modifier cette formule en log(zinc)~distance après avoir rajouté une colonne distance au sf dataframe.</p>
<p>Appeler plot sur un objet issu de variogram permet d’en obtenir un tracé.</p>
<div class="cell">
<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>start_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>test <span class="ot"><-</span> <span class="fu">variogram</span>(<span class="fu">log</span>(zinc)<span class="sc">~</span><span class="dv">1</span> , df_sf,<span class="at">cutoff =</span> <span class="dv">2000</span>)</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>end_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>end_time <span class="sc">-</span> start_time</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Time difference of 0.09534097 secs</code></pre>
</div>
<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">plot</span>(test)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="le-type-vgm" class="level2">
<h2 class="anchored" data-anchor-id="le-type-vgm">Le type vgm</h2>
<p>Plusieurs modèles de variogrammes spatiaux sont disponibles dans la librairie.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">show.vgms</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
<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>(<span class="fu">vgm</span>())</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> short long
1 Nug Nug (nugget)
2 Exp Exp (exponential)
3 Sph Sph (spherical)
4 Gau Gau (gaussian)
5 Exc Exclass (Exponential class/stable)
6 Mat Mat (Matern)
7 Ste Mat (Matern, M. Stein's parameterization)
8 Cir Cir (circular)
9 Lin Lin (linear)
10 Bes Bes (bessel)
11 Pen Pen (pentaspherical)
12 Per Per (periodic)
13 Wav Wav (wave)
14 Hol Hol (hole)
15 Log Log (logarithmic)
16 Pow Pow (power)
17 Spl Spl (spline)
18 Leg Leg (Legendre)
19 Err Err (Measurement error)
20 Int Int (Intercept)</code></pre>
</div>
</div>
<p>Un variogramme théorique est un objet de type vgm, qui doit être initialisé avec des paramètres avant de l’insérer dans la fonction d’estimation.</p>
<p>Un inconvénient dans la création d’un objet vgm est l’absence de nom pour certains paramètres, il faut se référer à la documentation pour savoir à quel place il faut rentrer quel argument lorsque’on appelle vgm(). Ne pas renseigner un argument initialise celui-ci à NA. Utiliser fit.variogram sur un objet avec des NA fait appel à une étape supplémentaire qui initialise avec des paramètres issus du variogramme empirique. Cela empêche de contrôler les valeurs initiales de l’estimation.</p>
</section>
<section id="estimation-du-variogramme-théorique" class="level2">
<h2 class="anchored" data-anchor-id="estimation-du-variogramme-théorique">Estimation du variogramme théorique</h2>
<p>Les paramètres du variogramme théorique sont ajustés par moindres carrés sur le variogramme empirique.</p>
<p>vgmModelwav <- fit.variogram(test,vgm(‘Wav’))</p>
<div class="cell">
<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>vgmModelexp <span class="ot"><-</span> <span class="fu">fit.variogram</span>(test, <span class="fu">vgm</span>(<span class="dv">2</span>, <span class="st">"Exp"</span>, <span class="dv">1000</span>))</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a>vgmModelexp</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> model psill range
1 Exp 0.6849652 421.2255</code></pre>
</div>
<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>vgmModelmat <span class="ot"><-</span> <span class="fu">fit.variogram</span>(test, <span class="fu">vgm</span>(<span class="dv">10</span>, <span class="st">"Mat"</span>, <span class="dv">300</span>, <span class="fl">4.5</span>, <span class="at">kappa =</span> <span class="fl">0.7</span>))</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>vgmModelmat</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> model psill range kappa
1 Nug 0.05112814 0.0000 0.0
2 Mat 0.62395526 340.0403 0.7</code></pre>
</div>
<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>vgmModelNA <span class="ot"><-</span> <span class="fu">fit.variogram</span>(test,<span class="fu">vgm</span>(<span class="st">'Mat'</span>))</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>vgmModelNA</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> model psill range kappa
1 Nug 0.001864289 0.0000 0.0
2 Mat 0.684275121 424.4234 0.5</code></pre>
</div>
<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>start_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>vgmModelwav <span class="ot"><-</span> <span class="fu">fit.variogram</span>(test,<span class="fu">vgm</span>(<span class="st">'Wav'</span>))</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a>end_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a>end_time <span class="sc">-</span> start_time</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Time difference of 0.00465107 secs</code></pre>
</div>
<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>vgmModelwav</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> model psill range
1 Nug 0.1556503 0.0000
2 Wav 0.4407740 643.9395</code></pre>
</div>
</div>
<p>La fonction plot() appelée sur un modèle estimé trace en continu le variogramme théorique et en points le variogramme empirique.</p>
<div class="cell">
<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">plot</span>(test, vgmModelmat)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(test, vgmModelwav)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-8-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="krigeage" class="level2">
<h2 class="anchored" data-anchor-id="krigeage">Krigeage</h2>
<div class="cell">
<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>start_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a>krig <span class="ot"><-</span> <span class="fu">krige</span>(<span class="fu">log</span>(zinc)<span class="sc">~</span><span class="dv">1</span> , df_sf, df_grid_sf, <span class="at">model =</span> vgmModelmat)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[using ordinary kriging]</code></pre>
</div>
<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>end_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a>end_time <span class="sc">-</span> start_time</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Time difference of 0.5502551 secs</code></pre>
</div>
<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>Kres_pred <span class="ot">=</span> <span class="fu">data.frame</span>(<span class="at">x=</span>meuse.grid<span class="sc">$</span>x,<span class="at">y=</span>meuse.grid<span class="sc">$</span>y,<span class="at">pred=</span>krig<span class="sc">$</span>var1.pred)</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a>krig_res<span class="ot"><-</span> terra<span class="sc">::</span><span class="fu">rast</span>(Kres_pred, <span class="at">type=</span><span class="st">"xyz"</span>, <span class="at">crs=</span><span class="st">"EPSG:2154"</span>)</span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a>Kres_var <span class="ot">=</span> <span class="fu">data.frame</span>(<span class="at">x=</span>meuse.grid<span class="sc">$</span>x,<span class="at">y=</span>meuse.grid<span class="sc">$</span>y,<span class="at">pred=</span>krig<span class="sc">$</span>var1.var)</span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a>krig_var<span class="ot"><-</span> terra<span class="sc">::</span><span class="fu">rast</span>(Kres_var, <span class="at">type=</span><span class="st">"xyz"</span>, <span class="at">crs=</span><span class="st">"EPSG:2154"</span>)</span>
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>((krig_res), <span class="at">main =</span> <span class="st">'Krigeage de log(zinc) - gstat'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>(krig_var,<span class="at">main =</span> <span class="st">'Erreur de krigeage pour log(zinc) - gstat'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-9-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
</section>
<section id="package-r-geomodels" class="level1">
<h1>2 - Package R GeoModels</h1>
<p>GeoModels est un package relativement récent (2022), il prend la suite de CompRandFld suite à une mise à jour majeure de R.</p>
<p>Il est disponible sur le CRAN :</p>
<p>https://CRAN.R-project.org/package=GeoModels</p>
<p>Le package propose des fonctions pour de la simulation, de l’inférence et de la prédiction de champs spatiaux et spatio-temporels, univarié ou multivarié. Il contient des méthode spour des champs Gaussiens et non Gaussiens.</p>
<p>On peut travailler en coordonnées euclidiennes et sphériques.</p>
<p>GeoModels propose de faire des modèles spatiaux et spatiotemporels.</p>
<p>Les estimations se font par maximisation de vraisemblance composite.</p>
<p>Du fait de la variété de modèles et de données possibles en entrée (spatial, spatiotemporel, 1 ou plusieurs variables …), chaque fonction est définie généralement pour prendre en entrée des données (notamment data et coordonnées) sous plusieurs types. Dans tous les cas, on ne dépend pas de structures “évoluées”, on n’a besoin que de vecteurs et de matrices. En contrepartie, on est probablement moins libres sur les systèmes de coordonnées.</p>
<p>Les données en entrée doivent être, si on fait du spatial uniquement :</p>
<p>- data : un vecteur de taille <span class="math inline">\(d\)</span></p>
<p>- coordx : une matrice <span class="math inline">\(d \times 2\)</span> si on met directement les coordonnées, ou un vecteur de taille <span class="math inline">\(d\)</span> si on sépare les coordonnées</p>
<p>- coordy : NULL si on a déjà mis les 2 coordonnées dans coordx, vecteur de taille <span class="math inline">\(d\)</span> sinon.</p>
<p>Il n’y a donc pas besoin de gérer des types de données spatiales comme dans gstat.</p>
<div class="cell">
<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><span class="fu">library</span>(GeoModels)</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>data <span class="ot">=</span><span class="fu">log</span>(df<span class="sc">$</span>zinc)</span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a>coordx <span class="ot">=</span> df<span class="sc">$</span>x</span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a>coordy <span class="ot">=</span> df<span class="sc">$</span>y</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<section id="variogramme-empirique-1" class="level2">
<h2 class="anchored" data-anchor-id="variogramme-empirique-1">Variogramme empirique</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Variogramme empirique : </span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a>start_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a>emp_vario <span class="ot">=</span> <span class="fu">GeoVariogram</span>(data, coordx, <span class="at">coordy=</span>coordy,<span class="at">numbins =</span> <span class="dv">16</span>,<span class="at">maxdist =</span> <span class="dv">2000</span>)</span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a>end_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a>end_time <span class="sc">-</span> start_time</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Time difference of 0.007436991 secs</code></pre>
</div>
<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><span class="fu">plot</span>(emp_vario,<span class="at">main =</span> <span class="st">"Variogramme empirique Zinc - GeoModels"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="modèles-supportés" class="level2">
<h2 class="anchored" data-anchor-id="modèles-supportés">Modèles supportés</h2>
<p>Voir documentation, fonction “GeoCovMatrix”</p>
<p>CorrParam, et NuisParam permettent d’obtenir la listes des paramètres requis pour les modèles.</p>
<div class="cell">
<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><span class="fu">NuisParam</span>(<span class="st">'Gaussian'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "mean" "nugget" "sill" </code></pre>
</div>
<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><span class="fu">CorrParam</span>(<span class="st">"Exp"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "scale"</code></pre>
</div>
<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><span class="fu">CorrParam</span>(<span class="st">'Matern'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "scale" "smooth"</code></pre>
</div>
</div>
</section>
<section id="estimation-du-variogramme-théorique-1" class="level2">
<h2 class="anchored" data-anchor-id="estimation-du-variogramme-théorique-1">Estimation du variogramme théorique</h2>
<p>L’estimation nécessite de donner en entrée deux listes, fixed et start, correspondant aux paramètres fixés et libres dans l’ajustement.</p>
<p>ATTENTION : si on ne fixe rien, il ne faut pas une liste vide mais un NULL, ou alors ne pas renseigner l’argument (NULL est la valeur par défaut)</p>
<p>L’algorithme vérifiera automatiquement que tous les paramètres nécessaires sont remplis. Il n’y a pas comme dans gstat de méthode qui rempli automatiquement des oublis, il faut vérifier avec les fonctions précédentes qu’on a bien tout initialisé.</p>
<div class="cell">
<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>fixed <span class="ot">=</span> <span class="cn">NULL</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a>start <span class="ot">=</span> <span class="fu">list</span>( <span class="at">mean =</span> <span class="dv">0</span> , <span class="at">sill=</span><span class="fl">0.5</span>,<span class="at">smooth=</span><span class="fl">0.7</span>,<span class="at">scale =</span> <span class="dv">300</span>,<span class="at">nugget =</span> <span class="fl">0.044</span>)</span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a>modelemat <span class="ot">=</span> <span class="fu">GeoFit</span>(data, <span class="at">coordx=</span>coordx, <span class="at">coordy=</span>coordy, <span class="at">fixed =</span> fixed, <span class="at">start=</span>start, </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 class="at">corrmodel=</span><span class="st">"Matern"</span>,<span class="at">distance=</span><span class="st">"Eucl"</span>,<span class="at">maxdist =</span> <span class="dv">2000</span>)</span>
<span id="cb41-8"><a href="#cb41-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb41-9"><a href="#cb41-9" aria-hidden="true" tabindex="-1"></a>modelemat</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
##################################################################
Maximum Composite-Likelihood Fitting of Gaussian Random Fields
Setting: Marginal Composite-Likelihood
Model: Gaussian
Type of the likelihood objects: Pairwise
Covariance model: Matern
Optimizer: Nelder-Mead
Number of spatial coordinates: 155
Number of dependent temporal realisations: 1
Type of the random field: univariate
Number of estimated parameters: 5
Type of convergence: Successful
Maximum log-Composite-Likelihood value: -37443.52
Estimated parameters:
mean nugget scale sill smooth
5.8692 0.9994 266.5697 0.5482 68.5193
##################################################################</code></pre>
</div>
<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><span class="co"># Vario fit : il faut mettre en entrée des valeurs de départ et des valeurs qu'on fixe pour les paramètres</span></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>fixed <span class="ot">=</span> <span class="fu">list</span>(<span class="at">nugget =</span> <span class="fl">0.044</span>) <span class="co">#fixe la valeur obtenue avec gstat</span></span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a>start <span class="ot">=</span> <span class="fu">list</span>( <span class="at">mean =</span> <span class="dv">0</span> , <span class="at">sill=</span><span class="fl">0.5</span>,<span class="at">smooth=</span><span class="fl">0.7</span>,<span class="at">scale =</span> <span class="dv">300</span>)</span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a>modelemat2 <span class="ot">=</span> <span class="fu">GeoFit</span>(data, <span class="at">coordx=</span>coordx, <span class="at">coordy=</span>coordy, <span class="at">fixed =</span> fixed, <span class="at">start=</span>start, </span>
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a><span class="at">corrmodel=</span><span class="st">"Matern"</span>,<span class="at">distance=</span><span class="st">"Eucl"</span>,<span class="at">maxdist =</span> <span class="dv">2000</span>)</span>
<span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a>modelemat2</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
##################################################################
Maximum Composite-Likelihood Fitting of Gaussian Random Fields
Setting: Marginal Composite-Likelihood
Model: Gaussian
Type of the likelihood objects: Pairwise
Covariance model: Matern
Optimizer: Nelder-Mead
Number of spatial coordinates: 155
Number of dependent temporal realisations: 1
Type of the random field: univariate
Number of estimated parameters: 4
Type of convergence: Successful
Maximum log-Composite-Likelihood value: -37218.17
Estimated parameters:
mean scale sill smooth
5.8673 114.4160 0.5495 1.5971
##################################################################</code></pre>
</div>
</div>
<div class="cell">
<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>fixed <span class="ot">=</span> <span class="cn">NULL</span></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>start <span class="ot">=</span> <span class="fu">list</span>( <span class="at">mean =</span> <span class="dv">0</span> , <span class="at">sill=</span><span class="fl">0.5</span>,<span class="at">scale =</span> <span class="dv">300</span>,<span class="at">nugget =</span> <span class="fl">0.044</span>)</span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a>start_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a>modelewav <span class="ot">=</span> <span class="fu">GeoFit</span>(data, <span class="at">coordx=</span>coordx, <span class="at">coordy=</span>coordy, <span class="at">fixed =</span> fixed, <span class="at">start=</span>start, </span>
<span id="cb45-7"><a href="#cb45-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-8"><a href="#cb45-8" aria-hidden="true" tabindex="-1"></a><span class="at">corrmodel=</span><span class="st">"Wave"</span>,<span class="at">distance=</span><span class="st">"Eucl"</span>,<span class="at">maxdist =</span> <span class="dv">2000</span>)</span>
<span id="cb45-9"><a href="#cb45-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-10"><a href="#cb45-10" aria-hidden="true" tabindex="-1"></a>end_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb45-11"><a href="#cb45-11" aria-hidden="true" tabindex="-1"></a>end_time <span class="sc">-</span> start_time</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Time difference of 1.352609 secs</code></pre>
</div>
<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>modelewav</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
##################################################################
Maximum Composite-Likelihood Fitting of Gaussian Random Fields
Setting: Marginal Composite-Likelihood
Model: Gaussian
Type of the likelihood objects: Pairwise
Covariance model: Wave
Optimizer: Nelder-Mead
Number of spatial coordinates: 155
Number of dependent temporal realisations: 1
Type of the random field: univariate
Number of estimated parameters: 4
Type of convergence: Successful
Maximum log-Composite-Likelihood value: -37191.31
Estimated parameters:
mean nugget scale sill
5.8669 0.3839 200.4165 0.5509
##################################################################</code></pre>
</div>
</div>
<p>On peut comparer les modèles que l’on vient de faire avec le variogramme empirique :</p>
<div class="cell">
<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="co"># Comparaison avec l'empirique matern :</span></span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a><span class="fu">GeoCovariogram</span>(modelemat, <span class="at">distance=</span><span class="st">"Eucl"</span>,<span class="at">show.vario=</span> <span class="cn">TRUE</span>,<span class="at">vario =</span> emp_vario)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid" width="672"></p>
</div>
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">GeoCovariogram</span>(modelemat2, <span class="at">distance=</span><span class="st">"Eucl"</span>,<span class="at">show.vario=</span> <span class="cn">TRUE</span>,<span class="at">vario =</span> emp_vario)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-15-2.png" class="img-fluid" width="672"></p>
</div>
<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="co"># Comparaison avec l'empirique Wave :</span></span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a><span class="fu">GeoCovariogram</span>(modelewav, <span class="at">distance=</span><span class="st">"Eucl"</span>,<span class="at">show.vario=</span> <span class="cn">TRUE</span>,<span class="at">vario =</span> emp_vario)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-15-3.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="krigeage-1" class="level2">
<h2 class="anchored" data-anchor-id="krigeage-1">Krigeage</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb52"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>locskrig <span class="ot">=</span> <span class="fu">data.frame</span> (<span class="at">x =</span>meuse.grid<span class="sc">$</span>x,<span class="at">y=</span> meuse.grid<span class="sc">$</span>y)</span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a>start_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a>krig_geomodels <span class="ot">=</span> <span class="fu">GeoKrig</span>(<span class="at">loc=</span> locskrig,<span class="at">coordx=</span>coordx, <span class="at">coordy=</span>coordy,<span class="at">corrmodel =</span> <span class="st">'Wave'</span>,<span class="at">param =</span> <span class="fu">c</span>(modelemat<span class="sc">$</span>fixed,modelewav<span class="sc">$</span>param),<span class="at">mse =</span><span class="cn">TRUE</span>,<span class="at">data=</span>data)</span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a>end_time <span class="ot"><-</span> <span class="fu">Sys.time</span>()</span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a>end_time <span class="sc">-</span> start_time</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Time difference of 1.284563 secs</code></pre>
</div>
<div class="sourceCode cell-code" id="cb54"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>coords <span class="ot">=</span> <span class="fu">cbind</span>(coordx,coordy)</span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a>Kres_pred_geo <span class="ot">=</span> <span class="fu">data.frame</span>(<span class="at">x=</span>meuse.grid<span class="sc">$</span>x,<span class="at">y=</span>meuse.grid<span class="sc">$</span>y,<span class="at">pred=</span>krig_geomodels<span class="sc">$</span>pred)</span>
<span id="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a>krig_res_geo<span class="ot"><-</span> terra<span class="sc">::</span><span class="fu">rast</span>(Kres_pred_geo, <span class="at">type=</span><span class="st">"xyz"</span>, <span class="at">crs=</span><span class="st">"EPSG:2154"</span>)</span>
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-7"><a href="#cb54-7" aria-hidden="true" tabindex="-1"></a>Kres_var_geo <span class="ot">=</span> <span class="fu">data.frame</span>(<span class="at">x=</span>meuse.grid<span class="sc">$</span>x,<span class="at">y=</span>meuse.grid<span class="sc">$</span>y,<span class="at">pred=</span>krig_geomodels<span class="sc">$</span>mse)</span>
<span id="cb54-8"><a href="#cb54-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-9"><a href="#cb54-9" aria-hidden="true" tabindex="-1"></a>krig_var_geo<span class="ot"><-</span> terra<span class="sc">::</span><span class="fu">rast</span>(Kres_var_geo, <span class="at">type=</span><span class="st">"xyz"</span>, <span class="at">crs=</span><span class="st">"EPSG:2154"</span>)</span>
<span id="cb54-10"><a href="#cb54-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-11"><a href="#cb54-11" aria-hidden="true" tabindex="-1"></a><span class="fu">plot</span>((krig_res_geo), <span class="at">main =</span> <span class="st">'Krigeage de log(zinc) - GeoModels'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" width="672"></p>
</div>
<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">plot</span>(krig_var_geo,<span class="at">main =</span> <span class="st">'Erreur de krigeage pour log(zinc) - GeoModels'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-16-2.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
</section>
<section id="package-julia-geostats" class="level1">
<h1>3 - Package Julia GeoStats</h1>
<p>Julia est un langage de programmation de haut niveau, performant et dynamique pour le calcul scientifique, avec une syntaxe familière aux utilisateurs d’autres environnements de développement similaires (Matlab, R, Scilab, Python, etc.). Il fournit un compilateur sophistiqué, un système de types dynamiques avec polymorphisme paramétré, une exécution parallèle distribuée, des appels directs de fonctions C, Fortran et Python. (source Wikipedia)</p>
<p>Un avantage de Julia est la possibilité d’écrire en LaTeX dans les lignes de code.</p>
<p>GeoStats est un package récent (avril 2015), sa dernière mise à jour date de 3 mois, c’est le package majeur en statistiques spatiales de Julia.</p>
<p>Il est disponible sur le :</p>
<p>https://juliaearth.github.io/GeoStats.jl/stable/</p>
<p>Pour l’installer faire la commande suivante dans le terminal de votre IDE : ]add GeoStats</p>
<p>Le package propose des fonctions pour de la simulation, de l’inférence et de la prédiction de champs spatiaux</p>
<p>On peut travailler en coordonnées euclidiennes et sphériques.</p>
<p>GeoStats propose de faire des modèles spatiaux. Sa particularité va être de tester tous les variogrammes disponibles (dans une même exécution) dans le package pour obtenir le plus optimisé avec les paramètres qui fonctionnent le mieux.</p>
<section id="bibliothèques-julia" class="level2">
<h2 class="anchored" data-anchor-id="bibliothèques-julia">Bibliothèques Julia</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Import package</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="im">using</span> <span class="bu">GeoDataFrames</span>,<span class="bu">Distributed</span>, <span class="bu">StatsAPI</span>, <span class="bu">Plots</span>, <span class="bu">DelimitedFiles</span>, <span class="bu">DataFrames</span>, <span class="bu">CSV</span>, <span class="bu">GeoStats</span>, <span class="bu">Rasters</span>, <span class="bu">Shapefile</span>,<span class="bu">GeoTables</span>, <span class="bu">CairoMakie; </span><span class="kw">const</span> GDF<span class="op">=</span>GeoDataFrames;<span class="im">import</span> <span class="bu">WGLMakie </span>as Mke</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="importer-les-données" class="level2">
<h2 class="anchored" data-anchor-id="importer-les-données">Importer les données</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb57"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="co">#import data</span></span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a>meuse <span class="op">=</span> CSV.<span class="fu">read</span>(<span class="st">"C:/Users/jerem/Documents/finistR2023/Stat_spatial/meuse.txt"</span>, delim<span class="op">=</span><span class="st">","</span> ,DataFrame)</span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a>meuse_grid <span class="op">=</span> CSV.<span class="fu">read</span>(<span class="st">"C:/Users/jerem/Documents/finistR2023/Stat_spatial/meuse.grid.txt"</span>, delim<span class="op">=</span><span class="st">","</span> ,DataFrame)</span>
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a>meuse_geom <span class="op">=</span> <span class="fu">georef</span>(CSV.<span class="fu">File</span>(<span class="st">"C:/Users/jerem/Documents/finistR2023/Stat_spatial/meuse.txt"</span>),(<span class="op">:</span>x,<span class="op">:</span>y,<span class="op">:</span>cadmium))</span>
<span id="cb57-5"><a href="#cb57-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb57-6"><a href="#cb57-6" aria-hidden="true" tabindex="-1"></a><span class="co">#describe data</span></span>
<span id="cb57-7"><a href="#cb57-7" aria-hidden="true" tabindex="-1"></a><span class="fu">describe</span>(meuse)</span>
<span id="cb57-8"><a href="#cb57-8" aria-hidden="true" tabindex="-1"></a><span class="fu">describe</span>(meuse_grid)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="transformation-en-données-spatiale" class="level2">
<h2 class="anchored" data-anchor-id="transformation-en-données-spatiale">Transformation en données spatiale</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="co">#add log zinc to the data</span></span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a>logmeuse <span class="op">=</span> <span class="fu">log</span>.(meuse.zinc)</span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a>meuse.logzinc <span class="op">=</span> logmeuse</span>
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a><span class="co">#create coord of data meuse</span></span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a>coord <span class="op">=</span> [(x, y) for (x, y) <span class="kw">in</span> <span class="fu">zip</span>(meuse.x, meuse.y)]</span>
<span id="cb58-7"><a href="#cb58-7" aria-hidden="true" tabindex="-1"></a><span class="co">#create spatial object of meuse</span></span>
<span id="cb58-8"><a href="#cb58-8" aria-hidden="true" tabindex="-1"></a>meuselog <span class="op">=</span> <span class="fu">georef</span>(meuse[<span class="op">:</span>,[<span class="st">"x"</span>,<span class="st">"y"</span>,<span class="st">"logzinc"</span>]],coord)</span>
<span id="cb58-9"><a href="#cb58-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-10"><a href="#cb58-10" aria-hidden="true" tabindex="-1"></a><span class="co"># list of properties with coordinates</span></span>
<span id="cb58-11"><a href="#cb58-11" aria-hidden="true" tabindex="-1"></a>props <span class="op">=</span> (logzinc<span class="op">=</span>meuse.logzinc,)</span>
<span id="cb58-12"><a href="#cb58-12" aria-hidden="true" tabindex="-1"></a>𝒟 <span class="op">=</span> <span class="fu">georef</span>(props, coord)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="variogramme" class="level2">
<h2 class="anchored" data-anchor-id="variogramme">Variogramme</h2>
<p>Le fit pour tester les différents variogramme va nous donner le SineHoleVariogram avec tous les paramètres optimisé. On décide à la base d’utiliser ce dernier mais il s’avère qu’il ne donne pas de bon résultat pour le Krigeage. On utilisera donc un variogramme de Matérn. Si vous voulez observer ce dernier dans le krigeage il suffira de mettre dans la fonction kriging, le variogramme désiré.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb59"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a><span class="co">#empirical variogram*</span></span>
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a><span class="pp">@time</span> empvario <span class="op">=</span> <span class="fu">EmpiricalVariogram</span>(meuselog, <span class="op">:</span>logzinc, maxlag <span class="op">=</span> <span class="fl">2000</span>.)</span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="co">#plot og empirical variogram</span></span>
<span id="cb59-6"><a href="#cb59-6" aria-hidden="true" tabindex="-1"></a><span class="co">#Mke.plot(empvario) plot interactive</span></span>
<span id="cb59-7"><a href="#cb59-7" aria-hidden="true" tabindex="-1"></a>CairoMakie.<span class="fu">plot</span>(empvario) <span class="co">#plot sans interaction</span></span>
<span id="cb59-8"><a href="#cb59-8" aria-hidden="true" tabindex="-1"></a><span class="co">#plot theorical variogram</span></span>
<span id="cb59-9"><a href="#cb59-9" aria-hidden="true" tabindex="-1"></a>CairoMakie.<span class="fu">plot</span>(<span class="fu">MaternVariogram</span>(range<span class="op">=</span><span class="fl">691</span>, nugget<span class="op">=</span><span class="fl">0.04</span>))</span>
<span id="cb59-10"><a href="#cb59-10" aria-hidden="true" tabindex="-1"></a>CairoMakie.<span class="fu">plot</span>(<span class="fu">SineHoleVariogram</span>(range<span class="op">=</span><span class="fl">691</span>, nugget<span class="op">=</span><span class="fl">0.25</span>))</span>
<span id="cb59-11"><a href="#cb59-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-12"><a href="#cb59-12" aria-hidden="true" tabindex="-1"></a><span class="co">#fit theorical and empirical variogramand </span></span>
<span id="cb59-13"><a href="#cb59-13" aria-hidden="true" tabindex="-1"></a><span class="pp">@time</span> fitvario <span class="op">=</span> <span class="fu">fit</span>(Variogram, empvario)</span>
<span id="cb59-14"><a href="#cb59-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-15"><a href="#cb59-15" aria-hidden="true" tabindex="-1"></a><span class="co"># CairoMakie.plot(empvario)</span></span>
<span id="cb59-16"><a href="#cb59-16" aria-hidden="true" tabindex="-1"></a><span class="co"># CairoMakie.plot!(fitvario, maxlag = 2000.)</span></span>
<span id="cb59-17"><a href="#cb59-17" aria-hidden="true" tabindex="-1"></a><span class="co"># CairoMakie.current_figure()</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="création-de-la-grille" class="level2">
<h2 class="anchored" data-anchor-id="création-de-la-grille">Création de la grille</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="co"># grid creation</span></span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert the x and y coordinates to numeric arrays</span></span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a>x_coords <span class="op">=</span> <span class="fu">Float64</span>.(meuse.x)</span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a>y_coords <span class="op">=</span> <span class="fu">Float64</span>.(meuse.y)<span class="co"># Create the Cartesian grid dimensions, origin, and spacing</span></span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a>dims <span class="op">=</span> (<span class="fl">100</span>, <span class="fl">100</span>) <span class="co"># Number of grid points in x and y directions</span></span>
<span id="cb60-6"><a href="#cb60-6" aria-hidden="true" tabindex="-1"></a>origin <span class="op">=</span> (<span class="fu">minimum</span>(x_coords), <span class="fu">minimum</span>(y_coords)) <span class="co"># Lower left corner of the grid</span></span>
<span id="cb60-7"><a href="#cb60-7" aria-hidden="true" tabindex="-1"></a>spacing <span class="op">=</span> ((<span class="fu">maximum</span>(x_coords) <span class="op">-</span> <span class="fu">minimum</span>(x_coords)) <span class="op">/</span> (dims[<span class="fl">1</span>] <span class="op">-</span> <span class="fl">1</span>),</span>
<span id="cb60-8"><a href="#cb60-8" aria-hidden="true" tabindex="-1"></a> (<span class="fu">maximum</span>(y_coords) <span class="op">-</span> <span class="fu">minimum</span>(y_coords)) <span class="op">/</span> (dims[<span class="fl">2</span>] <span class="op">-</span> <span class="fl">1</span>)) </span>
<span id="cb60-9"><a href="#cb60-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Cell spacing in x and y directions# Create the CartesianGrid</span></span>
<span id="cb60-10"><a href="#cb60-10" aria-hidden="true" tabindex="-1"></a>𝒢 <span class="op">=</span> <span class="fu">CartesianGrid</span>(dims, origin, spacing)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="krigeage-2" class="level2">
<h2 class="anchored" data-anchor-id="krigeage-2">Krigeage</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb61"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a>𝒫 <span class="op">=</span> <span class="fu">EstimationProblem</span>(𝒟, 𝒢, <span class="op">:</span>logzinc)</span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-3"><a href="#cb61-3" aria-hidden="true" tabindex="-1"></a><span class="pp">@time</span> 𝒮 <span class="op">=</span> <span class="fu">Kriging</span>(<span class="op">:</span>logzinc <span class="op">=></span> (variogram<span class="op">=</span><span class="fu">MaternVariogram</span>(nugget <span class="op">=</span> <span class="fl">0.21</span>,sill <span class="op">=</span> <span class="fl">0.59</span>,range<span class="op">=</span><span class="fl">706.78</span>),))</span>
<span id="cb61-4"><a href="#cb61-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-5"><a href="#cb61-5" aria-hidden="true" tabindex="-1"></a><span class="co"># perform estimation</span></span>
<span id="cb61-6"><a href="#cb61-6" aria-hidden="true" tabindex="-1"></a><span class="pp">@time</span> Ω <span class="op">=</span> <span class="fu">solve</span>(𝒫, 𝒮)</span>
<span id="cb61-7"><a href="#cb61-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb61-8"><a href="#cb61-8" aria-hidden="true" tabindex="-1"></a>CairoMakie.<span class="fu">plot</span>(Ω.geometry, color <span class="op">=</span> Ω.logzinc_variance)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="affichage-du-krigeage" class="level2">
<h2 class="anchored" data-anchor-id="affichage-du-krigeage">Affichage du Krigeage</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Export Kriging</span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a>GeoTables.<span class="fu">save</span>(<span class="st">"C:/Users/jerem/Documents/finistR2023/Stat_spatial/Kriging2.shp"</span>, Ω,force<span class="op">=</span><span class="cn">true</span> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1360100</code></pre>
</div>
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="co">#read kriging</span></span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a>shape <span class="op">=</span> Shapefile.<span class="fu">Handle</span>(<span class="st">"C:/Users/jerem/Documents/finistR2023/Stat_spatial/Kriging2.shp"</span>).shapes</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>10000-element Vector{Union{Missing, Shapefile.Polygon}}:
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
⋮
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)
Polygon(5 Points)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb66"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="co"># read grid </span></span>
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a>grid_shp <span class="op">=</span> Shapefile.<span class="fu">Handle</span>(<span class="st">"C:/Users/jerem/Documents/finistR2023/Stat_spatial/boundary1.shp"</span>).shapes</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1-element Vector{Union{Missing, Shapefile.Polygon}}:
Polygon(252 Points)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb68"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb68-2"><a href="#cb68-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Define a reducer function (e.g., to sum values within each cell)</span></span>
<span id="cb68-3"><a href="#cb68-3" aria-hidden="true" tabindex="-1"></a><span class="fu">reducer</span>(cell_values) <span class="op">=</span> <span class="fu">sum</span>(cell_values)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>reducer (generic function with 1 method)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb70"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb70-2"><a href="#cb70-2" aria-hidden="true" tabindex="-1"></a>rasterized_krig <span class="op">=</span> <span class="fu">rasterize</span>(shape, fill<span class="op">=</span>Ω.logzinc, reducer <span class="op">=</span> reducer, size<span class="op">=</span>(<span class="fl">100</span>, <span class="fl">100</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>100×100 Raster{Union{Missing, Float64},2} nothing with dimensions:
X Projected{Float64} 178605.0:28.13131313131307:181390.0 ForwardOrdered Regular Intervals,
Y Projected{Float64} 329714.0:39.363636363636466:333611.0 ForwardOrdered Regular Intervals
extent: Extent(X = (178605.0, 181418.1313131313), Y = (329714.0, 333650.36363636365))missingval: missingparent:
329714.0 … 3.33532e5 3.33572e5 333611.0
178605.0 6.89297 6.85203 6.83774 6.82406
1.78633e5 6.89086 6.86063 6.84598 6.83195
1.78661e5 6.88559 6.86938 6.85437 6.83998
1.78689e5 6.87608 6.87828 6.86289 6.84815
1.78718e5 6.8609 … 6.88733 6.87157 6.85647
⋮ ⋱ ⋮
1.81249e5 6.23662 5.92034 6.04422 6.14844
1.81277e5 6.24292 5.78752 5.919 6.03419
1.81306e5 6.24942 5.69181 5.82022 5.93852
1.81334e5 6.25608 … 5.63253 5.74922 5.86355
1.81362e5 6.26287 5.60164 5.70297 5.80864
181390.0 6.26976 5.59055 5.67657 5.7714</code></pre>
</div>
<div class="sourceCode cell-code" id="cb72"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a>Plots.<span class="fu">plot</span>(rasterized_krig)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-23-J1.png" class="img-fluid" width="300"></p>
</div>
<div class="sourceCode cell-code" id="cb73"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a><span class="co">#Crop </span></span>
<span id="cb73-3"><a href="#cb73-3" aria-hidden="true" tabindex="-1"></a>masked <span class="op">=</span> <span class="fu">mask</span>(rasterized_krig,with<span class="op">=</span>grid_shp)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>100×100 Raster{Union{Missing, Float64},2} nothing with dimensions:
X Projected{Float64} 178605.0:28.13131313131307:181390.0 ForwardOrdered Regular Intervals,
Y Projected{Float64} 329714.0:39.363636363636466:333611.0 ForwardOrdered Regular Intervals
extent: Extent(X = (178605.0, 181418.1313131313), Y = (329714.0, 333650.36363636365))missingval: missingparent:
329714.0 … 3.33532e5 3.33572e5 333611.0
178605.0 missing missing missing missing
1.78633e5 missing missing missing missing
1.78661e5 missing missing missing missing
1.78689e5 missing missing missing missing
1.78718e5 6.8609 … missing missing missing
⋮ ⋱ ⋮
1.81249e5 missing 5.92034 6.04422 missing
1.81277e5 missing 5.78752 missing missing
1.81306e5 missing missing missing missing
1.81334e5 missing … missing missing missing
1.81362e5 missing missing missing missing
181390.0 missing missing missing missing</code></pre>
</div>
<div class="sourceCode cell-code" id="cb75"><pre class="sourceCode julia code-with-copy"><code class="sourceCode julia"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a>Plots.<span class="fu">plot</span>(masked, title <span class="op">=</span> <span class="st">"Krigeage logzinc"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="terraR_files/figure-html/unnamed-chunk-23-J2.png" class="img-fluid" width="300"></p>
</div>
</div>
</section>
</section>
<section id="comparaison" class="level1">
<h1>4 - Comparaison</h1>
<table class="table">
<thead>
<tr class="header">
<th>Package</th>
<th>R : gstat</th>
<th>R : GeoModels</th>
<th>Julia : GeoStats</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Type de données d’entrée</td>
<td>sp, sf</td>
<td>matrices, vecteurs</td>
<td><p>dataframes</p>
<p>Coordonées de type CartesianGrid</p>
<p>Inconvénient : peu de documentation - difficile à prendre en main</p></td>
</tr>
<tr class="even">
<td>Documentation</td>
<td>CRAN</td>
<td><p>CRAN</p>
<p>https://vmoprojs.github.io/GeoModels-page/</p></td>
<td><p><a href="https://github.com/JuliaEarth/GeoStats.jl" class="uri">https://github.com/JuliaEarth/GeoStats.jl</a></p>
<p>https://juliapackages.com/p/geostats</p></td>
</tr>
<tr class="odd">
<td>Estimation de paramètres du variogramme</td>
<td><p>Moindres carrés sur variogramme empirique</p>
<p>1 modèle à la fois au choix</p></td>
<td><p>Vraisemblance composite sur les données</p>
<p>1 modèle à la fois</p>
<p>Paramètres fixes ou variables au choix</p></td>
<td><p>Moindres carrés sur le variogramme empirique</p>
<p>Tous les modèles disponibles à la fois</p></td>
</tr>
<tr class="even">
<td>Vitesse d’exécution - estimation du variogramme empirique</td>
<td>0.05s</td>
<td>0.07s</td>
<td>0.012s</td>
</tr>
<tr class="odd">
<td>Vitesse d’exécution - estimation du variogramme théorique</td>
<td>0.04s</td>
<td>1.15s</td>
<td>0.05s (TOUS LES MODELES)</td>
</tr>
<tr class="even">
<td>Vitesse d’exécution - krigeage</td>
<td>0.42s</td>
<td>0.87s</td>
<td><p>0.08s (Kriging)</p>
<p>10.6s (solve)</p></td>
</tr>
</tbody>
</table>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', 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!");
setTimeout(function() {
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
tippyHover(ref, function() {
var popup = window.document.createElement('div');
cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
});
</script>
</div> <!-- /content -->