Learn R Programming

spTimer (version 0.02)

spT.Gibbs: MCMC sampling for the spatio-temporal models.

Description

This function is used to draw MCMC samples using the Gibbs sampler.

Usage

spT.Gibbs(formula, data=parent.frame(), model, time.data, 
coords, knots.coords, pred.coords, pred.data, priors, initials, 
nItr, nBurn, report, tol.dist, distance.method, cov.fnc, 
scale.transform, spatial.decay, annual.aggrn)

Arguments

formula
The symnbolic description of the model equation of the regression part of the space-time model.
data
An optional data frame containing the variables in the model. If omitted, the variables are taken from environment(formula), typically the environment from which spT.Gibbs is called. The data should be ordered first by the time and then by the sites speci
model
The spatio-temporal models to be fitted, current input: "GP", "AR", and "GPP".
time.data
Defining the segments of the time-series set up using the function spT.time.
coords
The n by 2 matrix defining the locations (e.g., longitude/easting, latitude/northing) of the fitting sites, where n is the number of fitting sites.
knots.coords
The locations of the knots in similar format to coords above, only required if model="GPP".
pred.coords
The locations of the prediction sites in similar format to coords above, only required if fit and predictions are done simultaneously.
pred.data
The covariate values at the prediction sites specified by pred.coords. This should have same space-time structure as the original data frame.
priors
The prior distributions for the parameters. Default distributions are specified if these are not specified. See details in spT.priors.
initials
The preferred initial values for the parameters. If omitted, default values are provided automatically. Further details are provided in spT.initials.
nItr
Number of MCMC iterations.
nBurn
Number of burn-in samples. This number of samples will be discarded before making any inference.
report
Number of reports to display while running the Gibbs sampler. Defaults to number of iterations.
distance.method
The preferred method to calculate the distance between any two locations. The available options are "geodetic:km", "geodetic:mile", "euclidean", "maximum", "manhattan", and "canberra". See details in dist.
tol.dist
Minimum separation distance between any two locations out of those specified by coords, knots.coords and pred.coords. The default is 0.005. The programme will exit if the minimum distance is less than the non-zero specified value. This will ensure non-si
cov.fnc
Covariance function for the spatial effects. The available options are "exponential", "gaussian", "spherical" and "matern".
scale.transform
The transformation method for the response variable. Currently implemented options are: "NONE", "SQRT", and "LOG" with "NONE" as the deault.
spatial.decay
Provides the options for sampling the spatial decay parameter $\phi$. Currently implemented options are "DISCRETE", "MH" or "FIXED" and further options for each of these are specified by spT.decay. The d
annual.aggrn
This provides the options for calculating annual summary statistics by aggregating different time segments (e.g., annual mean). Currently implemented options are: "NONE", "ave" and "an4th", where "ave" = annual average, "an4th"= annual 4th highest. Only a

