data {
int<lower=0> Nobs; // # observations
int<lower=1> Npat; // # individuals
int<lower=1> NcovsPat; // # individual-level predictors (1, sex)
int<lower=1> NasymParm; // # parameters for non-linear model
int<lower=1,upper=Npat> jj[Nobs]; // individual for observation
vector[Nobs] time; // time of follow up
matrix[Npat,NcovsPat] Upat; // individual-level predictors [1, sex]_jj
vector[Nobs] y; // outcome
}
parameters {
matrix[NasymParm,Npat] z;
cholesky_factor_corr[NasymParm] CovsPatOmega; // correlation matrix for predictor-coefficients
vector<lower=0>[NasymParm] tau; // prior scale on covar-var matrix
matrix[NcovsPat,NasymParm] gamma; // individual-level coefficients (1,sex)X(Asym,R0,lrc)
real<lower=0> sigma; // obs-level error
}
transformed parameters {
matrix[Npat,NasymParm] beta; // individual-level predictors, each row is a NasymParm-length vector
vector[Npat] Asym; // Asymptote
vector[Npat] R0; // Initial value
vector[Npat] lrc; // log rate of change
matrix[NasymParm,NasymParm] Sigma_beta; // reparameterize as cov-var matrix
beta <- Upat * gamma + (diag_pre_multiply(tau,CovsPatOmega) * z)';
Asym <- col(beta,1);
R0 <- col(beta,2);
lrc <- col(beta,3);
Sigma_beta <- diag_pre_multiply(tau,CovsPatOmega) * diag_pre_multiply(tau,CovsPatOmega)'; // Recalculate as covar-var matrix
}
model {
vector[Nobs] Mod_jj;
for (n in 1:Nobs) {
Mod_jj[n] <- Asym[jj[n]] + (R0[jj[n]] - Asym[jj[n]]) * exp(-exp(lrc[jj[n]]) * time[n]);
}
y ~ normal(Mod_jj, sigma);
tau ~ cauchy(0,2.5);
to_vector(z) ~ normal(0,1);
CovsPatOmega ~ lkj_corr_cholesky(2);
to_vector(gamma) ~ normal(0,5);
}
The problem is more severe than I thought since I posted this. It took > 2 hours for 30 iterations and then gave a segfault from C stack overflow at the end (see below). It never generated stan sampling object after all that. Any guidance would be very much appreciated:
...Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:stan::prob::normal_log(N4stan5agrad3varE): Scale parameter is 0:0, but must be > 0!If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 6 / 30 [ 20%] (Warmup)
Iteration: 9 / 30 [ 30%] (Warmup)
Iteration: 12 / 30 [ 40%] (Warmup)
Iteration: 15 / 30 [ 50%] (Warmup)
Iteration: 16 / 30 [ 53%] (Sampling)
Iteration: 18 / 30 [ 60%] (Sampling)
Iteration: 21 / 30 [ 70%] (Sampling)
Iteration: 24 / 30 [ 80%] (Sampling)
Iteration: 27 / 30 [ 90%] (Sampling)
Iteration: 30 / 30 [100%] (Sampling)
# Elapsed Time: 1466.86 seconds (Warm-up)
# 6163.34 seconds (Sampling)
# 7630.2 seconds (Total)
Error: segfault from C stack overflow Hi Steve,
What interface are you using? And what version?
Are you able to share the data? If so, I'll try to reproduce the problem on my end.
Daniel
--
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.
to_vector(gamma) ~ Normal(0,5) gamma[1,1] ~ normal(6, 5);
gamma[1,2] ~ normal(4, 5);
gamma[1,3] ~ normal(0, 2);
gamma[2] ~ normal(0,5);...
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
stan::prob::normal_log(N4stan5agrad3varE): Scale parameter is 0:0, but must be > 0!
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 5 / 50 [ 10%] (Warmup)
Iteration: 10 / 50 [ 20%] (Warmup)
Iteration: 15 / 50 [ 30%] (Warmup)
Iteration: 20 / 50 [ 40%] (Warmup)
Iteration: 25 / 50 [ 50%] (Warmup)
Iteration: 26 / 50 [ 52%] (Sampling)
Iteration: 30 / 50 [ 60%] (Sampling)
Iteration: 35 / 50 [ 70%] (Sampling)
Iteration: 40 / 50 [ 80%] (Sampling)
Iteration: 45 / 50 [ 90%] (Sampling)
Iteration: 50 / 50 [100%] (Sampling)
# Elapsed Time: 5242.04 seconds (Warm-up)
# 6565.55 seconds (Sampling)
# 11807.6 seconds (Total)
Error: segfault from C stack overflow
> > > > > > > > 11807/3600[1] 3.279722> traceplot(fit1, 'gamma', ncol = 3, nrow = 2)
Process R segmentation fault: 11 at Fri Jun 26 12:41:44 2015
It finished 50 iterations at 3.3 hours. My computer apparently went to sleep overnight, so wasn't as long as I thought. However, still suffering from stack overflow even with the new narrower prior on lrc.
...
Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
stan::prob::normal_log(N4stan5agrad3varE): Scale parameter is 0:0, but must be > 0!
If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
Iteration: 5 / 50 [ 10%] (Warmup)
Iteration: 10 / 50 [ 20%] (Warmup)
Iteration: 15 / 50 [ 30%] (Warmup)
Iteration: 20 / 50 [ 40%] (Warmup)
Iteration: 25 / 50 [ 50%] (Warmup)
Iteration: 26 / 50 [ 52%] (Sampling)
Iteration: 30 / 50 [ 60%] (Sampling)
Iteration: 35 / 50 [ 70%] (Sampling)
Iteration: 40 / 50 [ 80%] (Sampling)
Iteration: 45 / 50 [ 90%] (Sampling)
Iteration: 50 / 50 [100%] (Sampling)
# Elapsed Time: 5242.04 seconds (Warm-up)
# 6565.55 seconds (Sampling)
# 11807.6 seconds (Total)
Error: segfault from C stack overflow
> > > > > > > > 11807/3600
[1] 3.279722
On Jun 26, 2015, at 12:04 AM, Steve Bellan <steve....@gmail.com> wrote:
But in thinking about this I realized that I have ridiculous priors right now and I wonder if that's part of the problem. I kept
from the manual code. But the first row of my gamma matrix are intercepts for each of the non-linear model parameters. And I know a priori that R0 should be in the range of of 2-6 and R0 and Asym should be 5-7. lrc is probably in the range of -2 to 2. So I changed the priors to:to_vector(gamma) ~ Normal(0,5)
gamma[1,1] ~ normal(6, 5);
gamma[1,2] ~ normal(4, 5);
gamma[1,3] ~ normal(0, 2);
gamma[2] ~ normal(0,5); y ~ normal(Mod_jj, sigma); n_eff Rhat
gamma[1,1] 3.05 4.88
gamma[1,2] 3.35 2.40
gamma[1,3] 3.34 4.25
gamma[2,1] 7.13 1.19
gamma[2,2] 47.30 1.01
gamma[2,3] 68.80 1.13
Sigma_beta[1,1] 1.01 30.50
Sigma_beta[1,2] 4.26 1.86
Sigma_beta[1,3] 1.03 13.50
Sigma_beta[2,1] 4.26 1.86
Sigma_beta[2,2] 1.06 5.04
Sigma_beta[2,3] 3.14 2.66
Sigma_beta[3,1] 1.03 13.50
Sigma_beta[3,2] 3.14 2.66
Sigma_beta[3,3] 3.08 5.55
sigma 1.16 7.50
tau[1] 1.01 60.00
tau[2] 1.10 4.48
tau[3] 3.40 3.79
CovsPatOmega[1,1] 100.00 NaN
CovsPatOmega[1,2] 100.00 NaN
CovsPatOmega[1,3] 100.00 NaN
CovsPatOmega[2,1] 1.13 5.28
CovsPatOmega[2,2] 1.93 2.86
CovsPatOmega[2,3] 100.00 NaN
CovsPatOmega[3,1] 1.07 4.14
CovsPatOmega[3,2] 3.90 1.89
CovsPatOmega[3,3] 4.95 1.54
lp__ 1.14 8.07 n_eff Rhat
gamma[1,1] 7.61 1.27
gamma[1,2] 14.30 1.16
gamma[1,3] 8.51 1.22
gamma[2,1] 100.00 1.02
gamma[2,2] 45.80 1.03
gamma[2,3] 100.00 1.02
Sigma_beta[1,1] 6.32 1.29
Sigma_beta[1,2] 7.37 1.30
Sigma_beta[1,3] 15.60 1.11
Sigma_beta[2,1] 7.37 1.30
Sigma_beta[2,2] 4.19 1.57
Sigma_beta[2,3] 17.30 1.03
Sigma_beta[3,1] 15.60 1.11
Sigma_beta[3,2] 17.30 1.03
Sigma_beta[3,3] 7.41 1.28
sigma 5.87 1.35
tau[1] 6.80 1.27
tau[2] 4.37 1.54
tau[3] 7.29 1.30
CovsPatOmega[1,1] 100.00 NaN
CovsPatOmega[1,2] 100.00 NaN
CovsPatOmega[1,3] 100.00 NaN
CovsPatOmega[2,1] 4.90 1.53
CovsPatOmega[2,2] 6.26 1.33
CovsPatOmega[2,3] 100.00 NaN
CovsPatOmega[3,1] 4.22 1.20
CovsPatOmega[3,2] 3.57 1.97
CovsPatOmega[3,3] 4.69 1.48
lp__ 6.20 1.32Well, those clearly haven't converged. Are the chains close
and just not mixing, or are they clearly stuck in different
modes? That is, does it look like running longer might help?
I don't know if you're getting any divergences here (typically
indicated by warmup messages, but you can also pull them out
of the fit in RStan and they're printed in CmdStan), but if you
are, you probably want to start with a smaller step size and
up the adapt_delta target above its default of 0.8 (perhaps as
high as 0.99). That'll keep step size down and allow the gradients
to do a better job of mimicking the curvature. But it'll slow
down the iterations (at the benefit of getting higher n_eff per
iteration).
The problem with increasing data is that it can increase posterior
curvature along narrow ridges, making it harder for the Hamiltonian
simulation to find a large step size to make large moves. Sometimes
this is good and you actually get sublinear growth with data. Sometimes
it's bad and constrains the parameters and makes them correlated in
a way that makes it much harder to move in Euclidean space with just
a gradient. So you'll probably see the number of leapfrog steps going up and
the step size going down as you get more data to try to compensate.
data {
int<lower=0> Nobs; // # observations
int<lower=1> Npat; // # individuals
int<lower=1> NcovsPat; // # individuals-level predictors (1, sex)
int<lower=1> NasymParm; // # parameters for non-linear model
int<lower=1,upper=Npat> jj[Nobs]; // individuals for observation
vector[Nobs] time; // time of follow up
matrix[Npat,NcovsPat] Upat; // individuals-level predictors [1, sex]_jj
vector[Nobs] y; // outcome
}
parameters {
corr_matrix[NasymParm] Omega; // correlation matrix for predictor-coefficients
vector<lower=0>[NasymParm] tau; // prior scale on covar-var matrix
matrix[NcovsPat,NasymParm] gamma; // individuals-level coefficients (1,sex)X(Asym,R0,lrc)
vector[NasymParm] beta[Npat]; // each individual's asymptotic parameters
real<lower=0> sigma; // obs-level error
}
model {
matrix[NasymParm,NasymParm] Sigma_beta; // covar-var matrix
vector[Nobs] Mod_jj;
vector[NasymParm] u_gamma_t[Npat];
Sigma_beta <- quad_form_diag(Omega,tau);
tau ~ cauchy(0,2.5);
Omega ~ lkj_corr(2);
gamma[1,1] ~ normal(6, 5);
gamma[1,2] ~ normal(4, 5);
gamma[1,3] ~ normal(0, 2);
gamma[2] ~ normal(0,5);
for(j in 1:Npat)
u_gamma_t[j] <- (Upat[j] * gamma)';
beta ~ multi_normal(u_gamma_t, Sigma_beta);
for (n in 1:Nobs) {
Mod_jj[n] <- beta[jj[n],1] + (beta[jj[n],2] - beta[jj[n],1]) * exp(-exp(beta[jj[n],3]) * time[n]);
}
y ~ normal(Mod_jj, sigma);
}You want to be thinking in terms of posterior mass more than
density.
For example, consider beta(0.9,0.9), which has unbounded
density as y approaches 0 or 1, even though roughly 90% of
the mass is in (0.05, 0.95).