#GENERATE DATAn_obs <-1000dbh <- rlnorm(n_obs, 3.443309, 0.9253412)sda <- 0.927sdb <- 0.0038SD2 <- 25.6theta <- 0.028true_val <- 0
sim <- c(rnorm(n_obs*(1-theta), true_val, sda + sdb*dbh), rnorm(n_obs*theta, true_val, SD2)) #STAN MODEL model <- 'data {int<lower=1> n_obs;vector[n_obs] sim;vector[n_obs] dbh;}parameters { real<lower=0, upper=2> sda;real<lower=0, upper=0.1> sdb;real<lower=0, upper=40> SD2;real<lower=0, upper=1> theta;}model { real log_theta; real log1m_theta; real true_val; true_val <- 0; log_theta <- log(theta); log1m_theta <- log1m(theta);
for (i in 1:n_obs) { increment_log_prob(log_sum_exp(log1m_theta + normal_log(sim[i], true_val, sda + sdb * dbh[i]), log_theta + normal_log(sim[i], true_val, SD2))); }}'
#RUN MODEL - should be complete within 30 secondsmod <- stan(model_code = model, data = list(n_obs=n_obs, dbh=dbh, sim=sim), pars = c('sda','sdb','SD2','theta'), chains = 3, iter = 2000, control=list(adapt_delta=0.9))Inference for Stan model: model.3 chains, each with iter=2000; warmup=1000; thin=1; post-warmup draws per chain=1000, total post-warmup draws=3000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhatsda 0.9634 0.0012 0.0391 0.8886 0.9361 0.9622 0.9894 1.0416 1128 1.0009sdb 0.0039 0.0000 0.0008 0.0024 0.0034 0.0039 0.0044 0.0055 1098 1.0005SD2 27.4668 0.1306 3.7380 21.2797 24.8004 27.2006 29.6947 35.9837 820 1.0021theta 0.0281 0.0001 0.0058 0.0178 0.0240 0.0278 0.0317 0.0403 1555 1.0009lp__ -1743.8433 0.0571 1.6060 -1747.8380 -1744.6132 -1743.4287 -1742.6693 -1741.9318 791 1.0024
Samples were drawn using NUTS(diag_e) at Mon Jun 22 23:13:12 2015.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).#STAN MODEL model <- 'data {int<lower=1> n_obs;vector[n_obs] sim;vector[n_obs] dbh;}parameters { real<lower=0> sda;real<lower=0> sdb;real<lower=0> SD2;real<lower=0, upper=1> theta;}model { real log_theta; real log1m_theta; real true_val; true_val <- 0; log_theta <- log(theta); log1m_theta <- log1m(theta); sda ~ normal(0,5) T[0,]; sdb ~ normal(0,5) T[0,]; SD2 ~ cauchy(0,5); for (i in 1:n_obs) { increment_log_prob(log_sum_exp(log1m_theta + normal_log(sim[i], true_val, sda + sdb * dbh[i]), log_theta + normal_log(sim[i], true_val, SD2))); }}'
#RUN MODEL - should be complete within 30 secondsmod2 <- stan(model_code = model, data = list(n_obs=n_obs, dbh=dbh, sim=sim), pars = c('sda','sdb','SD2','theta'), chains = 3, iter = 2000, control=list(adapt_delta=0.9))
Inference for Stan model: model.3 chains, each with iter=2000; warmup=1000; thin=1; post-warmup draws per chain=1000, total post-warmup draws=3000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhatsda 15.77 0.17 4.80 5.93 12.43 15.97 19.43 23.93 784 1sdb 0.51 0.01 0.40 0.01 0.18 0.43 0.75 1.44 839 1SD2 1.14 0.00 0.03 1.08 1.12 1.14 1.16 1.19 1231 1theta 0.97 0.00 0.01 0.96 0.97 0.97 0.97 0.98 1171 1lp__ -1765.76 0.06 1.48 -1769.57 -1766.53 -1765.46 -1764.66 -1763.85 693 1
Samples were drawn using NUTS(diag_e) at Mon Jun 22 23:34:54 2015.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).
Inference for Stan model: model.3 chains, each with iter=2000; warmup=1000; thin=1; post-warmup draws per chain=1000, total post-warmup draws=3000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhatsda 15.20 0.05 1.64 11.94 14.15 15.25 16.33 18.25 1228 1sdb 0.10 0.00 0.06 0.00 0.05 0.09 0.14 0.25 1150 1SD2 1.12 0.00 0.03 1.07 1.10 1.12 1.14 1.17 1598 1theta 0.97 0.00 0.01 0.95 0.96 0.97 0.97 0.98 1436 1lp__ -1769.73 0.05 1.49 -1773.50 -1770.44 -1769.42 -1768.63 -1767.84 895 1
Samples were drawn using NUTS(diag_e) at Tue Jun 23 00:29:50 2015.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).dbh <- rlnorm(n_obs, 3.443309, 0.9253412) #mock datasda <- 0.927sdb <- 0.0038SD2 <- 25.6theta <- 0.028
sim <- c(rnorm(n_obs*(1-theta), 0, sda + sdb * dbh), rnorm(n_obs*theta, 0, SD2))
model <- 'data {int<lower=1> n_obs;vector[n_obs] sim;vector[n_obs] dbh;}parameters { real<lower=0, upper=1> theta;positive_ordered[3] sigma; # sort sigmas from smallest to largest - helps with switching issues}model { for (i in 1:n_obs) { increment_log_prob(log_sum_exp(log1m(theta) + normal_log(sim[i], 0, sigma[2] + sigma[1] * dbh[i]), #most of data log(theta) + normal_log(sim[i], 0, sigma[3]))); #few data }}'
mod <- stan(model_code = model, data = list(n_obs=n_obs, dbh=dbh, sim=sim), pars = c('sigma','theta'), chains = 1, iter = 500, control=list(adapt_delta=0.9))Inference for Stan model: model.1 chains, each with iter=500; warmup=250; thin=1; post-warmup draws per chain=250, total post-warmup draws=250.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhatsigma[1] 1.02 0.01 0.07 0.84 0.97 1.03 1.07 1.13 88 1.00sigma[2] 1.08 0.01 0.06 0.95 1.06 1.09 1.13 1.16 99 1.02sigma[3] 1.15 0.00 0.02 1.10 1.13 1.15 1.16 1.20 229 1.00theta 0.97 0.00 0.01 0.96 0.97 0.97 0.98 0.98 156 1.00lp__ -1756.05 0.14 1.36 -1759.19 -1756.73 -1755.80 -1755.04 -1754.33 94 1.00
Samples were drawn using NUTS(diag_e) at Tue Jun 23 17:24:15 2015.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). data { int<lower=1> n_obs; vector[n_obs] census_length; vector[n_obs] obs_dbh1; vector[n_obs] obs_dbh2; } parameters { real log_raw_true_dbh1[n_obs]; real log_mu_true_dbh1; real<lower=0, upper=10> log_sigma_true_dbh1; real log_raw_dbh_increment[n_obs]; real log_mu_dbh_increment; real<lower=0, upper=10> log_sigma_dbh_increment; } model { real true_dbh1; real true_dbh2; real dbh_increment; for (i in 1:n_obs) { // Calculating true growth for fitted data true_dbh1 <- exp(log_raw_true_dbh1[i] * log_sigma_true_dbh1 + log_mu_true_dbh1); dbh_increment <- exp(log_raw_dbh_increment[i] * log_sigma_dbh_increment + log_mu_dbh_increment); true_dbh2 <- true_dbh1 + dbh_increment; // theta and SDs are fixed and where determined from another model on data explicitly collected to examine measurement error increment_log_prob(log_sum_exp(log1m(0.046908) + normal_log(obs_dbh1[i], true_dbh1, 0.085871 + 0.005716 * true_dbh1), log(0.046908) + normal_log(obs_dbh1[i], true_dbh1, 3.496852))); increment_log_prob(log_sum_exp(log1m(0.046908) + normal_log(obs_dbh2[i], true_dbh2, 0.085871 + 0.005716 * true_dbh2), log(0.046908) + normal_log(obs_dbh2[i], true_dbh2, 3.496852))); } // Priors // Fitted data DBH measurement error priors log_raw_true_dbh1 ~ normal(0,1); log_mu_true_dbh1 ~ normal(0,2.5); #log_sigma_true_dbh1 ~ cauchy(0,2.5); log_raw_dbh_increment ~ normal(0,1); log_mu_dbh_increment ~ normal(-2,1); #log_sigma_dbh_increment ~ cauchy(0,2.5); } generated quantities { real true_dbh1[n_obs]; real true_dbh2[n_obs]; real dbh_increment; real dbh_dt[n_obs]; real dbh_dt_rel[n_obs]; real basal_area_dt[n_obs]; real basal_area_dt_rel[n_obs]; for (i in 1:n_obs) { // recalculating true growth for fitted data true_dbh1[i] <- exp(log_raw_true_dbh1[i] * log_sigma_true_dbh1 + log_mu_true_dbh1); dbh_increment <- exp(log_raw_dbh_increment[i] * log_sigma_dbh_increment + log_mu_dbh_increment); true_dbh2[i] <- true_dbh1[i] + dbh_increment; dbh_dt[i] <- (true_dbh2[i] - true_dbh1[i])/census_length[i]; dbh_dt_rel[i] <- (log(true_dbh2[i]) - log(true_dbh1[i]))/census_length[i]; basal_area_dt[i] <- (0.25*pi()*true_dbh2[i]^2 - 0.25*pi()*true_dbh1[i]^2)/census_length[i]; basal_area_dt_rel[i] <- (log(0.25*pi()*true_dbh2[i]^2) - log(0.25*pi()*true_dbh1[i]^2))/census_length[i]; } }--
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.
For more options, visit https://groups.google.com/d/optout.
data { int<lower=1> n_obs; vector[n_obs] census_length; vector[n_obs] obs_dbh1; vector[n_obs] obs_dbh2; } transformed data { real quarter_pi; quarter_pi <- 0.25 *pi(); } parameters { real<lower=0> true_dbh1[n_obs]; real log_mu_true_dbh1; real<lower=0> log_sigma_true_dbh1; real<lower=0> dbh_increment[n_obs]; real log_mu_dbh_increment; real<lower=0> log_sigma_dbh_increment; } model { real true_dbh2[n_obs]; true_dbh1 ~ lognormal(log_mu_true_dbh1, log_sigma_true_dbh1); dbh_increment ~ lognormal(log_mu_dbh_increment, log_sigma_dbh_increment);
for (i in 1:n_obs) { // Calculating true growth for fitted data true_dbh2[i] <- true_dbh1[i] + dbh_increment[i]; // theta and SDs are fixed and where determined from another model on data explicitly collected to examine measurement error increment_log_prob(log_mix(0.046908,normal_log(obs_dbh1[i], true_dbh1[i], 3.496852), normal_log(obs_dbh1[i], true_dbh1[i], 0.085871 + 0.005716 * true_dbh1[i]))); increment_log_prob(log_mix(0.046908, normal_log(obs_dbh2[i], true_dbh2[i], 3.496852), normal_log(obs_dbh2[i], true_dbh2[i], 0.085871 + 0.005716 * true_dbh2[i]))); } // Priors log_mu_true_dbh1 ~ normal(0,5); log_sigma_true_dbh1 ~ cauchy(0,2.5);
log_mu_dbh_increment ~ normal(0,5); log_sigma_dbh_increment ~ cauchy(0,2.5); } data { int<lower=1> n_obs; vector[n_obs] census_length; vector[n_obs] obs_dbh1; vector[n_obs] obs_dbh2; } transformed data { real quarter_pi; quarter_pi <- 0.25 *pi(); } parameters { real raw_true_dbh1[n_obs]; real log_mu_true_dbh1; real<lower=0> log_sigma_true_dbh1; real raw_dbh_increment[n_obs]; real log_mu_dbh_increment; real<lower=0> log_sigma_dbh_increment; } model { real true_dbh1[n_obs]; real true_dbh2[n_obs]; real dbh_increment[n_obs]; for (i in 1:n_obs) { // Calculating true growth for fitted data true_dbh1[i] <- exp(log_mu_true_dbh1 + log_sigma_true_dbh1 * raw_true_dbh1[i]); dbh_increment[i] <- exp(log_mu_dbh_increment + log_sigma_dbh_increment * raw_dbh_increment[i]); true_dbh2[i] <- true_dbh1[i] + dbh_increment[i]; // theta and SDs are fixed and where determined from another model on data explicitly collected to examine measurement error increment_log_prob(log_mix(0.046908,normal_log(obs_dbh1[i], true_dbh1[i], 3.496852), normal_log(obs_dbh1[i], true_dbh1[i], 0.085871 + 0.005716 * true_dbh1[i]))); increment_log_prob(log_mix(0.046908, normal_log(obs_dbh2[i], true_dbh2[i], 3.496852), normal_log(obs_dbh2[i], true_dbh2[i], 0.085871 + 0.005716 * true_dbh2[i]))); } // Priors raw_true_dbh1 ~ normal(0,1); raw_dbh_increment ~ normal(0,1);
log_mu_true_dbh1 ~ normal(0,5); log_sigma_true_dbh1 ~ cauchy(0,2.5); log_mu_dbh_increment ~ normal(0,5); log_sigma_dbh_increment ~ cauchy(0,2.5); }'mean se_mean sd 2.5% 25% 50% 75%
log_mu_true_dbh1 1.03 0.00 0.03 0.97 1.01 1.03 1.05
log_sigma_true_dbh1 0.91 0.00 0.02 0.87 0.90 0.91 0.92
log_mu_dbh_increment -2.71 0.01 0.12 -2.96 -2.79 -2.71 -2.63
log_sigma_dbh_increment 1.89 0.01 0.10 1.70 1.82 1.89 1.95
lp__ -258.96 6.23 58.59 -376.81 -298.19 -257.72 -217.38
97.5% n_eff Rhat
log_mu_true_dbh1 1.09 3000 1.00
log_sigma_true_dbh1 0.95 3000 1.00
log_mu_dbh_increment -2.48 277 1.01
log_sigma_dbh_increment 2.09 192 1.03
lp__ -148.56 89 1.02
mean se_mean sd 2.5% 25% 50% 75% 97.5%
log_mu_true_dbh1 1.02 0.01 0.03 0.97 1.00 1.02 1.04 1.07
log_sigma_true_dbh1 0.91 0.01 0.02 0.87 0.89 0.91 0.92 0.95
log_mu_dbh_increment -2.72 0.00 0.12 -2.97 -2.80 -2.72 -2.64 -2.49
log_sigma_dbh_increment 1.86 0.01 0.10 1.67 1.79 1.86 1.93 2.06
lp__ 245.29 11.63 54.72 135.19 210.03 244.90 282.38 352.16
n_eff Rhat
log_mu_true_dbh1 14 1.14
log_sigma_true_dbh1 13 1.29
log_mu_dbh_increment 1272 1.00
log_sigma_dbh_increment 166 1.03
lp__ 22 1.15
Im confused. Isn't what I just described the exact opposite of the expected behaviour.That is I'm getting fewer effective samples and longer compute times with the non-centered reparameterisation.
data { int<lower=1> n_obs; vector[n_obs] census_length; vector[n_obs] obs_dbh1; vector[n_obs] obs_dbh2; } transformed data { real quarter_pi; quarter_pi <- 0.25 *pi(); } parameters { real<lower=0> true_dbh1[n_obs]; real log_mu_true_dbh1; real<lower=0> log_sigma_true_dbh1; real raw_dbh_increment[n_obs]; real log_mu_dbh_increment; real<lower=0> log_sigma_dbh_increment; } model { real dbh_increment[n_obs]; real true_dbh2[n_obs];
for (i in 1:n_obs) { dbh_increment[i] <- exp(raw_dbh_increment[i] * log_sigma_dbh_increment + log_mu_dbh_increment); true_dbh2[i] <- true_dbh1[i] + (dbh_increment[i] * census_length[i]); // theta and SDs are fixed and where determined from another model on data explicitly collected to examine measurement error increment_log_prob(log_mix(0.047,normal_log(obs_dbh1[i], true_dbh1[i], 3.498), normal_log(obs_dbh1[i], true_dbh1[i], 0.086 + 0.006 * true_dbh1[i]))); increment_log_prob(log_mix(0.047, normal_log(obs_dbh2[i], true_dbh2[i], 3.498), normal_log(obs_dbh2[i], true_dbh2[i], 0.086 + 0.006 * true_dbh2[i]))); } // Priors
log_mu_true_dbh1 ~ normal(0,2.5); log_sigma_true_dbh1 ~ cauchy(0,2.5); true_dbh1 ~ lognormal(log_mu_true_dbh1, log_sigma_true_dbh1);
raw_dbh_increment ~ normal(0,1); log_mu_dbh_increment ~ normal(-2,2); log_sigma_dbh_increment ~ cauchy(0,2.5); } generated quantities { real dbh_increment[n_obs]; real true_dbh2[n_obs]; for (i in 1:n_obs) { dbh_increment[i] <- exp(raw_dbh_increment[i] * log_sigma_dbh_increment + log_mu_dbh_increment); true_dbh2[i] <- true_dbh1[i] + (dbh_increment[i] * census_length[i]); } }'