[R] eval-parse and lme in a loop
Eik Vettorazzi
E.Vettorazzi at uke.uni-hamburg.de
Fri Aug 6 00:02:48 CEST 2010
Hi Colm,
the replaced code below works (its probably not the most elegant way,
but rather the result of hours trial and error with eval, paste, bquote
and Co).
But if you want to keep all of your model results you should read the
help for 'assign' and insert something like
assign(paste("lme.",meanCol,sep=""),#your model here)
hth.
Am 05.08.2010 23:17, schrieb Connolly, Colm:
> Hi everybody,
>
> I'm having trouble getting an eval-parse combination to work with lme in a for loop.
>
> Consider the code below. The call to lme outside the loop works. The call to aov inside the loop works, but the call to to lme inside the loop does not.
> The latter fails with
>
> Error in model.frame.default(formula = ~meanCol + Group + c1 + c3 + Subject, :
> variable lengths differ (found for 'Group')
>
> and I don't know why the eval-parse portion of the code is node being replaced as it is in the call to aov. Anybody got any idea what's wrong?
>
> rm(list=ls())
>
> library('nlme')
>
> df=data.frame(
> Group=rep(c("ctrl", "short", "long"), each=9, times=1),
> Subject=paste("S", 1:27, sep=""),
> Mean_1=c(-2.335482, 8.269832, 6.704734, -2.355664, 0.724432, 11.212987, 5.966652, 10.741065, 3.567522, 5.359832, 4.899951, 4.084452, 5.976326, 8.634508, 5.750942, 7.455622, 6.823843, 0.903801, 13.198157, 5.238985, 5.171453, 10.273715, 5.184785, 6.225874, 8.889997, 12.096968, 9.843028),
> Mean_2=c(16.244444, -1.486571, 1.817303, 3.814328, -0.343775, 19.323227, -0.390764, 7.149855, 4.517766, -0.967120, 11.219844, 0.561103, 5.188478, -4.819444, 6.253271, 7.062638, 4.502291, -0.469567, 12.739646, 5.832679, 9.193511, 7.664636, 6.083930, 9.493136, 7.478424, 7.642287, 7.205822),
> Mean_3=c(7.213662, 2.147219, -1.345795, -0.495567, 3.733844, 3.544084, 0.949573, 4.318238, 2.914248, 3.913391, 9.240128, 8.315122, 12.323612, 14.622557, 11.169847, 10.857631, 14.637243, 7.203096, 7.777472, -1.457673, 2.766380, 9.675148, -5.047292, 1.982447, 3.865502, 5.045913, 10.660579),
> c1=rnorm(9, mean=0.1, sd=0.4),
> c2=rnorm(9, mean=0.5, sd=0.7),
> c3=rnorm(9, mean=0.9, sd=1)
> );
>
> [...]
>
for (meanCol in paste("Mean_", 1:3, sep="")) {
frm<-as.formula(bquote(.(as.name(meanCol))~Group + c1 + c1 + c3)
lme1<-lme(eval(frm, data=df, random = ~ 1 | Subject)
}
> }
> Regards,
> --
> Colm G. Connolly, Ph. D.
> Dept of Psychiatry
> University of California, San Diego
> 9500 Gilman Dr. #0738
> La Jolla, CA 92093-0738
> Tel: +1-858-246-0684
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
--
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
More information about the R-help
mailing list