[R] subsetting within a function using lme

Andrew Robinson andrewr at uidaho.edu
Wed Jan 5 02:00:13 CET 2005


Hi Pam,

can you provide just enough code for us to reproduce the error on one
of the sample datasets included with lme?  No more and no less than
the minimum necessary, please.

I find it easier to debug when things are concrete :)

Thanks,

Andrew

On Wed, Jan 05, 2005 at 08:48:45AM +0800, Pamela McCaskie wrote:
> 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
> >
> 
> ______________________________________________
> 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

-- 
Andrew Robinson                      Ph: 208 885 7115
Department of Forest Resources       Fa: 208 885 6226
University of Idaho                  E : andrewr at uidaho.edu
PO Box 441133                        W : http://www.uidaho.edu/~andrewr
Moscow ID 83843                      Or: http://www.biometrics.uidaho.edu
No statement above necessarily represents my employer's opinion.




More information about the R-help mailing list