[R] Passing arguments to glm()

Christian Bieli christian.bieli at unibas.ch
Fri Jun 30 16:49:45 CEST 2006


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



More information about the R-help mailing list