sorry if this is a stupid question, but how do I specify a response half-normal in brms, i tried to do it like this but I get an error
bf(LDM ~ -1 +harvest + (1 | gr(genotype, id = “genotype”)) + (1 | gr(block, id = “blocK”))) + half_normal()
sorry if this is a stupid question, but how do I specify a response half-normal in brms, i tried to do it like this but I get an error
bf(LDM ~ -1 +harvest + (1 | gr(genotype, id = “genotype”)) + (1 | gr(block, id = “blocK”))) + half_normal()
brms supports half-normal priors, but I’m not aware that it supports half-nornal likelihoods. You might consider a gamma or lognormal likelihood instead. Here’s some documentation on the supported likelihoods: Parameterization of Response Distributions in brms.
This might work for your purposes?
response | trunc(lb = 0) ~ predictors
(in any case, Solomon’s suggestions to use gamma or lognormal are probably more appropriate…)
I did this and it was ok:
halfnormal_family ← custom_family(
“halfnormal”, dpars = c(“mu”, “sigma”),
links = c(“identity”, “log”),
lb = c(0, 0),
type = “real”
)
stan_funs ← "
real halfnormal_lpdf(real y, real mu, real sigma) {
return normal_lpdf(y | mu, sigma) + log(2);
}
real halfnormal_rng(real mu, real sigma) {
return fabs(normal_rng(mu, sigma));
}
"
add_custom_family(halfnormal_family,
dpars = c(“mu”, “sigma”),
type = “real”,
links = c(“identity”, “log”),
log_lik = function(i, draws, data) {
mu ← draws$mu[, i]
sigma ← draws$sigma[, i]
y ← data$Y[i]
dnorm(y, mu, sigma, log = TRUE) + log(2)
})
stanvars ← stanvar(scode = stan_funs, block = “functions”)
FWIW, this isn’t a half-normal, it’s a zero-truncated normal, and it’s not normalized. The lpdf differs from the normal lpdf by a constant log(2)
, which means that as long as you don’t have any negative data that you’re passing to this family, it will yield the same fits as if you just used the normal()
family.
I just compared the family I created with the gaussian, and they gave practically the same estimates. I just want the family to be half-normal because that’s how my data for that response variable are distributed. I just don’t know how to do it :(.
Would you mind telling us more about your data?
I am conducting a simulation study where I have a multivariate model with six response variables and one of them is half-normally distributed.
Ah. Okay, that is an interesting use case where I can see why you’d want a half-Normal likelihood.
Yes, except that when I do the univariate half-normal model I can calculate the WAIC, but when I am doing the multivariate model with different families, including the half-normal as I showed in this post, I no longer know how to calculate the WAIC because the WAIC function of the brms package cannot calculate it :((
If this is a simulation study, I assume you have the code that generated the plotted data? It would be helpful if you can share that to ensure we all have the correct model in mind.