Shifted_lognormal model: prior for ndt leads to rejected inital values

I have trouble when I set priors for ndt for a shifted_lognormal model.
Here is an example that shows my problem:

df = data.frame(measure = c(2,2.1,2.5,3,3,3,5))

#this works
fit1= brm(
  prior = prior(uniform(0, 2), class = ndt),
  data = df,
  family = shifted_lognormal,
  formula = measure ~ 1,
  chains = 1

#this does not work. the only difference is the upper bound on the uniform prior
fit2= brm(
  prior = prior(uniform(0, 1.99), class = ndt),
  data = df,
  family = shifted_lognormal,
  formula = measure ~ 1,
  chains = 1

fit1 works and fit2 does not. this is because in fit2, the upper bound for the prior for ndt is smaller than the minimum of the data (1.99 vs. 2).
I am using a uniform prior because that seems to be the default for ndt in shifted_lognormal models.
I get the following output:

Chain 1: Rejecting initial value:
Chain 1:   Log probability evaluates to log(0), i.e. negative infinity.
Chain 1:   Stan can't start sampling from this initial value.
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1:  Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
error occurred during calling the sampler; sampling not done

When I add
inits = list(list(ndt = 0.001))
fit1 still runs, but for fit2, the error message stays the same.
I have tried different values for the inital values, but none seem to work.
If the value is outside the interval [0,2], I get the following error message_

Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: Error transforming variable ndt: lub_free: Bounded variable is *value I set*, but must be in the interval [0, 2]  (in 'model37715100cad_cd50431a01cb874ee5792d00a9b7836f' at line 15)

[1] "Error in sampler$call_sampler(args_list[[i]]) : "                                                                                                                                       
[2] "  Exception: Error transforming variable ndt: lub_free: Bounded variable is -1, but must be in the interval [0, 2]  (in 'model37715100cad_cd50431a01cb874ee5792d00a9b7836f' at line 15)"

I run R version 3.6.2 and brms 2.14.4 on macOS Mojave 10.14.5

What is the problem here? Any hints are appreciated.

I think you have set a prior on something you didn’t mean to.

The model is very simple. I suggest you use make_stancode and make_standata to look at the data being fed in and the stan code being used. It should be evident that, for the loglikelihood statement, the one claim that is larger than the upper bound of the uniform is given a zero likelihood, which will result in an error.

That’s my guess.

here is your stancode for the model that doesn’t work:

// generated with brms 2.14.6
functions {
data {
  int<lower=1> N;  // total number of observations
  vector[N] Y;  // response variable
  int prior_only;  // should the likelihood be ignored?
transformed data {
  real min_Y = min(Y);
parameters {
  real Intercept;  // temporary intercept for centered predictors
  real<lower=0> sigma;  // residual SD
  real<lower=0,upper=min_Y> ndt;  // non-decision time parameter
transformed parameters {
model {
  // likelihood including constants
  if (!prior_only) {
    // initialize linear predictor term
    vector[N] mu = Intercept + rep_vector(0.0, N);
    target += lognormal_lpdf(Y - ndt | mu, sigma);
  // priors including constants
  target += student_t_lpdf(Intercept | 3, 1.1, 2.5);
  target += student_t_lpdf(sigma | 3, 0, 2.5)
  - 1 * student_t_lccdf(0 | 3, 0, 2.5);
  target += uniform_lpdf(ndt | 0, 1.99)
  - 1 * log_diff_exp(uniform_lcdf(min_Y | 0, 1.99), uniform_lcdf(0 | 0, 1.99));
generated quantities {
  // actual population-level intercept
  real b_Intercept = Intercept;

you will see the line 45:

real<lower=0,upper=min_Y> ndt

that your ndt maximum is the minimum of the data so your prior isn’t needed by the definition of the model.
When your prior is applied, line 61 asks for the difference in 2 cdf’s.

log_diff_exp(uniform_lcdf(min_Y | 0, 1.99), uniform_lcdf(0 | 0, 1.99))

It is this line that is giving an error. Potentially because 2 is outside the acceptable range for a uniform from 0 to 1.99 or the two lcdf’s when exponentiated (i.e. cdf’s) are equal to each other and the log_diff_exp returns negative_infinity() (an error).

I suggest you ditch the prior as it is not helping your model in any case.

1 Like

Thank you for your reply and the stancode! This is just a toy example to illustrate my problem and I agree that in this case the prior could be ditched. In my (more complicated) model, this is not a good solution. I usually would not use a uniform prior, but since the default here is uniform I thought I’d keep it that way. Is it just not possible to have a cutoff below min_Y in this kind of model? I can of course just go for a differently shaped prior, but I would like to understand why this causing a problem here.

Can I get around the use of Y_min here somehow?

from the look of things, I don’t think you need to, but directly modifying the stan code is easy enough. I would look at the code generated without your additional prior and see if you need to add anything. From what I can tell, I don’t think you need to.