I ran the following state space model, but got a warning message. Please find an attached file to reproduce my results.
stan code
data {
int P;
int T;
int T_pred;
real<lower=0, upper=10> H[P,T];
}
parameters {
real mu_H[P,T];
real<lower=0> s_mu[P];
real<lower=0> s_H;
}
model {
for (p in 1:P){
s_mu[p] ~ student_t(4,0,2);
}
for (t in 3:T)
for (p in 1:P){
mu_H[p,t] ~ normal(2 * mu_H[p,t - 1] - mu_H[p,t - 2], s_mu[p]);
//assuming that s_mu differs across prefectures
H[p,t] ~ normal(mu_H[p,t], s_H);
}
}
generated quantities {
real mu_all[P,T + T_pred];
real h_pred[P,T_pred];
for (t in 1:T)
for (p in 1:P)
mu_all[p,t] = mu_H[p,t];
for (t in 1:T_pred)
for (p in 1:P){
mu_all[p,T + t] = normal_rng(2 * mu_all[p,T + t - 1]
- mu_all[p,T + t - 2], s_mu[p]);
h_pred[p,t] = normal_rng(mu_all[p,T + t], s_H);
}
}
r code
setwd("~/docdis")
library(rstan)
library(dplyr)
library(tidyr)
#import data
d <- read.csv(file ='input/ch3/reginfotest.csv', header = TRUE,
stringsAsFactors = FALSE)
#long -> wide
d <- spread(d, key = year, value = mean)
data <- list(P = nrow(d),T = ncol(d) - 1,T_pred = 3,H = d[,-1])
stanmodel <- stan_model(file='model/ch3/model-treghapSNC_simpmatxp.stan')
fit <- sampling(
stanmodel,
data = data,
seed =243,
chains=4, iter=2000, warmup=1000, thin=1, control=list(adapt_delta=0.99, max_treedepth=15)
)
warning message
1: There were 68 divergent transitions after warmup. Increasing adapt_delta above 0.99 may help. See
Runtime warnings and convergence problems
2: There were 2 chains where the estimated Bayesian Fraction of Missing Information was low. See
Runtime warnings and convergence problems
3: Examine the pairs() plot to diagnose sampling problems
Then, I ran another stan code with reparameterization to solve them as follows, but got another warning message. I suppose that this message is related to mistakenly specified index, but I could not find any solutions for this. I would appreciate if someone would respond to me.
stan code
data {
int P;
int T;
int T_pred;
real<lower=0, upper=10> H[P,T];
}
parameters {
real<lower=0> s_mu;
real mu_H_raw[P,T-2];
real<lower=0> s_H;
}
transformed parameters {
real mu_H[P,T];
for (t in 3:T)
for (p in 1:P)
mu_H[p,t] = 2 * mu_H[p,t - 1] - mu_H[p,t - 2]
+ s_mu * mu_H_raw[p,t]; //reparameterization
}
model {
for (t in 3:T)
for (p in 1:P){
mu_H_raw[p,t] ~ normal(0, 1); //reparameterization
H[p,t] ~ normal(mu_H[p,t], s_H);
}
}
generated quantities {
real mu_all[P,T + T_pred];
real h_pred[P,T_pred];
for (t in 1:T)
for (p in 1:P)
mu_all[p,t] = mu_H[p,t];
for (t in 1:T_pred)
for (p in 1:P){
mu_all[p,T + t] = 2 * mu_all[p,t - 1] - mu_all[p,t - 2]
+ s_mu * normal_rng(0, 1);
h_pred[p,t] = normal_rng(mu_all[p,T + t], s_H);
}
}
r code
setwd("~/docdis")
library(rstan)
library(dplyr)
library(tidyr)
#import data
d <- read.csv(file ='input/ch3/reginfotest.csv', header = TRUE,
stringsAsFactors = FALSE)
#long -> wide
d <- spread(d, key = year, value = mean)
data <- list(P = nrow(d),T = ncol(d) - 1,T_pred = 3,H = d[,-1])
stanmodel <- stan_model(file='model/ch3/model-treghapSNC_simpmatxrep.stan')
fit <- sampling(
stanmodel,
data = data,
seed = 243,
chains=4, iter=2000, warmup=1000, thin=1
)
warning message
SAMPLING FOR MODEL ‘model-treghapSNC_simpmatxrep’ NOW (CHAIN 1).
Unrecoverable error evaluating the log probability at the initial value.
Exception: : accessing element out of range. index 8 out of range; expecting index to be between 1 and 7; index position = 2mu_H_raw (in ‘model250c34c853fe_model_treghapSNC_simpmatxrep’ at line 18)[1] “Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: : accessing element out of range. index 8 out of range; expecting index to be between 1 and 7; index position = 2mu_H_raw (in ‘model250c34c853fe_model_treghapSNC_simpmatxrep’ at line 18)”
[1] “error occurred during calling the sampler; sampling not done”
session info
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)Matrix products: default
locale:
[1] LC_COLLATE=Japanese_Japan.932 LC_CTYPE=Japanese_Japan.932
[3] LC_MONETARY=Japanese_Japan.932 LC_NUMERIC=C
[5] LC_TIME=Japanese_Japan.932attached base packages:
[1] stats graphics grDevices utils datasets methods baseother attached packages:
[1] ggmcmc_1.1 tidyr_0.6.3 dplyr_0.7.1
[4] rstan_2.16.2 StanHeaders_2.16.0-1 ggplot2_2.2.1loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 compiler_3.4.1 RColorBrewer_1.1-2 plyr_1.8.4
[5] bindr_0.1 tools_3.4.1 digest_0.6.12 tibble_1.3.3
[9] gtable_0.2.0 lattice_0.20-35 pkgconfig_2.0.1 rlang_0.1.1
[13] GGally_1.3.1 parallel_3.4.1 mvtnorm_1.0-6 loo_1.1.0
[17] bindrcpp_0.2 gridExtra_2.2.1 coda_0.19-1 stats4_3.4.1
[21] grid_3.4.1 reshape_0.8.6 inline_0.3.14 glue_1.1.1
[25] R6_2.2.2 rethinking_1.59 magrittr_1.5 scales_0.4.1
[29] codetools_0.2-15 matrixStats_0.52.2 MASS_7.3-47 assertthat_0.2.0
[33] colorspace_1.3-2 labeling_0.3 lazyeval_0.2.0 munsell_0.4.3
reginfotest.csv (7.6 KB)