Sres
Description
Sres returns two values, as a row vector: the standard deviation of the residuals, , and the standard deviation of the deleted (leave-one-out) residuals, . 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
Yis the dependent data, as a column vectorXis a matrix containing the independent data, one column for each variableintcptis a boolean, defaulting toTRUE, that governs whether the intercept term is included in the regression modelSis optional: the standard deviations of theYvalues, as a column vector. If given, the fit is weighted with weights (as inWLS) and both returned values are weighted.
A row containing a non-number (in Y, X or S) is removed. The function returns two values, and , as a row vector. For a weighted fit 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))
)
);