Dear Stan community,

I need to use the `loo`

package for model comparison. To do so, one needs to calculate log-lik in the generated quantity block. I’m simultaneously running two models here: Bernoulli and Normal. When I try to generate `normal_lpmf`

, it gives me the error “Identifier ‘mu’ not in scope”. Not everything involved in the model comes directly from the data; aside from ‘x’ as a covariate, some functions are involved in generating other covariates like ‘E’. I wonder how to calculate log_lik for my model when random variables are involved, and can I generate log-likelihood even from the binary part, which involves longitudinal data? Could you please guide me a bit? Thanks a lot.

``

data{

int<lower=1> n; // number of observations

int<lower=1> n1; // number of observations x 2

vector[n1] y; // dep_data

int x_p;

int p; //nr of parameters

matrix[n1,x_p] x; //covariates

int PP1[n,n]; //network

int PP2[n,n]; //network

int PP3[n,n]; //network

```
int<lower=1> J; // dimension of observations
vector[J] Zero; // a vector of Zeros (fixed means of observations)
corr_matrix[J] I;
}
```

parameters {

vector<lower=0>[J] tau;

real<lower=0> sigmasq;

vector[p] beta;// add beta 2

matrix[n,J] ZZ; // latent-observations

vector[3] alpha;

}

transformed parameters{

cov_matrix[J] Tau= quad_form_diag(I, tau);

matrix[n, n] ZZ1 = dist(ZZ, n); //dist functions appply

}

model{

matrix[n,n] W = weight(ZZ1, n); // weight function applied

matrix [n1,n1] DW=double_rows_extend_columns(W); // double row function apply

vector [n1] E =DW *x[,2];
matrix [n1,3] DD =append_col(x,E);
vector[n1] mu = DD*beta;

```
for (i in 1:n){
ZZ[i,] ~ multi_normal(Zero, Tau);
}
sigmasq ~ student_t(4,0,5);//gamma(0.1, 0.1);
tau ~ student_t(4,0,5);
```

beta ~ normal(0, 10); //regression parameters

alpha ~ normal(0, 10); //regression parameters

for (i in 1:(n-1)){

for (j in (i+1):(n-1)){

real mu2 = alpha[1] + alpha[2]*ZZ1[i,j]+ alpha[3]*PP1[i,j];

real mu3 = alpha[1] + alpha[2]*ZZ1[i,j]+ alpha[3]*PP2[i,j];

PP2[i,j] ~ bernoulli(Phi(mu2));

PP3[i,j] ~ bernoulli(Phi(mu3));

}

}

y ~ normal(mu, sigmasq);

}

generated quantities {

vector[n1] log_lik;

for (k in 1:n1)

log_lik[k] = normal_lpmf(y[k] | mu[k]*beta); // ???

}

```
****
Error got:
Error in stanc(filename, allow_undefined = TRUE) : 0
Semantic error in 'string', line 121, column 32 to column 34:
-------------------------------------------------
119: vector[n1] log_lik;
120: for (k in 1:n1)
121: log_lik[k] = normal_lpmf(y[k] | mu[k]*beta);
^
122: }
-------------------------------------------------
Identifier 'mu' not in scope. Did you mean 'I'?
```