[R] extracting p-values from lmer outputs

Martin Maechler maechler at stat.math.ethz.ch
Fri Mar 3 17:31:26 CET 2006

>>>>> "lngmyers" == lngmyers  <lngmyers at ccu.edu.tw>
>>>>>     on Fri, 3 Mar 2006 21:54:52 +0800 (CST) writes:

    lngmyers> I would like to write a function that runs GLMM using lmer
    lngmyers> on a user-input model containing interactions, but if the
    lngmyers> model doesn't produce significant results for the interaction,
    lngmyers> a reduced model will be run without the interaction.
    lngmyers> Doing this seems to require getting the p-values out of an
    lngmyers> lmer object, but I don't know how to do this. (The grand
    lngmyers> DF debate seems to be irrelevant since the number of observations
    lngmyers> for what I want to do with this will always be over 600 or so.)
    lngmyers> The problem is that the output of lmer is a list of length 0.
    lngmyers> My "brilliant" idea, shown below, is to divert the display to
    lngmyers> a file, then read the file as a string. I guess it's useful
    lngmyers> to save the full summary somewhere, but is there really
    lngmyers> no more elegant way to do this?

I'm not answering your question directly,
just the (implicit) 
       ``how can I extract parts from an 'lmer' object ? ''

If you used    str(lmerout)
to inspect the STRucture of the lmerout object,
you'd seen something like

> str(lmerout)
Formal class 'lmer' [package "Matrix"] with 35 slots
  ..@ assign  : int(0) 
  ..@ frame   :`data.frame':	180 obs. of  3 variables:
  .. ..$ Reaction: num [1:180] 250 259 251 321 357 ...
  .. ..$ Days    : num [1:180] 0 1 2 3 4 5 6 7 8 9 ...
  .. ..$ Subject : Factor w/ 18 levels "308","309","310",..: 1 1 1 1 1 1 1 1 1 1 ...


which tells you two things (and more) :

1) you have an object of a *formal* class, 
   ``i.e.'' an S4 object

2) you can (and typically will) use  " lmerout @ <slotname> " 
   to extract the slots of your lmerout result object.

Note that the above is a low-level / technical approach and you
should rather work with the official documentation:

 ?lmer		   points you to "lmer-class" and

 class ? lmer   or  
 ?lmer-class    {that one is needed for ESS}

contains a description of the "lmer" object and its (most
important) slots.

Note BTW that in the most uptodate versions of  'lme4'/'Matrix'
you won't get any P-values anymore directly
{just because of the never-ending "DF war"}.

Martin Maechler, ETH Zurich

    lngmyers> - James Myers
    lngmyers> National Chung Cheng University
    lngmyers> Minhsiung Chiayi 621 Taiwan

    lngmyers> lmerout = lmer(Y ~ X1 * X2 + (1|Subj), data = dat, family = "binomial")
    lngmyers> sink("lmerout.txt")
    lngmyers> lmerout
    lngmyers> sink()
    lngmyers> lmerout.string = readChar("lmerout.txt",20000)
    lngmyers> splitstring = strsplit(lmerout.string,"\r\n")
    lngmyers> # If I counted right, the line with the interaction is [19]:
    lngmyers> interstring = strsplit(splitstring[[1]][19]," ")
    lngmyers> # Lines contain TABs, so the p-value is the eighth element along:
    lngmyers> pvalue = as.numeric(interstring[[1]][8])
    lngmyers> if(pvalue < .05) print("The interaction is significant!")

    lngmyers> ______________________________________________
    lngmyers> R-help at stat.math.ethz.ch mailing list
    lngmyers> https://stat.ethz.ch/mailman/listinfo/r-help
    lngmyers> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list