commons-statistics-regression/src/main/java/org/apache/commons/statistics/regression/stored/ols/OLSRegression.java
/**
     * <p>
     * Compute the "hat" matrix.
     * </p>
     * <p>
     * The hat matrix is defined in terms of the design matrix X by
     * X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup>
     * </p>
     * <p>
     * The implementation here uses the QR decomposition to compute the hat matrix
     * as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the p-dimensional
     * identity matrix augmented by 0's. This computational formula is from "The Hat
     * Matrix in Regression and ANOVA", David C. Hoaglin and Roy E. Welsch, <i>The
     * American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22.
     * </p>
     * <p>
     * Data for the model must have been successfully loaded using one of the
     * {@code inputNewSampleData} methods before invoking this method; otherwise a
     * {@code NullPointerException} will be thrown.
     * </p>
     *
     * @return the hat matrix
     */

Compute the "hat" matrix.


The hat matrix is defined in terms of the design matrix X by
X(XTX)-1XT


The implementation here uses the QR decomposition to compute the hat matrix
as Q IpQT where Ip is the p-dimensional
identity matrix augmented by 0's. This computational formula is from "The Hat
Matrix in Regression and ANOVA", David C. Hoaglin and Roy E. Welsch, The
American Statistician, Vol. 32, No.

@return the hat matrix
Compute the " hat " matrix . The hat matrix is defined in terms of the design matrix X by X ( XTX ) - 1XT The implementation here uses the QR decomposition to compute the hat matrix as Q IpQT where Ip is the p - dimensional identity matrix augmented by 0 ' s . This computational formula is from " The Hat Matrix in Regression and ANOVA " David C . Hoaglin and Roy E . Welsch The American Statistician Vol . 32 No . @return the hat matrix
public StatisticsMatrix calculateHat() {
        QRDecomposition<DMatrixRMaj> qr = new QRDecomposition_DDRB_to_DDRM();
        qr.decompose(getX().getDDRM().copy());
        StatisticsMatrix qrQ = new StatisticsMatrix(qr.getQ(null, false));
        StatisticsMatrix qrR = new StatisticsMatrix(qr.getR(null, false));
        final int p = qrR.numCols();
        final int n = qrQ.numCols();
        double[][] augIData = new double[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j && i < p) {
                    augIData[i][j] = 1d;
                } else {
                    augIData[i][j] = 0d;
                }
            }
        }
        StatisticsMatrix augI = new StatisticsMatrix(new DMatrixRMaj(augIData));
        return qrQ.mult(augI).mult(qrQ.transpose());
    }
public StatisticsMatrix calculateHat ( ) { QRDecomposition < DMatrixRMaj > qr = new QRDecomposition_DDRB_to_DDRM ( ) ; qr . decompose ( getX ( ) . getDDRM ( ) . copy ( ) ) ; StatisticsMatrix qrQ = new StatisticsMatrix ( qr . getQ ( null , false ) ) ; StatisticsMatrix qrR = new StatisticsMatrix ( qr . getR ( null , false ) ) ; final int p = qrR . numCols ( ) ; final int n = qrQ . numCols ( ) ; double [ ] [ ] augIData = new double [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j && i < p ) { augIData [ i ] [ j ] = 1d ; } else { augIData [ i ] [ j ] = 0d ; } } } StatisticsMatrix augI = new StatisticsMatrix ( new DMatrixRMaj ( augIData ) ) ; return qrQ . mult ( augI ) . mult ( qrQ . transpose ( ) ) ; }
<p>
 Compute the "hat" matrix.
<p > Compute the " hat " matrix .
// Create augmented identity matrix