Problems Adjusting Linear Regression in Stan

Asked

Viewed 49 times

1

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

See below the structure of code in Stan.

Packages:

library(rstan)
library(bayesplot)

Dice:

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

Code in Stan:

Note that the block structure inside Stan follows the recommended pattern, but I cannot 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.

1 answer

0


From what I understand from your model, you’re defining a class real while you should be wearing vector for the objects x, y and mu:

data{
  int n;
  vector[n] y;
  vector[n] x;
}

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

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

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

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

It is always important to test your model with false data, in which you already know the values of the parameters.

Also try to define [logical] limits of parameters to reduce divergent transitions and accelerate approximation:

real<lower = 0, upper = 2> alpha;

EDIT

You can keep the same model structure presented in the question and add a loop for in the block transformed_parameters:

data{
int n;
real y[n];
real x[n];
}

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

transformed parameters{
    real mu[n];
        for(i in 1:n){
        mu[i] = alpha + beta*x[i];
    }
}

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

//Likelihood
    y ~ normal(mu, sigma);
}
  • If yours n is the same as 35 I think it’s normal for him to estimate 35 mus, no? I edited the responsta with another possibility to calculate the mu with a vector.

  • Then why do you define real mu[n]; this way in your initial model? The way you define mu is for him to be esteemed n times.

  • Take a look at this example, I might be able to help you: https://github.com/stan-dev/example-models/blob/master/ARM/Ch.23/electric.stan

Browser other questions tagged

You are not signed in. Login or sign up in order to post.