I am using the bridge_sampler() function to evaluate the log marginal likelihood of the SEM stan fit object. The stan code I used comes below:
"data{
int N; // sample size
int P; // number of variables
matrix[N, P] Y; // data matrix Y with N rows and P columns
int D; // number of total latent variables
int K; // number of explanatory latent variables (ksi)
int E; // number of outcome latent variables (eta)
matrix[K, K] R; // diagonal matrix
}
parameters{
vector[P] nu; // intercepts of observed variables
vector[P-D] lam; // factor loadings
vector[K] beta; // structural regressions
matrix[N, K] FS_K; // factor scores of explanatory latent variables
vector[N] FS_E; // factor scores of outcome latent variables
vector<lower=0>[P] var_P; // variance for observed variables
cov_matrix[K] cov_K; // covariance for explanatory latent variables
vector<lower=0>[E] var_E; // variance for outcome latent variables
}
transformed parameters{
matrix[N, P] mu_pred; // predicted values of observed variables
vector[N] nu_pred; // predicted values of eta
matrix[K, K] L;
L = cholesky_decompose(cov_K);
for(i in 1:N){
mu_pred[i,1] = nu[1] + FS_K[i,1];
mu_pred[i,2] = nu[2] + lam[1]*FS_K[i,1];
mu_pred[i,3] = nu[3] + lam[2]*FS_K[i,1];
mu_pred[i,4] = nu[4] + FS_K[i,2];
mu_pred[i,5] = nu[5] + lam[3]*FS_K[i,2];
mu_pred[i,6] = nu[6] + lam[4]*FS_K[i,2];
mu_pred[i,7] = nu[7] + FS_K[i,3];
mu_pred[i,8] = nu[8] + lam[5]*FS_K[i,3];
mu_pred[i,9] = nu[9] + lam[6]*FS_K[i,3];
mu_pred[i,10] = nu[10] + FS_E[i];
mu_pred[i,11] = nu[11] + lam[7]*FS_E[i];
mu_pred[i,12] = nu[12] + lam[8]*FS_E[i];
}
for(i in 1:N){
nu_pred[i] = beta[1]*FS_K[i,1]+beta[2]*FS_K[i,2]+beta[3]*FS_K[i,3];
}
}
model{
// hyperpriors
vector[K] u;
u = rep_vector(0, K);
// priors on intercepts
for (i in 1:P) {nu[i] ~ normal(0, 10);}
// priors on factor loadings
for (i in 1:(P-D)) {lam[i] ~ normal(0, 10);}
// priors on regression coefficients
for (i in 1:K) {beta[i] ~ normal(0, 10);}
// priors on variance
for(i in 1:P) {var_P[i] ~ gamma(1, 0.5);}
var_E ~ gamma(1, 0.5);
cov_K ~ wishart(12, R);
// likelihood
for(i in 1:N){
FS_E[i] ~ normal(nu_pred[i], var_E);
FS_K[i] ~ multi_normal_cholesky(u, L);
for(j in 1:P){
Y[i, j] ~ normal(mu_pred[i,j],var_P[j]);
}
}
}
The file name of the above code is stan_sem2, and I fitted a stan fit object with the below code.
sem.fit.sc <- stan(file = stan_sem2, data = stan.dat, seed = 322, warmup = 3000, iter = 6000,
chains = 2, save_warmup = FALSE)
Next, I fed the Stan fit object to the bridge_sampler() function.
set.seed(322)
bs.sem.fit.sc <- bridge_sampler(sem.fit.sc)
After the processing, I get the following error messages:
bs.sem.fit.sc ← bridge_sampler(sem.fit.sc)
Error: cannot allocate vector of size 1.1 Gb
Error during wrapup: cannot allocate vector of size 918.6 Mb
Error: no more error handlers available (recursive errors?); invoking ‘abort’ restart
The first and second messages should have something to do with the memory issue. What about the third message, then? I have contacted this issue to one of the authors of the bridgesampling
package (Quentin), and he said the error message is not from the bridgesampling
package. Therefore, I am wondering if this error is related to the rstan. Is there anyone who has ideas about how to solve this error?
Thanks!