[R] Unable to use `eval(parse(text))' in nlme::lme

Ben Bolker bbolker at gmail.com
Mon Feb 9 02:16:55 CET 2015


Ravi Varadhan <ravi.varadhan <at> jhu.edu> writes:
 
> I would like to run lme() on a number of response variables 
> in a dataframe in an automatic manner.  Bu, when I
> use eval(parse(text=yname)) to denote the LHS of the formula in lme(), 
> I get the following error message:
> 
> > require(nlme)
> 
> > mod2 <- with(subset(labdata2, Transplant_type!=0 & time >0), 
> lme(eval(parse(text=yname)) ~ time + 
> as.factor(gvhd), random = ~1|Patient, correlation = corAR1(), 
> method="ML", na.action=na.omit))
> Error in model.frame.default(formula = ~Patient + yname + time + gvhd,  :
>   variable lengths differ (found for 'yname')
> 
> The same usage works well in lme4::lmer without any problems.
>
> It seems that there is a problem in 
> how the formula object is evaluated in lme().  Is there an alternative way
> to do this?
> 
  
  While I'm pleased that lmer is more robust, I would say that the
safest/most robust way to do this would be:

ff <- reformulate("time","as.factor(gvhd)",response=yname)
dd <- subset(labdata2, Transplant_type!=0 & time >0)
lme(ff, random=~1|Patient, data=dd, ...)



More information about the R-help mailing list