# Exception: mismatch in number dimensions declared

**URL:** <https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291>\
**Category:** RStan\
**Tags:** rstan\
**Created:** [May 22, 2018, 6:43pm UTC](https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291 "2018-05-22T18:43:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [May 22, 2018, 6:43pm UTC](https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291/1 "2018-05-22T18:43:44Z")

</div>

I am thinking this my question is related to an earlier bug [here](http://discourse.mc-stan.org/t/passing-data-as-a-list-vs-character-vector/2740). If so, I am just looking for some kind of work around.

I am getting the following error:

> Error in new\_CppObject\_xp(fields$.module, fields$.pointer, …) :  
> Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=Y; dims declared=(92); dims found=(92,1) (in ‘model4234235eadc\_model’ at line 16)

The stan code is on my github [here](https://github.com/alexhallam/media_mix_sim/blob/master/model.stan). A csv of the data is [here](https://github.com/alexhallam/media_mix_sim/blob/master/clean_data.csv)

Also, below is the `R` code I use to prepare the data for Stan,

```r
clean_data <- read_csv("clean_data.csv")
media_data <- clean_data %>% select(contains("m_"))
# data Prep
N <- nrow(clean_data)
Y <- clean_data[,"sales"] %>% as.vector()
max_lag <- 13
num_media <- 3
lag_vec <- seq(0, max_lag - 1)
X_media <- array(data = media_data, dim = 3)
num_ctrl <- 1
X_ctrl <- clean_data %>% select(price) %>% as.matrix()

stan_data <- list(N=N, Y=Y, max_lag=max_lag, num_media=num_media,
                  lag_vec=lag_vec,X_media=X_media,
                  num_ctrl=num_ctrl,X_ctrl=X_ctrl)

```

---

<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 22, 2018, 6:48pm UTC](https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291/2 "2018-05-22T18:48:47Z")

</div>

I think you can just do `Y <- clean_data$sales`. Then look at `dims(Y)`, which should be `92` only.

---

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [May 22, 2018, 6:52pm UTC](https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291/3 "2018-05-22T18:52:32Z")

</div>

> [@bgoodri](#):
>
> clean\_data$sales

Thanks! That fixed it!

Now I am getting an error

> variable name=X\_media; dims declared=(92,3,13); dims found=(92,3)

I don’t usually work with 3D arrays so I am not sure how people normally create them.

---

<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 22, 2018, 6:58pm UTC](https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291/4 "2018-05-22T18:58:15Z")

</div>

It would be `X_media <- array(data = media_data, dim = c(92, 3, 13))` but I am not sure where the 13 comes from.

---

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [May 22, 2018, 7:02pm UTC](https://discourse.mc-stan.org/t/exception-mismatch-in-number-dimensions-declared/4291/5 "2018-05-22T19:02:56Z")

</div>

Thanks, that code is useful. I know that 13 is `max_lag`, but I am not sure how to combine it here. I will look into it more.
