[R] Passing arguments to glm()

Jacques VESLOT jacques.veslot at good.ibl.fr
Fri Jun 30 17:07:31 CEST 2006


 > f.myglm <- function(y=y, subset="x2 == 'yes'", data=d.d.mydata) eval(parse(text="glm(", 
deparse(substitute(y)), "~ x1, family=binomial, data=", deparse(substitute(data)), ", subset =, 
subset, ")"))
 > f.myglm()

-------------------------------------------------------------------
Jacques VESLOT

CNRS UMR 8090
I.B.L (2ème étage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex

Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31

http://www-good.ibl.fr
-------------------------------------------------------------------


Christian Bieli a écrit :
> 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 at 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
>



More information about the R-help mailing list