Value

  • acceptThe acceptance rate for the $\phi$ parameter if the "MH" method of sampling is chosen.
  • phipMCMC samples for the parameter $\phi$.
  • sig2epMCMC samples for the parameter $\sigma^2_\epsilon$.
  • sig2etapMCMC samples for the parameter $\sigma^2_\eta$.
  • betapMCMC samples for the parameter $\beta$.
  • opMCMC samples for the true observations.
  • fittedMCMC summary (mean and sd) for the fitted values.
  • sig2epMCMC samples for the parameter $\sigma^2_\epsilon$.
  • tol.distMinimum tolerance distance limit between the locations.
  • distance.methodName of the distance calculation method.
  • cov.fncName of the covariance function used in model fitting.
  • scale.transformName of the scale.transformation method.
  • sampling.sp.decayThe method of sampling for the spatial decay parameter $\phi$.
  • covariate.namesName of the covariates used in the model.
  • Distance.matrixThe distance matrix.
  • coordsThe coordinate values.
  • nTotal number of sites.
  • rTotal number of segments in time, e.g., years.
  • TTotal points of time, e.g., days within each year.
  • pTotal number of model coefficients, i.e., $\beta$'s including the intercept.
  • initialsThe initial values used in the model.
  • priorsThe prior distributions used in the model.
  • PMCCThe predictive model choice criteria obtained by minimising the expected value of a loss function, see Gelfand and Ghosh (1998). Results for both goodness of fit and penalty are given.
  • iterationsThe number of samples for the MCMC chain, without burn-in.
  • nBurnThe number of burn-in period for the MCMC chain.
  • computation.timeThe computation time required for the fitted model.
  • modelThe spatio-temporal model used for analyse the data.
  • Text OutputThis option is only applicable when fit and predictions are done simultaneously. For GP models: OutGP_Values_Parameter.txt: (nItr x parameters matrix) has the MCMC samples for the parameters, ordered as: beta's, sig2eps, sig2eta, and phi. OutGP_Stats_FittedValue.txt: (N x 2) matrix of fitted summary, with 1st column as mean and 2nd column as standard deviations, where N=nrT. OutGP_Stats_PredValue.txt: ((predsites*r*T) x 2) matrix of prediction summary, with 1st column as mean and 2nd column as standard deviations. OutGP_Values_Prediction.txt: (nItr x (predsites*r*T)) matrix of MCMC predicted values in the predicted sites. If annual.aggregation="ave" then we get text output as: OutGP_Annual_Average_Prediction.txt: (nItr x (predsites*r)) matrix. If annual.aggregation="an4th" then we get text output as: OutGP_Annual_4th_Highest_Prediction.txt: (nItr x (predsites*r)) matrix. For AR models: OutAR_Values_Parameter.txt: (nItr x parameters matrix) has the MCMC samples for the parameters, ordered as: beta's, rho, sig2eps, sig2eta, mu_l's, sig2l's and phi. OutAR_Stats_TrueValue.txt: (N x 2) matrix of true summary values, with 1st column as mean and 2nd column as standard deviations. OutAR_Stats_FittedValue.txt: (N x 2) matrix of fitted summary, with 1st column as mean and 2nd column as standard deviations. OutAR_Stats_PredValue.txt: ((predsites*r*T) x 2) matrix of prediction summary, with 1st column as mean and 2nd column as standard deviations. OutAR_Values_Prediction.txt: (nItr x (predsites*r*T)) matrix of MCMC predicted values in the predicted sites. If annual.aggregation="ave" then we get text output as: OutAR_Annual_Average_Prediction.txt: (nItr x (predsites*r)) matrix. If annual.aggregation="an4th" then we get text output as: OutAR_Annual_4th_Highest_Prediction.txt: (nItr x (predsites*r)) matrix. For models using GPP approximations: OutGPP_Values_Parameter.txt: (nItr x parameters matrix) has the MCMC samples for the parameters, ordered as: beta's, rho, sig2eps, sig2eta, and phi. OutGPP_Stats_FittedValue.txt: (N x 2) matrix of fitted summary, with 1st column as mean and 2nd column as standard deviations. OutGPP_Stats_PredValue.txt: ((predsites*r*T) x 2) matrix of prediction summary, with 1st column as mean and 2nd column as standard deviations. OutGPP_Values_Prediction.txt: (nItr x (predsites*r*T)) matrix of MCMC predicted values in the predicted sites. If annual.aggregation="ave" then we get text output as: OutGPP_Annual_Average_Prediction.txt: (nItr x (predsites*r)) matrix. If annual.aggregation="an4th" then we get text output as: OutGPP_Annual_4th_Highest_Prediction.txt: (nItr x (predsites*r)) matrix.

See Also

spT.priors, spT.initials, spT.geodist, dist, spT.MCMC.stat, spT.MCMC.plot, spT.prediction, spT.forecast.

Examples

Run this code
##

###########################
## Read data:
###########################

library(spTimer)

data(NYdata)
data(NYsite)

head(NYdata)
head(NYsite)

