Learn R Programming

apor (version 0.1.1)

ordPredRandom: Randomized Mapping from an Estimated Probability Distribution (EPD) to a Predicted Class

Description

Stochastically maps each row of an estimated probability distribution (EPD) matrix to a single predicted class by drawing one sample from the row's categorical distribution. Rows are normalized to sum to one (within tolerance), and the cut-points method is used with intervals \((c_{i,j-1}, c_{i,j}]\), ensuring \(z_i=1\) maps to class \(k\).

Usage

ordPredRandom(P, z = NULL, tol = 1e-12)

Value

An integer vector of length \(n\) with the predicted class indices in \(\{1,\ldots,k\}\) for each row of P.

Arguments

P

A numeric matrix of size \(n \times k\), where each row contains the estimated probabilities \(\hat\pi_{ij}\) for subject \(i\) and classes \(j = 1,\ldots,k\). Values must be nonnegative; rows are normalized to sum to one if needed.

z

Optional numeric vector of length \(n\) with values in \((0,1]\) providing external uniforms for reproducibility or control. If NULL (default), draws are generated internally via runif(n).

tol

Numeric tolerance used for row-sum checks and for guarding against underflow when normalizing. Defaults to 1e-12.

Details

The mapping follows the cumulative cut-points \(c_{i,0}=0\), \(c_{i,j}=\sum_{\ell=1}^j \hat\pi_{i\ell}\) for \(j=1,\ldots,k\), and assigns class \(j\) whenever \(c_{i,j-1} < z_i \le c_{i,j}\). When z is supplied, values are clipped to \((0,1]\) to respect interval boundaries. Rows with (near) zero total probability trigger an error.

See Also

nopa, ordPredArgmax opdRef

Examples

Run this code
set.seed(1)
P <- rbind(
  c(0.05, 0.10, 0.25, 0.60),
  c(0.40, 0.40, 0.10, 0.10),
  c(0.00, 0.20, 0.80, 0.00)
)

# Stochastic draws from each row's EPD
ordPredRandom(P)

# Reproducible draws using provided uniforms
z <- c(0.2, 0.85, 1.0)
ordPredRandom(P, z = z)

Run the code above in your browser using DataLab