#!/usr/bin/env python3
"``naginterfaces.library.blgm.lm_formula`` Python Example."
# NAG Copyright 2017-2019.
# pylint: disable=invalid-name
from naginterfaces.base import utils
from naginterfaces.library import blgm
[docs]def main():
"""
Example for :func:`naginterfaces.library.blgm.lm_formula`.
Specify a linear model via a formula string.
Demonstrates using the ``Handle`` class.
>>> main()
naginterfaces.library.blgm.lm_formula Python Example Results.
Specify a linear model via a formula string.
Formula: 1+F2+CON+F1+F2.CON+F2.F1+CON.F1
Design matrix:
[[ 1. 0. 0. -2.4 0. 1. -0. -0. 0. 0. 0. 0. -0. -2.4]
[ 1. 0. 1. 0.2 0. 1. 0. 0.2 0. 0. 0. 1. 0. 0.2]
[ 1. 0. 1. -1.4 0. 0. -0. -1.4 0. 0. 0. 0. -0. -0. ]
[ 1. 0. 0. -5.4 1. 0. -0. -0. 0. 0. 0. 0. -5.4 -0. ]
[ 1. 0. 1. 0.2 0. 1. 0. 0.2 0. 0. 0. 1. 0. 0.2]
[ 1. 1. 0. 1.4 0. 1. 1.4 0. 0. 1. 0. 0. 0. 1.4]
[ 1. 1. 0. 6.8 0. 0. 6.8 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 1. 0. 6.7 0. 0. 6.7 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 0. 5.3 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 1. -1.3 1. 0. -0. -1.3 0. 0. 1. 0. -1.3 -0. ]
[ 1. 1. 0. -3.6 0. 1. -3.6 -0. 0. 1. 0. 0. -0. -3.6]
[ 1. 1. 0. -0.7 0. 1. -0.7 -0. 0. 1. 0. 0. -0. -0.7]
[ 1. 0. 0. 5.7 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 1. 2.3 0. 1. 0. 2.3 0. 0. 0. 1. 0. 2.3]
[ 1. 1. 0. 3.3 0. 0. 3.3 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 1. -0.5 1. 0. -0. -0.5 0. 0. 1. 0. -0.5 -0. ]
[ 1. 0. 0. -2.6 0. 0. -0. -0. 0. 0. 0. 0. -0. -0. ]
[ 1. 1. 0. 3.7 0. 0. 3.7 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 1. 0. 0.9 0. 0. 0.9 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 0. -1.1 0. 1. -0. -0. 0. 0. 0. 0. -0. -1.1]
[ 1. 1. 0. 2.1 1. 0. 2.1 0. 1. 0. 0. 0. 2.1 0. ]
[ 1. 0. 1. 4.6 0. 0. 0. 4.6 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 1. 4.6 1. 0. 0. 4.6 0. 0. 1. 0. 4.6 0. ]
[ 1. 1. 0. 5.1 0. 0. 5.1 0. 0. 0. 0. 0. 0. 0. ]
[ 1. 0. 1. 0.9 0. 0. 0. 0.9 0. 0. 0. 0. 0. 0. ]]
"""
print('naginterfaces.library.blgm.lm_formula Python Example Results.')
print('Specify a linear model via a formula string.')
# Specify a linear model and redisplay its value to demonstrate the
# option-getting facility:
hform = utils.Handle()
blgm.lm_formula(hform, '(F2 + Con + F1)^2')
print('Formula: ' + str(blgm.optget(hform, 'Formula')['value']))
# We want the design matrix to include an explicit term for the mean
# effect:
blgm.optset(hform, 'Explicit Mean = Yes')
# Create a description of the data matrix.
# The data:
dat = [
[3, 1, -2.4],
[3, 3, 0.2],
[1, 3, -1.4],
[2, 1, -5.4],
[3, 3, 0.2],
[3, 2, 1.4],
[1, 2, 6.8],
[1, 2, 6.7],
[1, 1, 5.3],
[2, 3, -1.3],
[3, 2, -3.6],
[3, 2, -0.7],
[1, 1, 5.7],
[3, 3, 2.3],
[1, 2, 3.3],
[2, 3, -0.5],
[1, 1, -2.6],
[1, 2, 3.7],
[1, 2, 0.9],
[3, 1, -1.1],
[2, 2, 2.1],
[1, 3, 4.6],
[2, 3, 4.6],
[1, 2, 5.1],
[1, 3, 0.9],
]
# Its description:
hddesc = utils.Handle()
nobs = len(dat)
levels = [3, 3, 1]
vnames = ['F1', 'F2', 'Con']
blgm.lm_describe_data(hddesc, nobs, levels, vnames=vnames)
# Generate and print the design matrix:
hxdesc = utils.Handle()
print('Design matrix:')
print(blgm.lm_design_matrix(hform, hddesc, dat, hxdesc))
# Free the blgm handles:
map(blgm.handle_free, [hform, hddesc, hxdesc])
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.REPORT_NDIFF,
).failed
)