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

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 * matrix

No 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
+matrix

Expression is ill formed.
error in ‘model43a47412792f_Linear_regression’ at line 24, column 54

22: 
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)

The problem is that Stan does not know what multiplication of two vectors is supposed to be. (It could could be either dot product or element-wise product) Since you want element-wise multiplication here you can use the vectorized operator .*

   y ~ normal(alpha + beta1*x1 + beta2*x2 + beta3*x1 .* x2, sigma);
1 Like

Thanks. Problem solved. I spent several hours to solve this problem, but I could not. Thank you for taking time to answer my question. have a nice day. :)

An alternative solution is to write out the loop.

for (i in 1:N) {
   y[i] ~ normal(alpha + beta1*x1[i] + beta2*x2[i] + beta3*x1[i]*x2[i], sigma);
}
1 Like

I don’t know if my sincerity will be delivered because I’m not good at English, but I sincerely appreciate you.