Hi everyone!
I tried to estimate State-space models with latent variables in r, but I failed. Does someone know how to do it better?
Let’s say we have two observable variables: gdp and inflation. We also know there are three unobservable variables: potential grow, potential gdp, output gap. We want to estimate unobservable variables and two coefficient (a1 and a2).
Our model
State Equations:
- grow[t] = grow[t-1] + e
- potential[t] = potential[t-1] + grow[t] + e
- gap[t] = a1*gap[t-1] + e
Measurement Equations:
- gdp[t] = potential[t] + gap[t]
- inflation[t] = inflation[t-1] + a2*gap[t] + e
Here I generated data:
a2 <- 0.3
grow <- c()
potential <- c()
gap <- c()
gdp <- c()
inflation <- c()
grow[1] <- 2
potential[1] <- 10
gap[1] <- 0
gdp[1] <- potential[1] + gap[1]/100
inflation[1] <- 2
for (i in 2:100) {
grow[i] <- grow[i-1] + rnorm(1, 0, 0.1)
potential[i] <- potential[i-1] + grow[i]/100 + rnorm(1, 0, 0.1)
gap[i] <- a1*gap[i-1] + rnorm(1,0,0.1)
gdp[i] <- potential[i] + gap[i]/100
inflation[i] <- inflation[i-1] + a2*gap[i] + rnorm(1,0,0.1)
}
And here is my rstan code:
int T; // number of obs
int P; //number of variables
matrix[T,P] Y; //dataset of generated series
}
parameters {
#Coefficients
vector[1] alfa1; //ar gap
vector[1] alfa2; //phillips curve
#State Variables (unobserved economic variables)
vector[T] gap; // output gap
vector[T] potential; // potential output
vector[T] grow; // growth of potential output
#Innovations
real<lower = 0> sigma_pot; // The scale of innovations to potential output
real<lower = 0> sigma_grow; // The scale of innovations to growth in potential output
real<lower = 0> sigma_gap; // The scale of innovations to output gap
real<lower = 0> sigma_inf; // The scale of innovations to phillips curve
}
model {
// priors
//Innovations
sigma_pot ~ cauchy(0.2,3);
sigma_grow ~ cauchy(0.3,3);
sigma_gap ~ cauchy(0.9,5);
sigma_inf ~ cauchy(2,5);
//coefficients
alfa1 ~ normal(0,1);
alfa2 ~ normal(0,1);
//Initialize State Equations
potential[1] ~ normal(0,1);
grow[1] ~ normal(0,1);
gap[1] ~ normal(0,1);
// State Equations
for(t in 2:T) {
grow[t] ~ normal(grow[t-1], sigma_grow);
potential[t] ~ normal(potential[t-1] + grow[t], sigma_pot);
gap[t] ~ normal( alfa1*gap[t-1], sigma_gap);
}
// Measurement Equations
for(t in 1:T) {
Y[t,1] = potential[t] + gap[t];
Y[t,2] ~ normal(Y[t-1,2] + alfa1*gap[t],sigma_inf);
}
}
Here I tried model
mvf_model <- stan(file = "newstan.stan" ,
data = list(T = nrow(data),
P = ncol(data),
Y = data),
chains = 4)
And here is the error I’ve got
Cannot assign to variable outside of declaration block; left-hand-side variable origin=data
Illegal statement beginning with non-void expression parsed as
Y[[t, 1]]
Not a legal assignment, sampling, or function statement. Note that
* Assignment statements only allow variables (with optional indexes) on the left;
* Sampling statements allow arbitrary value-denoting expressions on the left.
* Functions used as statements must be declared to have void returns
error in 'model16a4a72378d_newstan' at line 27, column 3
-------------------------------------------------
25: transformed parameters {
26: for(t in 1:T) {
27: Y[t,1] = potential[t] + gap[t];
^
28: }
-------------------------------------------------
PARSER EXPECTED: "}"
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'newstan' due to the above error.