This is the R code for performing Generalized Least Squares regression without (mod0
) and with spatial autocorrelation (mod1
), using the gls
function in the library nlme
:
# first load the nlme library
library(nlme)
# read data (forest recovery after forest fire)
# 30000 cells from satellite imagery
datos - read.csv(‘variables.csv’, sep=’;’, head=T)
# create matrices to store the results
sign_pre - matrix(0, nrow=1000, ncol=1)
anov_pre - matrix(0, nrow=1000, ncol=1)
# bootstrap loop
for (i in c(1:1000)) {
# random sampling
sample - datos[sample(1:30000, 30),]
# model without autocorrelation
mod0 - gls(SLOPE.AGO ~ 1 + PRECIP, data=sample)
# model with autocorrelation
mod1 - update(mod0, correlation=corExp(form=~X+Y))
# the two models are nested, so we can use anova for comparing them
anov_pre[i] - anova(mod0, mod1)[2,9]
# store the p‑value associated to the independent variable
sign_pre[i] - summary(mod1)$tTable[8]
}
# write results to disk
write(sign_pre, file=‘sign_pre.csv’, sep=’;’)
write(anov_pre, file=‘sign_pre.csv’, sep=’;’)
Sin comentarios