###############################################
##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)
###############################################
##prediction
###############################################
##resample
n.pred <- 100
BEF.pred <- BEF[sample(1:nrow(BEF),n.pred),]
pred.coords <- cbind(BEF.pred$XUTM, BEF.pred$YUTM)
pred <- sp.predict(ggt.sp.out, pred.joint=TRUE, pred.coords=pred.coords, pred.covars=BEF.pred, start=500, thin=2)
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")
contour(int.obs, add=TRUE)
int.pred <- interp(BEF.pred$XUTM, BEF.pred$YUTM, rowMeans(pred$pp.samples))
image(int.pred, xlab="Longitude (meters)", ylab="Latitude (meters)", main="Predicted density
of American beech")
contour(int.pred, add=TRUE)Run the code above in your browser using DataLab