Rstan manual multivariate hierarchical regression example: row_vector problem in multi_normal

905 views
Skip to first unread message

Daniel Grigat

unread,
Jun 9, 2014, 6:59:28 AM6/9/14
to stan-...@googlegroups.com

Hi all

I am trying to recreate the multivariate regression example from page 92 (section 9.9) in the most recent Rstan manual, as it fits the type of analysis that I want to run on a psychological experiment.

My goal is to adjust the subject level data (‘groups’) that I have with the population/group-level predictors. Each subject undertakes 22 trials in a decision-making task, with binary responses. Hence I am using a linear regression model at the group level and a logistic function at the subject level.

My problem is that I am running into a well-documented problem: I am creating a row-vector in line 27 when multiplying the group predictors (u[s]) with gamma. the multi-normal function only accepts vectors as inputs:

Error in stanc("GBE_hierarchical_logistic_linear.stan") : 

  failed to parse Stan model 'GBE_hierarchical_logistic_linear' with error message:

EXPECTATION FAILURE LOCATION: file=input; line=27, column=56

    beta[s] ~ multi_normal((u[s] * gamma), Sigma_beta);

                                                       ^-- here

DIAGNOSTIC(S) FROM PARSER:

no matches for function name="multi_normal_log"

    arg 0 type=vector

    arg 1 type=row vector

    arg 2 type=matrix

available function signatures for multi_normal_log:

0.  multi_normal_log(vector, vector, matrix) : real

unknown distribution=multi_normal

Parser expecting: <eps>


I simulated data to test this model. The simulated data is structured as follows: 50 subjects, three parameters to be estimated per subject, two group predictors, each subject's true parameters are drawn from a normal distribution, with non-zero mean and std. of 3.  

My questions:

1. Since I have exactly recreated the example rstan code given on page 95; I wonder whether I made a mistake or whether the example as described in the manual cannot currently be run in rstan given the row_vector restriction in the multi_normal function.

