title: How can I use RStan without for loops?
body: <p>Is there a way to more efficiently perform the following calculations in RStan?</p>
  
  <p>I have only provided the minimal amount of coded that is needed:</p>
  
  <pre><code>parameters {
    real beta_0;
    real beta_1;
  }     
  model {
    vector [n] p_i = exp(beta_0 + beta_1*x)/[1 + exp(beta_0 + beta_1*x)];
    y ~ bernoulli(p_i);
    /* Likelihood:
    for(i in 1:n){
      p_i[i] = exp(beta_0 + beta_1*x[i])/(1 + exp(beta_0 + beta_1*x[i]));
      y[i] ~ bernoulli(p_i[i]);
    */}
  // Prior:
    beta_0 ~ normal(m_beta_0, s_beta_0);
    beta_1 ~ normal(m_beta_1, s_beta_1);
  }
  </code></pre>
  
  <p>I obtain the following error message: "Matrix expression elements must be type row_vector and row vector expression elements must be int or real, but found element of type vector". If I use the for loop (which is commented out), the code works fine, but I would like to limit the use of for loops in my code. In the above code, x, is a vector of length n. </p>
  
  <p>Another example: </p>
  
  <pre><code>parameters {
    real gamma1;
    real gamma2;
    real gamma3;
    real gamma4;
  }
  model {
  // Likelihood:
    real lambda;
    real beta;
    real phi;
    for(i in 1:n){
      lambda = exp(gamma1)*x[n_length[i]]^gamma2;
      beta = exp(gamma3)*x[n_length[i]]^gamma4;
      phi = lambda^(-1/beta);
      y[i] ~ weibull(beta, phi);
    }
    //y ~ weibull(exp(gamma1)*x^gamma2, exp(gamma3)*x^gamma4); //cannot raise a vector to a power
  // Prior:
    gamma1 ~ normal(m_gamma1, s_gamma1);
    gamma2 ~ normal(m_gamma2, s_gamma2);
    gamma3 ~ normal(m_gamma3, s_gamma3);
    gamma4 ~ normal(m_gamma4, s_gamma4);
  }
  </code></pre>
  
  <p>The above code works, but the commented out likelihood calculation does not work since I "cannot raise a vector to a power" (but you can in R). I would, once again, like to not be forced to use for loops. In the above code, n_length, is a vector of length n.</p>
  
  <p>A final example. If I want to draw 10000 samples from a normal distribution in R, I can simply specify </p>
  
  <pre><code>rnorm(10000, mu, sigma)
  </code></pre>
  
  <p>But in RStan, I would have to use a for loop, for example </p>
  
  <pre><code>parameters {
        real mu;
        real sigma;
      }
  generated quantities {
    vector[n] x;
    for(i in 1:n) {
      x[i] = normal_rng(mu, sigma);
    }
  }
  </code></pre>
  
  <p>Is there anything that I can do to speed up my RStan examples? </p>