Skip to contents

Computes \(R^2\) between an observed series and model estimates. Supports two definitions:

  • method = "corr" (default): \(R^2 = \mathrm{cor}(y,\hat y)^2\) (Pearson), always in \([0,1]\).

  • method = "sse": \(R^2 = 1 - \mathrm{SSE}/\mathrm{SST}\) with optional weights. This can be negative when the model underperforms the intercept-only baseline.

Usage

calc_model_R2(
  observed,
  estimated,
  method = c("corr", "sse"),
  bounded = FALSE,
  na_rm = TRUE,
  finite_only = TRUE,
  weights = NULL,
  verbose = FALSE
)

Arguments

observed

Numeric vector or matrix.

estimated

Numeric vector or matrix (same length after flattening).

method

One of "corr" (default) or "sse".

bounded

Logical; if TRUE, clamp the result to \([0,1]\). Default FALSE.

na_rm

Logical; drop NA pairs. Default TRUE.

finite_only

Logical; drop non-finite values. Default TRUE.

weights

Optional non-negative weights: length 1 (recycled), the input length (subset with the validity mask), or the number of valid pairs (pre-filtered); used for method="sse" and weighted Pearson in "corr".

verbose

Logical; if TRUE, emit brief diagnostics.

Value

Scalar \(R^2\). May be negative for method="sse" unless bounded=TRUE.

See also