I have a function that, among other things, runs a linear model and returns r2. But, the number of predictor variables passed to the function changes from 1 to 3. How can I change the formula inside the function depending on the number of variables passed in? An example: get.model.fit <- function(response.dat, pred1.dat, pred2.dat = NULL, pred3.dat = NULL) { res <- lm(response.dat ~ pred1.dat + pred2.dat + pred3.dat) summary(res)$r.squared # other stuff happens here... } y <- rnorm(10) x1 <- y + runif(10) x2 <- y + runif(10) x3 <- y + runif(10) get.model.fit(y, x1, x2, x3) get.model.fit(y, x1, x2) get.model.fit(y, x1) Many thanks.... DrC> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 0.1 year 2004 month 11 day 15 language R
as.formula(paste(response.dat" ~ ", paste(pred.dat, collapse= "+"))) pred.dat is a list of predictors Sandip -----Original Message----- From: Dr Carbon [mailto:drcarbon@gmail.com] Sent: Thursday, March 03, 2005 10:28 AM To: r-help@r-project.org Subject: [R] creating a formula on-the-fly inside a function I have a function that, among other things, runs a linear model and returns r2. But, the number of predictor variables passed to the function changes from 1 to 3. How can I change the formula inside the function depending on the number of variables passed in? An example: get.model.fit <- function(response.dat, pred1.dat, pred2.dat = NULL, pred3.dat = NULL) { res <- lm(response.dat ~ pred1.dat + pred2.dat + pred3.dat) summary(res)$r.squared # other stuff happens here... } y <- rnorm(10) x1 <- y + runif(10) x2 <- y + runif(10) x3 <- y + runif(10) get.model.fit(y, x1, x2, x3) get.model.fit(y, x1, x2) get.model.fit(y, x1) Many thanks.... DrC> version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 0.1 year 2004 month 11 day 15 language R ______________________________________________ R-help@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 [[alternative HTML version deleted]]
On Thu, 2005-03-03 at 10:28 -0500, Dr Carbon wrote:> I have a function that, among other things, runs a linear model and > returns r2. But, the number of predictor variables passed to the > function changes from 1 to 3. How can I change the formula inside the > function depending on the number of variables passed in? > > An example: > > get.model.fit <- function(response.dat, pred1.dat, pred2.dat = NULL, > pred3.dat = NULL) > { > res <- lm(response.dat ~ pred1.dat + pred2.dat + pred3.dat) > summary(res)$r.squared > # other stuff happens here... > } > > y <- rnorm(10) > x1 <- y + runif(10) > x2 <- y + runif(10) > x3 <- y + runif(10) > get.model.fit(y, x1, x2, x3) > get.model.fit(y, x1, x2) > get.model.fit(y, x1)Consider using as.formula() to take a character vector that you pass as an argument instead of specifying each IV separately: get.model.fit <- function(my.form) { res <- lm(as.formula(my.form)) summary(res)$r.squared # other stuff happens here... } Then call it with: get.model.fit("y ~ x1 + x2 + x3") Internally, the vector will be converted to:> as.formula("y ~ x1 + x2 + x3")y ~ x1 + x2 + x3 Doing it this way provides for greater flexibility if you want to use a more complicated formula construct. See ?as.formula for more information and further examples, including the use of paste() if you want to separate the DV from the IVs for an additional approach for a long set of similarly named IV's (ie x1:x25). HTH, Marc Schwartz
Gabor Grothendieck
2005-Mar-03 15:57 UTC
[R] creating a formula on-the-fly inside a function
Dr Carbon <drcarbon <at> gmail.com> writes: : : I have a function that, among other things, runs a linear model and : returns r2. But, the number of predictor variables passed to the : function changes from 1 to 3. How can I change the formula inside the : function depending on the number of variables passed in? : : An example: : : get.model.fit <- function(response.dat, pred1.dat, pred2.dat = NULL, : pred3.dat = NULL) : { : res <- lm(response.dat ~ pred1.dat + pred2.dat + pred3.dat) : summary(res)$r.squared : # other stuff happens here... : } The following allows any number of predictors: f <- function(y, ...) summary(lm(y ~., data.frame(y = y, ...)))$r.squared Another possibility is to just pass the formula itself: f <- function(fo) summary(lm(fo))$r.squared
Seemingly Similar Threads
- Comparing and Interpreting GAMMs
- Recommended package nlme: bug in predict.lme when an independent variable is a polynomial (PR#8905)
- How to prevent inclusion of intercept in lme with interaction
- GLM and normality of predictors
- weird pasting of ".value" when list is returned