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. b=g1(?) does what? 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. Mary A. Marion [[alternative HTML version deleted]]
1. You need to learn to use R Help. It is there for a purpose. help (help) ## or ?help is where to start. 2. Before posting further, please read "An Introduction to R." Ships with every distro. 3. ?gl 4. This is not the best way to do this anyway. dd <- expand.grid(b=1:4,a=1:3) ## is preferable imho 5. A matrix is not a data frame. If you do not understand the difference between these 2 structures and do not wish to spend the time to learn, please stop using R. 6. You cannot "reparametrize in terms of 1's 0's and -1's." b has 4 levels. Cheers, Bert On Tue, Jul 3, 2012 at 1:10 PM, <mmstat@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. > b=g1(?) does what? > > 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. > > Mary A. Marion > > > > > [[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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
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
On 03/07/2012 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. > b=g1(?) does what?That is gl, not g1 (i.e. gee ell not gee one). See ?gl for a description. Duncan Murdoch> > 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. > > Mary A. Marion > > > > > [[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.