# map
library(maps)
map(database="state",regions="new york")
points(NYsite[,2:3],pch=19)

# descriptive stat

summary(NYdata[,7:10])

# 
n.sites <- length(unique(NYsite[,1])) # Total number of sites
n.valsites <- 4                       # Let 4 sites are set aside  
nall.sites <- 1:n.sites               # Numbered sites 
nrand.sites <- sort(sample(nall.sites, n.valsites)) # Select 4 sites randomly
nval.sites <- nrand.sites              # Numbered validation sites 
nfit.sites <- nall.sites[-nrand.sites] # Numbered fitted sites

# split the NYdata into fitted and validation part

val.dat <- spT.data.selection(data=NYdata,random=F,s=nval.sites)
fit.dat <- spT.data.selection(data=NYdata,random=F,s=nfit.sites)

val.site <- spT.data.selection(data=NYsite,random=F,s=nval.sites)
fit.site <- spT.data.selection(data=NYsite,random=F,s=nfit.sites)

# set aside last day of August for forecast validation

fit.fore<-fit.dat[fit.dat$Month==8 & fit.dat$Day==31,]
val.fore<-val.dat[val.dat$Month==8 & val.dat$Day==31,]

# new fitted and validation data with 61 days

fit.dat<-fit.dat[fit.dat$Month !=8 | fit.dat$Day !=31,]
val.dat<-val.dat[val.dat$Month !=8 | val.dat$Day !=31,]


#
library(maps)
map(database="state",regions="new york")
points(fit.site[,2:3],pch=2,col=4)
points(val.site[,2:3],pch=19,col=2)
#

###########################
## The GP models:
###########################

# define the coordinates
coords<-as.matrix(fit.site[,2:3])

# define the time-series 
time.data<-spT.time(t.series=61,segment=1)

# hyper-parameters for the prior distributions
priors<-spT.priors(model="GP",var.prior=Gam(2,1),
        beta.prior=Nor(0,10^4))
#priors<-NULL

# initial values for the model parameters
initials<-spT.initials(model="GP", sig2eps=0.01, 
            sig2eta=0.5, beta=NULL, phi=0.001)
#initials<-NULL

# input for spatial decay
#spatial.decay<-spT.decay(type="FIXED", value=0.01)
spatial.decay<-spT.decay(type="MH", tuning=0.08)
#spatial.decay<-spT.decay(type="DISCRETE",limit=c(0.01,0.02),segments=5)


# input for the MCMC algorithms
nItr<-5000

# MCMC via Gibbs
post.gp <- spT.Gibbs(formula=o8hrmax ~ cMAXTMP+WDSP+RH, 
         data=fit.dat, model="GP", time.data=time.data, 
         coords=coords, priors=priors, initials=initials, 
         nItr=nItr, nBurn=0, report=nItr, 
         tol.dist=2, distance.method="geodetic:km", 
         cov.fnc="exponential", scale.transform="SQRT", 
         spatial.decay=spatial.decay)

#object.size(post.gp)/(1024*1024) #MB
names(post.gp)

# model selection criteria
post.gp$PMCC # 

# MCMC summary and plots
spT.MCMC.stat(post.gp,nBurn=1000)
spT.MCMC.plot(post.gp,nBurn=1000)

# Use of coda pakcage

library(coda)
tmp<-as.mcmc(post.gp)
plot(tmp)
summary(tmp)


##
## Fit and Prediction simultaneously
##


pred.coords<-as.matrix(val.site[,2:3])

coords<-as.matrix(fit.site[,2:3])
time.data<-spT.time(t.series=61,segment=1)

priors<-spT.priors(model="GP",var.prior=Gam(2,1),
        beta.prior=Nor(0,10^4))
initials<-spT.initials(model="GP", sig2eps=0.01, 
            sig2eta=0.5, beta=NULL, phi=0.001)

# input for spatial decay
#spatial.decay<-spT.decay(type="FIXED", value=0.01)
spatial.decay<-spT.decay(type="MH", tuning=0.08)
#spatial.decay<-spT.decay(type="DISCRETE",limit=c(0.01,0.02),segments=5)

