n-kall / priorsense

priorsense: an R package for prior diagnostics and sensitivity
https://n-kall.github.io/priorsense/
GNU General Public License v3.0
55 stars 6 forks source link

Error in powerscale_sequence complaining about missing chain_id argument #4

Closed paul-buerkner closed 2 years ago

paul-buerkner commented 2 years ago

When I run

library(brms)  # requires the priorsense branch of brms
library(priorsense)

prior1 <- prior(normal(0,10), class = b) +
  prior(cauchy(0,2), class = sd)
fit1 <- brm(count ~ zAge + zBase * Trt + (1|patient),
            data = epilepsy, family = poisson(), prior = prior1)

extract_log_prior2 <- function(...) {
  extract_log_prior(..., parameter_name = "lprior")
}

ps_fit1 <- priorsense::powerscale_sequence(
  fit1, 
  log_prior_fn = extract_log_prior2,
  joint_log_lik_fn = log_lik
)

I get

Error in llmatrix_to_array(x, chain_id) : 
  argument `chain_id` is missing

Is there a place where I need to pass the chain-ID or is this a bug?

n-kall commented 2 years ago

Thanks for filing this issue! This seems like it's related to the specified joint_log_lik_fn. What happens if you leave the joint_log_lik_fn argument as the default (which would then be extract_joint_log_lik)? This is how it's currently implemented for brmsfit objects:

extract_joint_log_lik.brmsfit <- function(x, parameter_name = "log_lik", merge_chains = TRUE, ...) {
  if (!requireNamespace("brms", quietly = TRUE))
    stop("Please load the 'brms' package.", call. = FALSE)

  log_lik <- rowSums(brms::log_lik(x))
  names(log_lik) <- parameter_name
  chains <- x$fit@sim$chains

  log_lik <- posterior::draws_array(
    log_lik = log_lik,
    .nchains = chains
  )

  return(log_lik)
}
paul-buerkner commented 2 years ago

Thanks! That works, but only if I use priorsense:::extract_joint_log_lik since extract_joint_log_lik seems to be not exported currently.

n-kall commented 2 years ago

Good that it fixed it! Yes, I should export it like extract_log_prior. Will do this soon

n-kall commented 2 years ago

This should be fixed in current version