I’m trying to fit a zero-inflated lognormal model using Stan. SAS PROC NLMIXED allows me to write a custom likelihood function to fit this model, but now I’d like to fit a Bayesian model, hence Stan.

Here’s my code:

```
zero_inflated_lognormal <- custom_family(
"zero_inflated_lognormal", dpars = c("mu", "sigma", "zi"),
links = c("identity", "identity", "logit"), lb = c(NA, 0, NA),
type = "real"
)
```

```
stan_funs <- "
// Arguments:
// y: the response value
// mu: mean parameter of the lognormal distribution
// sigma: sd parameter of the lognormal distribution
// zi: zero-inflation probability
// Returns:
// a scalar to be added to the log posterior
// zero-inflated lognormal log-PDF of a single response
real zero_inflated_lognormal_lpdf(real y, real mu, real sigma, real zi) {
if (y == 0) {
return log_sum_exp(bernoulli_lpmf(1 | zi),
bernoulli_lpmf(0 | zi) +
lognormal_lpdf(0 | mu, sigma));
} else {
return bernoulli_lpmf(0 | zi) +
lognormal_lpdf(y | mu, sigma);
}
}
// zero-inflated lognormal log-PDF of a single response
// logit parameterization of the zero-inflation part
real zero_inflated_lognormal_logit_lpdf(real y, real mu, real sigma, real zi) {
if (y == 0) {
return log_sum_exp(bernoulli_logit_lpmf(1 | zi),
bernoulli_logit_lpmf(0 | zi) +
lognormal_lpdf(0 | mu, sigma));
} else {
return bernoulli_logit_lpmf(0 | zi) +
lognormal_lpdf(y | mu, sigma);
}
}
// zero-inflated lognormal log-CCDF and log-CDF functions
real zero_inflated_lognormal_lccdf(real y, real mu, real sigma, real zi) {
return bernoulli_lpmf(0 | zi) + lognormal_lccdf(y | mu, sigma);
}
real zero_inflated_lognormal_lcdf(real y, real mu, real sigma, real zi) {
return log1m_exp(zero_inflated_lognormal_lccdf(y | mu, sigma, zi));
}
}
"
```

```
stanvars <- stanvar(scode = stan_funs, block = "functions")
```

```
fit <- brm(tvhours ~ 1, data = rDat,
family = zero_inflated_lognormal, stanvars = stanvars)
```

I believe I wrote the custom likelihood correctly, but I’m receiving the following error message after running the last chunk of code (directly above):

“PARSER FAILED TO PARSE INPUT COMPLETELY STOPPED AT LINE 49:”

I read the following in the Stan User’s Guide:

“Zero inflation and hurdle models can be formulated for discrete distributions other than the Poisson. **Zero inflation does not work for continuous distributions in Stan** because of issues with derivatives; in particular, there is no way to add a point mass to a continuous distribution, such as zero-inflating a normal as a regression coefficient prior.”

My question is, **is it really impossible to fit a zero-inflated lognormal model using Stan**??

Madeline