Dear Mitchell,
At 05:52 PM 8/5/2002 -0400, Michaell Taylor wrote:>Dear Listers,
>
>I am having trouble figuring out how to build a formula using a variable
>list. For example, I have:
>
>a _ data.frame(a=rnorm(1000))
>a$b_rnorm(1000)+.5*a$a
>a$c_rnorm(1000)+.5*a$b
>a$d_rnorm(1000)+.5*a$b+.1*a$a
>attach(a)
>
>and I estimate,
>
>lm(d ~ b+c+d)
Did you really mean to regress d on predictors including d? (Note that the
data frame a shadows a$a.)
>BUT,
>
>I wish to construct a generalized solution in which,
>
>ListOfVar _ c('b','c','d')
>
>The question is how to leverage ListOfVar into the constuction of the
>formula lm(a$d ~ a$b+a$c+a$b). I know this must be fairly simple, but
>unfortunately my readings of the various help files/books haven't been
>productive yet.
Note that a$b appears twice on the RHS of the formula -- probably not what
you meant.
>Any help would be appreciated. Thanks in advance.
If all of the variables on the RHS are numeric, as here, then something like
lm(a$a ~ as.matrix(a[,ListOfVar]))
should work, where a is the data frame containing the variables in the list
(actually a character vector).
If there are also factors on the RHS of the formula, then you could do
something like
lm(eval(parse(text=paste('a ~', paste(ListOfVar,
collapse="+")))),
data=a)
though someone will probably think of something simpler.
Does that do what you need?
John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._