Message-ID: <2C91EF72-F613-41D9-ADBB-6C22303FC533@comcast.net>
Date: 2012-07-03T20:38:40Z
From: David Winsemius
Subject: design matrix creation in R
In-Reply-To: <651619790.763507.1341346227715.JavaMail.root@sz0115a.westchester.pa.mail.comcast.net>
On Jul 3, 2012, at 4:10 PM, mmstat at comcast.net wrote:
>
> Hello,
>
> I want to create a design matrix using R. Can you explain the code
> which creates the following please? I understand the first part.
What first part if the next part is ... this ?
> b=g1(?) does what?
That's just generating a factor variable.
>
> dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way
> dd
> a b
> 1 1 1
> 2 1 2
> 3 1 3
> 4 1 4
> 5 2 1
> 6 2 2
> 7 2 3
> 8 2 4
> 9 3 1
> 10 3 2
> 11 3 3
> 12 3 4
>
> I am using the tree dataset in R. I want to form a reparameterized
> design matrix in ones, zeroes and minus ones. The dataframe dd is
> very important here.
>
> Can anyone assist here? Thanks in advance.
Something like this?
model.matrix(~a+b, data= dd, contrasts = list(a="contr.sum",
b="contr.sum") )
(Intercept) a1 a2 b1 b2 b3
1 1 1 0 1 0 0
2 1 1 0 0 1 0
3 1 1 0 0 0 1
4 1 1 0 -1 -1 -1
5 1 0 1 1 0 0
6 1 0 1 0 1 0
7 1 0 1 0 0 1
8 1 0 1 -1 -1 -1
9 1 -1 -1 1 0 0
10 1 -1 -1 0 1 0
11 1 -1 -1 0 0 1
12 1 -1 -1 -1 -1 -1
attr(,"assign")
[1] 0 1 1 2 2 2
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.sum"
attr(,"contrasts")$b
[1] "contr.sum"
--
David Winsemius, MD
West Hartford, CT