Beta regression taking too long

I am trying to use beta regression for modeling cognitive scores and am running into the problem of very long run time for 500 iteration with 1 chain it says :
1000 transitions using 10 leapfrog steps per transition would take 47960 seconds.

My outcome is a cognitive score between 0 - 1.94, which I have transformed to lie in the interval (0,1), 0 and 1 not inclusive. And have about 4 categorical (with 2 or 3 categories) and 1 continuous predictor (age centered). the number of obervations is about 9000.

  int<lower = 1> N; // sample size
  int<lower = 1> k; // number of auxilliary variables (independent predictors)
  vector<lower = 0, upper = 1>[N] Y; // vector of outcome (dependent variable)
  matrix[N, k] X; // design matrix

  vector[k] beta; // fixed effects (regression parameters)
  real<lower = 0> phi;  //dispersion parameter

transformed parameters{
  vector[N] Xbeta;
  vector[N] mu; // transformed (inverse logit) linear predictor
  vector[N] p; // shape parameter 1 for beta distribution
  vector[N] q; // shape parameter 1 for beta distribution
  Xbeta = X * beta;
  for(i in 1:N)
  mu = inv_logit(Xbeta);

  p = mu * phi;
  q = (1 - mu) * phi;

    // priors
   beta ~ normal(0, 1);
   phi ~ cauchy(0, 5);
  // likelihood
   Y ~ beta(p, q);


Could some one suggest why this would be taking so long to run? I also tried using the stan_betareg function of the betareg and rstanarm packages and everything works alright (it is slow, but definitely like the stan code I’ve written).


Looks like you are doing N times inv_logit(Xbeta), but Xbeta has also N entries.
Try mu[n]=inv_logit(Xbeta[n]).

(I knew what I was looking for because this is a typical bug when a model is unexpectedly slow)

Ahh! Thank you so much for pointing this out!