# How can I predict the variable interaction in the Hierarchical model

I am a beginner, I want to predict the “y” using a intercept and three variables(X1, X2, and X3), while two of the explanatory variables (X1 and X2)
which interact with each other to affect the response variable “y”.

How can I predict the X1 and y relationship when X2 was at different level, just like this picture.

here is my code:
data {
int N; //the number of observations
int J; //the number of groups
int K; //number of columns in the model matrix
int id[N]; //vector of group indeces
matrix[N,K] X; //the model matrix
vector[N] y; //the response variable
}
parameters {
vector[K] gamma; //population-level regression coefficients
vector[K] tau; //the standard deviation of the regression coefficients
//implementing Matt’s trick
vector[K] beta_raw[J];
real sigma; //standard deviation of the individual observations
}
transformed parameters {
vector[K] beta[J]; //matrix of group-level regression coefficients
//computing the group-level coefficient, based on non-centered parametrization based on section 22.6 STAN (v2.12) user’s guide
for(j in 1:J){
beta[j] = gamma + tau .* beta_raw[j];
}
}
model {
vector[N] mu; //linear predictor
//priors
gamma ~ normal(0,5); //weakly informative priors on the regression coefficients
tau ~ cauchy(0,2.5); //weakly informative priors, see section 6.9 in STAN user guide
sigma ~ gamma(2,0.1); //weakly informative priors, see section 6.9 in STAN user guide
for(j in 1:J){
beta_raw[j] ~ normal(0,1); //fill the matrix of group-level regression coefficients
}
for(n in 1:N){
mu[n] = X[n] * beta[id[n]]; //compute the linear predictor using relevant group-level regression coefficients
}
//likelihood
y ~ normal(mu,sigma);
}

Your Stan code looks like the standard hierarchical model, so to achieve inference on an interaction you simply have to pass in a predictor matrix X that includes a column encoding the contrast.