[R] Passing arguments to glm()

From: Christian Bieli <christian.bieli_at_unibas.ch>
Date: Sat 01 Jul 2006 - 00:49:45 EST


Hi there

I want to pass arguments (i.e. the response variable and the subset argument) in a self-made function to glm. Here is one way I can do this:

f.myglm <- function(y,subfact,subval) {   

glm(d.mydata[,y]~d.mydata[,'x1'],family=binomial,subset=d.mydata[,subfact]==subval) }

> str(d.mydata)

`data.frame': 15806 obs. of 3 variables:

 $ y : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 NA 1 1 ...
 $ x1: Factor w/ 2 levels "no","yes": 2 2 1 2 2 2 2 2 2 2 ...
 $ x2: Factor w/ 2 levels "no","yes": 1 1 1 1 1 2 2 1 2 2 ...

> f.myglm('y','x2','yes')

But is there a way I can pass the arguments and use the data argument of glm()?
In a naive way of thinking I'd like to something like this: f.myglm <- function(y,sub) {
  glm(y~x1,family=binomial,data=d.mydata,subset=sub) }
> f.myglm(y=y,sub=x2=='yes')

I know that's not possible, because the objects y and x2 are not defined in the user workspace.
So, something like passing the arguments as an expression and evaluate it in the glm function should work, but I didn't manage to do it.

I'd appreciate your advice.
Christian

> R.version

         _
platform i386-pc-mingw32

arch     i386          
os       mingw32       
system   i386, mingw32 
status                 
major    2             
minor    2.1           
year     2005          
month    12            
day      20            
svn rev  36812         

language R

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jul 01 01:00:35 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 01 Jul 2006 - 04:14:35 EST.

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