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. ###########################################
Patrik Waldmann <Patrik.Waldmann at genfys.slu.se> writes:> 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))That could get you in trouble by masking the factor() function...> 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 1f <- factor(c(1,1,2,2,3,3,3)) model.matrix(~f-1) Or, a different approach: diag(3)[f,] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
f<-as.factor(c(1,1,2,2,3,3,3)) model.matrix(~f-1) Patrik Waldmann <Patrik.Waldmann <at> genfys.slu.se> writes: : : 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 : :
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
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 > >
On Wed, 12 May 2004, Patrik Waldmann wrote:> 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.Eh? You have to construct the factor, and nothing else.> 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?Uwe was quite correct, and you were still too quick. [Don't call an object after an R function. Let's use a sensible name like `f'.] f <- as.factor(c(1,1,2,2,3,3,3)) model.matrix(~ 0 + f) or diag(nlevels(f))[f,] gives what you illustrate.> 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> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlPLEASE do. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Dear Patrick, Try model.matrix(~ factor - 1). I hope this helps, John> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Patrik Waldmann > Sent: Wednesday, May 12, 2004 11:34 AM > To: 'r-help at stat.math.ethz.ch' > Subject: [R] Design matrix not identity > > 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########################################### >