Compiling error in example on page 95 of Stan Reference Manual v. 2.2.0

282 views
Skip to first unread message

Edwin Leb

unread,
Mar 31, 2014, 11:27:41 AM3/31/14
to stan-...@googlegroups.com
Dear Community,
I am trying to run the example model for a multivariate (hierarchical) regression on page 95 of the Reference Manual for Stan version 2.2.0 but I am running into a compiling error.

This is the error message returned when calling stan():

TRANSLATING MODEL 'fullmodel' FROM Stan CODE TO C++ CODE NOW.
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'fullmodel' with error message:
EXPECTATION FAILURE LOCATION: file=input; line=27, column=57

beta[j] ~ multi_normal((u[j] * 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 have actually slightly modified the code to fit a logistic rather than a linear model but I doubt this is the problem. Here is the modified code (note commented out lines):
fullmodel<- '
data {
int<lower=0> N; // num individuals
int<lower=1> K; // num ind predictors
int<lower=1> J; // num groups
int<lower=1> L; // num group predictors
int<lower=1,upper=J> jj[N]; // group for individual
matrix[N,K] x; // individual predictors
matrix[J,L] u; // group predictors
vector[N] y; // outcomes
}
parameters {
corr_matrix[K] Omega; // prior covariance
vector<lower=0>[K] tau; // prior scale
matrix[L,K] gamma; // group coeffs
vector[K] beta[J]; // indiv coeffs by group
#real<lower=0> sigma; // prediction error scale of the original linear model (commented out)
}
model {
matrix[K,K] Sigma_beta;
Sigma_beta <- diag_matrix(tau) * Omega * diag_matrix(tau);
tau ~ cauchy(0,2.5);
Omega ~ lkj_corr(2);
for (l in 1:L)
gamma[l] ~ normal(0,5);
for (j in 1:J)
beta[j] ~ multi_normal((u[j] * gamma)’, Sigma_beta);
for (n in 1:N)
#y[n] ~ normal(x[n] * beta[jj[n]], sigma); // original linear predictor commented out
y[n] ~ bernoulli(inv_logit(x[n] * beta[jj[n]]));
}
'
I am using R version 3.0.2 (2013-09-25) in Platform: x86_64-w64-mingw32/x64 (64-bit) and rstan_2.2.0.
Info on the compiler is:
> system("g++ --version")
G__~1.EXE (GCC) 4.6.3 20111208 (prerelease)

Is this really an error in the manual? Or what can I be doing wrong?
Thanks for any guidance.
Edwin
P.S. Explanations for beginners are highly appreciated.

Soga Shota

unread,
Mar 31, 2014, 11:45:24 PM3/31/14
to stan-...@googlegroups.com
your error is in data block.

when you sample a random number from bernoulli distribution, the left hand side should be int. However, your code is

vector[N] y; // outcomes

which implicitly assume real values.

Therefore, you should modify your code like

data {
int<lower=0> N; // num individuals
int<lower=1> K; // num ind predictors
int<lower=1> J; // num groups
int<lower=1> L; // num group predictors
int<lower=1,upper=J> jj[N]; // group for individual
matrix[N,K] x; // individual predictors
matrix[J,L] u; // group predictors
# you have a typo here.
#vector[N] y; // outcomes
int y[N];
}

The error massage is kinda misreading. I spent a little bit of time to debug your code.

Bob Carpenter

unread,
Apr 1, 2014, 9:54:47 AM4/1/14
to stan-...@googlegroups.com
> The error massage is kinda misreading. I spent a little bit of time to debug your code.b

Which error message? We might be able to make it clearer.

The one originally reported seemed clear enough in terms
of arguments to multi_normal. But it sounds like you were
responding to a different issue.

- Bob

Edwin Leb

unread,
Apr 1, 2014, 12:30:22 PM4/1/14
to stan-...@googlegroups.com
Hi,
You are both right. I´ll recap to clarify.
There were two errors in my code. I reported the first one in my original mail was it was related indeed to the arguments to multi-normal. It seems I was using an unrecognized prime character (by R or stan? I don't know) to transpose (u[j] * gamma). I solved it by chance after unsuccessfully trying the change suggested by Soga ("the idea fell form sky"... hope the expression translates correctly to English).
Soga detected the second error in my code which would have popped as an error if I would have not changed the code as Soga suggested. 
In short the error messages thrown by stan seem correct. 
At the end I copy the error message I get when I run the model with the first problem corrected but without fixing the definition of y.

Many thanks for your input. Now I can go on exploring stan.
Edwin

Error for incorrectly defined data:
TRANSLATING MODEL 'Full_poly' FROM Stan CODE TO C++ CODE NOW.
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'Full_poly' with error message:
EXPECTATION FAILURE LOCATION: file=input; line=30, column=53

y[n] ~ bernoulli(inv_logit(x[n] * beta[jj[n]]));
                                                    ^-- here


DIAGNOSTIC(S) FROM PARSER:
no matches for function name="bernoulli_log"
    arg 0 type=real
    arg 1 type=real
available function signatures for bernoulli_log:
0.  bernoulli_log(int, real) : real
1.  bernoulli_log(int, real[1]) : real
2.  bernoulli_log(int, vector) : real
3.  bernoulli_log(int, row vector) : real
4.  bernoulli_log(int[1], real) : real
5.  bernoulli_log(int[1], real[1]) : real
6.  bernoulli_log(int[1], vector) : real
7.  bernoulli_log(int[1], row vector) : real
unknown distribution=bernoulli
Parser expecting: <eps>

Soga Shota

unread,
Apr 2, 2014, 3:18:11 AM4/2/14
to
It is my bad. An error massage was appropriate.

I fixed both errors at the same time (I should have mentioned both of them in my post) while I only saw the error from a wrong prime (and I thought an error in data section was not displayed... It was displayed if only a prime error was fixed).

Sorry for my misunderstanding.
Reply all
Reply to author
Forward
0 new messages