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
[EMAIL PROTECTED]
PROTECTED]
Sent: Thursday, April 03, 2003 2:50 AM
To: [EMAIL PROTECTED]
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
How about the following?
lm(as.formula(paste(y~,paste(names(data.frame),
collapse=+),sep=),cbind(data.frame,y))
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Richard Nixon
Sent: 02 April 2003 19:50
To: [EMAIL PROTECTED]
Subject: [R] lm with an arbitrary
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
:-)
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
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
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:[EMAIL PROTECTED]
Sent: Wednesday, April 02, 2003 8:50 AM
To: [EMAIL PROTECTED]
Subject: [R] lm with an arbitrary number of terms
Hello