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
> 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 >
This is the same as glm except that - formula may be of class character in which case its regarded as the name of the response variable and the formula defaults to resp ~ Species for that response - the data frame defaults to iris - modify as appropriate for your case myglm <- function (formula, family = gaussian, data, weights, subset, na.action, start = NULL, etastart, mustart, offset, control = glm.control(...), model = TRUE, method = "glm.fit", x = FALSE, y = TRUE, contrasts = NULL, ...) { cl <- match.call() cl[[1]] <- as.name("glm") if (is.character(formula)) { fo <- . ~ Species ### default formula fo[[2]] <- as.name(formula) cl$formula <- fo } if (missing(data)) cl$data <- as.name("iris") # default data frame eval(cl, parent.frame()) } # test myglm("Sepal.Length", subset = Petal.Length > mean(Petal.Length)) myglm(Sepal.Length ~ Petal.Length) myglm("Sepal.Length", subset = 1:100) On 6/30/06, Christian Bieli <christian.bieli at unibas.ch> wrote:> 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 >