# Assign value to a subvector

**URL:** <https://discourse.mc-stan.org/t/assign-value-to-a-subvector/4247>\
**Category:** General\
**Created:** [May 18, 2018, 1:41pm UTC](https://discourse.mc-stan.org/t/assign-value-to-a-subvector/4247 "2018-05-18T13:41:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![inmybrain](https://avatars.discourse-cdn.com/v4/letter/i/b5ac83/32.png) [@inmybrain](https://discourse.mc-stan.org/u/inmybrain)\
**Post date:** [May 18, 2018, 1:41pm UTC](https://discourse.mc-stan.org/t/assign-value-to-a-subvector/4247/1 "2018-05-18T13:41:00Z")

</div>

Hi,

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
  segment(delta,pos,numInst[ii])
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.

```stan
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?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [May 18, 2018, 2:41pm UTC](https://discourse.mc-stan.org/t/assign-value-to-a-subvector/4247/2 "2018-05-18T14:41:27Z")

</div>

> [@inmybrain](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![inmybrain](https://avatars.discourse-cdn.com/v4/letter/i/b5ac83/32.png) [@inmybrain](https://discourse.mc-stan.org/u/inmybrain)\
**Post date:** [May 18, 2018, 5:02pm UTC](https://discourse.mc-stan.org/t/assign-value-to-a-subvector/4247/3 "2018-05-18T17:02:20Z")

</div>

Thanks a lot! It works well.
