Make summary output of lmer model use the actual names for the factor levels

280 views Asked by At

Output of summary() changes the names of my factor levels so that I have no idea which effect corresponds to which level in my data. For example, it will change levels of the factor "hemisphere" from (lh, rh) to (hemi1, hemi2). This becomes a problem for factors with 10+ levels.

Here is a reproducible example:

library(lmerTest)

# Set the seed for reproducibility
set.seed(42)

# Define the levels for each column
subjects <- 1:5
hemispheres <- c("lh", "rh")
groups <- c("Control", "Treatment")
labels <- paste0("label", 1:3)

# Create an empty dataframe with the specified columns
df <- data.frame(subject = integer(),
                 hemisphere = character(),
                 label = character(),
                 y = double(),
                 group = character(),
                 stringsAsFactors = FALSE)

# Populate the dataframe with random values
for (sub in subjects) {
  grp <- sample(groups, 1)  # Assign each subject to one group randomly
  for (hemi in hemispheres) {
    for (lbl in labels) {
      y <- rnorm(n = 1, mean = 0, sd = 0.2)
      row <- data.frame(subject = sub,
                        hemisphere = hemi,
                        label = lbl,
                        group = grp,
                        y = y)
      df <- rbind(df, row)
    }
  }
}

# Print the first 5 rows of the dataframe
head(df)

# Run mixed effects model
mod_lme <- lmerTest::lmer(y ~ group*hemisphere*label + (1|subject/hemisphere), data = df)
summary(mod_lme)

And got this as part of the output: enter image description here

I expected the leftmost column to use the names of factor levels from my actual data: e.g. "Treatment" instead of "group1", etc.

How can I make the summary() function use the actual names from the data?

Edit: Tried update.packages() to see if it was an outdated package issue, but that didn't work. Also my R version is 4.2.3 and it's aarch64-apple-darwin20.

0

There are 0 answers