SYNTAX ERROR, MESSAGE(S) FROM PARSER:
No matches for:vector * vector
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 * matrixNo matches for:
vector + 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
+matrixExpression is ill formed.
error in āmodel43a47412792f_Linear_regressionā at line 24, column 5422: 23: //likelihood 24: y ~ normal(alpha + beta1*x1 + beta2*x2 + beta3*x1*x2, sigma); ^ 25: }
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model āLinear regressionā due to the above error.
How could I solve this problem?
Below is code I typed.
# Set Seed
set.seed(1234)
# Number of observations
N<-5000
# True parameters
alpha<-5
beta1<ā1
beta2<-2
beta3<-1
sigma<-1
**# Storage **
y<-double(N)
x1<-runif(N,-10,10)
x2<-runif(N,-5,5)
# Simulate Data
for (i in 1:N){
** y[i]<-alpha+beta1x1[i]+beta2x2[i]+beta3*x1[i]x2[i]+rnorm(1,0,sigma)*
}
##################
lmcode = "
data {
** int<lower=0> N;**
** vector[N] x1;**
** vector[N] x2;**
** vector[N] y;**
}
parameters {
** real alpha;**
** real beta1;**
** real beta2;**
** real beta3;**
** real<lower=0> sigma;**
}
model {
//prior
** alpha~normal(0,100);**
** beta1~normal(0,100);**
** beta2~normal(0,100);**
** beta3~normal(0,100);**
** sigma~uniform(0,1000); **
//likelihood
** y ~ normal(alpha + beta1x1 + beta2x2 + beta3x1x2, sigma);**
}
"
#################
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
dat1 = list(N=length(y),y=as.vector(y),x1=as.vector(x1),x2=as.vector(x2))
R<-1000
nch<-1
lm = stan(model_name=āLinear regressionā, model_code = lmcode, data=dat1 , iter = R, chains = nch, verbose = T)