AR(1) Syntax Error Parse

I am trying to do a simple AR(1) model, however, I keep getting an error that reads:
Dimension mismatch in assignment; variable name = y_rep, type = real; right-hand side type = real[ ].
Illegal statement beginning with non-void expression parsed as
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

I have posted the code below.

data {
  int<lower=0> N;
  int<lower=0> k;
  vector[N] g;
  matrix[N,k] x;
parameters {
  real alpha;
  real beta;
  real <lower=0> sigma;
model {
  for (n in 2: N)
    g ~ normal (alpha + x[n-1]*beta, sigma);
generated quantities {
  int<lower=0, upper=1> mean_gt;
  int<lower=0, upper=1> sd_gt;
  int<lower=0, upper=1> max_gt;
  vector[N] log_lik;
  vector[N] y_rep;
  for (n in 2:N){
  log_lik[n]= normal_lpdf(g | alpha + x[n-1]*beta,sigma);
  for (n in 2:N) {
  y_rep[n]= normal_rng(alpha + x[n-1]*beta, sigma) ;
  mean_gt= mean(y_rep) > mean(g); // p-value for mean
  sd_gt = sd(y_rep) > sd(g); // p-value for sd
  max_gt = max(y_rep) > max(g); // p-value for max

g<- log(data$sale/data$xbar)
dat <- list(N=nrow(data),k=k, g=g, x=x)

Hi Connor,

The issue here is that x is an N\text{x}K matrix, implying N observations and K covariates, but you’re only specifying a single regression coefficient \beta (i.e., real beta). This implies that you want every covariate to have the same regression coefficient, which I’m guessing you don’t want.

In this case, you just need to specify that you want K regression coefficients:

vector[k] beta;

In the case where you did want to have every covariate use the same regression coefficient, you just need to sum the multiplication of x and beta:

model {
    g ~ normal (alpha + sum(x[n-1]*beta), sigma);
generated quantities {
  log_lik[n]= normal_lpdf(g | alpha + sum(x[n-1]*beta),sigma);
  y_rep[n]= normal_rng(alpha + sum(x[n-1]*beta), sigma) ;