CARBayes: Spatial Generalised Linear Mixed Models for Areal Unit Data

> library(CARBayes)
Loading required package: MASS

Attaching package: 'MASS'
The following object is masked from 'package:dplyr':

    select
Loading required package: Rcpp

バージョン: 4.6


関数名 概略
CARBayes-package Spatial Generalised Linear Mixed Models for Areal Unit Data
MVS.CARleroux Fit a multivariate spatial generalised linear mixed model to data, where the random effects are modelled by a multivariate conditional autoregressive model.
S.CARbym Fit a spatial generalised linear mixed model to data, where the random effects have a BYM conditional autoregressive prior.
S.CARdissimilarity Fit a spatial generalised linear mixed model to data, where the random effects have a localised conditional autoregressive prior.
S.CARleroux Fit a spatial generalised linear mixed model to data, where the random effects have a Leroux conditional autoregressive prior.
S.CARlocalised Fit a spatial generalised linear mixed model to data, where a set of spatially smooth random effects are augmented with a piecewise constant intercept process.
combine.data.shapefile Combines a data frame with a shapefile to create a SpatialPolygonsDataFrame object.
highlight.borders Creates a SpatialPoints object identifying a subset of borders between neighbouring areas.
print.carbayes Print a summary of a fitted carbayes model to the screen.
summarise.lincomb Compute the posterior distribution for a linear combination of the covariates from the linear predictor.
summarise.samples Summarise a matrix of Markov chain Monte Carlo samples.

S.CARbym

> #### Set up a square lattice region
> x.easting <- 1:10
> x.northing <- 1:10
> Grid <- expand.grid(x.easting, x.northing)
> K <- nrow(Grid)
> 
> #### set up distance and neighbourhood (W, based on sharing a common border) matrices
> distance <- array(0, c(K,K))
> W <- array(0, c(K,K))
>   for(i in 1:K)
+     {
+         for(j in 1:K)
+         {
+         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 covariates and response data
> x1 <- rnorm(K)
> x2 <- rnorm(K)
> theta <- rnorm(K, sd = 0.05)
> phi <- mvrnorm(n = 1, mu = rep(0,K), Sigma = 0.4 * exp(-0.1 * distance))
> logit <- x1 + x2 + theta + phi
> prob <- exp(logit) / (1 + exp(logit))
> Y <- rbinom(n = K, size = trials, prob = prob)
Error in rbinom(n = K, size = trials, prob = prob): object 'trials' not found
> #### Run the BYM model
> model <- S.CARbym(formula = Y ~ x1 + x2, 
+                   family = "binomial", 
+                   trials = rep(50, K), 
+                   W = W, 
+                   burnin = 20000,
+                   n.sample = 100000)
Setting up the model
Error: the formula inputted contains an error, e.g the variables may be different lengths.