2. As my data has to be structured the way it is set up, I wonder whether there is a work around for me? I saw that the issue (#589) is fixed on the development platform.

Please find attached my stan code; a matlab file that simulates data; the matlab data output (mat file); the code that runs the rstan model. Note that R.matlab has to be installed to read the mat file into R.

For some reason the file upload isn't working, here's a dropbox link: https://www.dropbox.com/sh/hx7gqkcy3b3sjah/AACIyOcjzXmNY76Xx89d0mf4a

Thank you very much for your help in advance!

Daniel

Bob Carpenter

unread,
Jun 9, 2014, 9:31:58 AM6/9/14
to stan-...@googlegroups.com
Thanks for looking for answers and sorry the list is so hard
to search!

For now, the simplest thing to do is transpose (postfix ' operator),
so you'd use:

beta[s]' ~ multi_normal((u[s] * gamma), Sigma_beta);

A more efficient alternative would to be declare beta as
an array of vectors (I'm not actually sure what M and N
should be in your case --- this assumes beta was declared as
an M x N matrix).

vector[N] beta[M];

There's a discussion of vectors/matrices vs. arrays and efficiency
in a chapter of the manual.

The generalization and vectorization of multi_normal hasn't
been merged into develop yet, but it's going to be soon and
will be in the next release, 2.3.0, which we hope to get out later
this week or early next week.

Here's the issue and pull request to track:

https://github.com/stan-dev/stan/issues/589

https://github.com/stan-dev/stan/pull/608

When Marco's fix is merged, your original code will work as written.

- Bob


> beta[s]' ~ multi_normal((u[s] * gamma), Sigma_beta);


> --
> 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/d/optout.

Daniel Grigat

unread,
Jun 9, 2014, 12:14:49 PM6/9/14
to stan-...@googlegroups.com
Hi Bob

Thanks for your advice. Great that the new realease is out so soon. I will wait for it to come out. As all other adjustments are not as ideal for me.

Thanks
Daniel

Daniel Grigat

unread,
Jun 20, 2014, 3:35:33 AM6/20/14
to stan-...@googlegroups.com
Hi guys,

I am just wondering when the new version 2.3.0. will be out?

Thanks
Daniel

Bob Carpenter

unread,
Jun 20, 2014, 4:53:40 AM6/20/14
to stan-...@googlegroups.com
Either today or early next week.

- Bob

Daniel Grigat

unread,
Jun 20, 2014, 11:42:19 AM6/20/14
to stan-...@googlegroups.com
Perfect, thanks for the update Bob!

D

Daniel Grigat

unread,
Jun 25, 2014, 4:36:43 PM6/25/14
to stan-...@googlegroups.com
Hi Bob

Sorry that I have to bother you with this again. I just updated to the new rstan version, however the error persists (see in red at the end of this post).

I tried to transpose, as you suggested but that doesnt work either. I am not sure whether transforming beta (either by transposing or changing beta to an array of vectors) would change anything, as rstan seems to complain about the (u[s] * gamma) operation.

Has the multi_normal issue not been fixed? What should I do?

Thanks

Daniel


Error in stanc("GBE_hierarchical_logistic_linear.stan") : 
  failed to parse Stan model 'GBE_hierarchical_logistic_linear' with error message:
EXPECTATION FAILURE - DIAGNOSTIC(S) FROM PARSER:

no matches for function name="multi_normal_log"
    arg 0 type=vector
    arg 1 type=row vector
    arg 2 type=matrix
available function signatures for multi_normal_log:
0.  multi_normal_log(vector, vector, matrix) : real
unknown distribution=multi_normal


LOCATION OF PARSING ERROR (line = 27, position = 59):

PARSED:

model{
    matrix[nEV,nEV] Sigma_beta;
    Sigma_beta <- diag_matrix(tau) * Omega * diag_matrix(tau);

    tau ~ cauchy(0,2.5);
    Omega ~ lkj_corr(2);
    for (g in 1:nGP)
      gamma[g] ~ normal(0,5);
  {  
    for (s in 1:nS) // calculate population level, nS x nEV
    beta[s]’ ~ multi_normal((u[s] * gamma), Sigma_beta);

EXPECTED: <eps> BUT FOUND: 


  }


On Friday, June 20, 2014 9:53:40 AM UTC+1, Bob Carpenter wrote:

Daniel Lee

unread,
Jun 25, 2014, 5:02:04 PM6/25/14
to stan-...@googlegroups.com
Hi Daniel,


On Wed, Jun 25, 2014 at 4:36 PM, Daniel Grigat <daniel...@icloud.com> wrote:
Hi Bob

Sorry that I have to bother you with this again. I just updated to the new rstan version, however the error persists (see in red at the end of this post).

The issue is with how you're calling multi_normal_log() and updating to a new rstan version isn't going to help.
 

I tried to transpose, as you suggested but that doesnt work either. I am not sure whether transforming beta (either by transposing or changing beta to an array of vectors) would change anything, as rstan seems to complain about the (u[s] * gamma) operation.

You need to transpose the (u[s] * gamma) operation. The error message is telling you that the the second argument, (u[s] * gamma), is a row_vector. The available function signatures expects that to be a vector. In Stan, vector and row_vector are not equivalent.

 
Has the multi_normal issue not been fixed? What should I do?

Is there an issue we're not aware of? The fix is to use the types correctly. That is, make sure the second argument is actually a vector, not a row vector. Can you try transposing the expression?


Andrew Gelman

unread,
Jun 25, 2014, 5:02:49 PM6/25/14
to stan-...@googlegroups.com
Hi, I just went to the Stan page and it still says it’s version 2.2.0.
A

Bob Carpenter

unread,
Jun 26, 2014, 7:37:46 AM6/26/14
to stan-...@googlegroups.com
Pull request #608, which fixes issue #589 is still held up
in testing. I'd thought it had already passed and been merged
into 2.3. It will allow vectors and row vectors to be used for
the y in y ~ multi_normal(...); and will also allow vectorization in
y (and I think mu).

- Bob


On Jun 25, 2014, at 11:01 PM, Daniel Lee <bea...@alum.mit.edu> wrote:

Daniel Grigat

unread,
Jun 26, 2014, 5:01:49 PM6/26/14
to stan-...@googlegroups.com
Hi Bob

Thanks for your reply. For some reason transposing (u[s] * gamma) also doesnt work. rstan still complains about it being a row vector.

Do you have any idea as to when request #608 will be fixed?

Thanks

Daniel

Daniel Lee

unread,
Jun 26, 2014, 5:25:01 PM6/26/14
to stan-...@googlegroups.com
Hi Daniel,

Hopefully soon, but it won't be on a release version for a while.

With regards to transposing (u[s] * gamma), how did you do that?

Bob Carpenter

unread,
Jun 26, 2014, 5:37:46 PM6/26/14
to stan-...@googlegroups.com
If you add a ' after (u[s] * gamma), it should work.

- Bob

Daniel Grigat

unread,
Jun 27, 2014, 6:20:46 AM6/27/14
to stan-...@googlegroups.com
Hey Daniel and Bob

thanks for your replies! turns out that with my mac keyboard i typed in the wrong transpose sign, which looked just like the ' . Its working well now.

thanks for your help! Really appreciate it.

Daniel
Reply all
Reply to author
Forward
0 new messages