simplest ANCOVA ?

2,925 views
Skip to first unread message

josef...@gmail.com

unread,
Jun 29, 2010, 6:32:29 PM6/29/10
to pystat...@googlegroups.com
There is a Stackoverflow question about ANCOVA in python that hasn't
been answered in quite some time.

I was trying to figure out what ANCOVA really is, since to me it's all
just regression with dummy variables.

If my interpretation is correct, this is the simplest example.

http://stackoverflow.com/questions/2916760/ancova-in-python-with-scipy-numpy-stats

e.g.
http://faculty.vassar.edu/lowry/ch17pt1.html (too verbose for my taste)
http://en.wikipedia.org/wiki/Analysis_of_covariance (not very informative)
https://www.ats.ucla.edu/stat/SPSS/library/hetreg.htm (looks good
to match up, but I haven't gone through it yet)

Josef


Summary:
========

Relevant part of construction of design matrix
xg includes group numbers/labels,
x1 is continuous explanatory variable

>>> dummy = (xg[:,None] == np.unique(xg)).astype(float)
>>> X = np.c_[x1, dummy[:,1:], np.ones(nsample)]

Estimate the model

>>> res2 = sm.OLS(y, X).fit()
>>> print res2.params
[ 1.00901524 3.08466166 -2.84716135 9.94655423]
>>> print res2.bse
[ 0.07499873 0.71217506 1.16037215 0.38826843]
>>> prstd, iv_l, iv_u = wls_prediction_std(res2)

"Test hypothesis that all groups have same intercept"

>>> R = [[0, 1, 0, 0],
... [0, 0, 1, 0]]

>>> print res2.f_test(R)
<F test: F=array([[ 91.69986847]]), p=[[ 8.90826383e-17]],
df_denom=46, df_num=2>

strongly rejected because differences in intercept are very large

tut_ols_ancova.py
Reply all
Reply to author
Forward
0 new messages