I’m new to Stan and met a problem of my first try. I’m using the ARMA from the book to simulate a few data of the future. but it showed me errors in the block. I think the generated quantities block is correct but it has problems.
the code is:
ARMA_code ="""
data {
int<lower=1> T;
real y[T];
int<lower=0> n_new;
}
parameters {
real mu;
real phi;
real theta;
real<lower=0> sigma;
}
model {
// real err;
vector[T] nu;
vector[T] err;
nu[1] <- mu + phi * mu;
err[1] <- y[1] - nu[1];
for (t in 2:T) {
nu[t] <- mu + phi * y[t-1] + theta * err[t-1];
err[t] <- y[t] - nu[t];
}
err ~ normal(0,sigma);
mu ~ normal(0,10);
phi ~ normal(0,2);
theta ~ normal(0,2);
sigma ~ cauchy(0,5);
}
generated quantities{
vector[n_new+T] y_new;
// y_new[1:T] <- mu;
vector[n_new] x_pred;
y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
for (i in 2:n_new){
y_new[T+i] = normal_rng(mu+phi*y_new[T+i-1]+theta*err, sigma);
}
}
"""
Here is the error in detail:
ValueError: Failed to parse Stan model 'anon_model_983af5eff8a1020ec870838c279bf070'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Info: assignment operator <- deprecated in the Stan language; use = instead.
Info: assignment operator <- deprecated in the Stan language; use = instead.
Info: assignment operator <- deprecated in the Stan language; use = instead.
Info: assignment operator <- deprecated in the Stan language; use = instead.
Variable "err" does not exist.
error in 'unknown file name' at line 35, column 37
-------------------------------------------------
33: // y_new[1:T] <- mu;
34: vector[n_new] x_pred;
35: y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
^
36: for (i in 2:n_new){
------------------------------------------------
Additive question:
Can anyone explain what is the process inner of stan when fitting? I already read papers but still confuse.
Morning and welcome! First off all your <- need to be changed to = . And it’s a bit hard to read you model code so let me see if I can reformat that for you.
generated quantities{
vector[n_new+T] y_new;
vector[n_new] x_pred;
y_new[1:T] = mu;
y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
for (i in 2:n_new){
y_new[T+i] = normal_rng(mu+phi*y_new[T+i-1]+theta*err, sigma);
}
}
not sure what you’re trying to do, but your generated quantities block should look something like the above.
your original program had some lines commented out - I uncommented them, and put the data declarations before assignments.
try using CmdStanR or CmdStanPy to get the new Stan3 parser which has better error messages.
I want to predict the future n_new data.
I tried your block.
error in 'unknown file name' at line 32, column 3
-------------------------------------------------
30: vector[n_new+T] y_new;
31: vector[n_new] x_pred;
32: y_new[1:T] = mu;
^
33: y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
-------------------------------------------------
PARSER EXPECTED: <one of the following:
a variable declaration, beginning with type
(int, real, vector, row_vector, matrix, unit_vector,
simplex, ordered, positive_ordered,
corr_matrix, cov_matrix,
cholesky_corr, cholesky_cov
or a <statement>
or '}' to close variable declarations and definitions>
hi S, in the Forums posts, use 3 backticks for blockquotes.
I would look in the language ref manual or the users manual for more information on the generated quanitites block. the error message indicates that the kind of vectorized assigment you’re trying to do isn’t allowed.
Did you manage to resolve this? Also note that if your goal is to just run the model (as opposed to learning Stan to be able to do advanced modelling), you should be IMHO able to setup an ARMA model using brms, which has much friendlier learning curve and has facilities to predict and other common operations out of the box.
Thanks, I’m exploring the unknown error right now and try to use a acceptable to represent it. As you said err[T+1:x]=0 is one way, but I used the random walk to represent the error after T, maybe it’s not good. but I will try to using statistical model to simulate the errors after T.
it should be nu[t] in stead of y[t], I just want to plot the original value so that I used y[t].
do you think I should drop theta(prediction[t-1]-prediction[t-2]) out instead, because I’m doing the test, I should make the model as simple as possible first.