[R] Design matrix not identity

Liaw, Andy andy_liaw at merck.com
Wed May 12 18:45:29 CEST 2004


I don't think the design matrix and the incidence matrix are the same thing.
A design matrix is the X matrix in a usual linear model, which would
(usually) include the column of ones, and the contrasts.  So model.matrix()
is the right thing for a design matrix.

For incidence matrix, you can try:

> f <- as.factor(c(1,1,2,2,3,3,3))
> sapply(levels(f), function(x) as.numeric(f == x))
     1 2 3
[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


Andy

> From: Patrik Waldmann
> 
> 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###########################################
> 
> This message has been scanned by F-Secure 
> Anti-Virus for Microsoft Exchange.
> 
> ###########################################
> 
> ______________________________________________
> R-help at 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
> 
>




More information about the R-help mailing list