Hi Daniel and Mitzi,
Thank you for the clarification. I’m currently looking at the “5 nearest neighbors” approach with NYC Planning’s 2010 Census Tract (clipped to shoreline). I made one manual connection between Brooklyn and Staten Island by the Verrazano Bridge, yielding a fully connected map.
However, in the final step prior to running the model, where the adjacency matrix and scaling are set up, I’m now getting an error message as follows. I’m stumped as to what it means:
This is INLA_17.06.20 built 2017-06-20 03:42:30 UTC.
See R-INLA Project - Contact us for how to get help.
Error in validObject(r) :
invalid class “dsTMatrix” object: all row indices (slot ‘i’) must be between 0 and nrow-1 in a TsparseMatrix
In addition: Warning messages:
1: package ‘INLA’ was built under R version 3.4.0
2: In fun(libname, pkgname) : bytecode version mismatch; using eval
Here is the code I used:
#Run ICAR model using STAN - developed by Mitzi Morris et al
library(rstan);
options(mc.cores = parallel::detectCores());
library(maptools);
library(spdep);
library(tripack);
library(rgdal)
nyckidped_tract<-readRDS(file=“D:/NYC/nyckidped_tract.rds”)
#source(“nyckidped_tract.rds”);
y = nyckidped_tract$count;
x = nyckidped_tract$expected;
nyc_poptractIDs<-nyckidped_tract$ct2010full
head(nyc_poptractIDs)
nyc_all_tracts.shp<-readOGR(“D:/Census/nycTracts10/nyct2010.shp”, layer=“nyct2010”);
print(nyc_all_tracts.shp)
table(nyc_all_tracts.shp$CT2010)
table(nyc_all_tracts.shp$BoroCode, nyc_all_tracts.shp$BoroName)
nyc_all_tracts.shp$BoroFips<-ifelse(nyc_all_tracts.shp$BoroCode==1, “061”, ifelse(nyc_all_tracts.shp$BoroCode==2, “005”, ifelse(nyc_all_tracts.shp$BoroCode==3, “047”, ifelse(nyc_all_tracts.shp$BoroCode==4, “081”, ifelse(nyc_all_tracts.shp$BoroCode==5, “085”, “”)))))
table(nyc_all_tracts.shp$BoroName, nyc_all_tracts.shp$BoroFips)
nyc_all_tracts.shp$CT2010full<-paste0(“36”,nyc_all_tracts.shp$BoroFips,nyc_all_tracts.shp$CT2010)
table(nyc_all_tracts.shp$CT2010full)
pop_tracts ← nyc_all_tracts.shp$CT2010full %in% nyc_poptractIDs;
nyc_poptracts.shp ← nyc_all_tracts.shp[pop_tracts,];
nyc_poptracts.shp ← nyc_poptracts.shp[order(nyc_poptracts.shp$BoroCT2010),];
#nearest neighbors
nb_nyc_5nn ← knn2nb(knearneigh(coords,k=5), row.names=IDs);
plot(nb_nyc_5nn, coords, pch = “.”, col = “red”);
#Manually add connection between Brooklyn and Staten Island
nb_nyc_5nn1n <-edit.nb(nb_nyc_5nn, coords)
#Identifying contiguity for deletion …
#No contiguity between chosen points
#Add contiguity? (y/n) y
#added contiguity between point 733 and 2018
#Options: quit[q] refresh[r] continue[c] r
#Options: quit[q] continue[c]q
plot(nb_nyc_5nn1n, coords, pch = “.”, col = “red”);
#source(“nbdata4stan.R”);
nbdata4stan = function(x) {
N = length(x);
n_links = 0;
for (i in 1:N) {
if (x[[i]][1] != 0) {
n_links = n_links + length(x[[i]]);
}
}
N_edges = n_links / 2;
node1 = vector(mode=“numeric”, length=N_edges);
node2 = vector(mode=“numeric”, length=N_edges);
idx = 0;
for (i in 1:N) {
if (x[[i]][1] != 0) {
for (j in 1:length(x[[i]])) {
n2 = unlist(x[[i]][j]);
if (i < n2) {
idx = idx + 1;
node1[idx] = i;
node2[idx] = n2;
}
}
}
}
return (list(“N”=N,“N_edges”=N_edges,“node1”=node1,“node2”=node2));
}
nbs=nbdata4stan(nb_nyc_5nn1n);
N = nbs$N;
node1 = nbs$node1;
node2 = nbs$node2;
N_edges = nbs$N_edges;
DO_CALC = TRUE;
#Calculate the scaling of the model. Requires the INLA package.
if(DO_CALC) {
library(INLA)
#Build the adjacency matrix
adj.matrix = sparseMatrix(i=nbs$node1,j=nbs$node2,x=1,symmetric=TRUE)
#The ICAR precision matrix (note! This is singular)
Q= Diagonal(nbs$N, rowSums(adj.matrix)) - adj.matrix
#Add a small jitter to the diagonal for numerical stability (optional but recommended)
Q_pert = Q + Diagonal(nbs$N) * max(diag(Q)) * sqrt(.Machine$double.eps)
Compute the diagonal elements of the covariance matrix subject to the
constraint that the entries of the ICAR sum to zero.
#See the function help for further details.
Q_inv = inla.qinv(Q_pert, constr=list(A = matrix(1,1,nbs$N),e=0))
#Compute the geometric mean of the variances, which are on the diagonal of Q.inv
scaling_factor = exp(mean(log(diag(Q_inv))))
} else{
scaling_factor= 0.5;
}
bym2_nyc_stanfit = stan(“D:/NYC/bym2_predictor_only.stan”, data=list(N,N_edges,node1,node2,y,x,scaling_factor), iter=6000);
print(bym2_nyc_stanfit, digits=3, pars=c(“lp__”, “beta0”, “beta1”, “rho”, “sigma”, “mu[1]”, “mu[2]”, “mu[3]”, “mu[2116]”, “mu[2117]”, “mu[2118]”), probs=c(0.025, 0.5, 0.975));
Katie