Re: [R] extracting information from lmer objects

From: Douglas Bates <bates_at_stat.wisc.edu>
Date: Wed, 28 May 2008 16:25:34 -0500

On Wed, May 28, 2008 at 6:18 AM, epigone26 <epigone26_at_googlemail.com> wrote:
> Hi,

> I wish to extract a subset of the information of given by
> summary(lmer.object) as a dataframe. In particular, I wish to extract
> just a table listing the Estimate, Std Error, and t-values rounded to
> 3 decimal places. I have learned how to extract the coefficients with
> "round(fixef(lmer.object),3)" and the standard errors with
> "round(sqrt(diag(vcov(a.lmer))),3)"
> but I do not know how to extract the t-values; the extractor methods
> do not seem to help in this regard. I inspected the structure of the
> summary with "str(summary(lmer.object))" but unfortunately I am new to
> R and didn't find this very enlightening. Here is an example of the
> dataframe I would like to produce:

You are most of the way there because the t statistic is the ratio of the estimate to the standard error. (Well, that's the way it is defined in this case. It shouldn't be called a t statistic because it doesn't really have a t distribution except in special cases of balanced designs but we won't go into that now.)

I would do this in two stages - first create the matrix or data frame then, as part of printing the result, do the rounding. You definitely want to create the t ratio before you do the rounding. For the purposes of the rounding it may be more convenient to create a matrix instead of a data frame. You can call the round function on a matrix and it will perform the rounding while keeping the matrix attributes. If you create a data frame you will need to do the rounding column by column.

To create a data frame use

coef.tbl <- function(fm)
{

```     ## check that fm is an object of the "mer" class
stopifnot(is(fm, "mer"))
cc <- fixef(fm)
ss <- sqrt(diag(vcov(fm))
data.frame(Estimate = cc, Std.Err = ss, t = cc/ss, row.names = names(cc))
```
}

If you prefer a matrix, replace the last line with

cbind(Estimate = cc, Std.Err = ss, t = cc/ss)

I am pleased to see you asking the question in the way that you did, using the fixef and vcov extractors to get the basic information for the table. That's exactly what you are supposed to do.

> Factor Estimate Std. Err t
> FixedFac1 0.091 0.140 0.651
> FixedFac2 0.054 0.012 4.461
> FixedFac3 -0.078 0.021 -3.664
>
> Cheers,
>
> Barry.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help