[R] About the interaction A:B
blue sky
bluesky315 at gmail.com
Fri Mar 5 15:08:01 CET 2010
Suppose, 'fr' is data.frame with columns 'Y', 'A' and 'B'. 'A' has levels 'Aa'
'Ab' and 'Ac', and 'B' has levels 'Ba', 'Bb', 'Bc' and 'Bd'. 'Y'
columns are numbers.
I tried the following three sets of commands. I understand that A*B is
equivalent to A+B+A:B. However, A:B in A+B+A:B is different from A:B
just by itself (see the 3rd and 4th set of commands). Would you please
help me understand why the meanings of A:B are different in different
contexts?
I also see the coefficient of AAc:BBd is NA (the last set of
commands). I'm wondering why this coefficient is not removed from the
'coefficients' vector. Since lm(Y~A) has coefficients for (intercept),
Ab, Ac (there are no NA's), I think that it is reasonable to make sure
that there are no NA's when there are interaction terms, namely, A:B
in this case.
Thank you for answering my questions!
> alm=lm(Y ~ A*B,fr)
> alm$coefficients
(Intercept) AAb AAc BBb BBc BBd
-3.548176 -2.086586 7.003857 4.367800 11.887356 -3.470840
AAb:BBb AAc:BBb AAb:BBc AAc:BBc AAb:BBd AAc:BBd
5.160865 -11.858425 -12.853116 -20.289611 6.727401 -2.327173
>
> alm=lm(Y ~ A + B + A:B,fr)
> alm$coefficients
(Intercept) AAb AAc BBb BBc BBd
-3.548176 -2.086586 7.003857 4.367800 11.887356 -3.470840
AAb:BBb AAc:BBb AAb:BBc AAc:BBc AAb:BBd AAc:BBd
5.160865 -11.858425 -12.853116 -20.289611 6.727401 -2.327173
>
> alm=lm(Y ~ A:B - 1,fr)
> alm$coefficients
AAa:BBa AAb:BBa AAc:BBa AAa:BBb AAb:BBb AAc:BBb AAa:BBc
-3.5481765 -5.6347625 3.4556808 0.8196231 3.8939016 -4.0349449 8.3391795
AAb:BBc AAc:BBc AAa:BBd AAb:BBd AAc:BBd
-6.6005222 -4.9465744 -7.0190168 -2.3782017 -2.3423322
>
> alm=lm(Y ~ A:B,fr)
> alm$coefficients
(Intercept) AAa:BBa AAb:BBa AAc:BBa AAa:BBb AAb:BBb
-2.34233221 -1.20584424 -3.29243033 5.79801305 3.16195534 6.23623377
AAc:BBb AAa:BBc AAb:BBc AAc:BBc AAa:BBd AAb:BBd
-1.69261273 10.68151168 -4.25819000 -2.60424217 -4.67668454 -0.03586951
AAc:BBd
NA
More information about the R-help
mailing list