Skip to contents

For a frmtmb::mixture(), frmtmb.latent::mixture_mvn() or frmtmb.latent::lca() fit, the posterior probability that each observation came from each component, propagating the uncertainty in the parameters: the fit-side frmtmb::mixture_probs() computation is run at every draw. brms calls this pp_mixture().

Usage

# S3 method for class 'frmtmb_draws'
pp_mixture(
  x,
  newdata = NULL,
  re_formula = arg_unset(),
  resp = NULL,
  ndraws = NULL,
  draw_ids = NULL,
  log = FALSE,
  summary = TRUE,
  robust = FALSE,
  probs = c(0.025, 0.975),
  ...
)

Arguments

x

A frmtmb_draws from frm_sample().

newdata, re_formula

Accepted for brms's signature and refused: frmtmb::mixture_probs() is a statement about the rows the model was fitted on.

resp

The response whose mixture to report, for a multivariate model.

ndraws

Number of draws to use (default: all).

draw_ids

The draws to use, by row index, instead of the evenly spaced subsample ndraws takes.

log

If TRUE, log probabilities.

summary

If TRUE (the default, as in brms), an observations x statistics x components array of summaries; otherwise the raw draws x observations x components array.

robust

If TRUE, median and MAD instead of mean and SD.

probs

The two quantiles the summary reports.

...

Unused.

Value

An array; see summary. For a group-level mixture (mixture(groups = ), frmtmb.latent::lca()) the rows are groups, as in frmtmb::mixture_probs().

Details

The argument order is brms's, so summary sits in brms's own eighth position and not in the second: the second is newdata.

See also

frmtmb::pp_mixture(), the same array on a fit, at the estimates.

Examples

# \donttest{
if (requireNamespace("tmbstan", quietly = TRUE) &&
    requireNamespace("rstan", quietly = TRUE) &&
    !frmtmb.sample:::tmbstan_build_broken()) {
  set.seed(4)
  dd <- data.frame(y = c(rnorm(60, -2), rnorm(60, 3)))
  fit <- frm(bf(y ~ 1), family = frmtmb::mixture(gaussian(), gaussian()),
             data = dd)
  ds <- frm_sample(fit, chains = 1, iter = 400, refresh = 0)
  head(pp_mixture(ds)[, "Estimate", ])
}
#> frm_sample(): default priors (brms 2.23 defaults; prior = "flat" puts brms's (flat) in every slot)
#>   Intercept (dpar = mu1) student_t(3, 0.6, 3.5)
#>   sigma1             student_t(3, 0, 3.5)  [natural scale]
#>   Intercept (dpar = mu2) student_t(3, 0.6, 3.5)
#>   sigma2             student_t(3, 0, 3.5)  [natural scale]
#>   b                  (flat), as brms leaves slopes
#>   theta: dirichlet(1), brms's default, on the mixing weights (theta1, theta2); it is the flat density on the simplex, and prior = "flat" keeps it - see ?frm_sample
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#>   P(K = 1 | Y) P(K = 2 | Y)
#> 1    0.9999818 1.823435e-05
#> 2    0.9999990 1.018304e-06
#> 3    0.9996830 3.169852e-04
#> 4    0.9999123 8.771204e-05
#> 5    0.9898322 1.016780e-02
#> 6    0.9998691 1.308767e-04
# }