Problems adjusting Linear Regression at Stan

I’m having trouble adjusting a linear regression model on the stan. When observing the error message, the identification in the block part of the transformed parameters is noted.

See below the structure of the code in stan.


Packages:

library(rstan)
library(bayesplot)


Data:


head(Orange)
cols <- c(colnames(Orange[-1]))
Orange <- Orange[,cols]
str(Orange)

Code in stan:

See that the block structure within the stan follows the recommended pattern, however I am not able to identify which part of the code may seem wrong to me.


****
y = Orange$circumference
x = Orange$age
n = length(y)

regresstan = '
data{
  int n;
  real y[n];
  real x[n];
}

parameters{
  real alpha;
  real beta;
  real sigma;
}

transformed parameters{
    real mu[n];
    mu = alpha + beta*x;
}

model{
  //Priors
  alpha ~ normal(0, 100);
  beta ~ normal(0, 100);
  sigma ~ uniform(0, 100);

  //Likelihood
    y ~ normal(mu, sigma);
}
'

****
Error:


****
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for: 

  real * real[ ]

Available argument signatures for operator*:

  real * real
  vector * real
  row_vector * real
  matrix * real
  row_vector * vector
  vector * row_vector
  matrix * vector
  row_vector * matrix
  matrix * matrix
  real * vector
  real * row_vector
  real * matrix

No matches for: 

  real + ill-formed

Available argument signatures for operator+:

  int + int
  real + real
  vector + vector
  row_vector + row_vector
  matrix + matrix
  vector + real
  row_vector + real
  matrix + real
  real + vector
  real + row_vector
  real + matrix
  +int
  +real
  +vector
  +row_vector
  +matrix

Expression is ill formed.
 error in 'modele28054257a16_a9d23411185fa271b60f20be43062e80' at line 16, column 23
  -------------------------------------------------
    14: transformed parameters{
    15:     real mu[n];
    16:     mu = alpha + beta*x;
                              ^
    17: }
  -------------------------------------------------

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'a9d23411185fa271b60f20be43062e80' due to the above error.
****

Declare x as a vector instead of an array.

So:

vector[n] x;

instead of:

real x[n];

Thank you!
However, the error still persists.

****SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Dimension mismatch in assignment; variable name = mu, type = real[ ]; right-hand side type = vector.
Illegal statement beginning with non-void expression parsed as
mu
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 ‘modele2805730397a_32e2d63d73226372d5eb17a068648aa0’ at line 16, column 4

14: transformed parameters{
15:     real mu[n];
16:     mu = alpha + beta*x;
       ^
17: }

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
or ‘}’ to close variable declarations and definitions>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model ‘32e2d63d73226372d5eb17a068648aa0’ due to the above error.


What this means is the right hand side is a vector and the left hand side is an array.

Stan is strict about types, so you have to cast between them manually. This is similar to the issue above – loosely vector/row_vector/matrix types in Stan are for doing math while array types are for storing things.

Do:

vector[n] mu;
mu = alpha + beta*x;