Jacob Wegelin
2009-Jul-26 19:14 UTC
[R] obtain names of variables and data from glm object
Suppose we have some glm object such as: myglm <- glm( y ~ x, data=DAT) Is there an elegant way--or the "right way" within the R way of thinking--to obtain the names of the response variable, the predictor variables, and the dataset, as character strings? For instance, suppose the "right way" was to use the (currently fictitious) functions theresponse(), thepredictors(), and theDataSet(). Then I would be able to write a function that obtains the names and subsequently pastes text along the following lines: theResponse <- theresponse( myglm ) theFirstPredictor <- thepredictors( myglm )[1] theDataSet <- theDataSet(myglm) title(main= paste(theResponse, " is the response and ", theFirstPredictor, " is the only predictor") In reality, I can of course extract> formula(myglm)y ~ x but I see no elegant way to extract the names of the predictor and response from this object. The deparse() function doesn't quite solve this problem:> deparse(formula(myglm))[1] "y ~ x"> deparse(formula(myglm)[2])[1] "y()"> deparse(formula(myglm)[3])[1] "x()" Ideally the elegant method would, in this example, return the character strings "x", "y", and "DAT". Thanks for any insights. Jake Jacob A. Wegelin Assistant Professor Department of Biostatistics Virginia Commonwealth University 730 East Broad Street Room 3006 P. O. Box 980032 Richmond VA 23298-0032 U.S.A. E-mail: jwegelin@vcu.edu URL: http://www.people.vcu.edu/~jwegelin [[alternative HTML version deleted]]
Gabor Grothendieck
2009-Jul-26 19:21 UTC
[R] obtain names of variables and data from glm object
Try this: g <- glm(demand ~ Time, BOD, family = gaussian) all.vars(formula(g)) The result will be a character vector whose 1st component is the name of the response and whose subsequent components are the names of the predictor variables. On Sun, Jul 26, 2009 at 3:14 PM, Jacob Wegelin<jacob.wegelin at gmail.com> wrote:> Suppose we have some glm object such as: > > myglm <- glm( y ~ x, data=DAT) > > Is there an elegant way--or the "right way" within the R way of thinking--to > obtain the names of the response variable, the predictor variables, and the > dataset, as character strings? > > For instance, suppose the "right way" was to use the (currently fictitious) > functions theresponse(), thepredictors(), and theDataSet(). ?Then I would be > able to write a function that obtains the names and subsequently pastes text > along the following lines: > > theResponse <- theresponse( myglm ) > > theFirstPredictor <- thepredictors( myglm )[1] > > theDataSet <- theDataSet(myglm) > > title(main= paste(theResponse, " is the response and ", theFirstPredictor, " > is the only predictor") > > In reality, I can of course extract > >> formula(myglm) > y ~ x > > but I see no elegant way to extract the names of the predictor and response > from this object. The deparse() function doesn't quite solve this problem: > >> deparse(formula(myglm)) > [1] "y ~ x" >> deparse(formula(myglm)[2]) > [1] "y()" >> deparse(formula(myglm)[3]) > [1] "x()" > > Ideally the elegant method would, in this example, return the character > strings "x", "y", and "DAT". > > Thanks for any insights. > > Jake > > Jacob A. Wegelin > Assistant Professor > Department of Biostatistics > Virginia Commonwealth University > 730 East Broad Street Room 3006 > P. O. Box 980032 > Richmond VA 23298-0032 > U.S.A. > E-mail: jwegelin at vcu.edu > URL: http://www.people.vcu.edu/~jwegelin > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >