Passing data as a list vs character vector


#1

Hi all,

Been getting a frustrating “dimension mismatch” error when trying to pass some data to Stan. I was positive that the dimensions were right (fake data), so couldn’t figure it out. Then I decided to pass the data as a list rather than a named vector (and let Stan look up my local environment) and it worked!

Sure, I can fix the issue by passing a list now, but the data is exactly the same, and I’m curious to know why one works and the other doesn’t. Here’s the minimal code (p.s I need y to be an int as it’s a discrete variable):

# Fake Data
N <- 5 # no. of dimensions
D <- 2 # no. of covariates
K <- 3 # no. of groups
x <- list()
for (i in 1:K) {x[[i]] <- matrix(data=1, nrow=N, ncol=D)}
y <- matrix(data=1, nrow=N, ncol=K)
df <- list(N=N, D=D, K=K, x=x, y=y) # data list

# "Model"
stan_code <- "
data {
    int<lower=0> N;
    int<lower=0> K;
    int<lower=0> D;
    matrix[N, D] x[K];
    int y[N, K];
}
"
fit <- stan(model_code=stan_code, data=df) # works
fit <- stan(model_code=stan_code, data=c("N", "D", "K", "x", "y")) # dimension mismatch

Operating System: macOS High Sierra
Interface Version: RStan
Output of writeLines(readLines(file.path(Sys.getenv(“HOME”), “.R/Makevars”))):
Output of devtools::session_info("rstan”):
Session info --------------------------------------------------------------------------------------------------------------------------
setting value
version R version 3.4.1 (2017-06-30)
system x86_64, darwin15.6.0
ui RStudio (1.0.153)
language (EN)
collate en_GB.UTF-8
tz
date 2017-12-12

Packages ------------------------------------------------------------------------------------------------------------------------------
package * version date source
BH 1.65.0-1 2017-08-24 CRAN (R 3.4.1)
colorspace 1.3-2 2016-12-14 CRAN (R 3.4.0)
dichromat 2.0-0 2013-01-24 CRAN (R 3.4.0)
digest 0.6.12 2017-01-27 CRAN (R 3.4.0)
ggplot2 * 2.2.1 2016-12-30 CRAN (R 3.4.0)
graphics * 3.4.1 2017-07-07 local
grDevices * 3.4.1 2017-07-07 local
grid 3.4.1 2017-07-07 local
gridExtra 2.3 2017-09-09 CRAN (R 3.4.1)
gtable 0.2.0 2016-02-26 CRAN (R 3.4.0)
inline 0.3.14 2015-04-13 CRAN (R 3.4.0)
labeling 0.3 2014-08-23 CRAN (R 3.4.0)
lattice 0.20-35 2017-03-25 CRAN (R 3.4.1)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.4.0)
magrittr 1.5 2014-11-22 CRAN (R 3.4.0)
MASS 7.3-47 2017-02-26 CRAN (R 3.4.1)
Matrix 1.2-11 2017-08-16 CRAN (R 3.4.1)
methods * 3.4.1 2017-07-07 local
munsell 0.4.3 2016-02-13 CRAN (R 3.4.0)
plyr 1.8.4 2016-06-08 CRAN (R 3.4.0)
R6 2.2.2 2017-06-17 CRAN (R 3.4.0)
RColorBrewer 1.1-2 2014-12-07 CRAN (R 3.4.0)
Rcpp 0.12.13 2017-09-28 CRAN (R 3.4.2)
RcppEigen 0.3.3.3.0 2017-05-01 CRAN (R 3.4.0)
reshape2 1.4.2 2016-10-22 CRAN (R 3.4.0)
rlang 0.1.2 2017-08-09 CRAN (R 3.4.1)
rstan * 2.16.2 2017-07-03 CRAN (R 3.4.1)
scales 0.5.0 2017-08-24 CRAN (R 3.4.1)
StanHeaders * 2.16.0-1 2017-07-03 CRAN (R 3.4.1)
stats * 3.4.1 2017-07-07 local
stats4 3.4.1 2017-07-07 local
stringi 1.1.5 2017-04-07 CRAN (R 3.4.0)
stringr 1.2.0 2017-02-18 CRAN (R 3.4.0)
tibble 1.3.4 2017-08-22 CRAN (R 3.4.1)
tools 3.4.1 2017-07-07 local
utils * 3.4.1 2017-07-07 local
viridisLite 0.2.0 2017-03-24 CRAN (R 3.4.0)


Exception: mismatch in number dimensions declared
#2

It’s just getting the dimensions of x wrong. There must be different data handling code for the different methods of getting data in. In either case it should really just turn all the things into an environment and process from there… so this looks like a bug. I started an issue:

https://github.com/stan-dev/rstan/issues/471

IDK when I’ll have time to look into it yet…

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=x; dims declared=(3,5,2); dims found=(5,3)  (in 'model2ce578688e05_f7b3c902ee58883a4e62c57087eb21f6' at line 6)


#3

Thanks!


#4

This is a bug. For the time being, put the names of lists at the end of the character vectors or pass the data as a list. For example, in this case, use data = c(“N”, “D”, “K”, “y”, “x”))

It’ll be fixed in next release.