stan-dev / example-models

Example models for Stan
http://mc-stan.org/
771 stars 479 forks source link

shrinkage uniform prior in Magnesium meta-analysis doesn't work #121

Closed xuxuxuzz closed 6 years ago

xuxuxuzz commented 6 years ago

Recently, I have been studying the Stan example of Magnesium meta-analysis(). It works well as a whole. However, when I tried to use shrinkage uniform prior only, it didn't work. Here is the code that I have rewritten according to the Magnesium meta-analysis example.

m.mgo <- 'data {
int N_studies; 
int rt[N_studies];
int nt[N_studies]; 
int rc[N_studies]; 
int nc[N_studies]; 
} 
transformed data {
real<lower=0> s0_sq;
s0_sq <- 0.1272041; 
} 
parameters {
real<lower=-10,upper=10> mu;
real theta[N_studies];
real<lower=0,upper=1> pc[N_studies];
real<lower=0,upper=1> B0;
}
transformed parameters {
real<lower=0> tau;
tau <- sqrt(s0_sq * (1-B0) / B0);
}
model {

// Prior 4: Uniform shrinkage on tau.sqrd
B0 ~ uniform(0,1);
vector[N_studies] tmpm;
mu ~ uniform(-10, 10);
pc ~ uniform(0,1);
theta ~ normal(mu, tau);
for (i in 1:N_studies){
tmpm[i] <- theta[i] + logit(pc[i]);
rc ~ binomial(nc, pc);
rt ~ binomial_logit(nt, tmpm);
}
} 
mgo <- stan(model_code = m.mgo,data=mgmdt,iter = 3000, cores=1,chains = 2,warmup =500)

This is the warning I have got! image

I try to change the position of vector[N_studies] tmpm; to the first line of the model block. It still didn't work This is the warning.

[383] "Rejecting initial value:"                                                                                                              
[384] "  Error evaluating the log probability at the initial value."                                                                          
[385] "Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:"                        
[386] "Exception thrown at line 23: normal_log: Random variable is nan, but must not be nan!"                                                 
[387] "If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,"
[388] "but if this warning occurs often then your model may be either severely ill-conditioned or misspecified."                              
[389] "Rejecting initial value:"                                                                                                              
[390] "  Error evaluating the log probability at the initial value."

Can anyone tell me what the problem this is and how to write it correctly? I will highly appreciate your help.

[edited to escape code]

bob-carpenter commented 6 years ago

Indenting really helps with code. I edited to escape with three backticks (```) on their own line.

I'm not sure what you mean by a shrinkage uniform prior. Shrinkage is a notion from point estimation which usually pushes estimates toward zero---you're just restricting to a range. That shouldn't be a problem, though we don't recommend interval-bounded priors because they are poorly behaved statistically and computationally if values very close to the boundaries are consistent with the data.

What's happening is that you're providing an undefined variable to the normal distribution. Figure out what line 23 of your code is (helps to have Stan programs in their own files). Then make sure all the variables are defined.

Please follow-up on our forum if you need more help:

http://discourse.mc-stan.org

This isn't a bug report on Stan, so I'm going to close the issue.