Hi all,
I want to fit a model with latent variables restricted to the unit interval [0,1] and nonlinear relations. This is a toy model of a real application with much more relations. Below is the complete R code including simulation of a data set. When I run this model I get the error “Rejecting initial value” because “Log probability evaluates to log(0)” . I have no clue where the the problem comes from. Any help?
Reinhard
require(rstan)
n=100;
L1=runif(n); L2=runif(n)
x1=0.1+0.7*L1+rnorm(n,0,0.2) # path weigth 1,2 : 0.1, 0.7
x2=0.3+0.5*L2+rnorm(n,0,0.1) # path weigth 3,4 : 0.3, 0.5
x3=0.2+0.8*L1*L2+rnorm(n,0,0.2) # path weight 5,6 : 0.2, 0.8
dat=data.frame(x1,x2,x3)
stan.dat <- list(N=nrow(dat), P=ncol(dat), Y=as.matrix(dat), L=2, B=6 )
model="
data{
int N; // sample size
int P; // number of manifest variables
matrix[N, P] Y; // data matrix
int L; // number of latent variables
int B; // number of path weights
}
parameters{
matrix<lower = 0, upper = 1>[N, L] LV; // factor scores of latent variables
vector[B] beta; // path weights
vector<lower=0>[P] var_P; // error variance for observed variables
}
transformed parameters{
matrix[N,P] mu_pred; // predicted values of observed variables
for(i in 1:N){
mu_pred[i,1]= beta[1] +beta[2]*LV[i,1];
mu_pred[i,2]= beta[3] +beta[4]*LV[i,2];
mu_pred[i,3]= beta[5] +beta[5]*LV[i,1]*LV[i,2];
}
}
model{ // priors on factor loadings and error variance
for (i in 1:B) {beta[i] ~ uniform(0, 1);};
for(i in 1:P) {var_P[i] ~ gamma(1, 0.5);};
// likelihood
for(i in 1:N){ for(j in 1:P){
Y[i, j] ~ normal(mu_pred[i,j],var_P[j]);
} }
}
"
stan.fit <- rstan::stan(model_code=model,
data = stan.dat, seed = 322, cores = 4, chains = 1,
iter = 1000, verbose=TRUE)