###############################################
##Subset data
###############################################
set.seed(1234)
data(BEF)
n.subset <- 100
BEF.subset <- BEF[sample(1:nrow(BEF),n.subset),]
##############################################
##General ggt 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))
##Get the distance matrix.
D <- as.matrix(dist(cbind(BEF.subset$XUTM, BEF.subset$YUTM)))
##Define the variance function (e.g., exponential covariance)
var.function <- function(){
sigmasq*exp(-phi*D)+tausq*diag(n.subset)
}
##Specify the number of samples.
run.control <- list("n.samples" = 1500)
##Specify the 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 alternatively if you want a flat prior
##do not include the beta.prior tag in the beta.control list.
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)
ggt.out <- ggt(formula=model, data=BEF.subset,
var.update.control=var.update.control,
beta.control=beta.control,
var.function = var.function,
run.control=run.control, DIC=TRUE, verbose=TRUE)
print(ggt.out$accept)
print(ggt.out$DIC)
##Get the effective range of spatial dependence.
eff.range <- -log(0.05)/ggt.out$p.samples[,"phi"]
##Summarize and plot the chain with coda.
mcmc.obj <- mcmc(cbind(ggt.out$p.samples, eff.range))
summary(mcmc.obj)
##plot(mcmc.obj)
##############################################
##ggt setup and model fit with single
##block update of the variance and regression
##parameters
##############################################
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))
#Using Metropolis-Hastings and default flat prior for regression parameters.
beta.control <- list(beta.update.method="mh", sample.order=0)
ggt.out <- ggt(formula=model, data=BEF.subset,
var.update.control=var.update.control,
beta.control=beta.control,
var.function = var.function,
run.control=run.control, DIC=TRUE, verbose=TRUE)
##Note single acceptance rate.
print(ggt.out$accept)Run the code above in your browser using DataLab