center_within_cluster: Center a Numeric Variable Within Clusters

View source: R/centering.R

center_within_clusterR Documentation

Center a Numeric Variable Within Clusters

Description

Subtracts each cluster mean from a numeric vector. Missing cluster values produce missing centered values. This helper is used by the regression wrappers in their generated 'setup' code and can also be used in hand-written 'rtmb_code()' models.

Usage

center_within_cluster(x, cluster, na.rm = TRUE)

Arguments

x

Numeric vector to center.

cluster

Vector identifying the cluster for each element of 'x'.

na.rm

Logical; whether missing values are removed when calculating cluster means.

Value

A numeric vector with the same length as 'x'.

Examples

center_within_cluster(c(1, 3, 2, 6), c("a", "a", "b", "b"))

BayesRTMB documentation built on Nov. 24, 2026, 9:06 a.m.