Not accepting assignment statements

346 views
Skip to first unread message

Jonathan Harris

unread,
Jun 25, 2016, 7:56:30 AM6/25/16
to Stan users mailing list
In a relatively simple autoregressive model I declare in the data block:
int N;
vector[N] x;
vector[N] y;
...
and in the parameters block:

real alpha;
real beta;
...
and in the model block:

vector[N] yhat;
alpha ~ cauchy(abar,asig);
beta ~ cauchy(betabar,betasig);

yhat=beta*x+alpha;

 and get error:

Illegal statement beginning with non-void expression parsed as
  yhat
Not a legal assignment, sampling, or function statement.


I also get a similar error if I write this in loop format. Am I not allowed to create a vector in the model block? Is there something else going on?

Daniel Lee

unread,
Jun 25, 2016, 8:04:38 AM6/25/16
to stan-...@googlegroups.com
Currently, all variable declarations must come at the start of any block. If there are any statements between the declaration of yhat and the start of the block, "{", you'll see a message like the one you're getting. 



Daniel

P.S. It really helps to post the whole Stan program. I'm just guessing as to what's wrong. 
P.P.S. It also helps to have the whole error message. 
--
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.

Jonathan Harris

unread,
Jun 25, 2016, 8:20:36 AM6/25/16
to Stan users mailing list
I even tried the MA(Q) example on page 93 of the manual and get a similar error. I am using R 3.2.3 and rstan:
rstan (Version 2.9.0-3, packaged: 2016-02-11 15:54:41 UTC, GitRev: 05c3d0058b6a)

Jonathan Harris

unread,
Jun 25, 2016, 9:00:14 AM6/25/16
to Stan users mailing list
Thanks Daniel: But this sample program from the reference manual also fails:

Full program stanalt.stan:
data {
int<lower=0> Q; // num previous noise terms
int<lower=3> T; // num observations
vector[T] y; // observation at time t
}
parameters {
real mu; // mean
real<lower=0> sigma; // error scale
vector[Q] theta; // error coeff, lag -t
}
transformed parameters {
vector[T] epsilon; // error term at time t
for (t in 1:T) {
epsilon[t] = y[t] - mu;
for (q in 1:min(t - 1, Q))
epsilon[t] = epsilon[t] - theta[q] * epsilon[t - q];
}
}
model {
vector[T] eta;
mu ~ cauchy(0, 2.5);
theta ~ cauchy(0, 2.5);
sigma ~ cauchy(0, 2.5);
for (t in 1:T) {
eta[t] = mu;
for (q in 1:min(t - 1, Q))
eta[t] = eta[t] + theta[q] * epsilon[t - q];
}
y ~ normal(eta, sigma);
}

Error message and session info:

> fit1=stan(file="C:/Users/jgharris/RProjects/stanalt.stan",data=standataalt,chains=2,iter=2000)             
SYNTAX ERROR, MESSAGE(S) FROM PARSER:

Illegal statement beginning with non-void expression parsed as
  epsilon[t]
Not a legal assignment, sampling, or function statement.  Note that
  * Assignment statements only allow variables (with optional indexes) on the left;
    if you see an outer function logical_lt (<) with negated (-) second argument,
    it indicates an assignment statement A <- B with illegal left
    side A parsed as expression (A < (-B)).
  * Sampling statements allow arbitrary value-denoting expressions on the left.
  * Functions used as statements must be declared to have void returns


ERROR at line 14

 12:    vector[T] epsilon; // error term at time t
 13:    for (t in 1:T) {
 14:    epsilon[t] = y[t] - mu;
        ^
 15:    for (q in 1:min(t - 1, Q))

PARSER EXPECTED: "}"
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'stanalt' due to the above error.
In addition: Warning messages:
1: package ‘rstan’ was built under R version 3.2.5 
2: package ‘ggplot2’ was built under R version 3.2.5 
> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] reshape2_1.4.1 dplyr_0.4.3    rstan_2.9.0-3  ggplot2_2.1.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      assertthat_0.1   grid_3.2.3       plyr_1.8.4       R6_2.1.2         gtable_0.2.0    
 [7] DBI_0.4-1        magrittr_1.5     stats4_3.2.3     scales_0.4.0     stringi_1.1.1    tools_3.2.3     
[13] stringr_1.0.0    munsell_0.4.3    parallel_3.2.3   inline_0.3.14    colorspace_1.2-6 gridExtra_2.2.1 
> 

