bluesky315 at gmail.com
2010-Feb-09 23:33 UTC
[R] Model matrix using dummy regressors or deviation regressors
The model matrix for the code at the end the email is shown below. Since the model matrix doesn't have -1, I think that it is made of dummy regressors rather than deviation regressors. I'm wondering how to make a model matrix using deviation regressors. Could somebody let me know?> model.matrix(aaov)(Intercept) A2 B2 B3 A2:B2 A2:B3 1 1 0 0 0 0 0 2 1 0 0 0 0 0 3 1 0 0 0 0 0 4 1 0 0 0 0 0 5 1 1 0 0 0 0 6 1 1 0 0 0 0 7 1 1 0 0 0 0 8 1 1 0 0 0 0 9 1 0 1 0 0 0 10 1 0 1 0 0 0 11 1 0 1 0 0 0 12 1 0 1 0 0 0 13 1 1 1 0 1 0 14 1 1 1 0 1 0 15 1 1 1 0 1 0 16 1 1 1 0 1 0 17 1 0 0 1 0 0 18 1 0 0 1 0 0 19 1 0 0 1 0 0 20 1 0 0 1 0 0 21 1 1 0 1 0 1 22 1 1 0 1 0 1 23 1 1 0 1 0 1 24 1 1 0 1 0 1 attr(,"assign") [1] 0 1 2 2 3 3 attr(,"contrasts") attr(,"contrasts")$A [1] "contr.treatment" attr(,"contrasts")$B [1] "contr.treatment" ############# 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)) aaov=aov(Y ~ A * B,fr) summary(aaov) model.matrix(aaov)
David Winsemius
2010-Feb-09 23:47 UTC
[R] Model matrix using dummy regressors or deviation regressors
On Feb 9, 2010, at 6:33 PM, bluesky315 at gmail.com wrote:> The model matrix for the code at the end the email is shown below. > Since the model matrix doesn't have -1, I think that it is made of > dummy regressors rather than deviation regressors. I'm wondering how > to make a model matrix using deviation regressors. Could somebody let > me know?If I understand your terminology, then: ?contr.sum -- David.> >> model.matrix(aaov) > (Intercept) A2 B2 B3 A2:B2 A2:B3 > 1 1 0 0 0 0 0 > 2 1 0 0 0 0 0 > 3 1 0 0 0 0 0 > 4 1 0 0 0 0 0 > 5 1 1 0 0 0 0 > 6 1 1 0 0 0 0 > 7 1 1 0 0 0 0 > 8 1 1 0 0 0 0 > 9 1 0 1 0 0 0 > 10 1 0 1 0 0 0 > 11 1 0 1 0 0 0 > 12 1 0 1 0 0 0 > 13 1 1 1 0 1 0 > 14 1 1 1 0 1 0 > 15 1 1 1 0 1 0 > 16 1 1 1 0 1 0 > 17 1 0 0 1 0 0 > 18 1 0 0 1 0 0 > 19 1 0 0 1 0 0 > 20 1 0 0 1 0 0 > 21 1 1 0 1 0 1 > 22 1 1 0 1 0 1 > 23 1 1 0 1 0 1 > 24 1 1 0 1 0 1 > attr(,"assign") > [1] 0 1 2 2 3 3 > attr(,"contrasts") > attr(,"contrasts")$A > [1] "contr.treatment" > > attr(,"contrasts")$B > [1] "contr.treatment" > > > > ############# > 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)) > aaov=aov(Y ~ A * B,fr) > summary(aaov) > model.matrix(aaov) > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
John Fox
2010-Feb-10 00:09 UTC
[R] Model matrix using dummy regressors or deviation regressors
Dear bluesky315,
There are several ways in R to determine regressors associated with factors.
One way is to set the global contrasts option. To get "deviation"
regressors, use options(contrasts=c("contr.sum",
"contr.poly")), and see
?options and ?contrasts for details. Also see Section 11.1.1 of the
Introduction to R manual that comes with R.
I hope this helps,
John
--------------------------------
John Fox
Senator William McMaster
Professor of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org]
On> Behalf Of bluesky315 at gmail.com
> Sent: February-09-10 6:33 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Model matrix using dummy regressors or deviation regressors
>
> The model matrix for the code at the end the email is shown below.
> Since the model matrix doesn't have -1, I think that it is made of
> dummy regressors rather than deviation regressors. I'm wondering how
> to make a model matrix using deviation regressors. Could somebody let
> me know?
>
> > model.matrix(aaov)
> (Intercept) A2 B2 B3 A2:B2 A2:B3
> 1 1 0 0 0 0 0
> 2 1 0 0 0 0 0
> 3 1 0 0 0 0 0
> 4 1 0 0 0 0 0
> 5 1 1 0 0 0 0
> 6 1 1 0 0 0 0
> 7 1 1 0 0 0 0
> 8 1 1 0 0 0 0
> 9 1 0 1 0 0 0
> 10 1 0 1 0 0 0
> 11 1 0 1 0 0 0
> 12 1 0 1 0 0 0
> 13 1 1 1 0 1 0
> 14 1 1 1 0 1 0
> 15 1 1 1 0 1 0
> 16 1 1 1 0 1 0
> 17 1 0 0 1 0 0
> 18 1 0 0 1 0 0
> 19 1 0 0 1 0 0
> 20 1 0 0 1 0 0
> 21 1 1 0 1 0 1
> 22 1 1 0 1 0 1
> 23 1 1 0 1 0 1
> 24 1 1 0 1 0 1
> attr(,"assign")
> [1] 0 1 2 2 3 3
> attr(,"contrasts")
> attr(,"contrasts")$A
> [1] "contr.treatment"
>
> attr(,"contrasts")$B
> [1] "contr.treatment"
>
>
>
> #############
> 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))
> aaov=aov(Y ~ A * B,fr)
> summary(aaov)
> model.matrix(aaov)
>
> ______________________________________________
> 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.