# [R] interaction terms in formula of lm or glm

Peter Ehlers ehlers at math.ucalgary.ca
Mon Apr 17 18:16:38 CEST 2006

```Luke,

A really useful thing to do would be to read section 11.1 of
"An Introduction to R", which comes with your R installation.

Peter Ehlers

Luke wrote:
> 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 at 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 at stat.math.ethz.ch
>>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Luke
>>Sent: Monday, April 17, 2006 12:08 AM
>>To: R-help at 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 at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>http://www.R-project.org/posting-guide.html
>>
>>
>>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help