Hi, 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? Thank you, Ravi [[alternative HTML version deleted]]
On 09/02/15 06:46, Ravi Varadhan wrote:> Hi, > > 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?What about trying some'at lahk: fmla <- as.formula(paste(yname,"~ time + as.factor(gvhd)")) mod2 <- with(...., lme(fmla, random = ....)) Also you would probably be better off using the data argument rather then using with(); this could have some impact on the environment in which the formula is evaluated. Just stabbing in the dark here since you did not provide a reproducible example. cheers, Rolf Turner -- Rolf Turner Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 Home phone: +64-9-480-4619
On 08/02/2015 3:49 PM, Rolf Turner wrote:> On 09/02/15 06:46, Ravi Varadhan wrote: >> Hi, >> >> 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? > > What about trying some'at lahk: > > fmla <- as.formula(paste(yname,"~ time + as.factor(gvhd)")) > mod2 <- with(...., lme(fmla, random = ....)) > > Also you would probably be better off using the data argument rather > then using with(); this could have some impact on the environment in > which the formula is evaluated.Formulas are a little tricky: effectively they are evaluated twice. If you type something like y ~ x (or eval(parse(text="y ~ x")), or as.formula("y ~ x")) then a formula object is created. That object remembers the environment in which it was created, so later when the modelling function uses it, the x and y variables are evaluated in the original context. In your example, as.formula() will convert the string to a formula, and attach the current environment. So you'd better hope that whatever variable yname names, as well as time and gvhd, are all available there. Duncan Murdoch> > Just stabbing in the dark here since you did not provide a reproducible > example. > > cheers, > > Rolf Turner >
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, ...)