I have coding question related to writing custom distribution families.
I’m working on a custom family for the Negative Binomial Distribution Type P. I’m using the lognormal_natural.R
(written by @StaffanBetner ) as a guide since I’m in personally uncharted territory . Within the code for log_lik_lognormal_natural()
log_lik_lognormal_natural <- function(i, prep) {
mu <- prep$dpars$mu[, i]
if(NCOL(prep$dpars$sigma)==1){
sigma <- prep$dpars$sigma
} else {
sigma <- prep$dpars$sigma[, i] ## [, i] if sigma is modelled, without otherwise
}
y <- prep$data$Y[i]
common_term = log(1+sigma^2/mu^2)
Vectorize(dlnorm)(y, log(mu)-common_term/2, sqrt(common_term), log = TRUE)
}
we see the Vectorize()
function is used at the very end. However, I’m confused as to what this is actually doing since
- it seems like only scalars are being passed to the function
dlnorm()
. - Why vectorize an child function each time you call it in the parent function. Wouldn’t make more sense to vectorize
log_lik_lognormal
If someone could clarify my confusion, I’d greatly appreciate it.
Thanks,
Mike
- Operating System: Ubuntu 22.04
- brms Version: 2.19
- R Version: 4.3
edited by @jsocolar for syntax highlighting.