Hi stan community,
I’m trying to optimise the stan code for some models, and want to vectorise the following random intercept and slope model equation to speed up the computation process:
model {
for (o in 1:No)
z_exp[o] = mu + i[ID[o],1] + (psi + i[ID[o],2])*x[o] + i[partnerID[o],3];
}
where
real psi; // slope for response to x
matrix[Nid,3] i; // individual effects
i = iz * diag_pre_multiply(sd_I, LI)';
vector<lower=0>[3] sd_I; // individual standard deviations
matrix[Na,3] iz; // standardised individual effects
cholesky_factor_corr[3] LI;
to
z_exp = mu + i[ID,1] + (psi + i[ID,2])*x + i[partnerID,3];
However, I get the following error concerning the random slopes part:
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
0
Semantic error in 'string', line 38, column 31 to column 55:
-------------------------------------------------
38: z_exp = mu + i[ID,1] + (psi + i[ID,2])*x + i[partnerID,3];
^
-------------------------------------------------
Ill-typed arguments supplied to infix operator .*. Available signatures:
(int, int) => int
(real, real) => real
(real, vector) => vector
(vector, real) => vector
(vector, vector) => vector
(complex, complex) => complex
(real, row_vector) => row_vector
(row_vector, real) => row_vector
(row_vector, row_vector) => row_vector
(real, matrix) => matrix
(matrix, real) => matrix
(matrix, matrix) => ma
I think this because the real element for the slope psi and the matrix i provide an ill-typed argument? I would like to maintain the matrix element because it is used to estimate the correlation between the random intercept and slope. What would be the solution to vectorise this equation whilst maintaining the matrix format?