rlme4model-fitting

printCoefmat() function turns back an error


I've built the followinf model

model2 <- lme4::lmer(metric ~ treatment + (1 | participant), data = data_metrics)

If I try to run the following function it turns back this error:

printCoefmat(summary(model2)$tTable, 
             has.Pvalue = T, P.values = T)

Here a short dataframe from the dataset I'm working on

   dput(head(data_metrics))
structure(list(
  participant = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("U001", "U002", "U003", "U004", "U005", "U006", "U007", "U008", "U009", "U010"), class = "factor"),
  cohort = c("G1", "G1", "G1", "G1", "G1", "G1"),
  shift = c("AM", "AM", "AM", "AM", "AM", "AM"),
  treatment = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Label = c("TX-A", "TX-B", "TX-C"), class = "factor"),
  metric = c(12.1, 14.3, 15.0, 5.2, 6.8, 7.9)
), row.names = c(NA, 6L), class = "data.frame")

Error in printCoefmat(summary(model2)$tTable, has.Pvalue = T, P.values = T) : 
  'x' must be coefficient matrix/data frame

Anyone is able to understand what the error is?


Solution

  • Following up and clarifying @aosmith's comments a little bit.

    Extracting the coefficient table works differently for different mixed-model packages. The newer default (which works for lme4, lmerTest, glmmTMB) is that the coefficient table can be extracted as summary(model)$coefficients (under the hood, this means that the summary() method returns a list with the coeff table stored as the $coefficients) element. For these packages, coef(summary(model)) is even better practice.

    For objects from the nlme package (i.e. lme), the equivalent coefficient table is stored as summary(model)$tTable (this is unfortunate, but nlme is older than R itself ...)

    It doesn't give exactly the same results as printCoefmat, but you might also look into some of the options for pretty-printing the output of broom.mixed::tidy(), which aims to build a compatibility layer so you don't have to remember all this stuff ...