Re: [R] Design matrix not identity

About this list Date view Thread view Subject view Author view Attachment view

From: Sundar Dorai-Raj (sundar.dorai-raj@pdf.com)
Date: Thu 13 May 2004 - 02:44:03 EST


Message-id: <40A25453.6050902@pdf.com>


Patrik Waldmann wrote:

> Hello again,
>
> I was too quick before. What I was looking for was a function that
> constructs the design (or incidence) matrix (X in a linear model) from a
> factor. Uwe Ligges suggested using model.matrix and this does almost what I
> want, but it is first necessary to construct a data variable. It also asigns
> ones to all rows of the first column (because this is set to be the
> contrast, not really what I want - see below). Maybe time for a function
> that just converts a factor into a design matrix?
>
> I have a factor
> factor<-as.factor(c(1,1,2,2,3,3,3))
>
> and I want a matrix
> 1 0 0
> 1 0 0
> 0 1 0
> 0 1 0
> 0 0 1
> 0 0 1
> 0 0 1
>
>
> Patrik Waldmann###########################################

model.matrix will do this for you.

R> fac <- as.factor(c(1, 1, 2, 2, 3, 3, 3))
R> model.matrix(~ fac - 1)
   fac1 fac2 fac3
1 1 0 0
2 1 0 0
3 0 1 0
4 0 1 0
5 0 0 1
6 0 0 1
7 0 0 1
attr(,"assign")
[1] 1 1 1
attr(,"contrasts")
attr(,"contrasts")$fac
[1] "contr.treatment"

The "-1" drops the intercept.

Is this what you need?

--sundar

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:09 EST