data{
int<lower=0> J1;#number of spatial regions for map 1
int<lower=0> J2;#number of spatial regions for map 2
int Y[J1+J2];#event counts for two maps, modeled in Poisson regression
int pop[J1+J2];#corresponding population, offset
matrix[J1, J1] W_1; #adgacency matrix for the first map
matrix[J2, J2] W_2; #adgacency matrix for the first map
matrix[J1, J1] D_1; #diagonal matrix with diag elemetns being number of neighbors
matrix[J2, J2] D_2; #diagonal matrix with diag elemetns being number of neighbors
matrix[J1, J2] Psi_12# a matrix to indicate how the regions in 2 maps are correlated
}
transformed data {
# zeros for MVN prior
vector[N] zeros;
zeros <- rep_vector(0.0, N);
}
parameters{
real<lower=0, upper=1> rho[2]; #strength of spatial correlation, for two maps
vector[J1+J2] u; #spatial effect for two maps
real<lower=-0.3,upper=0.3> gamma12;#correlation between two maps, restriced for positive definiteness
vector[2] alpha; #intercept for each spatial map
vector[J1+J2] epsilon;# standard Normal
real<lower=0> sigma2; #variance for the error term in log mean of Poisson
vector[2] delta;#variance compentent of the CAR model
}
transformed parameters{
real<lower=0> sigma; #sqrt of sigma2
sigma <- sqrt(sigma2);
}
model{
matrix[J1, J1] B_1; #precision matrix for the 1st map
matrix[J2, J2] B_2; #precision matrix for the 1st map
matrix[J1+J2, J1+J2] Sigma;#covariance matrix for spatial effect;
matrix[J1+J2, J1+J2] Sigma_full;#covariance matrix for spatial effect;
vector[J1+J2] epsilon;# parameter for Poisson distribution in log scale
vector[J1+J2] alpha_vec;#broadcast the intercept
vector[J1+J2] delta_vec;#broadcast the variance compentent
matrix[J1+J2, J1+J2] DELTA;#cange delta_vec to diag matrix
delta_vec[1:J1] <- rep_vector(delta[1], J1);
delta_vec[(J1+1):J2] <- rep_vector(delta[2], J2);
DELTA <- diag_matrix(delta_vec)
alpha_vec[1:J1] <- rep_vector(alpha[1], J1);
alpha_vec[(J1+1):J2] <- rep_vector(alpha[2], J2);
B_1 <- D_1 - rho[1] * W_1;
B_2 <- D_2 - rho[2] * W_2;
#assign Sigma in block
#Sigma = DELTA%*%
#B_1_inv gamma12*Psi_12
#t(gamma12*Psi_12) B_2_inv
#%*%DELTA
Sigma[1:J1, 1:J1] <- inverse_spd(B_1);
Sigma[(J1+1):(J1+J2), (J1+1):(J1+J2)] <- inverse_spd(B_2);
Sigma[1:J1, (J1+J2)] <- gamma12 * Psi_12;
Sigma[(J1+1), 1:J1] <- (Sigma[1:J1, (J1+J2)])';
Sigma_full <- DELTA*Sigma*DELTA;
#prior for the spatial effect
u ~ multi_normal(zeros, Sigma_full);
epsilon ~ normal(0,1);
log_p <- log(pop) + alpha_vec + u + sigma*epsilon;
y ~ poisson_log(log_p);#likelihood
}