Skip to content

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

  • Y and X are the dependent and independent variables as row or column vectors
  • xu is the value or values at which to evaluate the fitted values (y^\hat{y} predicted by the polynomial fit). If omitted then the fit is evaluated at all X values
  • p is the order of the polynomial fit; if omitted it defaults to p = 1 (a fitted line)
  • S is a vector of estimates of the standard deviations of the Y values. If this (optional) argument is given then weighted least squares is used to make the y^\hat{y} predictions.

The function returns the value y^\hat{y} 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.

Cooling data with PolyHat fitted values for the order typed in cell E1 (here 3), plotted as a dashed curve through the points

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)))
    )
);