Skip to content

Commit b3273f2

Browse files
committed
Update spatial lesson
1 parent 6be53ae commit b3273f2

File tree

3 files changed

+179
-144
lines changed

3 files changed

+179
-144
lines changed

lessons/spatial_models.Rmd

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ variables.
2424

2525
## Readings
2626

27-
* The R Book p778-785 on Generalized Least Squares models with spatially
28-
correlated errors
29-
* Numerical Ecology in R, p228-238 on detecting spatial dependence.
27+
* Crawley (2007) The R Book, pp. 778-785 on Generalized Least Squares models with
28+
spatially correlated errors
29+
* Borcard et al. (2011) Numerical Ecology in R, pp. 228-238 on detecting spatial
30+
dependence.
31+
* Pinheiro and Bates (2000) Mixed-Effects Models in S and S-PLUS, pp. 249-267 on
32+
Fitting Extended Linear Mdoels with gls
3033
* https://beckmw.wordpress.com/2013/01/07/breaking-the-rules-with-spatial-correlation/
3134

3235
## Outline
@@ -336,18 +339,36 @@ error.
336339

337340
Crawley (2014) provides a straightforward description of these methods and a
338341
few examples. Pinheiro and Bates (2000) provide a more detailed discussion with
339-
more examples and they provide a useful table and figure that is helpful when
340-
deciding which error model to chose from:
342+
more examples and they provide a useful table and figure (below) that are helpful
343+
when deciding which error model to chose from:
341344

342345
This is Table 5.2 from Pinheiro and Bates (2000) in which *s* is the spatial lag
343346
and *rho* is the correlation parameter. This is a subset of the models
344347
presented in Cressie (1993).
345348
![table](figures/isotropic_variogram_models_table.png)
346349

347-
Graphically these models of spatial correlation can be visualized like this
348-
(Figure 5.9 of Pinheiro and Bates 2000):
350+
Graphically these models of spatial correlation can be visualized using variogram
351+
plots (Figure 5.9 of Pinheiro and Bates 2000):
349352
![plots](figures/isotropic_variogram_models_plots.png)
350353

354+
When we visually examine a vagriogram it is often useful to note the following
355+
three features:
356+
357+
* **nugget** - semivariance at a spatial lag of zero
358+
* **range** - the degree of difference or semivariance between two spatially
359+
independent samples
360+
* **sill** - the spatial distance at which samples are spatially independent (i.e.,
361+
variogram is flat)
362+
363+
Here is an illustration of these three terms:
364+
![variogram](figures/Schematic_variogram.png)
365+
366+
The nugget should in theory be zero because if two samples have no distance
367+
between them they should in theory be the same value, but in practice it is often
368+
necessary to have a positive valued nugget to accurately describe the spatial
369+
corelation function. Note that adding a nugget into a spatial model requires
370+
the addition of an extra parameter (i.e., an increase in model complexity).
371+
351372
### Model fitting, interpretation, and comparison
352373

353374
When we carried out ordinary regression we used the formula:
@@ -398,7 +419,7 @@ Generalized Linear Models (GLS).
398419

399420
**Note:** GLS models are distinct from General Linear Models (GLM) because GLS
400421
models are primarily concerned with modeling the covariance between samples
401-
while still adhering to the standard Normal distribution assumption for
422+
while still adhering to the standard Normal (i.e., Gaussian) distribution assumption for
402423
measurement error. In contract, GLMs are typically harnessed when the Normal
403424
distribution is not appropriate and we wish to use a different distribution for
404425
the error term such as Poisson or Binomial distributions. It is possible to
@@ -452,7 +473,7 @@ sum(vario_sr$variog * vario_sr$n.pairs) / sum(vario_sr$n.pairs)
452473
var(res)
453474
```
454475

455-
Ok so far we've learned that substrate density does not seem to be influencing
476+
Ok, so far we've learned that substrate density does not seem to be influencing
456477
richness spatially or otherwise and that there still remains autcorrelation in
457478
richness. Let's examine the most common model of spatial (and temporal)
458479
autocorrelation: the exponential model also know as Autoregressive 1 model or AR1.
@@ -474,7 +495,7 @@ plot(Variogram(sr_exp, resType='normalized', maxDist = max_dist))
474495
# actually which is a bit surprising given the output of the raw residuals
475496
476497
# let's look at the same model but with a nugget
477-
sr_exp_nug <- update(sr_exp, corr=corExp(c(0.5, 0.1), form=~x + y, nugget=T))
498+
sr_exp_nug <- update(sr_exp, corr=corExp(form=~x + y, nugget=T))
478499
# Notice above I had to put in starting values for the rate and nugget of the
479500
# spatial model. I found that if I left those off the model did not converge.
480501
# This may be due to the fact that the estimated value of the nugget is very

lessons/spatial_models.html

Lines changed: 139 additions & 132 deletions
Large diffs are not rendered by default.

scripts/utility_functions.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,15 @@ panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor=3, ...)
8080
symbols(0, 0, circles=radius, inches=FALSE, add=TRUE, fg=2)
8181
}
8282

83-
pseudo_r2 = function(glm_mod) {
84-
1 - glm_mod$deviance / glm_mod$null.deviance
83+
pseudo_r2 <- function(mod, null_mod=NULL) {
84+
if (class(mod) == 'glm')
85+
r2 <- 1 - glm_mod$deviance / glm_mod$null.deviance
86+
if (class(mod) == 'gls') {
87+
if (is.null(null_mod))
88+
null_mod <- update(mod, . ~ 1)
89+
r2 <- 1 - (as.numeric(logLik(mod) / logLik(null_mod)))
90+
}
91+
return(r2)
8592
}
8693

8794
get_spat_mods = function(gls_mod) {

0 commit comments

Comments
 (0)