Mismatch in number dimensions

I am declaring the following in the data-block

data{

int<lower=1> M; // number of data points
int<lower=1> t[M]; // time

}

I have checked and ensure t contains only integers in R.

M is 4074 in my current example.

I got the following error message:
Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=t; dims declared=(4074); dims found=(4074,1)

How do I get Stan to recognize a column of integers as an array of integers and not a matrix, despite my declaration? Thanks.

You probably need to be passing it like as.vector(t) rather than a one-column matrix.

Hi

But t is just a variable consisting of integers as I double-check it in R. It is really unclear to me why Stan is finding it to be an one-column matrix.

Also, where may I use the as.vector(t)? It is not allowed in the data block. I should have made it clear that this is how I plan to use it in my model block:

model{  
...
  y ~ neg_binomial_2_log(theta[t],phi);
}

I mean pass it to the list of data as as.vector(t) from R.

Thanks. But I tried that and it did not work. Stan is still showing the mismatch error message. Any idea?

What is dim(t) in R?

I store t along with other variables in a list.

data_list <- list(M=nrow(data),
                  N=length(unique(data[,"ID"])),
                  id=data[,"ID"],
                  t=data[,"time"],
                  y=data[,"DV"])

dim(data_list$t)
[1] 4074 1

Stan is telling you that should be 4074 rather than 4074 \times 1. Make it

data_list <- list(M=nrow(data),
                  N=length(unique(data[,"ID"])),
                  id=data[,"ID"],
                  t=c(data[,"time"]),
                  y=data[,"DV"])

It still does not work! Now the error message for c(data[,"time"]) is

Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=t; dims declared=(4074); dims found=(1,4074)

How did you use as.vector? Did you do this?:

data_list <- list(M=nrow(data),
                  N=length(unique(data[,"ID"])),
                  id=data[,"ID"],
                  t=as.vector(data[,"time"]),
                  y=data[,"DV"])

Yes, I did. The error message for as.vector(data[,"time"]) is:

Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=t; dims declared=(4074); dims found=(4074,1)

Is data a data.frame? Maybe data[ , "time", drop = TRUE]? You just have to convince R to treat it as a vector rather than a matrix.

Yes. data is data.frame. Even when I do t=c(data[,"time"]), Stan is finding:

Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=t; dims declared=(4074); dims found=(1,4074)

Maybe I should read t as a vector in the data block and then do something in a transformed data block??? But I don’t know how to change a vector into an integer array within Stan…

No. You should figure out how to get R to treat it as a vector rather than a matrix with one column. Perhaps data[,"time"][,1]?

Thanks. That’s what I am trying to do but I can’t think of a way…

Does data[,"time"][,1] yield a matrix or a vector?

data[,"time"][,1] yields this:

dim(data_list$t)
[1] 4074 1

How about

data_list$t <- data_list$t[,1]

?

data_list$t ← data_list$t[,1]
dim(data_list$t)
NULL

But this is the corresponding error message:

Exception: mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=d; dims declared=(4074); dims found=(4074,1)

Is my declaration wrong?

data{

int<lower=1> M; // number of data points
int<lower=1> t[M]; // time

}

Now you need to fix the variable called d.