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

#!/usr/bin/env python3
"``naginterface.library.correg.linregm_fit`` Python Example."

# NAG Copyright 2017-2019.

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

import numpy as np

from naginterfaces.library import correg

[docs]def main(): """ Example for :func:`naginterfaces.library.correg.linregm_fit`. Fit a general (multiple) linear regression model. >>> main() naginterfaces.library.correg.linregm_fit Python Example Results. Fit a general (multiple) linear regression model. Fitted model summary: Model is not of full rank Rank: 4 RSS is 2.223e+01 Degrees of freedom 8 Term Estimate Standard Error Intercept: 3.056e+01 3.849e-01 Variable: 1 5.447e+00 8.390e-01 Variable: 2 6.743e+00 8.390e-01 Variable: 3 1.105e+01 8.390e-01 Variable: 4 7.320e+00 8.390e-01 """ print('naginterfaces.library.correg.linregm_fit Python Example Results.') print('Fit a general (multiple) linear regression model.') # The observations: x = np.array([ [1., 0., 0., 0.], [0., 0., 0., 1.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.], [0., 1., 0., 0.], [0., 0., 0., 1.], [1., 0., 0., 0.], [0., 0., 1., 0.], [1., 0., 0., 0.], [0., 0., 1., 0.], [0., 1., 0., 0.] ]) # The independent variables to include: isx = [1]*x.shape[1] # The dependent observations: y = [ 33.63, 39.62, 38.18, 41.46, 38.02, 35.83, 35.99, 36.58, 42.92, 37.80, 40.43, 37.89, ] fit = correg.linregm_fit( x, isx, y, ) print('Fitted model summary:') print('Model is ' + ('not ' if fit.svd else '') + 'of full rank') print('Rank: {:d}'.format(fit.irank)) print('RSS is {:.3e}'.format(fit.rss)) print('Degrees of freedom {:d}'.format(fit.idf)) print('Term' + ' '*9 + 'Estimate' + ' '*2 + 'Standard Error') print('Intercept: {:.3e} {:.3e}'.format(fit.b[0], fit.se[0])) for i, b_i in enumerate(fit.b): if i == 0: continue print('Variable:' + ' '*2 + '{:d} {:.3e} {:.3e}'.format( i, b_i, fit.se[i] ))
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )