~scipystats/+junk/skipper-revjp

« back to all changes in this revision

Viewing changes to nipy/fixes/scipy/stats/models/examples/ols_fstat.py

  • Committer: joep
  • Date: 2009-08-16 14:16:08 UTC
  • Revision ID: josef.pktd@gmail.com-20090816141608-fopiso4rhy2zcwd9
add example for Ftest (Fcontrast) for ols

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""examples for usage of F-test on linear restrictions in OLS
 
2
 
 
3
linear restriction is R \beta = 0
 
4
R is (nr,nk), beta is (nk,1) (in matrix notation)
 
5
"""
 
6
 
 
7
import numpy as np
 
8
import numpy.testing as npt
 
9
from models.datasets.longley.data import load
 
10
from models.regression import OLS
 
11
from models import tools
 
12
 
 
13
data = load()
 
14
data.exog = tools.add_constant(data.exog)
 
15
res = OLS(data.endog, data.exog).fit()
 
16
 
 
17
# test pairwise equality of some coefficients
 
18
R = [[0,1,-1,0,0,0,0],[0, 0, 0, 0, 1, -1, 0]]
 
19
Ftest = res.Fcontrast(R)
 
20
print repr((Ftest.F, Ftest.p_val)) #use repr to get more digits
 
21
# 9.740461873303655 0.0056052885317360301
 
22
 
 
23
##Compare to R (after running R_lm.s in the longley folder) looks good.
 
24
##
 
25
##> library(car)
 
26
##> linear.hypothesis(m1, c("GNP = UNEMP","POP = YEAR"))
 
27
##Linear hypothesis test
 
28
##
 
29
##Hypothesis:
 
30
##GNP - UNEMP = 0
 
31
##POP - YEAR = 0
 
32
##
 
33
##Model 1: TOTEMP ~ GNPDEFL + GNP + UNEMP + ARMED + POP + YEAR
 
34
##Model 2: restricted model
 
35
##
 
36
## Res.Df      RSS Df Sum of Sq      F   Pr(>F)
 
37
##1      9   836424
 
38
##2     11  2646903 -2  -1810479 9.7405 0.005605 **
 
39
 
 
40
# test all variables have zero effect
 
41
R = np.eye(7)[:-1,:]
 
42
Ftest0 = res.Fcontrast(R)
 
43
print repr((Ftest0.F, Ftest0.p_val))
 
44
print '%r' % res.F
 
45
npt.assert_almost_equal(res.F, Ftest0.F, decimal=10)
 
46
# values differ in 11th decimal
 
 
b'\\ No newline at end of file'