[R] metaprogramming with lm
s.blomberg1 at uq.edu.au
Thu Nov 13 02:04:08 CET 2008
You can construct the formula on the fly. Say you have a data frame with
columns: y, x1,...x10:
dat <- data.frame(matrix(rnorm(1100), ncol=11, dimnames=list(NULL,c("y",
paste("x", 1:10, sep="")))))
Then you could construct the formula using:
form <- formula(paste("y ~ ", paste(names(dat)[which(names(dat) !=
fit <- lm(form, data=dat)
On Thu, 2008-11-13 at 09:27 +0900, June Kim wrote:
> Say I want to make a multiple regression model with the following expression:
> lm(y~x1 + x2 + x3 + ... + x_n,data=mydata)
> It gets boring to type in the whole independent variables, in this
> case x_i. Is there any simple way to do the metaprogramming for this?
> (There are different cases where the names of the independent
> variables might sometimes have apparent patterns or not)
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Simon Blomberg, BSc (Hons), PhD, MAppStat.
Lecturer and Consultant Statistician
Faculty of Biological and Chemical Sciences
The University of Queensland
St. Lucia Queensland 4072
Room 320 Goddard Building (8)
T: +61 7 3365 2506
1. I will NOT analyse your data for you.
2. Your deadline is your problem.
The combination of some data and an aching desire for
an answer does not ensure that a reasonable answer can
be extracted from a given body of data. - John Tukey.
More information about the R-help