[R] Predicting a single observatio using LME

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jun 7 15:22:25 CEST 2008


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



More information about the R-help mailing list