nItr<-5000 

# MCMC via Gibbs
post.gp.fit.pred <- spT.Gibbs(formula=o8hrmax~cMAXTMP+WDSP+RH, 
         data=fit.dat, model="GP", time.data=time.data, 
         coords=coords, pred.coords=pred.coords, pred.data=val.dat,
         priors=priors, initials=initials, 
         nItr=nItr, nBurn=1000, report=nItr, 
         tol.dist=2, distance.method="geodetic:km", 
         cov.fnc="exponential", scale.transform="SQRT", 
         spatial.decay=spatial.decay,
         annual.aggregation="NONE")

names(post.gp.fit.pred)

post.gp.fit.pred$parameter


###########################
## The AR models:
###########################

# define the coordinates
coords<-as.matrix(fit.site[,2:3])

# define the time-series 
time.data<-spT.time(t.series=61,segment=1)

# hyper-parameters for the prior distributions
priors<-spT.priors(model="AR",var.prior=Gam(2,1),
        beta.prior=Nor(0,10^4))

# initial values for the model parameters
initials<-spT.initials(model="AR", sig2eps=0.01, 
            sig2eta=0.5, beta=NULL, phi=0.001)

# input for spatial decay
#spatial.decay<-spT.decay(type="FIXED", value=0.01)
spatial.decay<-spT.decay(type="MH", tuning=0.08)
#spatial.decay<-spT.decay(type="DISCRETE",limit=c(0.01,0.02),segments=5)

# input for the MCMC algorithms
nItr<-500


# MCMC via Gibbs
post.ar <- spT.Gibbs(formula=o8hrmax~cMAXTMP+WDSP+RH, 
         data=fit.dat, model="AR", time.data=time.data, 
         coords=coords, priors=priors, initials=initials, 
         nItr=nItr, nBurn=0, report=nItr, 
         tol.dist=2, distance.method="geodetic:km", 
         cov.fnc="exponential", scale.transform="SQRT", 
         spatial.decay=spatial.decay)

object.size(post.ar)/(1024*1024) #MB
names(post.ar)

# model selection criteria
post.ar$PMCC # 

# MCMC summary and plots
spT.MCMC.stat(post.ar,nBurn=100)
spT.MCMC.plot(post.ar,nBurn=100)

# Use of coda pakcage
library(coda)
tmp<-as.mcmc(post.ar)
plot(tmp)
summary(tmp)


##
## fit and predict combinedly for AR models with text output
##

pred.coords<-as.matrix(val.site[,2:3])

coords<-as.matrix(fit.site[,2:3])
time.data<-spT.time(t.series=61,segment=1)

priors<-spT.priors(model="AR",var.prior=Gam(2,1),
        beta.prior=Nor(0,10^4))

initials<-spT.initials(model="AR", sig2eps=0.01, 
            sig2eta=0.5, beta=NULL, phi=0.001)

# input for spatial decay
#spatial.decay<-spT.decay(type="FIXED", value=0.01)
spatial.decay<-spT.decay(type="MH", tuning=0.08)
#spatial.decay<-spT.decay(type="DISCRETE",limit=c(0.01,0.02),segments=5)

nItr<-500
nBurn<-100

# MCMC via Gibbs
post.ar.fit.pred <- spT.Gibbs(formula=o8hrmax~cMAXTMP+WDSP+RH, 
         data=fit.dat, model="AR", time.data=time.data, 
         coords=coords, pred.coords=pred.coords, pred.data=val.dat,
         priors=priors, initials=initials, 
         nItr=nItr, nBurn=nBurn, report=nItr, 
         tol.dist=2, distance.method="geodetic:km", 
         cov.fnc="exponential", scale.transform="SQRT", 
         spatial.decay=spatial.decay,
         annual.aggregation="an4th")

names(post.ar.fit.pred)

post.ar.fit.pred$para




