Exemple #1
0
 def test_add(self):
     t1 = formula.Term("t1")
     t2 = formula.Term("t2")
     f = t1 + t2
     self.assert_(isinstance(f, formula.Formula))
     self.assert_(f.hasterm(t1))
     self.assert_(f.hasterm(t2))
Exemple #2
0
    def test_init(self):
        t1 = formula.Term("trivial")
        sqr = lambda x: x*x

        t2 = formula.Term("not_so_trivial", sqr, "sqr")

        self.assertRaises(ValueError, formula.Term, "name", termname=0)
Exemple #3
0
 def test_termcolumns(self):
     t1 = formula.Term("A")
     t2 = formula.Term("B")
     f = t1 + t2 + t1 * t2
     def other(val):
         return np.array([3.2*val,4.342*val**2, 5.234*val**3])
     q = formula.Quantitative(['other%d' % i for i in range(1,4)], termname='other', func=t1, transform=other)
     f += q
     q.namespace = f.namespace = self.formula.namespace
     assert_almost_equal(q(), f()[f.termcolumns(q)])
Exemple #4
0
    def test_namespace(self):
        space1 = {'X':np.arange(50), 'Y':np.arange(50)*2}
        space2 = {'X':np.arange(20), 'Y':np.arange(20)*2}
        space3 = {'X':np.arange(30), 'Y':np.arange(30)*2}
        X = formula.Term('X')
        Y = formula.Term('Y')

        X.namespace = space1
        assert_almost_equal(X(), np.arange(50))

        Y.namespace = space2
        assert_almost_equal(Y(), np.arange(20)*2)

        f = X + Y

        f.namespace = space1
        self.assertEqual(f().shape, (2,50))
        assert_almost_equal(Y(), np.arange(20)*2)
        assert_almost_equal(X(), np.arange(50))

        f.namespace = space2
        self.assertEqual(f().shape, (2,20))
        assert_almost_equal(Y(), np.arange(20)*2)
        assert_almost_equal(X(), np.arange(50))

        f.namespace = space3
        self.assertEqual(f().shape, (2,30))
        assert_almost_equal(Y(), np.arange(20)*2)
        assert_almost_equal(X(), np.arange(50))

        xx = X**2
        self.assertEqual(xx().shape, (50,))

        xx.namespace = space3
        self.assertEqual(xx().shape, (30,))

        xx = X * formula.I
        self.assertEqual(xx().shape, (50,))
        xx.namespace = space3
        self.assertEqual(xx().shape, (30,))

        xx = X * X
        self.assertEqual(xx.namespace, X.namespace)

        xx = X + Y
        self.assertEqual(xx.namespace, {})

        Y.namespace = {'X':np.arange(50), 'Y':np.arange(50)*2}
        xx = X + Y
        self.assertEqual(xx.namespace, {})

        Y.namespace = X.namespace
        xx = X+Y
        self.assertEqual(xx.namespace, Y.namespace)
Exemple #5
0
    def test_mul(self):
        t1 = formula.Term("t1")
        t2 = formula.Term("t2")
        f = t1 * t2
        self.assert_(isinstance(f, formula.Formula))

        intercept = formula.Term("intercept")
        f = t1 * intercept
        self.assertEqual(str(f), str(formula.Formula(t1)))

        f = intercept * t1
        self.assertEqual(str(f), str(formula.Formula(t1)))
Exemple #6
0
    def test_interactions(self):

        f = formula.interactions([formula.Term(l) for l in ['a', 'b', 'c']])
        assert_equal(set(f.termnames()), set(['a', 'b', 'c', 'a*b', 'a*c', 'b*c']))

        f = formula.interactions([formula.Term(l) for l in ['a', 'b', 'c', 'd']], order=3)
        assert_equal(set(f.termnames()), set(['a', 'b', 'c', 'd', 'a*b', 'a*c', 'a*d', 'b*c', 'b*d', 'c*d', 'a*b*c', 'a*c*d', 'a*b*d', 'b*c*d']))

        f = formula.interactions([formula.Term(l) for l in ['a', 'b', 'c', 'd']], order=[1,2,3])
        assert_equal(set(f.termnames()), set(['a', 'b', 'c', 'd', 'a*b', 'a*c', 'a*d', 'b*c', 'b*d', 'c*d', 'a*b*c', 'a*c*d', 'a*b*d', 'b*c*d']))

        f = formula.interactions([formula.Term(l) for l in ['a', 'b', 'c', 'd']], order=[3])
        assert_equal(set(f.termnames()), set(['a*b*c', 'a*c*d', 'a*b*d', 'b*c*d']))
Exemple #7
0
 def test_contrast2(self):
     dummy = formula.Term('zero')
     self.namespace['zero'] = np.zeros((40,), np.float64)
     term = dummy + self.terms[2]
     c = contrast.Contrast(term, self.formula)
     test = [0]*2 + [1] + [0]*7
     assert_almost_equal(c.matrix, test)
Exemple #8
0
    def test_subtract(self):
        f = formula.interactions([formula.Term(l) for l in ['a', 'b', 'c']])
        ff = f - f['a*b']
        assert_equal(set(ff.termnames()), set(['a', 'b', 'c', 'a*c', 'b*c']))

        ff = f - f['a*b'] - f['a*c']
        assert_equal(set(ff.termnames()), set(['a', 'b', 'c', 'b*c']))

        ff = f - (f['a*b'] + f['a*c'])
        assert_equal(set(ff.termnames()), set(['a', 'b', 'c', 'b*c']))
Exemple #9
0
    def test_contrast3(self):
        X = self.formula.design()
        P = np.dot(X, L.pinv(X))

        dummy = formula.Term('noise')
        resid = np.identity(40) - P
        self.namespace['noise'] = np.transpose(np.dot(resid, R.standard_normal((40,5))))
        terms = dummy + self.terms[2]
        terms.namespace = self.formula.namespace
        c = contrast.Contrast(terms, self.formula)
        self.assertEquals(c.matrix.shape, (10,))
Exemple #10
0
    def setUp(self):
        self.X = R.standard_normal((40,10))
        self.namespace = {}
        self.terms = []
        for i in range(10):
            name = '%s' % string.uppercase[i]
            self.namespace[name] = self.X[:,i]
            self.terms.append(formula.Term(name))

        self.formula = self.terms[0]
        for i in range(1, 10):
            self.formula += self.terms[i]
        self.formula.namespace = self.namespace
Exemple #11
0
import string
import numpy as np

from scikits.statsmodels.sandbox import formula
from scikits.statsmodels.sandbox import contrast_old as contrast

#Example: Formula from tests, setup

X = np.random.standard_normal((40, 10))
namespace = {}
terms = []
for i in range(10):
    name = '%s' % string.uppercase[i]
    namespace[name] = X[:, i]
    terms.append(formula.Term(name))

form = terms[0]
for i in range(1, 10):
    form += terms[i]
form.namespace = namespace
form.design().shape
(40, 10)
'''
>>> dir(form)
['_Formula__namespace', '__add__', '__call__', '__class__',
'__delattr__', '__dict__', '__doc__', '__getattribute__',
'__getitem__', '__hash__', '__init__', '__module__', '__mul__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__str__', '__sub__', '__weakref__', '_del_namespace',
'_get_namespace', '_names', '_set_namespace', '_termnames',
Exemple #12
0
 def test_str(self):
     t = formula.Term("name")
     s = str(t)