Assign value to a subvector


I’m trying to assign random variables (output of “multinomial_rng”) to a subvector of “delta” below, and rstan shows this error message that I couldn’t handle:

Illegal statement beginning with non-void expression parsed as
Not a legal assignment, sampling, or function statement.  Note that
  * Assignment statements only allow variables (with optional indexes) on the left;
    if you see an outer function logical_lt (<) with negated (-) second argument,
    it indicates an assignment statement A <- B with illegal left
    side A parsed as expression (A < (-B)).
  * Sampling statements allow arbitrary value-denoting expressions on the left.
  * Functions used as statements must be declared to have void returns

Here’s my .stan script, which does random samplings from multinomial distributions and binds generated vectors.

generated quantities{
  int delta[m];
  int pos = 1;
  for(ii in 1:n){
    vector[numInst[ii]] hp_pi_tilde = rep_vector(0, numInst[ii]);
    for(jj in 1:numInst[ii]){
      hp_pi_tilde[jj] = hp_pi[pos + jj - 1] * exp(-1/(2*sigma2_error) * square(Y[ii] - beta0[1] - dot_product(X[pos + jj - 1,], beta1))); // calculate success probabilities
    segment(delta, pos, numInst[ii]) = multinomial_rng(hp_pi_tilde / sum(hp_pi_tilde), 1);
    pos = pos + numInst[ii];

In R language, what I’m doing (and gives me an error, I guess) is
delta[pos:(pos+numInst[ii]-1)] <- rmultinom(1, success_prop)

Could anyone help me with this?

The segment function cannot be used on the left-hand side of an assignment. But the R-like colon syntax is legal in Stan, so just use that.

Thanks a lot! It works well.