###########################
## Models with GPP approximations:
###########################


# define the coordinates and knots
coords<-as.matrix(fit.site[,2:3])
knots.coords<-spT.grid.coords(Longitude=c(max(coords[,1]),
              min(coords[,1])),Latitude=c(max(coords[,2]),
              min(coords[,2])), by=c(4,4))

library(maps)
map(database="state",regions="new york")
points(coords,pch=2,col=2)
points(knots.coords,pch=19,col=4)

# define the time-series 
time.data<-spT.time(t.series=61,segment=1)

# hyper-parameters for the prior distributions
priors<-spT.priors(model="GPP",var.prior=Gam(2,1),
        beta.prior=Nor(0,10^4))
#priors<-NULL

# initial values for the model parameters
initials<-spT.initials(model="GPP", sig2eps=0.01, 
            sig2eta=0.5, beta=NULL, phi=0.001)
#initials<-NULL


# input for spatial decay
#spatial.decay<-spT.decay(type="FIXED", value=0.001)
spatial.decay<-spT.decay(type="MH", tuning=0.05) # 
#spatial.decay<-spT.decay(type="DISCRETE",limit=c(0.001,0.009),segments=10)


# input for the MCMC algorithms
nItr<-5000 

# MCMC via Gibbs
post.gpp <- spT.Gibbs(formula=o8hrmax~cMAXTMP+WDSP+RH, 
         data=fit.dat, model="GPP", time.data=time.data, 
         coords=coords, knots.coords=knots.coords,
         priors=priors, initials=initials, 
         nItr=nItr, nBurn=0, report=nItr/10, 
         tol.dist=2, distance.method="geodetic:km", 
         cov.fnc="exponential", scale.transform="SQRT", 
         spatial.decay=spatial.decay)


object.size(post.gpp)/(1024*1024) #MB
names(post.gpp)


# model selection criteria
post.gpp$PMCC # 

# MCMC summary and plots
spT.MCMC.stat(post.gpp,nBurn=1000)
spT.MCMC.plot(post.gpp,nBurn=1000,ACF=T)

# Use of coda pakcage
library(coda)
tmp<-as.mcmc(post.gpp)
plot(tmp)
summary(tmp)


##
## fit and predict together for the GPP with text output
##

pred.coords<-as.matrix(val.site[,2:3])

coords<-as.matrix(fit.site[,2:3])
knots.coords<-spT.grid.coords(Longitude=c(max(coords[,1]),
              min(coords[,1])),Latitude=c(max(coords[,2]),
              min(coords[,2])), by=c(4,4))
time.data<-spT.time(t.series=61,segment=1)

priors<-spT.priors(model="GPP",var.prior=Gam(2,1),
        beta.prior=Nor(0,10^4))
#priors<-NULL
initials<-spT.initials(model="GPP", sig2eps=0.01, 
            sig2eta=0.5, beta=NULL, phi=0.001)
#initials<-NULL

# input for spatial decay
#spatial.decay<-spT.decay(type="FIXED", value=0.01)
spatial.decay<-spT.decay(type="MH", tuning=0.0008)
#spatial.decay<-spT.decay(type="DISCRETE",limit=c(0.001,0.002),segments=5)


nItr<-500 

# MCMC via Gibbs
post.gpp.fit.pred <- spT.Gibbs(formula=o8hrmax~cMAXTMP+WDSP+RH, 
         data=fit.dat, model="GPP", time.data=time.data, 
         coords=coords, knots.coords=knots.coords,
         pred.coords=pred.coords, pred.data=val.dat,
         priors=priors, initials=initials, 
         nItr=nItr, nBurn=100, report=nItr, 
         tol.dist=2, distance.method="geodetic:km", 
         cov.fnc="exponential", scale.transform="SQRT", 
         spatial.decay=spatial.decay,
         annual.aggregation="NONE")

names(post.gpp.fit.pred)

post.gpp.fit.pred$para

##

Run the code above in your browser using DataLab