Re: [R] glm with multiple vars

From: Sascha Vieweg <saschaview_at_gmail.com>
Date: Sat, 09 Apr 2011 15:15:20 +0200 (CEST)

On 11-04-09 10:34, dirknbr wrote:

> I am looping through various models with different combinations of
> independent variables which are stored as columns in x
>
> glm(y ~ ??, data=x)
>
> How can I pass the colnames of the selected columns of x into ?? seperating
> them with a +
>
> ie I want to generate
>
> glm(y ~ x1 + x2, data=x)
> glm(y ~ x2 + x3, data=x)
> glm(y ~ x1 + x2 + x4, data=x)
> ...
>
> I have tried the sedit function (sedit(colnames(current),' ',' + ')) but glm
> doesn't understand the output.

Hi, you could try to generate the formula as a character string and then coerce it into a formula object, since glm expects a formula object:

vars <- names(x)
f <- as.formula(paste(vars[1], "~", paste(vars[-1], collapse="+"), collapse=""))
glm(f, data=x)

Of course, the pastes in the f-line need to be adjusted in each loop.

HTH, *S*

-- 
Sascha Vieweg, saschaview_at_gmail.com

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat 09 Apr 2011 - 13:20:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 11 Apr 2011 - 11:20:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive