I am using the clever formula notation of R to first do an OLS. E.g. I say m <- lm(y ~ x + f) where f is a factor, and R automatically constructs the dummy variables. Very nice. I need to then go on to do some other ML estimation using the same design matrix that's used for the OLS. I could, of course, do this manually. But it seems that lm() has done all this hard work. I wonder if there's a way to ask him nicely so as to get it. :-) -- Ajay Shah http://www.mayin.org/ajayshah ajayshah at mayin.org http://ajayshahblog.blogspot.com <*(:-? - wizard who doesn't know the answer.
?model.matrix On Tue, 9 Oct 2007, Ajay Shah wrote:> I am using the clever formula notation of R to first do an OLS. E.g. I > say > > m <- lm(y ~ x + f) > > where f is a factor, and R automatically constructs the dummy > variables. Very nice. > > I need to then go on to do some other ML estimation using the same > design matrix that's used for the OLS. I could, of course, do this > manually. But it seems that lm() has done all this hard work. I wonder > if there's a way to ask him nicely so as to get it. :-) > >-- 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
On Tue, 2007-10-09 at 20:04 +0530, Ajay Shah wrote:> I am using the clever formula notation of R to first do an OLS. E.g. I > say > > m <- lm(y ~ x + f) > > where f is a factor, and R automatically constructs the dummy > variables. Very nice. > > I need to then go on to do some other ML estimation using the same > design matrix that's used for the OLS. I could, of course, do this > manually. But it seems that lm() has done all this hard work. I wonder > if there's a way to ask him nicely so as to get it. :-)model.matrix()> f <- factor(sample(1:10, 100, replace = TRUE)) > x <- rnorm(100) > y <- rnorm(100) > m <- lm(y ~ x + f) > model.matrix(m)(Intercept) x f2 f3 f4 f5 f6 f7 f8 f9 f10 1 1 1.8256930313 0 0 0 0 0 1 0 0 0 2 1 0.5129526275 0 0 0 1 0 0 0 0 0 3 1 0.6791212194 0 0 1 0 0 0 0 0 0 4 1 2.4711715848 0 0 0 0 0 0 1 0 0 5 1 0.5646964940 0 1 0 0 0 0 0 0 0 6 1 0.2640087965 0 0 0 0 1 0 0 0 0 ###<snip /> You can also do this directly without the lm step: model.matrix(y ~ x + f) HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
If all you're doing is fitting different responses to the same X data, then you don't need model.matrix. See ?update, ?update.formula. Bert Gunter Genentech Nonclinical Statistic -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Ajay Shah Sent: Tuesday, October 09, 2007 7:34 AM To: R-help Subject: [R] How do I obtain the design matrix of an lm()? I am using the clever formula notation of R to first do an OLS. E.g. I say m <- lm(y ~ x + f) where f is a factor, and R automatically constructs the dummy variables. Very nice. I need to then go on to do some other ML estimation using the same design matrix that's used for the OLS. I could, of course, do this manually. But it seems that lm() has done all this hard work. I wonder if there's a way to ask him nicely so as to get it. :-) -- Ajay Shah http://www.mayin.org/ajayshah ajayshah at mayin.org http://ajayshahblog.blogspot.com <*(:-? - wizard who doesn't know the answer. ______________________________________________ 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.