[R] In a formula, what is the interaction of the intercept and a factor?
Kenn Konstabel
lebatsnok at gmail.com
Wed Jun 1 06:49:07 CEST 2011
With some guessing: does lm(formula = y ~ -1 + group + x:group, data = dat)
do what you want? I'm not sure now 1:group is treated, if at all.
Kenn
On Tue, May 31, 2011 at 11:35 PM, Kevin Wright <kw.stat at gmail.com> wrote:
> For a pedagogical purpose, I was trying to show how the formula for a simple
> regression line (~1+x) could be crossed with a factor (~1:group + x:group)
> to fit separate regressions by group. For example:
>
> set.seed(201108)
> dat <- data.frame(x=1:15, y=1:15+rnorm(15),
> group = sample(c('A','B'), size=15,
> replace=TRUE))
>
> m1 <- lm(y~ 1 + x, data=dat)
> m2 <- lm(y ~ group + x:group, data=dat)
> m3 <- lm(y ~ 1:group + x:group, data=dat)
> m4 <- lm(y ~ 1 + x:group, data=dat)
>
> The simple regression is model m1.
>
> The usual way to write the by-group regression is model m2.
>
> In model m3 was trying to be explicitly clear and interact "1+x" with
> "group".
>
> Looking only at the coefficients, it appears that model m3 is simplified to
> model m4.
>
> R> coef(m3)
> (Intercept) groupA:x groupB:x
> 0.3775140 0.9213835 0.9879690
>
> R> coef(m4)
> (Intercept) x:groupA x:groupB
> 0.3775140 0.9213835 0.9879690
>
> I wonder if anyone can shed some light on what R is doing with the "1:group"
> term.
>
> Kevin
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list