Re: [R] interaction terms in formula of lm or glm

From: Christos Hatzis <christos_at_silicoinsights.com>
Date: Tue 18 Apr 2006 - 02:08:00 EST


Yes, I think these two are equivalent. You ran into problems with V3:V3 because in R the form X:X is considered the same as X (see the MASS4 book for additional info, if you have it handy).  

In any case, you can check the models yourself by checking the corresponding model matrices:  

model.matrix(y~1+v2:v3)
model.matrix(y~1+I(v2*v3)  

This will show you how the formula is interpreted to built the model matrix for the linear model.  

-Christos


From: Luke [mailto:jyzz88@gmail.com]
Sent: Monday, April 17, 2006 11:46 AM
To: christos@silicoinsights.com
Cc: R-help@stat.math.ethz.ch
Subject: Re: [R] interaction terms in formula of lm or glm

Thanks, Christos. Another relevant question:

If I want to include the interaction term consisting of V2 and V3 (they are numeric vectors), should I use:

y ~ 1 + V2:V3 or y ~ 1 + I(V2*V3)

or both are good?

-Luke

On 4/17/06, Christos Hatzis <christos@silicoinsights.com> wrote:

If you want the quadratic term, you need to pass it as an argument in function I():

y ~ 1 + V1 + V3 + I(V3^2)

This is documented in ?formula

-Christos

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Luke Sent: Monday, April 17, 2006 12:08 AM
To: R-help@stat.math.ethz.ch
Subject: [R] interaction terms in formula of lm or glm

I would like to include pairwise interaction terms for lm(). For example, I want to include the quadratic term of variable "V3".

> my.formula

y ~ 1 + V1 + V3 + V3:V3

> my.data

   y V1 V2 V3
1 31 1 42 140
2 32 0 43 120
3 33 0 57 150
4 34 0 55 132

> foo <- lm(my.formula, data = my.data)

> foo$coefficients

(Intercept) V1 V3
29.47368421 -2.15789474 0.02631579

Why do the foo coefficients not include V3:V3 ?

I thought that the variable "V3:V3" has the values of squares of V3 elements, that is,
V3:V3
140*140
120*120
....
Am I wrong?

If I specify a fouth varaible "V4" with square values of V3, and the formula is y ~ 1 + V1 + V3 + V4, it seems that the foo will give me different in and

out-sample predictions.

-Luke

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html <http://www.R-project.org/posting-guide.html>

        [[alternative HTML version deleted]]



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Apr 18 02:11:28 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 18 Apr 2006 - 04:10:03 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.