Initializing a vector of length one

495 views
Skip to first unread message

David Knowles

unread,
Nov 12, 2015, 2:01:43 PM11/12/15
to stan-...@googlegroups.com
Hi all,

I have a vector which I would like to initialize. In some settings
this vector has length 1 (the context is a GLM where I might only have
an intercept), which breaks the initialization (I think because R
doesn't distinguish between a scalar and a length one vector).
Consider the super simple model:

data {
int<lower=0> P;
}
parameters {
vector[P] beta;
}
model {
beta ~ normal(0,1);
}

Then both sampling and optimizing are fine if P=2:

sm=stan_model("length_one_vector.stan")
P=2
optimizing(sm, dat=list(P=P), init=list(beta=rep(1,P))) # this is fine
sampling(sm, dat=list(P=P), init=list( list(beta=rep(1,P) ) ),
chains=1) # this is fine

But if P=1 then it fails.

P=1
optimizing(sm, dat=list(P=P), init=list(beta=rep(1,P)))
sampling(sm, dat=list(P=P), init=list( list(beta=rep(1,P) ) ), chains=1)
# Error: Initialization from source failed.
# mismatch in number dimensions declared and found in context;
processing stage=initialization; variable name=beta; dims
declared=(1); dims found=()

Trying to initialization beta as a 1x1 matrix doesn't help:

optimizing(sm, dat=list(P=P), init=list(beta=matrix(1,dat$P,1)))
# Error: Initialization from source failed. mismatch in number
dimensions declared and found in context; processing
stage=initialization; variable name=beta; dims declared=(1); dims
found=(1,1)

Obviously I can work around this by having a special case model for
the P=1 case, but that seems in poor taste. Any thoughts much
appreciated.

Thanks a lot

David.

--
David A. Knowles,
Stanford University.
E-mail: know...@gmail.com
Web: http://cs.stanford.edu/~davidknowles/

Ben Goodrich

unread,
Nov 12, 2015, 2:13:38 PM11/12/15
to Stan users mailing list
On Thursday, November 12, 2015 at 2:01:43 PM UTC-5, David Knowles wrote:
I have a vector which I would like to initialize. In some settings
this vector has length 1 (the context is a GLM where I might only have
an intercept), which breaks the initialization (I think because R
doesn't distinguish between a scalar and a length one vector).

You need to coerce it to an array in R before passing it, as in as.array(beta).

Ben

andre.p...@googlemail.com

unread,
Nov 14, 2015, 12:19:57 PM11/14/15
to Stan users mailing list
David,

I had this too in MatlabStan and a quick fix was to put NA at the end to 
let the vector be always greater than 1. 

Andre 

Bob Carpenter

unread,
Nov 14, 2015, 12:53:55 PM11/14/15
to stan-...@googlegroups.com
That won't work in RStan and NA isn't available in the
data format for CmdStan.

- Bob
> --
> 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.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages