Skip to content

Sres

Description

Sres returns two values, as a row vector: the standard deviation of the residuals, sress_\text{res}, and the standard deviation of the deleted (leave-one-out) residuals, sdels_\text{del}. A deleted residual is the residual value of a data point that was not used to fit the model.

The first statistic quantifies the scatter about the fitted regression model while the second quantifies the uncertainty of future predictions.

Syntax

Sres(Y, X, [intcpt = TRUE], [S]) where

  • Y is the dependent data, as a column vector
  • X is a matrix containing the independent data, one column for each variable
  • intcpt is a boolean, defaulting to TRUE, that governs whether the intercept term is included in the regression model
  • S is optional: the standard deviations of the Y values, as a column vector. If given, the fit is weighted with weights wi=1/si2w_i = 1/s_i^2 (as in WLS) and both returned values are weighted.

A row containing a non-number (in Y, X or S) is removed. The function returns two values, sress_\text{res} and sdels_\text{del}, as a row vector. For a weighted fit sress_\text{res} is the standard deviation of unit weight; if the standard deviations are in the same units as Y it is the square root of the reduced chi-square.

Example

See the documentation of the companion function, PolySres, for an example of how the output of this function can be used as an aid in choosing the correct regression model.

Source Code

Sres = LAMBDA(
    Y, // dependent values as a column vector
    X, // independent values, one column per variable
    [intcpt], // optional flag, if TRUE (the default) an intercept is included in the model
    [S], // optional std devs of Y for weighted LS
    LET(
        intcpt, IF(ISOMITTED(intcpt), TRUE, intcpt),
        // get indices of valid rows for X, Y (and S) inputs
        Xind, BYROW(X, LAMBDA(X, PRODUCT(--ISNUMBER(X)))),
        NumFilter, IF(ISOMITTED(S), Xind * ISNUMBER(Y), Xind * ISNUMBER(Y) * ISNUMBER(S)),
        Yn, FILTER(Y, NumFilter),
        n, ROWS(Yn),
        Xn, IF(intcpt, HSTACK(FILTER(X, NumFilter), Ones(n)), FILTER(X, NumFilter)),
        p, COLUMNS(Xn), // number of regression parameters
        // weights (all equal to 1 for ordinary LS)
        sdn, IF(ISOMITTED(S), Ones(n), FILTER(S, NumFilter)),
        wt, 1 / sdn ^ 2,
        // calculate the projection ("Hat") matrix, yhat = H*Y
        inner, MINVERSE(MMULT(TRANSPOSE(Xn * wt), Xn)),
        H, MMULT(MMULT(Xn, inner), TRANSPOSE(Xn * wt)),
        h_ii, MDiag(H),
        // calculate residuals and s_res
        resids, Yn - MMULT(H, Yn),
        sres, SQRT(SUM(wt * resids ^ 2) / (n - p)),
        // deleted residuals and their (weighted) std dev about their (weighted) mean
        delresids, resids / (1 - h_ii),
        dbar, SUM(wt * delresids) / SUM(wt),
        s_del, SQRT(SUM(wt * (delresids - dbar) ^ 2) / (n - p - 1)),
        IF(OR(sdn <= 0), "ERROR: std devs must be positive", HSTACK(sres, s_del))
    )
);