Hello folks, Any ideas how to do this? data.frame is a data frame with column names "x1",...,"xn" y is a response variable of length dim(data.frame)[1] I want to write a function function(y, data.frame){ lm(y~x1+...+xn) } This would be easy if n was always the same. If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? Thanks Richard -- Dr. Richard Nixon MRC Biostatistics Unit, Cambridge, UK http://www.mrc-bsu.cam.ac.uk/personal/richard Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038
The following might work: mdl <- paste("y~", paste(names(data.frame), collapse="+")) lm(mdl, ...) If y = "y" is a column of your data.frame, you can delete it be selecting "names(data.frame)[!is.element(y, names(data.frame)]" Can you solve the problem from here? Best Wishes, Spencer Graves Richard Nixon wrote:> Hello folks, > > Any ideas how to do this? > > data.frame is a data frame with column names "x1",...,"xn" > y is a response variable of length dim(data.frame)[1] > > I want to write a function > > function(y, data.frame){ > lm(y~x1+...+xn) > } > > This would be easy if n was always the same. > If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? > > Thanks > Richard > > -- > Dr. Richard Nixon > MRC Biostatistics Unit, Cambridge, UK > http://www.mrc-bsu.cam.ac.uk/personal/richard > Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
I think you can do it like this lm(y~., data=data.frame) # note the dot to the right of ~> -----Original Message----- > From: Richard Nixon [mailto:richard.nixon at mrc-bsu.cam.ac.uk] > Sent: Wednesday, April 02, 2003 8:50 AM > To: r-help at stat.math.ethz.ch > Subject: [R] lm with an arbitrary number of terms > > > Hello folks, > > Any ideas how to do this? > > data.frame is a data frame with column names "x1",...,"xn" > y is a response variable of length dim(data.frame)[1] > > I want to write a function > > function(y, data.frame){ > lm(y~x1+...+xn) > } > > This would be easy if n was always the same. > If n is arbitrary how could I feed the x1+...+xn terms into > lm(response~terms)? > > Thanks > Richard > > -- > Dr. Richard Nixon > MRC Biostatistics Unit, Cambridge, UK > http://www.mrc-bsu.cam.ac.uk/personal/richard > Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >-------------------------------------------------- DISCLAIMER\ This e-mail, and any attachments thereto, is intende... {{dropped}}
Richard Nixon <richard.nixon at mrc-bsu.cam.ac.uk> writes:> Hello folks, > > Any ideas how to do this? > > data.frame is a data frame with column names "x1",...,"xn" > y is a response variable of length dim(data.frame)[1] > > I want to write a function > > function(y, data.frame){ > lm(y~x1+...+xn) > } > > This would be easy if n was always the same. > If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? >You could build up the formula in a for loop; something like n <- names(my.data.frame) f <- as.symbol(n[1]) for (i in 2:length(n)) f <- substitute(f+a,list(a=as.symbol(n[i])) f <- substitute(y~f) (I didn't try it. Most likely it doesn't quite work.) However, what's wrong with lm(y ~ ., data=my.data.frame) ?? -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dear Richard, At 05:49 PM 4/2/2003 +0100, Richard Nixon wrote:>Any ideas how to do this? > >data.frame is a data frame with column names "x1",...,"xn" >y is a response variable of length dim(data.frame)[1] > >I want to write a function > >function(y, data.frame){ > lm(y~x1+...+xn) >} > >This would be easy if n was always the same. >If n is arbitrary how could I feed the x1+...+xn terms into >lm(response~terms)?If y contains the *name* of the response variable, which is also in the data frame, then the following should work: fun <- function(y, df){ lm(as.formula(paste(y, "~.")), data=df) } Alternatively, if y is a vector and is not in the data frame, then you might try fun <- function(y, df){ df <- data.frame(y, df) lm(y~., data=df) } I hope that this helps, John ____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox
You need to paste() together a formula. There's an example in ?formula. Try, n <- 10 rhs <- paste("x", 1:n, collapse = "+", sep = "") lhs <- "y ~" f <- as.formula(paste(lhs, rhs)) Then pass `f' into lm(). -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Wed, 2 Apr 2003, Richard Nixon wrote:> Hello folks, > > Any ideas how to do this? > > data.frame is a data frame with column names "x1",...,"xn" > y is a response variable of length dim(data.frame)[1] > > I want to write a function > > function(y, data.frame){ > lm(y~x1+...+xn) > } > > This would be easy if n was always the same. > If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? > > Thanks > Richard > > -- > Dr. Richard Nixon > MRC Biostatistics Unit, Cambridge, UK > http://www.mrc-bsu.cam.ac.uk/personal/richard > Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
What are you using the data frame argument for? If y, x1, x2, ..., xn were all the variables in the data frame all you need do is myfun <- function(y, dat) { lm(y ~ ., dat) } but your question is very vague and ill-posed. -----Original Message----- From: Richard Nixon [mailto:richard.nixon at mrc-bsu.cam.ac.uk] Sent: Thursday, April 03, 2003 2:50 AM To: r-help at stat.math.ethz.ch Subject: [R] lm with an arbitrary number of terms Hello folks, Any ideas how to do this? data.frame is a data frame with column names "x1",...,"xn" y is a response variable of length dim(data.frame)[1] I want to write a function function(y, data.frame){ lm(y~x1+...+xn) } This would be easy if n was always the same. If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? Thanks Richard -- Dr. Richard Nixon MRC Biostatistics Unit, Cambridge, UK http://www.mrc-bsu.cam.ac.uk/personal/richard Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
How about the following? lm(as.formula(paste("y~",paste(names(data.frame), collapse="+"),sep=""),cbind(data.frame,y)) -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Richard Nixon Sent: 02 April 2003 19:50 To: r-help at stat.math.ethz.ch Subject: [R] lm with an arbitrary number of terms Hello folks, Any ideas how to do this? data.frame is a data frame with column names "x1",...,"xn" y is a response variable of length dim(data.frame)[1] I want to write a function function(y, data.frame){ lm(y~x1+...+xn) } This would be easy if n was always the same. If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? Thanks Richard -- Dr. Richard Nixon MRC Biostatistics Unit, Cambridge, UK http://www.mrc-bsu.cam.ac.uk/personal/richard Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Thanks to James Holtman, Matt Wiener, Douglas Bates, Spencer Graves, John Fox, Roger Peng, Bill Venable, Darryl Greig, Vadim Ogranovich and Peter Dalgaard There are several ways to tackel this problem, and one easy one which I missed :-) ---------------------------------------------------------------------- Question data.frame is a data frame with column names "x1",...,"xn" y is a response variable of length dim(data.frame)[1] I want to write a function function(y, data.frame){ lm(y~x1+...+xn) } This would be easy if n was always the same. If n is arbitrary how could I feed the x1+...+xn terms into lm(response~terms)? ---------------------------------------------------------------------- y <- c(67,76,56,48,10,43,12,33,88,63,75,14,58,19,14) x1 <- as.factor(c(2,2,2,2,2,1,1,1,1,1,1,1,1,1,1)) x2 <- c(25,40,35,34,51,24,37,31,26,21,41,45,24,26,29) data<-data.frame(x1,x2) ---------------------------------------------------------------------- 1) James Holtman fun <- function(y, data.frame){ .vars <- paste(names(data.frame), collapse="+") eval(parse(text=paste('lm(y~',.vars,',data.frame)'))) } fun(y,data) Call: lm(formula = y ~ x1 + x2, data = data.frame) Coefficients: (Intercept) x12 x2 84.565 18.763 -1.403 ---------------------------------------------------------------------- 2) Matt Wiener fun <- function(y, data.frame){ lm(y ~ ., data = data.frame) } fun(y,data) Call: lm(formula = y ~ ., data = data.frame) Coefficients: (Intercept) x12 x2 84.565 18.763 -1.403 ---------------------------------------------------------------------- 3) Matt Wiener fun <- function(y, data.frame){ .vars <- paste(names(data.frame), collapse="+") lm(as.formula(paste('y~',.vars)),data.frame) } fun(y,data) Call: lm(formula = as.formula(paste("y~", .vars)), data = data.frame) Coefficients: (Intercept) x12 x2 84.565 18.763 -1.403 ---------------------------------------------------------------------- Douglas Bates: same as (2) Spencer Graves: similar to (3) John Fox: mixture of (2) and (3) Roger Peng: similar to (3) Bill Venable: same as (2) Darryl Greig: simmilar to (3) Vadim Ogranovich: same as (2) Peter Dalgaard n <- names(my.data.frame) f <- as.symbol(n[1]) for (i in 2:length(n)) f <- substitute(f+a,list(a=as.symbol(n[i])) f <- substitute(y~f) (I didn't try it. Most likely it doesn't quite work.) Neither did I - Richard ;) -- Dr. Richard Nixon MRC Biostatistics Unit, Cambridge, UK http://www.mrc-bsu.cam.ac.uk/personal/richard Tel: +44 (0)1223 330382, Fax: +44 (0)1223 33038