Jonathan Harris

unread,
Jun 25, 2016, 9:02:55 AM6/25/16
to Stan users mailing list
My variable declarations are at the start of the block (my original program was encapsulated in a string, the sample from the manual, pasted into a file).

Also the full original program is:

stan1<- "
data {
int N;
vector[N] x;
vector[N] y;
real abar;
real asig;
real betabar;
real betasig;
real sigmabar;
real sigmasig;
real rhobar;
real rhosig;
}

parameters {
real alpha;
real beta;
real<lower=0> sigma;
real rho;
}
model
{

vector[N] resid;
vector[N] eps;
vector[N] yhat;
alpha ~ cauchy(abar,asig);
beta ~ cauchy(betabar,betasig);
sigma ~ lognormal(sigmabar,sigmasig);
rho ~ cauchy(rhobar,rhosig);
yhat=beta*x+alpha;
for(n in 1:N){
    yhat[n]=beta*x[n]+alpha;
    resid[n]=y[n]-yhat[n];
}
eps[1]=resid[1];
for(n in 2:N) {
    eps[n]=resid[n]-rho*resid[n-1];
}
eps ~ normal (0,sigma);
}
"            
 


On Saturday, June 25, 2016 at 8:04:38 AM UTC-4, Daniel Lee wrote:

Jonathan Harris

unread,
Jun 25, 2016, 9:07:32 AM6/25/16
to Stan users mailing list
And this arrangement of the model block also fails:
model
{
vector[N] yhat;
yhat=beta*x+alpha;
vector[N] resid;
for(n in 1:N){
    resid[n]=y[n]-yhat[n];
}
vector[N] eps;
eps[1]=resid[1];
for(n in 2:N) {
    eps[n]=resid[n]-rho*resid[n-1];
}
alpha ~ cauchy(abar,asig);
beta ~ cauchy(betabar,betasig);
sigma ~ lognormal(sigmabar,sigmasig);
rho ~ cauchy(rhobar,rhosig);
eps ~ normal (0,sigma);

Eric Innocents EBOULET

unread,
Jun 25, 2016, 9:42:38 AM6/25/16
to Stan users mailing list
Hi,

You can try replacing in your model "=" with "<-"
I hope that it will run.

Bests

Eric

Jonathan Harris

unread,
Jun 25, 2016, 10:07:53 AM6/25/16
to Stan users mailing list
THat worked. I am puzzled by why the examples in the documentation show "="

Daniel Lee

unread,
Jun 25, 2016, 10:07:54 AM6/25/16
to stan-...@googlegroups.com
Ah. Yes. The "=" syntax is available starting with v2.10.0. You're running rstan 2.9.0. rstan 2.10 isn't available yet. 


Daniel

Bob Carpenter

unread,
Jun 25, 2016, 1:06:09 PM6/25/16
to stan-...@googlegroups.com
We should probably not add the new manual until
all of the interfaces are working with it. We'll
be backwards compatible with the old one.

- Bob

Daniel Lee

unread,
Jun 25, 2016, 3:00:59 PM6/25/16
to stan-...@googlegroups.com
Does the rstan distribution include the reference guide?

Ben Goodrich

unread,
Jun 25, 2016, 5:51:50 PM6/25/16
to Stan users mailing list
No

Daniel Lee

unread,
Jun 27, 2016, 9:49:53 AM6/27/16
to stan-users mailing list
Jonathan, sorry about the confusion.... I've updated the documentation page so there's a link to v2.9.0 of the manual.

Ben / Bob: if you think we should remove the v2.10.0 version link, please do so or let me know and I'll take care of it.



Daniel


On Sat, Jun 25, 2016 at 5:51 PM, Ben Goodrich <goodri...@gmail.com> wrote:
No

Ben Goodrich

unread,
Jun 27, 2016, 2:41:31 PM6/27/16
to Stan users mailing list
On Monday, June 27, 2016 at 9:49:53 AM UTC-4, Daniel Lee wrote:
Jonathan, sorry about the confusion.... I've updated the documentation page so there's a link to v2.9.0 of the manual.

Ben / Bob: if you think we should remove the v2.10.0 version link, please do so or let me know and I'll take care of it.

We can leave it; rstan 2.10.1 is fine now.

Ben

Reply all
Reply to author
Forward
0 new messages