Dear Bob and Jon,
I do appreciate your kind replies and they are of great help!
I got your points on label switching and I do see the perfect estimation once I set the chain to 1.
But for the first question, I'm still a bit puzzled. I find that the variable "prob_grp" in the section of transformed parameters never used in the section "model". I think it means that only forward algorithm is used in the estimation. To test my guess, I revised the code (in the attachment) by just deleting the section of "//backwards algorithm" and the section "// put it all together" and rerun the code. It shows that the estimation is still as perfect as it was.
> fit2<-stan(model_code=stan.code,data=stan.data,iter=2000,chains=1)
DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): assignment operator <- deprecated in the Stan language; use = instead.
Warning (non-fatal): increment_log_prob(...); is deprecated and will be removed in the future.
Use target += ...; instead.
C:/Users/R/win-library/3.2/StanHeaders/include/stan/math/rev/core/set_zero_all_adjoints.hpp:14:17: warning: 'void stan::math::set_zero_all_adjoints()' defined but not used [-Wunused-function]
SAMPLING FOR MODEL 'ea01d2f14349744528258b19753f0d36' NOW (CHAIN 1).
Chain 1, Iteration: 1 / 2000 [ 0%] (Warmup)
Chain 1, Iteration: 200 / 2000 [ 10%] (Warmup)
Chain 1, Iteration: 400 / 2000 [ 20%] (Warmup)
Chain 1, Iteration: 600 / 2000 [ 30%] (Warmup)
Chain 1, Iteration: 800 / 2000 [ 40%] (Warmup)
Chain 1, Iteration: 1000 / 2000 [ 50%] (Warmup)
Chain 1, Iteration: 1001 / 2000 [ 50%] (Sampling)
Chain 1, Iteration: 1200 / 2000 [ 60%] (Sampling)
Chain 1, Iteration: 1400 / 2000 [ 70%] (Sampling)
Chain 1, Iteration: 1600 / 2000 [ 80%] (Sampling)
Chain 1, Iteration: 1800 / 2000 [ 90%] (Sampling)
Chain 1, Iteration: 2000 / 2000 [100%] (Sampling)
Elapsed Time: 233.459 seconds (Warm-up)
228.165 seconds (Sampling)
461.624 seconds (Total)
> print(fit2, pars = c("p0","TP","mu","sigma"))
Inference for Stan model: ea01d2f14349744528258b19753f0d36.
1 chains, each with iter=2000; warmup=1000; thin=1;
post-warmup draws per chain=1000, total post-warmup draws=1000.
p0 0.51 0 0.02 0.47 0.50 0.51 0.52 0.54 523 1
TP[1] 0.70 0 0.01 0.69 0.70 0.70 0.71 0.72 681 1
TP[2] 0.80 0 0.01 0.79 0.80 0.80 0.81 0.82 698 1
mu[1] 5.02 0 0.02 4.98 5.01 5.02 5.04 5.06 458 1
mu[2] 2.01 0 0.02 1.98 2.00 2.01 2.02 2.04 436 1
sigma[1] 1.00 0 0.02 0.97 0.99 0.99 1.01 1.03 483 1
sigma[2] 1.00 0 0.01 0.98 0.99 1.00 1.01 1.03 644 1
Samples were drawn using NUTS(diag_e) at Mon Dec 19 12:19:04 2016.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
So my questions are:
1) Only by " //Forwards algorithm" part alone, we are already able to do the unsupervised HMM estimation?
2) When should I include the "//backwards algorithm" and "// put it all together" part?
Thanks a lot!
Weijia