-
Notifications
You must be signed in to change notification settings - Fork 1
/
Field-Trial-Spatial-Analysis-Guide.tex
8239 lines (4903 loc) · 203 KB
/
Field-Trial-Spatial-Analysis-Guide.tex
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
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{book}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math} % this also loads fontspec
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
% xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\usepackage{booktabs}
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\usepackage[]{natbib}
\bibliographystyle{apalike}
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
pdftitle={Incorporating Spatial Analysis into Agricultural Field Experiments.},
pdfauthor={Julia Piaskowski; William Price},
hidelinks,
pdfcreator={LaTeX via pandoc}}
\title{Incorporating Spatial Analysis into Agricultural Field Experiments.}
\author{Julia Piaskowski\footnote{University of Idaho, \href{mailto:[email protected]}{\nolinkurl{[email protected]}}} \and William Price\footnote{University of Idaho, \href{mailto:[email protected]}{\nolinkurl{[email protected]}}}}
\date{August 21, 2023}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{1}
\tableofcontents
}
\hypertarget{preface}{%
\chapter{Preface}\label{preface}}
\begin{center}\includegraphics[width=0.9\linewidth]{img/wheat_field_van_gogh} \end{center}
Vincent Van Gogh
\hypertarget{tutorial-goal}{%
\section{Tutorial goal}\label{tutorial-goal}}
\emph{To help people conducting planned agricultural field trials understand and incorporate spatial variation routinely into analysis of field trials.}
Current educational resources are focused largely on geospatial applications that typically require a a moderate to deep understanding of mapping tools and spatial analytic techniques. Furthermore, there is not a comprehensive resources for spatial analytic techniques for field experiments that is also freely available. This tutorial is intended to fill that gap.
\hypertarget{prerequisites}{%
\section{Prerequisites}\label{prerequisites}}
In order to run the scripts in this demonstration, you will to download R, available free through the \href{https://cran.r-project.org/}{Comprehensive R Archive Network} (CRAN). While this is sufficient for running R scripts, You may also find it helpful to use RStudio, which provides a nice graphical user interface for R. RStudio can be downloaded \href{https://www.rstudio.com/products/rstudio/download/}{here}.
If you already have R installed, please make sure you have version 4.0.0 or newer.
This demonstration is not intended to provide instructions on general R usage. However, There are numerous web resources for learning the Basics of R. \href{https://software-carpentry.org/lessons/}{Software carpentry} offers several lesson plans covering the foundation of R.
\hypertarget{r-requirements}{%
\section{R requirements}\label{r-requirements}}
This tutorial was built using R version 4.1 (``Camp Pontanezen''). R session information is provided in section \ref{the-end}.
\textbf{R Packages used in this tutorial}
\begin{longtable}[]{@{}ll@{}}
\toprule\noalign{}
Package & Usage in This Tutorial \\
\midrule\noalign{}
\endhead
\bottomrule\noalign{}
\endlastfoot
dplyr, tidyr, purrr & basic data manipulation \\
ggplot, desplot & plotting \\
agridat & contains demonstration data sets \\
sp, sf & standard manipulation of spatial objects \\
spdep & spatial dependence functions \\
gstat & empirical variogram estimation \\
nlme, lme4 & mixed model analysis \\
emmeans & extract treatments means \\
spaMM & Matérn covariance structure \\
SpATS & spatial splines for field trials \\
breedR & mixed modelling with AR1xAR1 estimation \\
\end{longtable}
All packages aside from \textbf{breedR} are available on CRAN. The package \textbf{breedR}, is available on GitHub can be installed within R with the following code:
\begin{verbatim}
remotes::install_github("famuvie/breedR")
\end{verbatim}
\hypertarget{sas-requirements}{%
\section{SAS requirements}\label{sas-requirements}}
In order to run the SAS portion of this tutorial, a valid copy of SAS Base and Stat products and a current SAS license are required. This tutorial was built using SAS 9.4 (TS1M5). Although older versions of SAS may also work, we have not evaluated this. Users can also consider downloading and using a free version of \href{https://www.sas.com/en_us/software/on-demand-for-academics/references/getting-started-with-sas-ondemand-for-academics-studio.html}{SAS® On Demand for Academics: Studio}.
\textbf{SAS procedures used in this tutorial}
\begin{longtable}[]{@{}ll@{}}
\toprule\noalign{}
Procedure & Usage in This Tutorial \\
\midrule\noalign{}
\endhead
\bottomrule\noalign{}
\endlastfoot
FORMAT, DATA, PRINT & basic data input, manipulation, and display \\
SORT, RANK & sort and rank estimated means \\
SGPLOT & plotting \\
MIXED, GLIMMIX & mixed model analysis \\
VARIOGRAM & empirical variogram estimation \\
\end{longtable}
\hypertarget{contributors}{%
\section{Contributors}\label{contributors}}
\href{mailto:[email protected]}{Julia Piaskowski} wrote the R sections and William Price wrote the SAS portions of this tutorial.
This book was written in \href{https://bookdown.org/yihui/bookdown}{bookdown}.
\hypertarget{license}{%
\section{License}\label{license}}
Incorporating Spatial Analysis into Agricultural Field Experiments by Julia Piaskowski and William Price is licensed under a \href{https://creativecommons.org/licenses/by-nc/4.0/}{Creative Commons Attribution-NonCommercial 4.0 International License}
\begin{center}\includegraphics[width=0.2\linewidth]{img/CC-by-nc} \end{center}
\hypertarget{intro}{%
\chapter{Introduction}\label{intro}}
\hypertarget{why-care-about-spatial-variation}{%
\section{Why care about spatial variation?}\label{why-care-about-spatial-variation}}
The goal of many agricultural field trials is to provide information about crop response to a set a treatments such as soil treatments, disease pressure or crop genetic variation. Agricultural field trials employ common experimental designs such as randomized complete block design to account for environmental heterogeneity. However, those techniques are quite often inadequate to fully account for spatial heterogeneity that arises due to field position, soil conditions, disease, wildlife impacts and more.
\begin{center}\includegraphics[width=0.9\linewidth]{img/plant_sciences_farm} \end{center}
\emph{University Research Farm}
When spatial autocorrelation is not accounted for in an analysis, the result can be incorrect treatment estimates, correlated errors (that violate the assumption of linear models and invalidate the analysis) and low experimental power. Incorporating spatial correlation between experimental plots can improve the overall accuracy and precision of these estimates.
\hypertarget{diagnosing-spatial-auto-correlation}{%
\section{Diagnosing spatial auto-correlation}\label{diagnosing-spatial-auto-correlation}}
Spatial correlation is similarity of plots that are close to one another. That correlation is expected to decline with distance. This is different from experiment-wide gradients, such as a salinity gradient or position on a slope.
\hypertarget{morans-i}{%
\subsection{Moran's I}\label{morans-i}}
Moran's I, sometimes called ``Global Moran's I'' is similar to a correlation coefficient. It is a test for correlation between units (plots in our case).
\[ I = \frac{N}{W}\frac{\sum_i \sum_j w_{ij} (x_i - \bar{x})(x_j - \bar{x})}{\sum_i(x_i - \bar{x})^2}
\qquad i \neq j\]
and \(j\), x is the variable of interest, \(w_{ij}\) are a spatial weights between each \(i\) and \(j\), and W is the sum of all weights. The expected values of Moran's I is \(-1/(N-1)\). Values greater than that indicate positive spatial correlation (areas close to each other are similar), while values less than the expected Moran's I indicate dissimilarity as spatial distance between points decreases.
Where N is total number of spatial locations indexed by \(i\)
There are several options for defining adjacent neighbors and how to weight each neighbor's influence. The two common configurations for defining neighbors are the rook and queen configurations. These are exactly what their chess analogy suggests: ``rook'' defines neighbors in an row/column fashion, while ``queen'' defines neighbors in a row/column configuration an also neighbors located diagonally at a 45 degree angle from the row/column neighbors. Determining this can be somewhat complicated when working with irregularly-placed data (e.g.~county seats), but is quite unambiguous for lattice data common in planned field experiments:
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/unnamed-chunk-5-1.pdf}
Another test for diagnosing spatial correlation is Geary's C:
\[ I = \frac{(N -1)}{2W}\frac{\sum_i \sum_j w_{ij} (x_i - x_j)^2}{\sum_i(x_i - \bar{x})^2} \qquad i \neq j\]
These terms have the same meaning in Moran's I. The expected value of Geary's C is 1. Values higher than 1 indicate positive spatial correlation and less than 1 indicate negative spatial correlation.
\hypertarget{empirical-variogram-semivariance}{%
\subsection{Empirical variogram \& semivariance}\label{empirical-variogram-semivariance}}
An empirical variogram is a visual tool for understanding how error terms are related to each other over spatial distance. It relies on semivariance (\(\gamma\)), a statistic expressing variance as a function of pairwise distances between data points at points \(i\) and \(j\).
\[\gamma(h) = \frac{1}{2|N(h)|}\sum_{N(h)}(x_i - x_j)^2\]
Semivariances are binned for distance intervals. The average values for semivariance and distance interval can be fit to correlated error models such a exponential, spherical, Gaussian and Matérn. How to do this is explored further in \ref{background} of this guide.
Three important concepts of an empirical variogram are \emph{nugget}, \emph{sill} and \emph{range}
\begin{figure}
\centering
\includegraphics{img/Sadoti2014_spherical.jpg}
\caption{Example Empirical Variogram}
\end{figure}
\begin{itemize}
\tightlist
\item
range = distance up to which is there is spatial correlation
\item
sill = uncorrelated variance of the variable of interest
\item
nugget = measurement error, or short-distance spatial variance and other unaccounted for variance
\end{itemize}
\textbf{2 other concepts:}
\begin{itemize}
\tightlist
\item
partial sill = sill - nugget
\item
nugget effect = the nugget/sill ratio, interpreted opposite of \(r^2\)
\end{itemize}
\hypertarget{background}{%
\chapter{Spatial Models}\label{background}}
This section contains some of the statistical background behind why spatial models are used and how they work. Understanding this section is not essential, but it is extremely helpful. This section relies on information introduced in \ref{intro}, so please make sure you have read that section if you are new to empirical variograms and spatial statistics.
General linear statistical models are commonly modeled as thus:
\[Y_i = \beta_0 + X_i\beta_1 + \epsilon_i\]
\(\beta_1\) is a slope describing the relationship between a continuous variable and the dependent variable, \(Y_i\). If \(X_i\) is a categorical variable, such as a crop variety, then there will be \(p-1\) slopes estimated, where p is the number of unique treatments levels in \(X\).
The error terms, \(\epsilon_i\) are assumed normally distributed with a mean of zero and a variance of \(\sigma^2\) :
\[e_i ~\sim N(0, \sigma^2)\]
The error terms, or residuals, are assumed to be \emph{identically} and \emph{independently} distributed (sometimes abbreviated ``iid''). This implies a constant variance of the error terms and zero covariance between residuals.
If N = 3, the expanded model looks like this:
\[\left[ {\begin{array}{ccc} Y_1\\ Y_2\\ Y_3 \end{array} } \right] = \beta_0 +
\left[ {\begin{array}{ccc} X_1\\ X_2\\ X_3 \end{array} } \right] \beta_1 +
\left[ {\begin{array}{ccc} \epsilon_1\\ \epsilon_2\\ \epsilon_3 \end{array} } \right] \]
\[e_i ~\sim N \Bigg( 0,
\left[ {\begin{array}{ccc} \sigma^2 & 0 & 0 \\ 0 & \sigma^2 & 0\\ 0 & 0 & \sigma^2\end{array} } \right] \Bigg) \]
If spatial variation is present, the off-diagonals of the variance-covariance matrix are not zero - hence the error terms are not independently distributed. As a result, hypotheses test and parameter estimates from uncorrected linear models will provide erroneous results.
\hypertarget{correlated-error-models}{%
\section{Correlated error models}\label{correlated-error-models}}
\hypertarget{distance-based-correlation-error-models}{%
\subsection{Distance-based correlation error models}\label{distance-based-correlation-error-models}}
There are mathematical tools for modelling how error terms are correlated with each other based on pairwise physical distance between observations. These models can be used to weight observations. Often, the data are assumed to be \emph{isotropic}, where distance but not direction impacts the spatial error correlation.
There are several methods for estimating the semivariance as a direct function of distance.
\hypertarget{exponential}{%
\subsubsection{Exponential}\label{exponential}}
\[ \gamma (h)\left\{ {\begin{array}{cc} 0 & h = 0\\ C_0+C_1 \left [ 1-e^{-(\frac{h}{r})} \right] & h > 0 \end{array} } \right. \]
where
\[ C_0 = nugget \\ C_1 = partial \: sill \\ r = range\]
\begin{figure}
{\centering \includegraphics[width=0.7\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/Exp-CE-fig-1}
}
\caption{Exponential Model}\label{fig:Exp-CE-fig}
\end{figure}
\(3r = r_p\) is the ``practical range'', which is 95\% of the true value for \(C_1\).
\hypertarget{gaussian}{%
\subsubsection{Gaussian}\label{gaussian}}
(a squared version of the exponential model)
\[ \gamma (h)\left\{ {\begin{array}{cc} 0 & h = 0\\ C_0+C_1 \left [ 1-e^{-(\frac{h}{r})^2} \right] & h > 0 \end{array} } \right. \]
where
\[ C_0 = nugget \\ C_1 = partial \: sill \\ r = range\]
\begin{figure}
{\centering \includegraphics[width=0.7\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/Gau-CE-fig-1}
}
\caption{Gaussian Model}\label{fig:Gau-CE-fig}
\end{figure}
\(\sqrt 3r = r_p\) is the ``practical range'', which is 95\% of the true value for \(C_1\).
\hypertarget{spherical}{%
\subsubsection{Spherical}\label{spherical}}
\[ \gamma (h) = \left\{ {\begin{array}{cc} 0 & h = 0\\ C_0+C_1 \left[ \frac{3h}{2r}-0.5\bigg( \frac{h}{r}\bigg)^3 \right] & 0 <h \leq r \\
C_0 + C_1 & h > r \end{array} } \right. \]
where
\[ C_0 = nugget \\ C_1 = partial \: sill \\ r = range\]
\begin{figure}
{\centering \includegraphics[width=0.7\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/Sph-CE-fig-1}
}
\caption{Spherical Model}\label{fig:Sph-CE-fig}
\end{figure}
\hypertarget{other-correlated-error-distance-models}{%
\subsubsection{Other correlated error distance models}\label{other-correlated-error-distance-models}}
There are many more models - Matérn, Cauchy, Logistic - that may describe spatial correlation in a data set.
There are two addition models that have no range or sill, the linear model and power model. If your data fits these, consider doing a trend analysis.
\hypertarget{linear}{%
\subsubsection{Linear}\label{linear}}
\[ \gamma (h)=\left\{ {\begin{array}{cc} 0 & h = 0\\ C_0+C_1h & h > 0 \end{array} } \right. \]
where
\[ C_0 = nugget \\ C_1 = slope \]
\begin{figure}
{\centering \includegraphics[width=0.7\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/linear-CE-fig-1}
}
\caption{Linear Error Model}\label{fig:linear-CE-fig}
\end{figure}
There is no sill or range in the linear model, so the variance will continue to increase as a function of distance.
\hypertarget{power}{%
\subsubsection{Power}\label{power}}
\[ \gamma (h)=\left\{ {\begin{array}{cc} 0 & h = 0\\ C_0+C_1h^\lambda & h > 0 \end{array} } \right. \]
where
\[ 0 \leq \lambda <\leq 2 \\ C_0 = nugget \\ C_1 = scaling \: factor \]
\begin{figure}
{\centering \includegraphics[width=0.7\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/power-CE-fig-1}
}
\caption{Power Model}\label{fig:power-CE-fig}
\end{figure}
When \(\lambda = 1\), that is equivalent to the linear model. Example above is when \(\lambda = 0.5\) (i.e.~a square-root transformation) and \(C_1 = 1\). There is also no sill or range in the power model.
\hypertarget{matuxe9rn}{%
\subsubsection{Matérn}\label{matuxe9rn}}
\[\gamma(h) = c_0 + c_1 \bigg( 1- \frac{1}{2^{\kappa -1}\Gamma(\kappa)}
\Big( \frac{h}{\alpha} \Big) ^{\kappa} K_\kappa
\Big( \frac{h}{\alpha} \Big) \bigg)\]
Where
\[ C_0 = nugget \\ C_1 = partial \: scale \\
\alpha = smoothing \: factor \\ \kappa = covariance \: parameter
\]
\(\Gamma(\kappa)\) is the gamma function:
\[\Gamma(\kappa) = (\kappa -1)!\]
and \(\kappa(\alpha)\) is a modified bessel function:
\[ K_\kappa(t) = \frac{\Gamma(\alpha)}{2} \big( \frac{t}{2} \big) ^{-\kappa}\]
\hypertarget{correlated-error-model-for-gridded-data}{%
\subsection{Correlated error model for gridded data}\label{correlated-error-model-for-gridded-data}}
Planned field experiments often have the advantage of being arranged in regular grid pattern that can be adequately described using Euclidean space. This simplifies aspects of understanding how error terms are related by distance since the data occur in evenly spaced increments. Furthermore, in many agricultural trials, there may be no interest in spatial interpolation between units. Some of the following models work with irregularly-spaced data, but the models below are simplified forms when the experimental units are arranged in regular grid.
For example, imagine an experiment consisting of 8 plots (plot = the experiment units) arranged in 2 rows, each with 4 ranges with this layout:
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/plot-grid-fig-1.pdf}
The statistical model for that experiment:
\[
\left[ {\begin{array} \ Y_1 \\ Y_2 \\ \vdots \\ Y_n \end{array} } \right] = \beta_0 +
\left[ {\begin{array} \ X_1 \\ X_2 \\ \vdots \\ X_n \end{array} } \right] \beta_1 +
\left[ {\begin{array} \ \epsilon_1 \\ \epsilon_2 \\ \vdots \\ \epsilon_n \end{array} } \right]
\]
\(X\beta\) refer to independent variable effects. This same model is also often presented in an abbreviated matrix form:
\[ \mathbf{Y = X\beta + \epsilon}\]
\hypertarget{first-order-auto-regressive-model-ar1}{%
\subsubsection{First order auto-regressive model (AR1)}\label{first-order-auto-regressive-model-ar1}}
This assumes that variance can be modelled as exponential function based on unit distance (e.g.~row or range), either in a single direction or anistropic.
The AR1 structure across 2 rows is modeled as thus:
\[\mathbf { V_{AR(1)row}} = \sigma^2
\left[ {\begin{array}{cc}
1 & \rho \\
\rho & 1
\end{array} } \right] \otimes \mathbf{I_4}\]
This covariance model describes the correlation of observations in the \emph{row direction only.}
And similarly the AR1 structure across 4 ranges is modeled as thus:
\[ \mathbf {V_{AR(1)range}} = \sigma^2 \mathbf{I_4}
\left[ {\begin{array}{cccc}
1 & \rho & \rho^2 & \rho^3 \\
\rho & 1 & \rho & \rho^2 \\
\rho^2 & \rho & 1 & \rho \\
\rho^3 & \rho^2 & \rho & 1
\end{array} } \right] \otimes \mathbf{I_2} \]
This covariance model describes the correlation of observations in the \emph{range direction only.}
In combined AR1xAR1 model, the parameter, \(\rho\) may need to estimated separated across the row and column directions depending on the shape of the plots and site-specific field variation. Very rectangular plots are likely to require a separate estimate of the \(\rho\) parameter for each direction.
\begin{equation}
\mathbf {V_{AR(1)row} \otimes V_{AR(1)range}} = \sigma^2
\left[ {\begin{array}{cccc | cccc}
1 & \rho_1 & \rho_1^2 & \rho_1^3 & \rho_2 & \rho_1\rho_2 & \rho_1^2\rho_2 & \rho_1^3\rho_2 \\
\rho_1 & 1 & \rho_1 & \rho_1^2 & \rho_1\rho_2 & \rho_2 & \rho_1\rho_2 & \rho_1^2\rho_2\\
\rho_1^2 & \rho_1 & 1 & \rho_1 & \rho_1^2\rho_2 & \rho_1\rho_2 & \rho_2 & \rho_1\rho_2\\
\rho_1^3 & \rho_1^2 & \rho_1 & 1 & \rho_1^3\rho_2 & \rho_1^2\rho_2 & \rho_1\rho_2 & \rho_2 \\
\hline
\rho_2 & \rho_1\rho_2 & \rho_1^2\rho_2 & \rho_1^3\rho_2 & 1 & \rho_1 & \rho_1^2 & \rho_1^3 \\
\rho_1\rho_2 & \rho_2 & \rho_1\rho_2 & \rho_1^2\rho_2 & \rho_1 & 1 & \rho_1 & \rho_1^2 \\
\rho_1^2\rho_2 & \rho_1\rho_2 & \rho_2 & \rho_1\rho_2 & \rho_1^2 & \rho_1 & 1 & \rho_1 \\
\rho_1^3\rho_2 & \rho_1^2\rho_2 & \rho_1\rho_2 & \rho_2 &\rho_1^3 & \rho_1^2 & \rho_1 & 1 \\
\end{array} } \right]
\end{equation}
\textbf{Note}: \(\rho_1\) and \(\rho_2\) are the parameter estimate for \(V_{AR1(1) row}\) and \(V_{AR(1)range}\), respectively.
Please note that these error models are for modelling localised variation based on physical proximity. It is assumed that eventually a distance in the experiment can be reached in which 2 observations can be treated independent.
If there are spatial trends that extend along the entire scope of an experiment (for instance, due to position on a slope), then an additional trend analysis should be conducted.
\hypertarget{spatial-regression-methods}{%
\section{Spatial Regression methods}\label{spatial-regression-methods}}
These approaches look use information from adjacent plots to adjust for spatial auto-correlation.
\hypertarget{spatial-autoregressive-sar}{%
\subsection{Spatial autoregressive (SAR)}\label{spatial-autoregressive-sar}}
Sometimes called a ``lag'' model, the SAR model uses correlations with neighboring plots dependent variable to predict Y. The auto-regressive model explicitly models correlations between neighboring points.
\[\mathbf{Y = \rho W Y + X\beta + \epsilon} \]
While this may look strange, \(\mathbf{W}\) is an \(n\) x \(n\) matrix weighting the neighbors with a diagonal of zero so the value at \(i=j\), that is \(Y_{ijk}\) itself, is not used on the right-hand side to predict \(Y_{ijk}\) on the left-hand side of the equation. The error terms are assumed iid.
\textbf{On Weights}
Setting weights of neighbors is dealt with in the next chapter \ref{rcbd-r}.
\hypertarget{trend-analysis}{%
\section{Trend analysis}\label{trend-analysis}}
\hypertarget{row-and-column-trends}{%
\subsection{Row and column trends}\label{row-and-column-trends}}
Experiment wide-trends should be modeled with directional trend models. These are comparatively simple models:
\[Y_{ijk} = \beta_0 + X_{i1}\beta_1 + Row_{j2}\beta_2 + Range_{k3}\beta_3 +\epsilon_{ijk}\]
If the assumption of independent, normal, and identical errors are met, then this model will suffice. If spatial variation is still present, additional measures will need to be taken.
\hypertarget{splines}{%
\subsection{Splines}\label{splines}}
There is a rich field of research on using localised splines to model field heterogeneity. They are similar to trend models where the row and/or column trends are modelled. These models are complex and hence not described here.
\hypertarget{spatial-r}{%
\chapter{Identifying Spatial Variation: R}\label{spatial-r}}
\hypertarget{load-data}{%
\section{Load data}\label{load-data}}
This tutorial uses the Nebraska Interstate wheat trials, first published by Stroup et al in 1994 \citep{stroup1994} and reused extensively in field spatial variation studies.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(agridat); }\FunctionTok{library}\NormalTok{(dplyr); }\FunctionTok{library}\NormalTok{(tidyr)}
\FunctionTok{data}\NormalTok{(}\StringTok{"stroup.nin"}\NormalTok{)}
\NormalTok{Nin }\OtherTok{\textless{}{-}}\NormalTok{ stroup.nin }\SpecialCharTok{\%\textgreater{}\%} \FunctionTok{mutate}\NormalTok{(}\AttributeTok{col.width =}\NormalTok{ col }\SpecialCharTok{*} \FloatTok{1.2}\NormalTok{, }
\AttributeTok{row.length =}\NormalTok{ row }\SpecialCharTok{*} \FloatTok{4.3}\NormalTok{) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{mutate}\NormalTok{(}\AttributeTok{name =} \FunctionTok{case\_when}\NormalTok{(}\FunctionTok{is.na}\NormalTok{(}\FunctionTok{as.character}\NormalTok{(rep)) }\SpecialCharTok{\textasciitilde{}} \ConstantTok{NA\_character\_}\NormalTok{, }
\ConstantTok{TRUE} \SpecialCharTok{\textasciitilde{}} \FunctionTok{as.character}\NormalTok{(gen))) }\SpecialCharTok{\%\textgreater{}\%}
\FunctionTok{arrange}\NormalTok{(col, row)}
\NormalTok{Nin\_na }\OtherTok{\textless{}{-}} \FunctionTok{filter}\NormalTok{(Nin, }\SpecialCharTok{!}\FunctionTok{is.na}\NormalTok{(rep))}
\end{Highlighting}
\end{Shaded}
\hypertarget{examine-data}{%
\subsection{Examine data}\label{examine-data}}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{head}\NormalTok{(Nin)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## gen rep yield col row col.width row.length name
## 1 Lancer <NA> NA 1 1 1.2 4.3 <NA>
## 2 NE83407 R1 19.40 1 2 1.2 8.6 NE83407
## 3 Buckskin R1 29.85 1 3 1.2 12.9 Buckskin
## 4 NE87612 R1 28.15 1 4 1.2 17.2 NE87612
## 5 Vona R2 26.80 1 5 1.2 21.5 Vona
## 6 NE87512 R2 20.20 1 6 1.2 25.8 NE87512
\end{verbatim}
This data set actually has no missing data -- this is a balanced trial. However, there are empty fill plot with no data which creates some issues regarding NA handling.
Plot raw yield data as it appeared in the field:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(ggplot2); }\FunctionTok{library}\NormalTok{(desplot)}
\end{Highlighting}
\end{Shaded}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{ggplot}\NormalTok{(Nin, }\FunctionTok{aes}\NormalTok{(}\AttributeTok{x =}\NormalTok{ row, }\AttributeTok{y =}\NormalTok{ col)) }\SpecialCharTok{+}
\FunctionTok{geom\_tile}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{fill =}\NormalTok{ yield), }\AttributeTok{col =} \StringTok{"white"}\NormalTok{) }\SpecialCharTok{+}
\CommentTok{\#geom\_text(aes(label = name)) +}
\FunctionTok{geom\_tileborder}\NormalTok{(}\FunctionTok{aes}\NormalTok{(}\AttributeTok{group =} \DecValTok{1}\NormalTok{, }\AttributeTok{grp =}\NormalTok{ rep), }\AttributeTok{lwd =} \FloatTok{1.2}\NormalTok{) }\SpecialCharTok{+}
\FunctionTok{scale\_fill\_gradient}\NormalTok{(}\AttributeTok{low =} \StringTok{"white"}\NormalTok{, }\AttributeTok{high =} \StringTok{"blue"}\NormalTok{) }\SpecialCharTok{+}
\FunctionTok{scale\_x\_continuous}\NormalTok{(}\AttributeTok{breaks =} \FunctionTok{seq}\NormalTok{(}\DecValTok{1}\NormalTok{,}\FunctionTok{max}\NormalTok{(Nin}\SpecialCharTok{$}\NormalTok{row), }\DecValTok{1}\NormalTok{)) }\SpecialCharTok{+}
\FunctionTok{scale\_y\_continuous}\NormalTok{(}\AttributeTok{breaks =} \DecValTok{1}\SpecialCharTok{:}\FunctionTok{max}\NormalTok{(Nin}\SpecialCharTok{$}\NormalTok{col)) }\SpecialCharTok{+}
\FunctionTok{labs}\NormalTok{(}\AttributeTok{x =} \StringTok{"row"}\NormalTok{, }\AttributeTok{y =} \StringTok{"column"}\NormalTok{, }\AttributeTok{title =} \StringTok{"field plot layout"}\NormalTok{) }\SpecialCharTok{+}
\FunctionTok{theme\_classic}\NormalTok{() }\SpecialCharTok{+}
\FunctionTok{theme}\NormalTok{(}\AttributeTok{axis.text =} \FunctionTok{element\_text}\NormalTok{(}\AttributeTok{size =} \DecValTok{12}\NormalTok{),}
\AttributeTok{axis.title =} \FunctionTok{element\_text}\NormalTok{(}\AttributeTok{size =} \DecValTok{14}\NormalTok{),}
\AttributeTok{legend.title =} \FunctionTok{element\_text}\NormalTok{(}\AttributeTok{size =} \DecValTok{14}\NormalTok{),}
\AttributeTok{legend.text =} \FunctionTok{element\_text}\NormalTok{(}\AttributeTok{size =} \DecValTok{12}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=1\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/Nin-yield-layout-fig-1}
Black lines delineate the blocks.
It's also helpful to plot raw response data:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{3}\NormalTok{))}
\FunctionTok{boxplot}\NormalTok{(yield }\SpecialCharTok{\textasciitilde{}}\NormalTok{ rep, }\AttributeTok{data =}\NormalTok{ Nin, }\AttributeTok{xlab =} \StringTok{"rep"}\NormalTok{, }\AttributeTok{ylab =} \StringTok{"yield (bu/acres)"}\NormalTok{, }\AttributeTok{col =} \StringTok{"red2"}\NormalTok{, }\AttributeTok{main =} \StringTok{"yield across blocks"}\NormalTok{)}
\FunctionTok{boxplot}\NormalTok{(yield }\SpecialCharTok{\textasciitilde{}}\NormalTok{ row, }\AttributeTok{data =}\NormalTok{ Nin, }\AttributeTok{xlab =} \StringTok{"row"}\NormalTok{, }\AttributeTok{ylab =} \StringTok{"yield (bu/acres)"}\NormalTok{,}\AttributeTok{col =} \StringTok{"dodgerblue2"}\NormalTok{, }\AttributeTok{main =} \StringTok{"yield across rows"}\NormalTok{)}
\FunctionTok{boxplot}\NormalTok{(yield }\SpecialCharTok{\textasciitilde{}}\NormalTok{ col, }\AttributeTok{data =}\NormalTok{ Nin, }\AttributeTok{col =} \StringTok{"gold"}\NormalTok{, }\AttributeTok{xlab =} \StringTok{"column"}\NormalTok{, }\AttributeTok{ylab =} \StringTok{"yield (bu/acres)"}\NormalTok{,}\AttributeTok{main =} \StringTok{"yield across columns"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=0.8\linewidth]{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/Nin-boxplot-fig-1}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{par}\NormalTok{(}\AttributeTok{mfrow=}\FunctionTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{,}\DecValTok{1}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\hypertarget{test-for-spatial-autocorrelation}{%
\section{Test for spatial autocorrelation}\label{test-for-spatial-autocorrelation}}
\hypertarget{morans-i-1}{%
\subsection{Moran's I}\label{morans-i-1}}
First, run a standard linear model of the experiment:
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(nlme)}
\NormalTok{nin.lme }\OtherTok{\textless{}{-}} \FunctionTok{lme}\NormalTok{(yield }\SpecialCharTok{\textasciitilde{}}\NormalTok{ gen, }\AttributeTok{random =} \SpecialCharTok{\textasciitilde{}}\DecValTok{1}\SpecialCharTok{|}\NormalTok{rep,}
\AttributeTok{data =}\NormalTok{ Nin,}
\AttributeTok{na.action =}\NormalTok{ na.exclude)}
\end{Highlighting}
\end{Shaded}
Next, establish and weight neighbors for each plot. In this example, only adjacent neighbors in the rook formation (see \ref{background}) are used and are weighted proportionally according to their representation as neighbors to an individual. That is, if a unit has 4 adjacent neighbors, each neighbor is weighted as 0.25. If there are only two neighbors, each is weighted 0.5.
The function \texttt{cell2nb()} is a function for setting neighbors when working with data laid out in a regular grid.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(spdep)}
\NormalTok{xy\_rook }\OtherTok{\textless{}{-}} \FunctionTok{cell2nb}\NormalTok{(}\AttributeTok{nrow =} \FunctionTok{max}\NormalTok{(Nin}\SpecialCharTok{$}\NormalTok{row), }\AttributeTok{ncol =} \FunctionTok{max}\NormalTok{(Nin}\SpecialCharTok{$}\NormalTok{col), }\AttributeTok{type=}\StringTok{"rook"}\NormalTok{, }\AttributeTok{torus =} \ConstantTok{FALSE}\NormalTok{, }\AttributeTok{legacy =} \ConstantTok{FALSE}\NormalTok{) }
\end{Highlighting}
\end{Shaded}
\textbf{Make sure your data sorted by the variables assigned to row and col (in that order), and there is one and only one observation in the data set for each unique row/col combination. Even missing plots need a row.}
\emph{Rook adjacency plot for the NIN data}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(sf)}
\NormalTok{rook\_matrix }\OtherTok{\textless{}{-}} \FunctionTok{st\_as\_sf}\NormalTok{(}\FunctionTok{expand.grid}\NormalTok{(}\AttributeTok{col=}\DecValTok{1}\SpecialCharTok{:}\DecValTok{22}\NormalTok{, }\AttributeTok{row=}\DecValTok{1}\SpecialCharTok{:}\DecValTok{11}\NormalTok{), }\AttributeTok{coords=}\FunctionTok{c}\NormalTok{(}\StringTok{"col"}\NormalTok{, }\StringTok{"row"}\NormalTok{))}
\FunctionTok{plot}\NormalTok{(xy\_rook, }\AttributeTok{coords =} \FunctionTok{st\_geometry}\NormalTok{(rook\_matrix), }\AttributeTok{points =} \ConstantTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/nin-rook-adj-fg-1.pdf}
Each observation considers ``neighbors'' to be those which touch the cell in a row or column orientation, but not diagonal.
Conduct Moran's test via standard t-test and using MC sampling.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{resid\_lme }\OtherTok{\textless{}{-}} \FunctionTok{residuals}\NormalTok{(nin.lme)}
\FunctionTok{names}\NormalTok{(resid\_lme) }\OtherTok{\textless{}{-}}\NormalTok{ Nin}\SpecialCharTok{$}\NormalTok{plot}
\FunctionTok{moran.test}\NormalTok{(resid\_lme, }\FunctionTok{nb2listw}\NormalTok{(xy\_rook), }\AttributeTok{na.action =}\NormalTok{ na.exclude)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
##
## Moran I test under randomisation
##
## data: resid_lme
## weights: nb2listw(xy_rook)
## omitted: 1, 12, 23, 34, 45, 56, 59, 67, 78, 89, 100, 108, 111, 122, 133, 144, 155, 204
##
## Moran I statistic standard deviate = 8.1602, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.402504491 -0.004484305 0.002487522
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{moran.mc}\NormalTok{(resid\_lme, }\FunctionTok{nb2listw}\NormalTok{(xy\_rook), }\DecValTok{999}\NormalTok{, }\AttributeTok{na.action =}\NormalTok{ na.exclude)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
##
## Monte-Carlo simulation of Moran I
##
## data: resid_lme
## weights: nb2listw(xy_rook)
## omitted: 1, 12, 23, 34, 45, 56, 59, 67, 78, 89, 100, 108, 111, 122, 133, 144, 155, 204
## number of simulations + 1: 1000
##
## statistic = 0.4025, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater
\end{verbatim}
Plot Spatial dependence of residuals
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(purrr)}
\NormalTok{res.nn1 }\OtherTok{\textless{}{-}} \FunctionTok{map\_dbl}\NormalTok{(xy\_rook, }\ControlFlowTok{function}\NormalTok{(j) }\FunctionTok{mean}\NormalTok{(resid\_lme[j]))}
\NormalTok{rc }\OtherTok{\textless{}{-}} \FunctionTok{signif}\NormalTok{(}\FunctionTok{cor}\NormalTok{(resid\_lme, res.nn1, }\AttributeTok{use =} \StringTok{"pairwise.complete.obs"}\NormalTok{), }\DecValTok{2}\NormalTok{)}
\FunctionTok{plot}\NormalTok{(}\AttributeTok{x =}\NormalTok{ resid\_lme, }\AttributeTok{y =}\NormalTok{ res.nn1, }
\AttributeTok{main =} \FunctionTok{paste0}\NormalTok{(}\StringTok{"r = "}\NormalTok{, rc), }\AttributeTok{xlab =} \StringTok{"residual"}\NormalTok{, }\AttributeTok{ylab =} \StringTok{"average residual of neighbor (rook)"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/resid-cor-fig-1.pdf}
\hypertarget{note-on-gearys-c}{%
\subsection{Note on Geary's C}\label{note-on-gearys-c}}
At this time, the \textbf{spdep} function \texttt{geary.test()} for Geary's C does not handle missing spatial points. It cannot be used it for the NIN data set because it contains empty plots between each block with no data available for those plots.
\hypertarget{empirical-variogram-fitting}{%
\section{Empirical variogram fitting}\label{empirical-variogram-fitting}}
First, create a spatial object by adding spatial coordinates to an ordinary data frame. I like to use the actual size of the plots since they are often exaggerated rectangles that have a significantly greater length than width.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(sp)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Warning: package 'sp' was built under R version 4.3.1
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{Nin\_spatial }\OtherTok{\textless{}{-}}\NormalTok{ Nin\_na}
\FunctionTok{coordinates}\NormalTok{(Nin\_spatial) }\OtherTok{\textless{}{-}} \ErrorTok{\textasciitilde{}}\NormalTok{ col.width }\SpecialCharTok{+}\NormalTok{ row.length}
\FunctionTok{class}\NormalTok{(Nin\_spatial)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
\end{verbatim}
Set the maximum distance for calculating the variogram model (which is one-half the maximum distance between two points).
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{max\_dist }\OtherTok{=} \FloatTok{0.6}\SpecialCharTok{*}\FunctionTok{max}\NormalTok{(}\FunctionTok{dist}\NormalTok{(}\FunctionTok{coordinates}\NormalTok{(Nin\_spatial)))}
\FunctionTok{round}\NormalTok{(max\_dist, }\AttributeTok{digits =} \DecValTok{2}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] 29.9
\end{verbatim}
Calculate semivariance for an isotropic model and plot the variogram.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(gstat)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## Warning: package 'gstat' was built under R version 4.3.1
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{class}\NormalTok{(Nin\_spatial)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{head}\NormalTok{(Nin\_spatial)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## coordinates gen rep yield col row name
## 1 (1.2, 8.6) NE83407 R1 19.40 1 2 NE83407
## 2 (1.2, 12.9) Buckskin R1 29.85 1 3 Buckskin
## 3 (1.2, 17.2) NE87612 R1 28.15 1 4 NE87612
## 4 (1.2, 21.5) Vona R2 26.80 1 5 Vona
## 5 (1.2, 25.8) NE87512 R2 20.20 1 6 NE87512
## 6 (1.2, 30.1) NE87408 R3 21.90 1 7 NE87408
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{resid\_var1 }\OtherTok{\textless{}{-}}\NormalTok{ gstat}\SpecialCharTok{::}\FunctionTok{variogram}\NormalTok{(yield }\SpecialCharTok{\textasciitilde{}}\NormalTok{ rep }\SpecialCharTok{+}\NormalTok{ gen, }
\AttributeTok{cutoff =}\NormalTok{ max\_dist,}
\AttributeTok{width =}\NormalTok{ max\_dist}\SpecialCharTok{/}\DecValTok{20}\NormalTok{, }\CommentTok{\# 20 is the number of bins}
\AttributeTok{data =}\NormalTok{ Nin\_spatial)}
\FunctionTok{plot}\NormalTok{(resid\_var1)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/possible-error-1.pdf}
Test out correlated error models:
First, set the starting nugget values as the minimum of the semi-variance. There is likely more sophisticated methods to establish the starting value for nugget, but I have found that extracting the minimum value of the semivariance to work well as a starting point.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{nugget\_start }\OtherTok{\textless{}{-}} \FunctionTok{min}\NormalTok{(resid\_var1}\SpecialCharTok{$}\NormalTok{gamma)}
\end{Highlighting}
\end{Shaded}
Establish models for variogram fitting.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{Nin\_vgm1 }\OtherTok{\textless{}{-}} \FunctionTok{vgm}\NormalTok{(}\AttributeTok{model =} \StringTok{"Exp"}\NormalTok{, }\AttributeTok{nugget =}\NormalTok{ nugget\_start) }\CommentTok{\# exponential}
\NormalTok{Nin\_vgm2 }\OtherTok{\textless{}{-}} \FunctionTok{vgm}\NormalTok{(}\AttributeTok{model =} \StringTok{"Sph"}\NormalTok{, }\AttributeTok{nugget =}\NormalTok{ nugget\_start) }\CommentTok{\# spherical}
\NormalTok{Nin\_vgm3 }\OtherTok{\textless{}{-}} \FunctionTok{vgm}\NormalTok{(}\AttributeTok{model =} \StringTok{"Gau"}\NormalTok{, }\AttributeTok{nugget =}\NormalTok{ nugget\_start) }\CommentTok{\# Gaussian}
\NormalTok{Nin\_vgm4 }\OtherTok{\textless{}{-}} \FunctionTok{vgm}\NormalTok{(}\AttributeTok{model =} \StringTok{"Mat"}\NormalTok{, }\AttributeTok{nugget =}\NormalTok{ nugget\_start) }\CommentTok{\# Matern}
\end{Highlighting}
\end{Shaded}
Fit the variograms to the data:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{Nin\_variofit1 }\OtherTok{\textless{}{-}} \FunctionTok{fit.variogram}\NormalTok{(resid\_var1, Nin\_vgm1)}
\NormalTok{Nin\_variofit2 }\OtherTok{\textless{}{-}} \FunctionTok{fit.variogram}\NormalTok{(resid\_var1, Nin\_vgm2)}
\NormalTok{Nin\_variofit3 }\OtherTok{\textless{}{-}} \FunctionTok{fit.variogram}\NormalTok{(resid\_var1, Nin\_vgm3)}
\NormalTok{Nin\_variofit4 }\OtherTok{\textless{}{-}} \FunctionTok{fit.variogram}\NormalTok{(resid\_var1, Nin\_vgm4, }\AttributeTok{fit.kappa =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
\hypertarget{compare-variograms}{%
\subsection{Compare variograms}\label{compare-variograms}}
Look at the results! (this is fun)
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(resid\_var1, Nin\_variofit1, }\AttributeTok{main =} \StringTok{"Exponential model"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/unnamed-chunk-18-1.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(resid\_var1, Nin\_variofit2, }\AttributeTok{main =} \StringTok{"Spherical model"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/unnamed-chunk-18-2.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(resid\_var1, Nin\_variofit3, }\AttributeTok{main =} \StringTok{"Gaussian model"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/unnamed-chunk-18-3.pdf}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{plot}\NormalTok{(resid\_var1, Nin\_variofit4, }\AttributeTok{main =} \StringTok{"Matern model"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Field-Trial-Spatial-Analysis-Guide_files/figure-latex/unnamed-chunk-18-4.pdf}
How to pick the best one model? The attribute ``SSError'' indicates how well each model was able to predict the binned error terms as a function of distance. However, it's important to look at the model as well to see if the model was able to fit the data.
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{print}\NormalTok{(}\StringTok{"Exponential"}\NormalTok{); }\FunctionTok{attr}\NormalTok{(Nin\_variofit1, }\StringTok{"SSErr"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "Exponential"
\end{verbatim}
\begin{verbatim}
## [1] 1129.799
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{print}\NormalTok{(}\StringTok{"Spherical"}\NormalTok{); }\FunctionTok{attr}\NormalTok{(Nin\_variofit2, }\StringTok{"SSErr"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "Spherical"
\end{verbatim}
\begin{verbatim}
## [1] 1012.76
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{print}\NormalTok{(}\StringTok{"Gaussian"}\NormalTok{); }\FunctionTok{attr}\NormalTok{(Nin\_variofit3, }\StringTok{"SSErr"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "Gaussian"
\end{verbatim}
\begin{verbatim}
## [1] 752.5491
\end{verbatim}
\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{print}\NormalTok{(}\StringTok{"Matern"}\NormalTok{); }\FunctionTok{attr}\NormalTok{(Nin\_variofit4, }\StringTok{"SSErr"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## [1] "Matern"
\end{verbatim}
\begin{verbatim}
## [1] 771.4813
\end{verbatim}
\texttt{Nin\_variofit3} had the lowest error terms, corresponding to the Gaussian model.
Results from the empirical variogram:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{Nin\_variofit3}
\end{Highlighting}
\end{Shaded}