Learn R Programming

CARBayes (version 3.0)

dissimilarityCAR.re: Fit a localised conditional autoregressive (CAR) model based on dissimilarity metrics with binary neighbourhood relations to spatial data

Description

The function fits a Bayesian hierarchical model with spatially correlated random effects to the data, whre the data likelihood can be binomial, Gaussian or Poisson. The random effects are modelled by a localised conditional autoregressive (CAR) model, which is a generalisation of that proposed by Lee and Mitchell (2012). The random effects in neighbouring areas (e.g. those that share a common border) are modelled as correlated or conditionally independent, depending on whether the populations living in the two areas are similar (correlated random effects) or very different (conditionally independent). The model represents the linear predictor by a combination of covariates and a set of random effects. Inference is based on Markov Chain Monte Carlo (MCMC) simulation, using a combination of Gibbs sampling and Metropolis steps.

Usage

dissimilarityCAR.re(formula, family=NULL, data=NULL,  trials=NULL, rho=0.99, W, 
Z, burnin=0, n.sample=1000, thin=1, blocksize.beta=5, prior.mean.beta=NULL, 
prior.var.beta=NULL, prior.nu2=NULL, prior.tau2=NULL, verbose=TRUE)

Arguments

formula
A formula for the covariate part of the model, using the same notation as for the lm() function. The offsets should also be included here using the offset() function.
family
One of either 'binomial', 'gaussian' or 'poisson', which respectively specify a binomial likelihood model with a logistic link function, a Gaussian likelihood model with an identity link function, or a Poisson likelihood model with a log link function.
data
A data.frame containing the variables in the formula.
trials
A vector the same length as the response containing the total number of trials for each area.
rho
The fixed value for the global spatial correlation parameter rho. The default is 0.99 which should be retained.
W
A binary n by n neighbourhood matrix (where n is the number of spatial units). The jkth element equals one if areas (j, k) are spatially close (e.g. share a common border) and is zero otherwise.
Z
A list, where each element is an n by n matrix of non-negative dissimilarity metrics. For details of their construction see the main help page for CARBayes.
burnin
The number of MCMC samples to discard as the burnin period. Defaults to 0.
n.sample
The number of MCMC samples to generate. Defaults to 1,000.
thin
The level of thinning to apply to the MCMC samples to reduce their temporal autocorrelation. Defaults to 1.
blocksize.beta
The size of the blocks in which to update the regression parameters in the MCMC algorithm. Defaults to 5.
prior.mean.beta
A vector of prior means for the regression parameters beta (Gaussian priors are assumed). Defaults to a vector of zeros.
prior.var.beta
A vector of prior variances for the regression parameters beta (Gaussian priors are assumed). Defaults to a vector with values 1000.
prior.nu2
Only used for the Gaussian model. The prior shape and scale in the form of c(shape, scale) for an Inverse-Gamma(shape, scale) prior for nu2. Defaults to c(0.001, 0.001).
prior.tau2
The prior shape and scale in the form of c(shape, scale) for an Inverse-Gamma(shape, scale) prior for tau2. Defaults to c(0.001, 0.001).
verbose
Logical, should the function update the user on its progress.

Value

  • formulaThe formula for the covariate and offset part of the model.
  • samplesA list containing the MCMC samples from the model.
  • fitted.valuesA summary matrix of the posterior distributions of the fitted values for each area. The summaries include: Mean, Sd, Median, and credible interval.
  • residualsA summary matrix of the posterior distributions of the residuals for each area. The summaries include: Mean, Sd, Median, and credible interval.
  • W.summaryThe neighbourhood matrix W from the model.
  • modelfitModel fit criteria including the Deviance Information Criterion (DIC), the effective number of parameters in the model(p.d), DIC3 and the Marginal Predictive Likelihood (MPL).
  • summary.resultsA summary table of the parameters.
  • modelA text string describing the model fit.
  • acceptThe acceptance probabilities for the parameters.

Details

For further details about how to apply the function see the examples below and in the vignette.

References

Lee, D. and R. Mitchell (2012). Boundary detection in disease mapping studies. Biostatistics, 13, 415-426.

Examples

Run this code
###########################################################
#### Run the model on simulated data - localised CAR model
###########################################################

#### Set up a square lattice region
x.easting <- 1:10
x.northing <- 1:10
Grid <- expand.grid(x.easting, x.northing)
n <- nrow(Grid)

#### Split the area into two groups between which there will be a boundary.
groups <-rep(1, n) 
groups[Grid$Var1>5] <- 2

#### set up distance and neighbourhood (W, based on sharing a common border) matrices
distance <-array(0, c(n,n))
W <-array(0, c(n,n))
  for(i in 1:n)
	{
		for(j in 1:n)
		{
		temp <- (Grid[i,1] - Grid[j,1])^2 + (Grid[i,2] - Grid[j,2])^2
		distance[i,j] <- sqrt(temp)
			if(temp==1)  W[i,j] <- 1 
		}	
	}
	
	
#### Generate the response data
phi <- mvrnorm(n=1, mu=groups, Sigma=0.2 * exp(-0.1 * distance))
logit <- phi
prob <- exp(logit) / (1 + exp(logit))
trials <- rep(50,n)
Y <- rbinom(n=n, size=trials, prob=prob)


#### Generate a dissimilarity metric
dissimilarity <- cbind(groups) + rnorm(n, sd=0.2)
dissimilarity.matrix <- as.matrix(dist(cbind(dissimilarity, dissimilarity), 
method="manhattan", diag=TRUE, upper=TRUE)) * W/2

Z <- list(dissimilarity.matrix=dissimilarity.matrix)

#### Run the localised smoothing model
formula <- Y ~ 1
model <- dissimilarityCAR.re(formula=formula, family="binomial", rho=0.99, 
trials=trials, W=W, Z=Z, burnin=5000, n.sample=10000)

Run the code above in your browser using DataLab