Re: [R] Unable to use `eval(parse(text))' in nlme::lme
Yes, this is a very important point. Thank you, Bill. Best, Ravi From: William Dunlap wdun...@tibco.com Sent: Friday, February 13, 2015 4:56 PM To: Ravi Varadhan Cc: r-h...@stat.math.ethz.ch Subject: Re: [R] Unable to use `eval(parse(text))' in nlme::lme ff - reformulate(termlabels=c(time,as.factor(gvhd)), response=yname, intercept=TRUE) If the right hand side of the formula were more complicated than an additive list of terms, say '~ time * as.factor(gvhd)' or the left side were more than a name, say 'log(yname)' you could use bquote() instead of reformulate. E.g., formulaTemplate - log(.(responseName)) ~ time * as.factor(gvhd) lapply(c(y1,y2,y3), function(yname)do.call(bquote, list(formulaTemplate, list(responseName=as.namehttp://as.name(yname) [[1]] log(y1) ~ time * as.factor(gvhd) [[2]] log(y2) ~ time * as.factor(gvhd) [[3]] log(y3) ~ time * as.factor(gvhd) I used 'do.call' because bquote does not evaluate its first argument, but we need to evaluate the name 'formulaTemplate'. You could avoid that by putting the template verbatim in the call to bquote, as in lapply(c(y1,y2,y3), function(yname)bquote(log(.(responseName)) ~ time * as.factor(gvhd), list(responseName=as.namehttp://as.name(yname I like the do.call method because I can bury it in a function and forget about it. bquote() retains the environment of the formula template. Bill Dunlap TIBCO Software wdunlap tibco.comhttp://tibco.com On Mon, Feb 9, 2015 at 6:44 AM, Ravi Varadhan ravi.varad...@jhu.edumailto:ravi.varad...@jhu.edu wrote: Thanks to Rolf, Duncan, and Ben. Ben, your suggestion worked (with a minor correction of concatenating the termlabels into a vector). Here is the solution to those interested. ff - reformulate(termlabels=c(time,as.factor(gvhd)), response=yname, intercept=TRUE) dd - subset(labdata2, Transplant_type!=0 time 0) lme(ff, random=~1|Patient, data=dd, correlation=corAR1(), na.action=na.omit) Best, Ravi Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg) Associate Professor Department of Oncology Division of Biostatistics Bionformatics Johns Hopkins University 550 N. Broadway Baltimore, MD 21205 40-502-2619 [[alternative HTML version deleted]] __ R-help@r-project.orgmailto:R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unable to use `eval(parse(text))' in nlme::lme
ff - reformulate(termlabels=c(time,as.factor(gvhd)), response=yname, intercept=TRUE) If the right hand side of the formula were more complicated than an additive list of terms, say '~ time * as.factor(gvhd)' or the left side were more than a name, say 'log(yname)' you could use bquote() instead of reformulate. E.g., formulaTemplate - log(.(responseName)) ~ time * as.factor(gvhd) lapply(c(y1,y2,y3), function(yname)do.call(bquote, list(formulaTemplate, list(responseName=as.name(yname) [[1]] log(y1) ~ time * as.factor(gvhd) [[2]] log(y2) ~ time * as.factor(gvhd) [[3]] log(y3) ~ time * as.factor(gvhd) I used 'do.call' because bquote does not evaluate its first argument, but we need to evaluate the name 'formulaTemplate'. You could avoid that by putting the template verbatim in the call to bquote, as in lapply(c(y1,y2,y3), function(yname)bquote(log(.(responseName)) ~ time * as.factor(gvhd), list(responseName=as.name(yname I like the do.call method because I can bury it in a function and forget about it. bquote() retains the environment of the formula template. Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 9, 2015 at 6:44 AM, Ravi Varadhan ravi.varad...@jhu.edu wrote: Thanks to Rolf, Duncan, and Ben. Ben, your suggestion worked (with a minor correction of concatenating the termlabels into a vector). Here is the solution to those interested. ff - reformulate(termlabels=c(time,as.factor(gvhd)), response=yname, intercept=TRUE) dd - subset(labdata2, Transplant_type!=0 time 0) lme(ff, random=~1|Patient, data=dd, correlation=corAR1(), na.action=na.omit) Best, Ravi Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg) Associate Professor Department of Oncology Division of Biostatistics Bionformatics Johns Hopkins University 550 N. Broadway Baltimore, MD 21205 40-502-2619 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unable to use `eval(parse(text))' in nlme::lme
Thanks to Rolf, Duncan, and Ben. Ben, your suggestion worked (with a minor correction of concatenating the termlabels into a vector). Here is the solution to those interested. ff - reformulate(termlabels=c(time,as.factor(gvhd)), response=yname, intercept=TRUE) dd - subset(labdata2, Transplant_type!=0 time 0) lme(ff, random=~1|Patient, data=dd, correlation=corAR1(), na.action=na.omit) Best, Ravi Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg) Associate Professor Department of Oncology Division of Biostatistics Bionformatics Johns Hopkins University 550 N. Broadway Baltimore, MD 21205 40-502-2619 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unable to use `eval(parse(text))' in nlme::lme
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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unable to use `eval(parse(text))' in nlme::lme
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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Unable to use `eval(parse(text))' in nlme::lme
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, ...) __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.