How to write out patsy formulas?

32 views
Skip to first unread message

saroele

unread,
Feb 9, 2015, 5:08:22 PM2/9/15
to pystat...@googlegroups.com
Hi all,

I have a basic question. If I have a fitted ols model with this formula  y ~ x1 * x2
(I know from the docs that this is the same as y ~ x1 + x2 + x1:x2)

The fit has 4 parameters, intercept and p1,p2, p3.  My question is how to write the mathematical equation of my model.  Something like:

y = intercept + p1*x1 + p2*x2 + p3 ??

Thanks!

Nathaniel Smith

unread,
Feb 9, 2015, 5:33:53 PM2/9/15
to pystatsmodels
[Trying again because Google is horrible and has decided to start
randomly rejecting my emails.]

---------- Forwarded message ----------
From: Nathaniel Smith <n...@pobox.com>
Date: Mon, Feb 9, 2015 at 2:30 PM
Subject: Re: [pystatsmodels] How to write out patsy formulas?
To: pystatsmodels <pystat...@googlegroups.com>
It depends on whether x1 and x2 are continuous or categorical.
Assuming they're continuous (since you say you end up with 4
parameters), then x1:x2 is just the pointwise product of the two
values, so

y = intercept + p1*x1 + p2*x2 + p3 * x1 * x2

so it's a kind of quadratic term. You can think of it as saying that
the slope of the x1 effect is allowed to vary linearly with the value
of x2, or vice-versa.

If you want to know how such things work in the fully general case,
there's some extremely detailed docs here:
http://patsy.readthedocs.org/en/latest/formulas.html

-n

--
Nathaniel J. Smith -- http://vorpus.org

--
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org

saroele

unread,
Feb 9, 2015, 5:41:37 PM2/9/15
to pystat...@googlegroups.com
Thanks Nathaniel, I hoped it was that simple, but I could nowhere find evidence of that. 
Even the documentation you refer to does not explain what a:b really is.  At least not for non-experts in the field :-)

Thanks,
Roel

Nathaniel Smith

unread,
Feb 9, 2015, 6:20:43 PM2/9/15
to pystatsmodels

[Grrrrr.]

---------- Forwarded message ----------
From: "Nathaniel Smith" <n...@pobox.com>
Date: 9 Feb 2015 15:19
Subject: Re: [pystatsmodels] How to write out patsy formulas?
To: "pystatsmodels" <pystat...@googlegroups.com>
Cc:

It is there, but definitely technical, yes :-). Specifically:

"A numerical factor represents the vector space spanned by its columns."

and

"An interaction between two factors represents the vector space spanned by the element-wise products between vectors in the first factor’s space with vectors in the second factor’s space."

Which is a bit obfuscated, but has the advantage of remaining correct even when you go from numerical interactions to categorical interactions with complicated coding rules.

-n

Roel De Coninck

unread,
Feb 10, 2015, 8:45:26 AM2/10/15
to pystat...@googlegroups.com
Indeed.  Somehow, this did not make sense to me before your answer :-)

Reply all
Reply to author
Forward
0 new messages