Skip to contents

Extracts the intercept each level of a grouping factor was given by a (1 | group) term in the formula, as a posterior mean or as every draw.

Usage

# S3 method for class 'bartisan_fit'
ranef(object, draws = FALSE, ...)

Arguments

object

a <bartisan_fit> object; the output of a call to bartisan(), fitted with at least one (1 | group) term.

draws

logical; whether to return every posterior draw of each intercept rather than its posterior mean. Default is FALSE.

...

not used.

Value

With draws = FALSE, a named list with one entry per grouping factor, each a data frame of one row per level of that factor and one column per additive predictor that has a group intercept, with the levels as row names. This is the shape lme4::ranef() returns, so anything that reads that reads this.

With draws = TRUE, a named list with one entry per grouping factor, each itself a named list of draws-by-levels matrices, one per additive predictor.

Details

The column is named (Intercept), as in lme4, when the family has a single additive predictor. A family with more than one gets a group intercept on each, independent of the others, and the columns are named for the predictors instead; vignette("families") lists them per family.

Only the intercepts are returned. The standard deviation each grouping factor was drawn under is in object$tau, one column per factor and one matrix per additive predictor, and prior_summary() reports the prior it was drawn from.

A posterior mean is the wrong summary for a level with few observations, which is the case a group intercept exists for. draws = TRUE is what gives an interval, and a level whose interval covers zero is one the data had little to say about.

The intercepts are shrunk towards zero by their prior and are deviations from the additive predictor, so they come out approximately centered without being constrained to sum to zero exactly. Nothing is lost by that: the level of the fitted function is the additive predictor's, and a shift common to every intercept is one the forest did not take.

See also

bartisan() for the (1 | group) syntax and what it fits; prior_summary() for the prior on these

Examples

set.seed(123)
d <- data.frame(x = runif(200),
                site = factor(sample(letters[1:5], 200, TRUE)))
d$y <- rnorm(200, d$x + as.numeric(d$site) / 3)

fit <- bartisan(y ~ x + (1 | site), data = d, num_trees = 10,
                num_burn = 50, num_draws = 50, verbose = FALSE)
#>  Using `family = dpm()`.
#>  Set `family` to choose another, which also silences this message.

# One intercept per site, as posterior means. The generic is \pkg{nlme}'s,
# which \pkg{lme4} re-exports, so either qualification reaches this.
nlme::ranef(fit)
#> $site
#>   (Intercept)
#> a  -0.1753165
#> b   0.1716366
#> c   0.6186320
#> d   0.6787379
#> e   1.4104597
#> 

# With the draws, so the intercepts come with intervals
apply(nlme::ranef(fit, draws = TRUE)$site[["(Intercept)"]], 2L, quantile,
      c(.025, .975))
#>                a          b         c         d        e
#> 2.5%  -0.6378739 -0.2492532 0.1594344 0.1790185 1.057249
#> 97.5%  0.2664883  0.6400939 1.1145651 1.1708190 1.764146