6.4 Spatial Autocorrelation
Spatial autocorrelation can be simulate by passing a spatial correlation matrix to the cov_str
argument of simulate_population()
.
First we need some kind of spatial autocorrelation matrix. This is a large correlation matrix, showing how close all the locations are to each other. We create different kinds of these. Here, we use the corClasses
structures in the nlme
package. To demonstrate, we generate a exponential spatial correlation matrix, with strong autocorrelation.
library(nlme)
##
## Attaching package: 'nlme'
## The following object is masked from 'package:lme4':
##
## lmList
## The following object is masked from 'package:dplyr':
##
## collapse
library(squidSim)
<- make_structure("location(2500)")
ds <- matrix(1:2500,nrow=50,ncol=50)
locations <-as.data.frame(t(sapply(1:2500,function(x)which(locations==x, arr.ind=TRUE))))
locations2colnames(locations2) <- c("x","y")
<- corExp(5, form = ~ x + y)
cs1Exp <- Initialize(cs1Exp, locations2)
cs1Exp <- corMatrix(cs1Exp)
spMcolnames(spM) <- rownames(spM) <- 1:2500
We can feed this matrix into the cor_str
argument of simulate_population()
. Here we simulate with and without spatial autocorrelation to show the difference.
<-simulate_population(
sim_dat_autoseed=22,
data_structure = ds,
parameters = list(
location = list(vcov=4),
residual = list(vcov=1)
),cov_str = list(location=spM)
)<-get_population_data(sim_dat_auto)
dat_auto
<-simulate_population(
sim_dat_noseed=27,
data_structure = ds,
parameters = list(
location = list(vcov=4),
residual = list(vcov=1)
)
)<-get_population_data(sim_dat_no)
dat_no
library(viridis)
## Loading required package: viridisLite
##
## Attaching package: 'viridis'
## The following object is masked from 'package:scales':
##
## viridis_pal
<- viridis(20)
color_scale
$y_group <- as.numeric(cut(dat_auto$y, breaks=20))
dat_auto$y_group <- as.numeric(cut(dat_no$y, breaks=20))
dat_no
par(mfrow=c(1,2))
plot(y~x,locations2, pch=19, col=color_scale[dat_auto$y_group], cex=1, main="Spatial Autocorrelation")
plot(y~x,locations2, pch=19, col=color_scale[dat_no$y_group], cex=1, main="No Autocorrelation")
6.4.1 Example: Species occurrence data with spatial autocorrelation
library(nlme)
library(squidSim)
<- make_structure("location(2500)")
ds <- matrix(1:2500,nrow=50,ncol=50)
locations <-as.data.frame(t(sapply(1:2500,function(x)which(locations==x, arr.ind=TRUE))))
locations2colnames(locations2) <- c("x","y")
<- corExp(3, form = ~ x + y)
cs1Exp <- Initialize(cs1Exp, locations2)
cs1Exp <- corMatrix(cs1Exp)
spMcolnames(spM) <- rownames(spM) <- 1:2500
<-simulate_population(
sim_datseed=236,
data_structure = ds,
n_response=2,
response_names = c("occurrence","observation"),
parameters = list(
intercept= c(-3,0),
location = list(vcov=c(4,0)),
# age=list(covariate=TRUE, beta=matrix(c(-0.2,0),ncol=2)),
residual=list(vcov=c(0,0))
),cov_str = list(location=spM),
family= "binomial",
link="probit"
)<-get_population_data(sim_dat)
dat$seen <- dat$occurrence* dat$observation
dat
<-simulate_population(
sim_dat_Rseed=224,
data_structure = ds,
n_response=2,
response_names = c("occurrence","observation"),
parameters = list(
intercept= c(-3,0),
residual=list(vcov=c(4,0))
),family= "binomial",
link="probit"
)<-get_population_data(sim_dat_R)
dat_R$seen <- dat_R$occurrence* dat_R$observation
dat_R
par(mfrow=c(1,2))
plot(y~x,locations2, pch=19, col=dat$occurrence)
points(y~x,locations2[dat$seen==1,], pch=19, col="yellow", cex=0.5)
plot(y~x,locations2, pch=19, col=dat_R$occurrence)
points(y~x,locations2[dat_R$seen==1,], pch=19, col="yellow", cex=0.5)