[R-sig-ME] Fitted values in lmer

Andy Fugard a.fugard at ed.ac.uk
Fri Mar 14 16:47:35 CET 2008

Thanks, rownames(mod at frame) gives indices I can use.  The following 
fitted.na code does the right sort of thing, but relies on the length of 
the original vector of outcomes (so I use the whole vector in the output 
anyway).  And it uses set differencing and merging, which might not be 
most computational efficient way to do things.  Any other suggestions 
for a way to do this gratefully received...

fitted.na = function(origvec,mod) {
   hits = as.numeric(rownames(mod at frame))
   misses = setdiff(1:length(origvec), hits)
   fits  = fitted(mod)
   d.hits   = data.frame(i = hits
                        ,fits = fitted(mod))
   d.misses = data.frame(i = misses
                        ,fits = rep(NA, length(misses)))
   res = merge(d.hits, d.misses, all.x = T, all.y=T)
   res$y = origvec

# For instance, try adding missing data to the sleepstudy

breakit = function(x,p) {
   breakv = sample(c(NA,0), length(x), T, prob = c(p,1-p))
   x + breakv

sleep2 = sleepstudy
sleep2$Reaction = breakit(sleepstudy$Reaction,0.1)

mod = lmer(Reaction ~ Days + (1|Subject) + (0+Days|Subject),
            data = sleep2)

fitted.na(sleep2$Reaction, mod)


Douglas Bates wrote:
> On Fri, Mar 14, 2008 at 7:31 AM, Andy Fugard <a.fugard at ed.ac.uk> wrote:
>>  Is there any way to get fitted values out of lmer so that any NAs in the
>>  predictors/outcome pop out in the correct position?
> Yes.
>>   From what I can tell, the "fitted" function doesn't have access to
>>  missingness in the inputs.
> It does not, in its current implementation.  I believe there is
> information regarding the pattern of missingness in the model frame
> (stored in the "frame" slot of the fitted model) and that can be used
> to generate an appropriate fitted response.
> Does anyone know exactly how this is done for linear models?  If so,
> are you willing to provide a patch to the fitted method for mer
> objects?  I can put it on my ToDo list but when the first item on the
> list is "finish the book" you almost never get to the second and
> subsequent items.

Andy Fugard, Postgraduate Research Student
Psychology (Room F3), The University of Edinburgh,
   7 George Square, Edinburgh EH8 9JZ, UK
Mobile: +44 (0)78 123 87190   http://www.possibly.me.uk

More information about the R-sig-mixed-models mailing list