# Variational inference in Rstan with errors

Hello Guys,

I am a very newer here, I have trie to build my own variational inference algorithm in pure R without any package help, however my parameters were very strange at the end of iterations and I had to change.
I am now trying to use the package rstan to help me with that, however when I try to fit the model, some errors occurs and I really need to understand where the problem is.

I will share with you guys, my entire data, stan file and the model I am working.

data {

int<lower=0> N; // Number of observations
int<lower=0> P; // AR model order
int<lower=0> n_u; // Number of lost samples
}

transformed data {

real<lower=0> beta = 0.001; //global beta
real<lower=0> alpha = 0.001; //global alpha
}

parameters {

vector[n_u] x_u; // lost samples vector
real<lower=0> sigma_e; #innovation error
vector[P] a;	//coefficients vector
}

model {

vector[N] x; // vector with all samples
matrix[N-P, P] matriz_X; //Matrix X
vector[N-P] x_1; //Vector x1
real p;  // related with likelihood

for (n in 1:10)
x[400 + n] = x_u[n]; // Replace the lost samples sampled  on position from 401 to 410 on vector x.

for (k in 1:P-1)
matriz_X[, P-k] = x[(k+1):(N-P+k)]; // Building matrix X

for (j in P+1:N)  //Building the  vector x1 with vector x  samples   from  P+1 to N
for (m in 1:N-P)
x_1[m] = x[j];

//no need to de declared improper priors  (x_u and a proportional 1)
sigma_e ~ inv_gamma(alpha, beta);  // innovation error prior

// likelihood
p = exp((-(x_1-matriz_X*a)'*(x_1-matriz_X*a))/2*sigma_e)/(2*pi()*sigma_e)^((N-P)/2.0);

}


Here is my R enviroment:

set.seed(1)

# y auxiliar to simulate data
y <- runif(1000, -1, 1)

#known samples + lost samples
c <- matrix(y, ncol =1)

#Fit AR(40) model
ar_model <- ar(c, FALSE, 40)

#Fix coefficients of AR(40) model
list1 <- ar_model$ar #Simulate 1000 (N = 1000) samples from AR(40) model with fixed coefficients x <- arima.sim(model = list(ar = list1), n = 1000) #Collect 10 samples (size = 10) from x as we know the position of lost samples list2 <- x[c(401:410)] N <- length(x) #Number of observations P <- length(list1) #AR model order n_u = length(list2) # Number of lost samples list3 = 1 # sigma_e = 1 (innovation error) # save the above model to a file and compile it model <- stan_model(file = 'first.stan') # data list stan_dat <- list(N=N, P=P, n_u=n_u, x=x) #Fit using variational bayes fit <- vb(model, data = stan_dat, init = c(x_u = list1, a = list2, sigma_e = list3), algorithm = "meanfield", check_data = TRUE, output_samples = 100, adapt_iter = 50, refresh = FALSE, seed = 1)  This is the error Error in sampler$call_sampler(c(args, dotlist)) :
Expecting a single string value: [type=character; extent=51].

Here you can see my model, its structure is very close to Normal, however there is a bad dependence between vector a and matrix X
p_E(\textbf{x},\textbf{a},\sigma^2) = \frac{1}{{(2 \pi\sigma^2_e)^{\frac{N-P}{2} + \alpha + 1}}} \exp\{-\frac{1}{2{\sigma^2}_e}((x_1 - Xa )^T(x_1 - Xa )+ \beta)\}

\mathbf{X}=\left[\begin{array}{ccccccc} x_P & x_{P-1} & \cdots & \cdots & x_2 & x_1 \\ x_{P+1} & x_P & \cdots & \cdots & x_3 & x_2 \\ \vdots & \ddots & \ddots & \ddots & \ddots & \vdots \\ x_{N-2} & x_{N-3} & \cdots & \cdots & x_{N-P} & x_{N-P-1} \\ x_{N-1} & x_{N-2} & \cdots & \cdots & x_{N-P+1} & x_{N-P} \end{array}\right]

It seems you have error in one of the vb() arguments. Try with minimal arguments to find out which argument has the error.