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.
If yours
n
is the same as 35 I think it’s normal for him to estimate 35mu
s, no? I edited the responsta with another possibility to calculate themu
with a vector.– Willian Vieira
Then why do you define
real mu[n];
this way in your initial model? The way you definemu
is for him to be esteemedn
times.– Willian Vieira
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
– Willian Vieira