[R] subsetting within a function using lme
Pamela McCaskie
pmccask at cyllene.uwa.edu.au
Wed Jan 5 01:48:45 CET 2005
Thankyou for your help with subsetting within a function. I have now tried
to apply the same theory in the framework of an lme as follows:
fit1.lme <- eval(substitute(lme(fixed=fixed, data=dataframe,
random=random, correlation=corCAR1(form= corr), na.action=na.omit,
subset=subset),list(subset=subs)))
but I get the following error:
Error in switch(mode(object), name = , numeric = , call = object,
character = as.name(object), :
[[ cannot be of mode logical
I'm not sure why. Can anyone help me with this?
kind regards
Pam
On Thu, 30 Dec 2004, Thomas Lumley wrote:
> 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
>
More information about the R-help
mailing list