###############################################
##subset data
###############################################
set.seed(1234)
data(BEF)
n.subset <- 100
BEF.subset <- BEF[sample(1:nrow(BEF),n.subset),]
##############################################
##general ggt.sp setup and model fit
##############################################
##Specify the priors, hyperparameters, and variance parameter starting values.
sigmasq.prior <- prior(dist="IG", shape=2, scale=30)
tausq.prior <- prior(dist="IG", shape=2, scale=30)
##the prior on phi corresponds to a prior of 500-2000 meters
##for the effective range (i.e., -log(0.05)/0.0015, -log(0.05)/0.006, when
##using the exponential covariance function).
phi.prior <- prior(dist="LOGUNIF", a=0.0015, b=0.006)
var.update.control <- list("sigmasq"=list(sample.order=0, starting=30, tuning=0.5, prior=sigmasq.prior),
"tausq"=list(sample.order=0, starting=30, tuning=0.5, prior=tausq.prior),
"phi"=list(sample.order=0, starting=0.006, tuning=0.8, prior=phi.prior))
##specify the number of samples
run.control <- list("n.samples" = 1500)
##specify some model, assign the prior and starting values for the regressors
model <- BE_BA_AREA~ELEV+SPR_02_TC1+SPR_02_TC2+SPT_02_TC3
##note, precision of 0 is same as flat prior.
beta.prior <- prior(dist="NORMAL", mu=rep(0, 5), precision=diag(0, 5))
beta.control <- list(beta.update.method="gibbs", beta.starting=rep(0, 5), beta.prior=beta.prior)
model.coords <- cbind(BEF.subset$XUTM, BEF.subset$YUTM)
ggt.sp.out <-ggt.sp(formula=model, data=BEF.subset,
coords=model.coords,
var.update.control=var.update.control,
beta.control=beta.control,
cov.model="exponential",
run.control=run.control, DIC=TRUE, verbose=TRUE)
print(ggt.sp.out$accept)
print(ggt.sp.out$DIC)
##############################################
##recover random spatial effect
##Y = X^tB + w + e, where w is the random
##spatial effect and e is pure error
##############################################
##get the spatial random effect w
sp.effect.out <- sp.effect(ggt.sp.out, start=500, thin=2)
w <- rowMeans(sp.effect.out$pp.samples)
par(mfrow=c(1,2))
int.obs <- interp(BEF.subset$XUTM, BEF.subset$YUTM, BEF.subset$BE_BA_AREA)
image(int.obs, xlab="Longitude (meters)", ylab="Latitude (meters)", main="Observed density
of American beech (Y)")
contour(int.obs, add=TRUE)
int.w <- interp(BEF.subset$XUTM, BEF.subset$YUTM, w)
image(int.w, xlab="Longitude (meters)", ylab="Latitude (meters)", main="Spatial random effect density (w)")
contour(int.w, add=TRUE)Run the code above in your browser using DataLab