How to get fitted values on the response scale for truncated 'asym_laplace' models

Dear all,

I’m using a truncated quantile (median) regression (using asym_laplace) of the type

bf(Y | trunc(lb = 0) ~ 1 + x + (1 | id1) + (1 + x | id2), quantile = 0.5),

and would like to get conditional effects via marginal_effects. brms tells me

Error: Fitted values on the respone scale not yet implemented for truncated 'asym_laplace' models.

My question: How difficult and effortful would it be (for me?) to properly implement that functionality and how would I approach this? And if it’s too difficult and effortful, is there a workaround I can use to create the kind of data marginal_effects is producing?

Thanks!

  • Operating System: macOS 10.14.6
  • brms Version: 2.10.0

What you need is mainly a formula for the mean of a truncated asymmetric laplace distribution.

I wasn’t able to find a closed-form formula online, but I found a numerical solution based on this paper: https://www.jstatsoft.org/article/view/v016c02 (see p. 3)

dtrunc <- function(x,
                   spec,
                   a = -Inf,
                   b = Inf,
                   ...)
{
  tt <- rep(0, length(x))
  g <- get(paste("d", spec, sep = ""), mode = "function")
  G <- get(paste("p", spec, sep = ""), mode = "function")
  tt[x >= a & x <= b] <-
    g(x[x >= a & x <= b], ...) / (G(b, ...) - G(a, ...))
  return(tt)
}

extrunc <- function(spec, a = -Inf, b = Inf, ...)
{
  f <- function(x)
    x * dtrunc(x, spec, a = a, b = b, ...)
  return(integrate(f, lower = a, upper = b)$value)
}

This then seems to work with brms’s dasym_laplace and pasym_laplace

extrunc("asym_laplace", mu = 1, a = 0) 

This approach seems generic enough to support any kind of truncated distributions that brms already supports in a non-truncated way.

Looking at vignette("brms_customfamilies") it seems to me that the quick fix would be to add a function fitted_trunc_asym_laplace in fitted.R.

fitted_trunc_asym_laplace <- function(draws, lb, ub) {
  ###
}

Or would it make more sense to replace dasym_laplace, pasym_laplace, qasym_laplace, and rasym_laplace in distributions.R with versions that can handle truncation? Or is this irrelevant for postprocessing?

Computing the truncated density is not a problem and brms does that already for arbitary models

The remaining problem is the truncated mean. Of course numerical integration, as done in extrunc is always a brute force solution but remember that this needs to be done for each single posterior draws of each single observation, which will be highly inefficient.

OK. Then simply focussing on the use case of me creating a workaround (where I accept the inefficiency of the numerical approach), that is, not a general solution for brms, would it be sufficient to implement it in

fitted_trunc_asym_laplace <- function(draws, lb, ub) {
  ###
}

?

Yes it should be sufficient.

1 Like