Mismatch in dimensionality - Stan is fixing the dimensionality to a number (why?)

I’m using RStan and logistic is the following S4 stanmodel class:

> logistic
S4 class stanmodel 'logistic' coded as follows:
data {
  int<lower=0> i;
  int<lower=0> j;
  int<lower=0> y[i];
  int<lower=0> n[i];
  matrix[i, j] X;
  cov_matrix[j] Sigma;
  real mu_intercept; # Prior on intercept of regression parameters
}

transformed data {
  vector[j] mu;
  mu = rep_vector(0, j);
  mu[1] = mu_intercept;
}

parameters {
  vector[j] beta;
}

model {
  beta ~ multi_normal(mu, Sigma);
  y ~ binomial_logit(n, X * beta);
}

generated quantities {
  real<lower=0,upper=1> yhat[j];
  yhat[1] = inv_logit(beta[1]);
  for (k in 2:j) {
    yhat[k] = inv_logit(beta[1] + beta[k]);
  }
} 

data is the following list:

> data
data
$i
[1] 3

$j
[1] 3

$y
[1] 10 11 12

$n
[1] 21 22 23

$X
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    1    1    0
[3,]    1    0    1

$mu_intercept
[1] 0

$Sigma
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0

Notice in data that y is of length 3 as specified by i.

I run the Stan model as follows and get the error:

> stan.fit <- rstan::sampling(logistic, data=data)
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  mismatch in dimension declared and found in context; processing stage=data initialization; variable name=y; position=0; dims declared=(7); dims found=(3)
failed to create the sampler; sampling not done

Why does Stan think that y should be length 7?

Welcome to Stan forums and sorry you’re experiencing this error.

This is very strange and I’m not entirely sure what’s going on. I’m not super familiar with our internal code for checking the data but my hunch is that you’ve found a bug in the RStan interface that we need to fix. Stan wouldn’t think the value is 7 unless RStan is passing it a 7.

Until this is fixed, I suspect you can get around this issue if you name it anything other than i (that is, change the name from i to something else in both your Stan code and R code). Does that work?

(@bgoodri I’m not sure but I think that i must be being used somewhere in some old RStan code to process the data and its value is being overwritten due to some weird R behavior or a coding error.)

Changing ‘i’ to ‘P’ to worked. Thanks!

Glad that works and thanks for confirming. That means this is almost certainly a bug in RStan’s R code or in an R function that it calls (probably the former). I’ll open an issue on GitHub and we’ll sort it out.

1 Like

Here’s the issue:

and a PR fixing it:

1 Like