On Thu, 30 Dec 2004, Pamela McCaskie wrote:
And so my attempt to wrap a simple function around this looks like:
test.fun <- function(formula, mydata, sub=NULL){
 subs <- with(mydata, eval(sub))
 fit.glm <- glm(formula=formula, data=mydata, family=binomial, subset=subs)
 return(fit.glm)
}

But when I tested it out with
test <- test.fun(y~x1+x2, mydata=testdata, sub=expression(SEX==0))

I get:
Error in "[.data.frame"(structure(list(N_ASTHMA = as.integer(c(0, 0, 0,  :
       invalid subscript type

I get a different error: it may be that you have an object called `subs` in the global environment


I'm guessing that it's looking in the global environment for
subs,

More precisely, it is looking in environment where `formula` was created, which happens to be the global environment.


This is the sort of thing that happens with the fitting functions because they go to such lengths to break the basic scoping of R.


You probably have to substitute() the evaluated subset into the glm call.

  fit.glm <- eval(substitute(glm(formula=formula, data=mydata,
     family=binomial, subset=subset),list(subset=subs)))


-thomas

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to