Source code for naginterfaces.library.examples.correg.linregm_fit_stepwise_ex

#!/usr/bin/env python
"``naginterface.library.correg.linregm_fit_stepwise`` Python Example."

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name,too-many-locals

import numpy as np

from naginterfaces.base import utils
from naginterfaces.library import correg

[docs]def main(): """ Example for :func:`naginterfaces.library.correg.linregm_fit_stepwise`. Stepwise linear regression. >>> main() naginterfaces.library.correg.linregm_fit_stepwise Python Example Results. Stepwise linear regression. Starting Stepwise Selection <BLANKLINE> Forward Selection Variable 1 Variance ratio = 1.260E+01 Variable 2 Variance ratio = 2.196E+01 Variable 3 Variance ratio = 4.403E+00 Variable 4 Variance ratio = 2.280E+01 <BLANKLINE> Adding variable 4 to model <BLANKLINE> Backward Selection Variable 4 Variance ratio = 2.280E+01 <BLANKLINE> Keeping all current variables <BLANKLINE> Forward Selection Variable 1 Variance ratio = 1.082E+02 Variable 2 Variance ratio = 1.725E-01 Variable 3 Variance ratio = 4.029E+01 <BLANKLINE> Adding variable 1 to model <BLANKLINE> Backward Selection Variable 1 Variance ratio = 1.082E+02 Variable 4 Variance ratio = 1.593E+02 <BLANKLINE> Keeping all current variables <BLANKLINE> Forward Selection Variable 2 Variance ratio = 5.026E+00 Variable 3 Variance ratio = 4.236E+00 <BLANKLINE> Adding variable 2 to model <BLANKLINE> Backward Selection Variable 1 Variance ratio = 1.540E+02 Variable 2 Variance ratio = 5.026E+00 Variable 4 Variance ratio = 1.863E+00 <BLANKLINE> Dropping variable 4 from model <BLANKLINE> Forward Selection Variable 3 Variance ratio = 1.832E+00 Variable 4 Variance ratio = 1.863E+00 <BLANKLINE> Finished Stepwise Selection Fitted model summary: Term Estimate Standard Error Intercept: 5.258e+01 2.294e+00 Variable: 1 1.468e+00 1.213e-01 Variable: 2 6.623e-01 4.585e-02 RMS is 5.790e+00 """ print( 'naginterfaces.library.correg.linregm_fit_stepwise ' 'Python Example Results.' ) print('Stepwise linear regression.') # The design matrix: z = np.array([ [7., 26., 6., 60., 78.5], [1., 29., 15., 52., 74.3], [11., 56., 8., 20., 104.3], [11., 31., 8., 47., 87.6], [7., 52., 6., 33., 95.9], [11., 55., 9., 22., 109.2], [3., 71., 17., 6., 102.7], [1., 31., 22., 44., 72.5], [2., 54., 18., 22., 93.1], [21., 47., 4., 26., 115.9], [1., 40., 23., 34., 83.8], [11., 66., 9., 12., 113.3], [10., 68., 8., 12., 109.4], ]) # Compute sums of squares and cross-products of deviations # from the mean. # No weights: sw, wmean, c = correg.ssqmat(z) # The variable-selection array: m = z.shape[1] - 1 isx = [1]*m # Other parameters for the regression function: fout = 2. # Use monitoring, NAG-supplied, without a NAG function prefix in # the output: monlev = 1 monfun = None io_manager = utils.FileObjManager(locus_in_output=False) fit = correg.linregm_fit_stepwise( z.shape[0], wmean, c, sw, isx, fout=fout, monlev=monlev, monfun=monfun, io_manager=io_manager, ) print('Fitted model summary:') print('Term' + ' '*9 + 'Estimate' + ' '*2 + 'Standard Error') print('Intercept: {:.3e} {:.3e}'.format(fit.b[0], fit.se[0])) for i in range(1, m + 1): if fit.isx[i - 1] not in [1, 2]: continue print('Variable:' + ' '*2 + '{:d} {:.3e} {:.3e}'.format( i, fit.b[i], fit.se[i] )) print('RMS is {:.3e}'.format(fit.rms))
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )