PolyHat
Description
PolyHat returns the value predicted from a polynomial fit of the data. It uses LINEST for the fitting but first filters out the non-numeric values in the inputs.
Syntax
PolyHat(Y, X, [xu=X], [p=1], [S]) where
YandXare the dependent and independent variables as row or column vectorsxuis the value or values at which to evaluate the fitted values ( predicted by the polynomial fit). If omitted then the fit is evaluated at allXvaluespis the order of the polynomial fit; if omitted it defaults top = 1(a fitted line)Sis a vector of estimates of the standard deviations of theYvalues. If this (optional) argument is given then weighted least squares is used to make the predictions.
The function returns the value predicted for each of the input xu values.
Example
One use case for PolyHat is to visually examine the effect of changing the order of the polynomial fit. This is illustrated below for data collected for water that is cooling to room temperature. Changing the order will change the fit used in PolyHat and thus will change the return values. One can examine the effect visually in the plot (or in related plots such as a residuals plot). The PolyResids function can be used for a similar purpose.

Download: PolyHat.xlsx
Source Code
Helpers used: _PolyFit
PolyHat = LAMBDA(
Y, // dependent values as row or column vector
X, // independent values as row or column vector
// X and Y must be same length but can contain non-numbers and empty cells
[xu], // value at which to predict y value (default xu=X)
[p], // optional polynomial order (default p=1)
[S], // optional std devs of Y for weighted LS
LET(
p, IF(ISOMITTED(p), 1, p),
fit, IF(ISOMITTED(S), _PolyFit(Y, X, p, 1), _PolyFit(Y, X, p, S)),
b, TAKE(fit, 1),
// default xu: the X values used in the fit
NumFilter, IF(ISOMITTED(S), ISNUMBER(X) * ISNUMBER(Y), ISNUMBER(X) * ISNUMBER(Y) * ISNUMBER(S)),
xu, TOCOL(IF(ISOMITTED(xu), FILTER(X, NumFilter), xu)),
Xblock, HSTACK(Ones(ROWS(xu)), xu ^ SEQUENCE(, p)),
IF(ISTEXT(INDEX(fit, 1, 1)), INDEX(fit, 1, 1), MMULT(Xblock, TRANSPOSE(b)))
)
);