This describes the way in which categorical variables are coded in
the model. In this case, we see "treatment" coding,
although this can go by different names in textbooks (for example, reference
cell coding).

attr(,"assign")
[1] 0 1 2 2

means that the first column of the model matrix corresponds to the
intercept,
the second column corresponds to the first variable A,
and the third and fourth columns correspond to the second variable B.

A has 2 levels so it can be represented by 1 column.
Similarly, B has 3 levels so it is represented by 2 columns.
For variable A, the regression coefficient represents an estimated contrast
(difference)
between the two levels when B is held constant.

On Thu, Sep 17, 2009 at 11:13 AM, Peng Yu <pengyu...@gmail.com> wrote:

> Hi,
>
> I don't understand what the meaning of the following lines returned by
> model.matrix(). Can somebody help me understand it? What can they be
> used for?
>
> attr(,"assign")
> [1] 0 1 2 2
> attr(,"contrasts")
> attr(,"contrasts")$A
> [1] "contr.treatment"
>
> attr(,"contrasts")$B
> [1] "contr.treatment"
>
> Regards,
> Peng
>
> > a=2
> > b=3
> > n=4
> > A = rep(sapply(1:a,function(x){rep(x,n)}),b)
> > B = as.vector(sapply(sapply(1:b, function(x){rep(x,n)}),
> function(x){rep(x,a)}))
> > Y = A + B + rnorm(a*b*n)
> > fr = data.frame(Y=Y,A=as.factor(A),B=as.factor(B))
> > afit=aov(Y ~ A + B,fr)
> > model.matrix(afit)
>   (Intercept) A2 B2 B3
> 1            1  0  0  0
> 2            1  0  0  0
> 3            1  0  0  0
> 4            1  0  0  0
> 5            1  1  0  0
> 6            1  1  0  0
> 7            1  1  0  0
> 8            1  1  0  0
> 9            1  0  1  0
> 10           1  0  1  0
> 11           1  0  1  0
> 12           1  0  1  0
> 13           1  1  1  0
> 14           1  1  1  0
> 15           1  1  1  0
> 16           1  1  1  0
> 17           1  0  0  1
> 18           1  0  0  1
> 19           1  0  0  1
> 20           1  0  0  1
> 21           1  1  0  1
> 22           1  1  0  1
> 23           1  1  0  1
> 24           1  1  0  1
> attr(,"assign")
> [1] 0 1 2 2
> attr(,"contrasts")
> attr(,"contrasts")$A
> [1] "contr.treatment"
>
> attr(,"contrasts")$B
> [1] "contr.treatment"
>
> ______________________________________________
> R-help@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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to