I am trying to do an areal unit analysis using the package CARBayes. As part of the analysis, I am using the below code. my issue comes when I try to create the neighbour matrix with nb2mat
. My sp object has 170,000 odd polygons in it so it can't make the matrix with the memory I have.
library(spdep)
library(CARBayes)
W.nb <- poly2nb(sp)
W <- nb2mat(W.nb, style = "B", zero.policy = TRUE)
test <- S.CARbym(case ~ covariate1),
family = "poisson",
data = sp,
W = W,
burnin = 10000,
n.sample = 30000,
thin = 20)
I found the below code in another thread to make a bigmemory
matrix but CARBayes won't recognise it as a matrix.
My question is, does anyone know a way to use bigmemory
or spam
/sparse matrix or something similar to create the matrix so that it can be used in the CARBayes package without throwing an error saying the W
isn't a matrix.
my_listw2mat = function (listw)
{
require(bigmemory)
n <- length(listw$neighbours)
if (n < 1)
stop("non-positive number of entities")
cardnb <- card(listw$neighbours)
if (any(is.na(unlist(listw$weights))))
stop("NAs in general weights list")
#res <- matrix(0, nrow = n, ncol = n)
res <- big.matrix(n, n, type='double', init=NULL)
options(bigmemory.allow.dimnames=TRUE)
for (i in 1:n) if (cardnb[i] > 0)
res[i, listw$neighbours[[i]]] <- listw$weights[[i]]
if (!is.null(attr(listw, "region.id")))
row.names(res) <- attr(listw, "region.id")
res
}
my_nb2mat = function (neighbours, glist = NULL, style = "W", zero.policy = NULL)
{
if (is.null(zero.policy))
zero.policy <- get("zeroPolicy", envir = .spdepOptions)
stopifnot(is.logical(zero.policy))
if (!inherits(neighbours, "nb"))
stop("Not a neighbours list")
listw <- nb2listw(neighbours, glist = glist, style = style,
zero.policy = zero.policy)
res <- my_listw2mat(listw)
attr(res, "call") <- match.call()
res
}
W <- my_nb2mat(W.nb, style = "B", zero.policy = TRUE)
test <- S.CARbym(case ~ covariate1),
family = "poisson",
data = sp,
W = W,
burnin = 10000,
n.sample = 30000,
thin = 20)