Unable to initialize sampler in 2 stage latent variable model

“Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform. Left-hand-side of sampling statement:
obs ~ normal(…)”

This happens because you’re declaring eps as a transformed parameter, then placing a prior on it in the model block. You probably want to move vector[N] eps; up to the parameters block. Here’s a thread that goes into detail on placing priors on transformed parameters: Putting priors on transformed parameters .

I’m guessing this isn’t your intention though, so just rerun the model like this and it should initialize (it did for me).

data {
	int<lower=1> N;
	vector[N] x;
	int<lower=0,upper=1> y[N];
}
parameters {
	real<lower=0> sigma;
	real<lower=0> beta;
	real mu;
	vector[N] eps;
}
transformed parameters{
	//vector[N] eps;
	vector[N] obs;
	vector[N] q_obs;
	obs = x + sigma*eps;
	q_obs = beta*(2*Phi((mu-obs)/sigma)-1);
}
model {
	mu ~ normal(12.5,3);
	eps ~ normal(0,1);
	y ~ bernoulli_logit(q_obs);
}

Unrelated to the initialization issue, you may want to place stronger prior/constraint on sigma to keep it away from 0 so that the (mu-obs)/sigma term doesn’t blow up. around slide 28 in this presentation by Andrew Gelman he gives a good example for a nonnegative parameter prior that avoids the boundary.

2 Likes