Spike and Slab question

Hi all,
I’m trying to set-up a spike and slab prior. Below is the code and error message and it seems to be related to the a conflict with the lambda parameter. I’m pretty sure the code is correct for the spike and slab, but if you see something strange, your comments would be appreciated. (I realize there are easier ways to specify the modeling, but this code is for pedagogical purposes).

Thank you!!!

modelString = "
data {
    int<lower=0> n;
    vector [n] readscore;
    vector [n] Female;      vector [n] ESCS;
    vector [n] METASUM;     vector [n] PERFEED;
    vector [n] JOYREAD;     vector [n] MASTGOAL;
    vector [n] ADAPTIVITY;  vector [n] TEACHINT;
    vector [n] SCREADDIFF;  vector [n] SCREADCOMP;
}

parameters {
    real alpha;
    real beta1; real beta6;
    real beta2; real beta7;
    real beta3; real beta8;
    real beta4; real beta9;
    real beta5; real beta10;
    real<lower=0> sigma;
    real<lower=0,upper=1> pi;
    int <lower=0,upper=1> lambda; // Needed for Spike and Slab
    real<lower=0> spike;    // Needed for Spike and Slab
    real<lower=0> slab;
    
}

model {
    real mu[n];
      for (i in 1:n)
       mu[i] = alpha + beta1*Female[i] + beta2*ESCS[i] + beta3*METASUM[i]
            + beta4*PERFEED[i] + beta5*JOYREAD[i] + beta6*MASTGOAL[i]
            + beta7*ADAPTIVITY[i] + beta8*TEACHINT[i]
            + beta9*SCREADDIFF[i] + beta10*SCREADCOMP[i] ;
  

  // Spike and Slab Priors
      sigma ~ cauchy(0,1);
      lambda ~ bernoulli(.5);
      spike ~ normal(0,0.001);
      slab ~ normal(0,1);
      alpha ~ normal(0,5));
      beta1 ~ lambda*slab+(1-lambda*spike); beta6 ~ lambda*slab+(1-lambda*spike);
      beta2 ~ lambda*slab+(1-lambda*spike); beta7 ~ lambda*slab+(1-lambda*spike);
      beta3 ~ lambda*slab+(1-lambda*spike); beta8 ~  lambda*slab+(1-lambda*spike);
      beta4 ~ lambda*slab+(1-lambda*spike); beta9 ~ lambda*slab+(1-lambda*spike);
      beta5 ~ lambda*slab+(1-lambda*spike); beta10 ~ lambda*slab+(1-lambda*spike);
      

  // Likelihood
      readscore ~ normal(mu, sigma);
      
}

// For posterior predictive checking and loo cross-validation
generated quantities {
  vector[n] readscore_rep;
  vector[n] log_lik;
  for (i in 1:n) {
    readscore_rep[i] = normal_rng(alpha + beta1*Female[i] + beta2*ESCS[i] + beta3*METASUM[i]
            + beta4*PERFEED[i] + beta5*JOYREAD[i] + beta6*MASTGOAL[i]+ beta7*ADAPTIVITY[i] + beta8*TEACHINT[i]
            + beta9*SCREADDIFF[i] + beta10*SCREADCOMP[i], sigma);

    log_lik[i] = normal_lpdf(readscore[i] | alpha + beta1*Female[i] + beta2*ESCS[i] + beta3*METASUM[i]
            + beta4*PERFEED[i] + beta5*JOYREAD[i] + beta6*MASTGOAL[i]+ beta7*ADAPTIVITY[i] + beta8*TEACHINT[i]
            + beta9*SCREADDIFF[i] + beta10*SCREADCOMP[i], sigma);
  }
}

"


SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Parameters or transformed parameters cannot be integer or integer array;  found int variable declaration, name=lambda
 error in 'model42434d456d7_6c6886eff1f4f4aa9f39f0d04f5eb52c' at line 21, column 32
  -------------------------------------------------
    19:     real<lower=0> sigma;
    20:     real<lower=0,upper=1> pi;
    21:     int <lower=0,upper=1> lambda; // Needed for Spike and Slab
                                       ^
    22:     real<lower=0> spike;    // Needed for Spike and Slab
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model '6c6886eff1f4f4aa9f39f0d04f5eb52c' due to the above error.

Please also provide the following information in addition to your question: 

* Operating System:
* brms Version:

Don't forget to add relevant tags to your topic (top right of this form) especially for application area. Delete this text before posting your question :-) Thx!

Looking forward to your topic!

Stan does not support integer parameters; these need to be marginalized out. However, this generally doesn’t work well for the spike and slab, and it’s often (but not universally) recommended to use a horseshoe prior as a viable alternative. For more, see

See also your earlier post here

And this post

Note also that in your code where you have

beta1 ~ lambda*slab+(1-lambda*spike);

you are using the ~ sampling notation but the right-hand side is not a distribution (it’s just a number). This is in one sense a secondary concern since the spike-and-slab probably isn’t achievable in the way you want in the first place. In another sense, it’s important to understand what ~ does and why statements like this one don’t mean anything in the Stan language to support your future modeling endeavors in Stan.

1 Like