# Problem Simulating Data with Generated Quantities (Dimension mismatch in assignment; type = real; right-hand side type = real\[ \])

**URL:** <https://discourse.mc-stan.org/t/problem-simulating-data-with-generated-quantities-dimension-mismatch-in-assignment-type-real-right-hand-side-type-real/22189>\
**Category:** RStan\
**Tags:** techniques, specification, rstan\
**Created:** [April 29, 2021, 1:31am UTC](https://discourse.mc-stan.org/t/problem-simulating-data-with-generated-quantities-dimension-mismatch-in-assignment-type-real-right-hand-side-type-real/22189 "2021-04-29T01:31:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLC](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jlc/32/12520_2.png) [@JLC](https://discourse.mc-stan.org/u/JLC)\
**Post date:** [April 29, 2021, 1:31am UTC](https://discourse.mc-stan.org/t/problem-simulating-data-with-generated-quantities-dimension-mismatch-in-assignment-type-real-right-hand-side-type-real/22189/1 "2021-04-29T01:31:30Z")

</div>

I am trying to start working directly with Stan code by simulating some data from priors using the `generated quantities` block. I’m struggling to match vectors and reals

```
# Fake starting data frame
n <- 20
data <- data.frame(
  SOGF = sample(c(0,1), n, replace = TRUE),
  STGF = sample(c(0,1), n, replace = TRUE),
  PK = rnorm(n, mean = 50, sd = 2),
  RTN = rnorm(n, mean = 0, sd = 1))
X <- model.matrix(data = data, 
                  RTN ~ SOGF + STGF + PK)

```

```
#Stan model
model_code = "
data {
  int n; // number of observations
  int k; // number of predictors
  matrix[n,k] X; // predictor matrix
  vector[n] Y; // outcome vector
  int<lower = 0, upper = 1> run_estimation; // evaluate the likelihood?
}
parameters {
  real alpha; // intercept
  vector[k] beta; // coefficients for predictors
  real<lower=0> sigma; // error scale
}
model {
  alpha ~ normal(0, 10);
  beta[1] ~ normal(0, 10); // prior for SOGF
  beta[2] ~ normal(0, 10); // prior for STGF
  beta[3] ~ normal(0, 10); // prior for PK
  sigma ~ std_normal();
  // conditionally run likelihood
  if(run_estimation==1){
    Y ~ normal(alpha + X * beta, sigma);
  }
}
generated quantities { 
 vector[n] Y_sim;
for(i in 1:n) {
    Y_sim[i] = normal_rng(alpha + X * beta, sigma); 
  }
} 
"

```

```
# Sample from model
sim_out <- stan(model_code = model_code, 
                data = list(n = n, 
                            k = ncol(X), 
                            X = X, 
                            Y = data$RTN, 
                            run_estimation = 0),
                    iter = 1000, 
                    chains = 1)

```

This gives me the following error:

```
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Dimension mismatch in assignment; variable name = Y_sim, type = real; right-hand side type = real[].
Illegal statement beginning with non-void expression parsed as
  Y_sim[i]
Not a legal assignment, sampling, or function statement. Note that
  * Assignment statements only allow variables (with optional indexes) on the left;
  * Sampling statements allow arbitrary value-denoting expressions on the left.
  * Functions used as statements must be declared to have void returns

 error in 'modelb69803c68a26d_6e8a02c9f71e8fbd8d0f5be02af0006c' at line 28, column 4
  -------------------------------------------------
    26: vector[n] Y_sim;
    27: for(i in 1:n) {
    28: Y_sim[i] = normal_rng(alpha + X * beta, sigma); 
           ^
    29: }
  -------------------------------------------------

PARSER EXPECTED: "}"

```

If I comment out the `generated quantities` block I can sample from the fake data.

What am I doing incorrectly in that `generated quantities` block?

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 29, 2021, 1:45am UTC](https://discourse.mc-stan.org/t/problem-simulating-data-with-generated-quantities-dimension-mismatch-in-assignment-type-real-right-hand-side-type-real/22189/2 "2021-04-29T01:45:16Z")

</div>

The error here is:

```
variable name = Y_sim, type = real; right-hand side type = real[]

```

Because the result of `alpha + X * beta` is a vector of length `n`, the vectorised version of `normal_rng` is being called, and attempting to return an array of reals (i.e., `real[]`), except you’re trying to assign the result to a single value (`Y_sim[i]`).

You can instead just allocate the vectorised return directly:

```stan
generated quantities { 
 real Y_sim[n] = normal_rng(alpha + X * beta, sigma); 
} 

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [April 29, 2021, 1:48am UTC](https://discourse.mc-stan.org/t/problem-simulating-data-with-generated-quantities-dimension-mismatch-in-assignment-type-real-right-hand-side-type-real/22189/3 "2021-04-29T01:48:13Z")

</div>

Also, stan has a built-in distribution for linear regression which will sample more efficiently (and is GPU accelerated if you’re using `cmdstanr`).

You would specify it like so:

```stan
  if(run_estimation==1){
    Y ~ normal_id_glm(X, alpha, beta, sigma);
  }

```

---

<div class="post-metadata">

**Author:** ![JLC](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jlc/32/12520_2.png) [@JLC](https://discourse.mc-stan.org/u/JLC)\
**Post date:** [April 29, 2021, 1:51am UTC](https://discourse.mc-stan.org/t/problem-simulating-data-with-generated-quantities-dimension-mismatch-in-assignment-type-real-right-hand-side-type-real/22189/4 "2021-04-29T01:51:30Z")

</div>

> [@andrjohns](#):
>
> `real Y_sim[n] = normal_rng(alpha + X * beta, sigma);`

Thank you once again, @andrjohns !!
