data{
int<lower=1> I;//counties
int<lower=1> T;//time periods
int<lower=1> K;//genders
int<lower=1> J;//age groups
int<lower=0> y[I*T*K*J];//obsed data
vector[I*T*K*J] n;//obsed population
matrix[I,I] C;//adjacency matrix
real max_rho;//upper boundary of rho
}
transformed data{
vector[1] zero ;
zero <- rep_vector(0,1);
}
parameters{
vector[I] gamma;//spatial effects
vector[T-1] theta_raw;//time
real alpha_raw;//gender
vector[J-1] beta_raw;//age
vector[I*T*K*J] e;//error term
vector[I] mu;//spatial mean
real<lower=0> delta_1;// variance component of B
real<lower=0,upper=max_rho> rho;
}
transformed parameters{
vector[T] theta;//time
vector[K] alpha;//gender
vector[J] beta;//age
matrix[I,I] delta_1_inv_B;//
vector[I*T*K*J] Z;
vector[I*T*K*J] poisson_mean;
theta <- append_row(zero, theta_raw);
alpha[1] <- 0;
alpha[2] <- alpha_raw;
beta <- append_row(zero, beta_raw);
delta_1_inv_B <- delta_1*inverse_spd(diag_matrix(rep_vector(1, I))-rho*C);
for(j in 1:J){
for(k in 1:K){
for(t in 1:T){
for(i in 1:I){
Z[i + (t-1)*I + (k-1)*I*T + (j-1)*I*T*K] <- gamma[i]+theta[t] +alpha[k]+beta[j];
}
}
}
}
poisson_mean <- exp(Z+e).*n;
//print (poisson_mean);
}
model{
//for gamma
mu ~ normal(0, 100);
delta_1 ~ inv_gamma(0.001, 0.001);
rho ~ uniform(0, max_rho);
gamma ~ multi_normal(mu, delta_1_inv_B);
//
theta_raw ~ normal(0, 100);
alpha_raw ~ normal(0, 100);
beta_raw ~ normal(0, 100);
e ~ normal(0, 100);
y ~ poisson(poisson_mean);
}
--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
To post to this group, send email to stan-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<Additive_model.stan>
model{ #likelihood
for (i in 1:I){
for (t in 1:T){
for (k in 1:K){
for (j in 1:J){
y[i, t, k, j] ~ dpois(lambda[i, t, k, j])
lambda[i, t, k, j] <- n[i, t, k, j] * p[i, t, k, j]
log(p[i, t, k, j]) <- z[i, t, k, j] + e[i, t, k, j]
z[i, t, k, j] <- gamma[i] + theta[t] + alpha[k] + beta[j]
#prior for e[i, t, k, j]
e[i, t, k, j] ~ dnorm(0, e.tau)
rate_per_hthousands[i, t, k, j] <- p[i, t, k, j]*100000
}
}
}
}
#K=2
alpha[1] <- 0
alpha[2] ~ dnorm(0, 1.0E-04)
beta[1] <- 0
for( j in 2:J){
beta[j] ~ dnorm(0, 1.0E-04)
}
e.tau ~dgamma(0.001, 0.001)
#for spatial smoothing
gamma[1:I] ~ car.proper(gamma_mu[1:I],C[], adj[], num[], M[], gamma_tau, gamma_rho)
for (i in 1:I) {
M[i] <- 1;
}
for (i in 1:sumNumNeigh) {
C[i] <- 1;
}
#intercept
mu ~ dnorm(0, 1.0E-04)
for( i in 1:I){
gamma_mu[i] <-mu
}
gamma_tau ~ dgamma(0.001, 0.001)
rho.min <- max(0, min.bound(C[], adj[], num[], M[]))# we just want positive correlation, so start at #0, not rho.min
rho.max <- max.bound(C[], adj[], num[], M[])
gamma_rho ~ dunif(rho.min, rho.max)
#for temporal smoothing, provide S and identity matrix
theta[1] <- 0
for (t in 2:T){
theta[t] ~dnorm(0, 1.0E-04)
}
} SAMPLING FOR MODEL 'Additive_model' NOW (CHAIN 1).
Chain 1, Iteration: 1 / 20 [ 5%] (Warmup)
Chain 1, Iteration: 2 / 20 [ 10%] (Warmup)
Chain 1, Iteration: 4 / 20 [ 20%] (Warmup)
Chain 1, Iteration: 6 / 20 [ 30%] (Warmup)
Chain 1, Iteration: 8 / 20 [ 40%] (Warmup)
Chain 1, Iteration: 10 / 20 [ 50%] (Warmup)
Chain 1, Iteration: 11 / 20 [ 55%] (Sampling)
Chain 1, Iteration: 12 / 20 [ 60%] (Sampling)
Chain 1, Iteration: 14 / 20 [ 70%] (Sampling)
Chain 1, Iteration: 16 / 20 [ 80%] (Sampling)
Chain 1, Iteration: 18 / 20 [ 90%] (Sampling)
Chain 1, Iteration: 20 / 20 [100%] (Sampling)
# Elapsed Time: 1896.77 seconds (Warm-up)
# 3111.6 seconds (Sampling)
# 5008.37 seconds (Total)
[1] "The following numerical problems occured the indicated number of times on chain 1"
count
Exception thrown at line 56: multi_normal_log: Covariance matrix is not symmetric. Covariance matrix 2
[1] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[2] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."
Warning messages:
1: There were 10 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth.
2: Examine the pairs() plot to diagnose sampling problems
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 0.000 1.000 3.453 5.000 22.000Min. 1st Qu. Median Mean 3rd Qu. Max.
1065 2108 3036 5903 7639 16620delta_1_inv_B <- delta_1*inverse_spd(diag_matrix(rep_vector(1, I))-rho*C);