What is a simple way to convert a factor into a matrix of dummy variables? fm<-lm(y~f) where f is a factor takes care of this in the estimation. I'd like to save the result of expanding f into a matrix for later use. Thanks. Charles -- Charles H. Franklin Professor, Political Science University of Wisconsin, Madison franklin at polisci.wisc.edu chfrankl at wisc.edu 608-263-2022 (voice) 608-265-2663 (fax)
See ?model.matrix. HTH, Andy From: Charles H. Franklin> > What is a simple way to convert a factor into a matrix of > dummy variables? > > fm<-lm(y~f) > > where f is a factor takes care of this in the estimation. I'd like to > save the result of expanding f into a matrix for later use. > > Thanks. > > Charles > > -- > > > Charles H. Franklin > Professor, Political Science > University of Wisconsin, Madison > franklin at polisci.wisc.edu > chfrankl at wisc.edu > 608-263-2022 (voice) > 608-265-2663 (fax) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
Dear Charles, Try model.matrix(~f)[,-1]. Regards, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > Charles H. Franklin > Sent: Tuesday, December 06, 2005 3:03 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Matrix of dummy variables from a factor > > What is a simple way to convert a factor into a matrix of > dummy variables? > > fm<-lm(y~f) > > where f is a factor takes care of this in the estimation. I'd > like to save the result of expanding f into a matrix for later use. > > Thanks. > > Charles > > -- > > > Charles H. Franklin > Professor, Political Science > University of Wisconsin, Madison > franklin at polisci.wisc.edu > chfrankl at wisc.edu > 608-263-2022 (voice) > 608-265-2663 (fax) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Charles H. Franklin wrote:> What is a simple way to convert a factor into a matrix of dummy variables? > > fm<-lm(y~f)model.matrix(y~f) Uwe Ligges> where f is a factor takes care of this in the estimation. I'd like to > save the result of expanding f into a matrix for later use. > > Thanks. > > Charles >
Good point. Probably not for any subsequent use in R. But, I work in an org that uses SAS, HLM, SPSS, among others. As a part of our control processes at times we replicate analyses in different software programs. For instance, we will replicate lmer functions using HLM. But, HLM requires a matrix of dummy variables. So, I have used R to create this matrix for use in another software package that does need it. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Berton Gunter Sent: Tuesday, December 06, 2005 4:27 PM To: 'Liaw, Andy'; 'Charles H. Franklin'; r-help at stat.math.ethz.ch Subject: Re: [R] Matrix of dummy variables from a factor But note: There are (almost?) no situations in R where the dummy variables coding is needed. The coding is (almost?) always handled properly by the modeling functions themselves. Question: Can someone provide a "straightforward" example where the dummy variable coding **is** explicitly needed? -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy > Sent: Tuesday, December 06, 2005 12:30 PM > To: 'Charles H. Franklin'; r-help at stat.math.ethz.ch > Subject: Re: [R] Matrix of dummy variables from a factor > > See ?model.matrix. > > HTH, > Andy > > From: Charles H. Franklin > > > > What is a simple way to convert a factor into a matrix of dummy > > variables? > > > > fm<-lm(y~f) > > > > where f is a factor takes care of this in the estimation. > I'd like to > > save the result of expanding f into a matrix for later use. > > > > Thanks. > > > > Charles > > > > -- > > > > > > Charles H. Franklin > > Professor, Political Science > > University of Wisconsin, Madison > > franklin at polisci.wisc.edu > > chfrankl at wisc.edu > > 608-263-2022 (voice) > > 608-265-2663 (fax) > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
_______________________________________________________________________________________ Interestingly, for the project I'm working on I actually do call model.matrix directly. The project is profit optimization for insurance, where you try to maximize returns as a function of your premium formula. The premium formula itself is essentially the output from a regression model, so the parameters are coefficients for various rating factors. To do the optimization, I create the model matrix of dummy variables corresponding to the individual parameters, and use that in the objective function. Of course, this really amounts to writing my own modelling function, so I'm not sure if it counts as "straightforward". PS. Have my messages suddenly had word wrap turned off? Just noticed this in my recent posts to the list. -- Hong Ooi Senior Research Analyst, IAG Limited 388 George St, Sydney NSW 2000 +61 (2) 9292 1566 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Berton Gunter Sent: Wednesday, 7 December 2005 8:27 AM To: 'Liaw, Andy'; 'Charles H. Franklin'; r-help at stat.math.ethz.ch Subject: Re: [R] Matrix of dummy variables from a factor But note: There are (almost?) no situations in R where the dummy variables coding is needed. The coding is (almost?) always handled properly by the modeling functions themselves. Question: Can someone provide a "straightforward" example where the dummy variable coding **is** explicitly needed? -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy > Sent: Tuesday, December 06, 2005 12:30 PM > To: 'Charles H. Franklin'; r-help at stat.math.ethz.ch > Subject: Re: [R] Matrix of dummy variables from a factor > > See ?model.matrix. > > HTH, > Andy > > From: Charles H. Franklin > > > > What is a simple way to convert a factor into a matrix of > > dummy variables? > > > > fm<-lm(y~f) > > > > where f is a factor takes care of this in the estimation. > I'd like to > > save the result of expanding f into a matrix for later use. > > > > Thanks. > > > > Charles > > > > -- > >_______________________________________________________________________________________ The information transmitted in this message and its attachme...{{dropped}}