Hello, I'm trying to extract the independent variables from a formula. The closest I've been able to come, aside from rolling my own, is the following:> a = y ~ b * x > attr(terms(formula(a)),"variables")The reason I'm doing this is that I'm building a grid of points that I use to construct a 3-d model prediction surface in rgl. If there are more than two independent variables, I need to set the other ones to 0 in the dataframe for the predict() routine. In order to do that, I need to know what those variables are. I suspect there's a better way to go about doing what I'm doing (perhaps using the model.* family of functions). But, in the meantime, just extracting the independent variables will move me forward. Thanks, Allie
Hi, On Wed, Nov 7, 2012 at 5:03 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:> Hello, > > I'm trying to extract the independent variables from a formula.This is somewhat ambiguous. Do you want the actual columns of values? The quoted variable names? Including the interaction term or not? If you just want the quoted column names names(as.quoted(a))[-1] will do it. If you want to include the interaction term you can use labels(terms(a))> closest I've been able to come, aside from rolling my own, is the following: > >> a = y ~ b * x >> attr(terms(formula(a)),"variables")a is already a formula, so you can shorten this to attr(terms(a),"variables") Hope this helps! Ista> > The reason I'm doing this is that I'm building a grid of points that I > use to construct a 3-d model prediction surface in rgl. If there are > more than two independent variables, I need to set the other ones to 0 > in the dataframe for the predict() routine. In order to do that, I need > to know what those variables are. > > I suspect there's a better way to go about doing what I'm doing (perhaps > using the model.* family of functions). But, in the meantime, just > extracting the independent variables will move me forward. > > Thanks, > Allie > > ______________________________________________ > 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.
On Nov 7, 2012, at 4:03 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:> Hello, > > I'm trying to extract the independent variables from a formula. The > closest I've been able to come, aside from rolling my own, is the following: > >> a = y ~ b * x >> attr(terms(formula(a)),"variables") > > The reason I'm doing this is that I'm building a grid of points that I > use to construct a 3-d model prediction surface in rgl. If there are > more than two independent variables, I need to set the other ones to 0 > in the dataframe for the predict() routine. In order to do that, I need > to know what those variables are. > > I suspect there's a better way to go about doing what I'm doing (perhaps > using the model.* family of functions). But, in the meantime, just > extracting the independent variables will move me forward. > > Thanks, > AllieYou might want to look at ?all.vars as one possible approach. The nice thing about all.vars() is that if you have transformations on the IV's in the formula (eg. poly(x, 3)), you will get the base variable names (eg. 'x', rather than 'poly(x, 3)'). A general approach would be: all.vars(formula(MODEL)) where the response variable would typically be the first element in the returned vector. So, if you just want the IV's you could use: all.vars(formula(MODEL))[-1] An example: # from ?cars fm1 <- lm(log(dist) ~ log(speed), data = cars)> all.vars(formula(fm1))[1] "dist" "speed" Regards, Marc Schwartz
...perhaps ?all.vars as in> form <- y~x+z > all.vars(form)[1] "y" "x" "z"> all.vars(form)[-1][1] "x" "z" -- Bert On Wed, Nov 7, 2012 at 2:03 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:> Hello, > > I'm trying to extract the independent variables from a formula. The > closest I've been able to come, aside from rolling my own, is the following: > >> a = y ~ b * x >> attr(terms(formula(a)),"variables") > > The reason I'm doing this is that I'm building a grid of points that I > use to construct a 3-d model prediction surface in rgl. If there are > more than two independent variables, I need to set the other ones to 0 > in the dataframe for the predict() routine. In order to do that, I need > to know what those variables are. > > I suspect there's a better way to go about doing what I'm doing (perhaps > using the model.* family of functions). But, in the meantime, just > extracting the independent variables will move me forward. > > Thanks, > Allie > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm