rstan: mismatch in number dimensions

3,227 views
Skip to first unread message

Ross Boylan

unread,
Oct 18, 2013, 2:06:15 PM10/18/13
to stan-users
I have a vector of size 1 I am passing to stan from R, but get this
error:
> sdclass <- rep(1, ncol(mm))
> NSD <- 1
> stanDat <- list(N=nrow(mm), J=dim(mm)[2]-2, NSD=NSD,
+ SDClass=sdclass[c(-2, -3)],
+ NCLUSTER=NCLUSTER, iCluster=mm[,3],
+ mnDetail=mm[,2], x=mm[,c(-2, -3)])
> stanDat$SDClass
[1] 1
> system.time(rb <- stan(model_code = stan_model, data=stanDat, seed=seed, chain_id=2,
+ iter=200, chains=1, test_grad=TRUE))

TRANSLATING MODEL 'stan_model' FROM Stan CODE TO C++ CODE NOW.
COMPILING THE C++ CODE FOR MODEL 'stan_model' NOW.
Error : mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=SDClass; dims declared=(1); dims found=()
failed to create the sampler; sampling not done

Ordinarily sdclass is a proper vector, but I'm trying a simplified model with only an intercept (for my poisson problem).

The relevant code is


data {
int<lower=0> N; // number of respondents
int<lower=0> J; //number of covariates
int<lower=0> NSD; // number of classes of beta for sd
int<lower=1,upper=NSD> SDClass[J]; // beta[j, k] has sigma_beta[SDClass[j]]
int<lower=0> NCLUSTER; //number of respondents
int iCluster[N]; // cluster id
matrix[N, J] x; // covariates
int<lower=1,upper=10> mnDetail[N]; // outcome
}

Am I doing something wrong, or is this a bug? Development code from Oct 9.


Ross Boylan

unread,
Oct 18, 2013, 2:11:46 PM10/18/13
to stan-users
J=1. Maybe stan dislikes arrays of size 1? A simplified version of the
data declaration is
int SDClass[1];

Ross


Daniel Lee

unread,
Oct 18, 2013, 2:45:03 PM10/18/13
to stan-...@googlegroups.com
Hi Ross,

This isn't really a bug, but just a boundary condition where R's default output for a single value matches up to Stan's real value, not a vector[1] or matrix[1,1] or a real 1-array.

There's a thread called "Specifying parameter/vector dimensions" that covers most of the gory details. (https://groups.google.com/forum/?hl=en#!searchin/stan-users/Specifying$20parameter$2Fvector$20dimensions/stan-users/d-R7J6MldkQ/XMjtpX0bmTsJ)

In short, the variable needs to be defined as an array:
SDClass=array(sdclass[c(-2,-3)], dim=1)



Daniel





--
You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ross Boylan

unread,
Oct 18, 2013, 3:12:23 PM10/18/13
to stan-...@googlegroups.com, ro...@biostat.ucsf.edu
On Fri, Oct 18, 2013 at 02:45:03PM -0400, Daniel Lee wrote:
> Hi Ross,
>
> This isn't really a bug, but just a boundary condition where R's default
> output for a single value matches up to Stan's real value, not a vector[1]
> or matrix[1,1] or a real 1-array.
>
> There's a thread called "Specifying parameter/vector dimensions" that
> covers most of the gory details. (
> https://groups.google.com/forum/?hl=en#!searchin/stan-users/Specifying$20parameter$2Fvector$20dimensions/stan-users/d-R7J6MldkQ/XMjtpX0bmTsJ
> )
>
> In short, the variable needs to be defined as an array:
>
> > SDClass=array(sdclass[c(-2,-3)], dim=1)
> >

Thanks. I actually went the other way, and changed the stan code to
use scalars. Using array would have been simpler.

Before doing so, I ran into variant of the problem, this time with 2
vs 1 dimensions instead of 1 vs 0:

Error : mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=x; dims declared=(737,1); dims found=(737)
failed to create the sampler; sampling not done

So I changed
matrix[N, J] x; // covariates
to
vector[N] x; // covariates

Does this really need to be R's responsibility? It seems to me that if stan gets an argument of the appropriate
size it should be able adjust if necessary.

Yes, I know, type safety is good, at least in C++ land. But in this case it seems undesirable.

Ross

Bob Carpenter

unread,
Oct 18, 2013, 3:28:09 PM10/18/13
to stan-...@googlegroups.com


On 10/18/13 3:12 PM, Ross Boylan wrote:
> On Fri, Oct 18, 2013 at 02:45:03PM -0400, Daniel Lee wrote:
>> Hi Ross,
>>
>> This isn't really a bug, but just a boundary condition where R's default
>> output for a single value matches up to Stan's real value, not a vector[1]
>> or matrix[1,1] or a real 1-array.
>>
>> There's a thread called "Specifying parameter/vector dimensions" that
>> covers most of the gory details. (
>> https://groups.google.com/forum/?hl=en#!searchin/stan-users/Specifying$20parameter$2Fvector$20dimensions/stan-users/d-R7J6MldkQ/XMjtpX0bmTsJ
>> )
>>
>> In short, the variable needs to be defined as an array:
>>
>>> SDClass=array(sdclass[c(-2,-3)], dim=1)
>>>
>
> Thanks. I actually went the other way, and changed the stan code to
> use scalars. Using array would have been simpler.
>
> Before doing so, I ran into variant of the problem, this time with 2
> vs 1 dimensions instead of 1 vs 0:
>
> Error : mismatch in number dimensions declared and found in context; processing stage=data initialization; variable name=x; dims declared=(737,1); dims found=(737)
> failed to create the sampler; sampling not done
>
> So I changed
> matrix[N, J] x; // covariates
> to
> vector[N] x; // covariates
>
> Does this really need to be R's responsibility? It seems to me that if stan gets an argument of the appropriate
> size it should be able adjust if necessary.

Yes, because R's doing all the work building the C++ data
structure Stan needs from the R data structure passed to it.

We do not want to go down the R route of overloading all of
our basic types. We want to catch user errors rather than
guess what they meant to do the way R does.

> Yes, I know, type safety is good, at least in C++ land. But in this case it seems undesirable.

Type safety's always good. It's R that's bad!

- Bob
Reply all
Reply to author
Forward
0 new messages