When I use a model fit with LME, I get an error if I try to use "predict" with a dataset consisting of a single line. For example, using this data:> simpledataY t D ID 1 -1.464740870 1 0 1 2 1.222911373 2 0 1 3 -0.605996798 3 0 1 4 0.155692707 4 0 1 5 3.849619772 1 0 2 6 4.289213902 2 0 2 7 2.369407737 3 0 2 8 2.249052533 4 0 2 9 0.920044316 1 0 3 10 2.003262622 2 0 3 11 0.003833438 3 0 3 12 1.578300927 4 0 3 13 -0.842322442 1 1 4 14 -0.657256158 2 1 4 15 1.504491575 3 1 4 16 2.896007045 4 1 4 17 0.990505440 1 1 5 18 2.722942793 2 1 5 19 4.395861278 3 1 5 20 4.849296475 4 1 5 21 3.049616421 1 1 6 22 2.874405962 2 1 6 23 4.359511097 3 1 6 24 6.165419699 4 1 6 This happened:> testLME <- lme(Y~t+D,data=simpledata,random=~1|ID) > predict(testLME, simpledata[1,])Error in val[revOrder, level + 1] : incorrect number of dimensions This has occurred with other datasets as well. Is this a bug in the code, or am I doing something wrong? (Also, is there a way to parse a formula of a type given to "random"? For example, given ~1+t|ID, I'd like to be able to extract all the variable names to the left of | and to the right of |, the way one can with a normal formula.) Thanks in advance! Rebecca
Rebecca Sela <rsela <at> stern.nyu.edu> writes:> > When I use a model fit with LME, I get an error if I try to use "predict" witha dataset consisting of a single line.> > For example, using this data: > > simpledata > Y t D ID > 23 4.359511097 3 1 6 > 24 6.165419699 4 1 6 > > This happened: > > testLME <- lme(Y~t+D,data=simpledata,random=~1|ID) > > predict(testLME, simpledata[1,]) > Error in val[revOrder, level + 1] : incorrect number of dimensions > > This has occurred with other datasets as well. Is this a bug in the code, oram I doing something wrong? No, this looks like a bug due to dimension-dropping when using one row. Probably nobody used it with one value before. As a workaround, do some cheating predict(testLME, simpledata[c(1,2),]) Dieter
On Sat, 7 Jun 2008, Dieter Menne wrote:> Rebecca Sela <rsela <at> stern.nyu.edu> writes: > >> >> When I use a model fit with LME, I get an error if I try to use "predict" with > a dataset consisting of a single line. >> >> For example, using this data: >>> simpledata >> Y t D ID >> 23 4.359511097 3 1 6 >> 24 6.165419699 4 1 6 >> >> This happened: >>> testLME <- lme(Y~t+D,data=simpledata,random=~1|ID) >>> predict(testLME, simpledata[1,]) >> Error in val[revOrder, level + 1] : incorrect number of dimensions >> >> This has occurred with other datasets as well. Is this a bug in the code, or > am I doing something wrong? > > No, this looks like a bug due to dimension-dropping when using one row. Probably > nobody used it with one value before.The problem is in fitted.lmeStruct, which is documented to drop to a vector if there is only one column. So it should usually drop, but not here when there are multiple levels. The real problem is that predict.lme asked for the fitted values at levels it does not use. But the quickest workaround is to change the final line of fitted.lmeStruct to if(length(level) > 1) fit[, level + 1, drop = FALSE] else fit[, level+1]> As a workaround, do some cheating > > predict(testLME, simpledata[c(1,2),]) > > > Dieter > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595