Skip to contents

Thin wrapper around statar::xtile() documenting the contract this package relies on: NA in, NA out; non-missing values split into n groups of (approximately) equal size, numbered 1..n. See Pseudocode/00_conventions.md's QUARTILE primitive. Callers are responsible for applying this only within already-grouped data (this package additionally imposes a minimum group size of 50 before calling this at all — see compute_predictors()).

Usage

quartile_bin(x, n = 4)

Arguments

x

A numeric vector.

n

Number of equal-frequency bins (default 4, i.e. quartiles).

Value

An integer vector of bin numbers 1..n (or NA).

Details

Stata's own xtile is confirmed to use a percentile algorithm equivalent to R's quantile(type = 2) (see percentile_stata() and its source comment); statar::xtile() is a separate, independent R reimplementation, and its exact tie-breaking at quartile boundaries has not been verified here to match Stata's bit-for-bit. For this package's use (classifying scalers into descriptive predictor-quartile groups), this is a low-stakes residual risk — a handful of firms sitting exactly on a cutpoint could land in an adjacent bin — flagged rather than silently assumed away.