0

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.

Joe K
  • 11
  • 2
  • 1
    Hi, thanks for including reproducible example! Running your example, I get different output: I get for example `groupTreatment:hemisphererh` and not `group1:hemisphere1`. I get `label1` but that's because in your data, there is no `sulcus1` - the values actually _are_ `label1` etc (looking at your example `df`). – jakub Apr 01 '23 at 08:47
  • Thanks!! So that has happened to me too. I run it once and get the desired result. But then on the 2nd or 3rd time I run it, it changes. Can you try it a few times and see what you get? Maybe it's an R version thing? (And you're right, I forgot to change the label data. I'll fix that.) – Joe K Apr 01 '23 at 20:10
  • Any chance you're setting sum-to-zero contrasts? Also, you might like to look at the `faux` package ... – Ben Bolker Apr 05 '23 at 12:31
  • Unless you can tell us exactly how to reproduce this, we probably can't help. Do you run the whole code block multiple times from beginning to end? (Please edit your question to include the output as text rather than as a screenshot/image ...) – Ben Bolker Apr 05 '23 at 21:01
  • I ended up reinstalling R (v4.2.3) and that solved it. Not sure why that is. Also not sure if I should put write that as a solution to this question...please advise. – Joe K Apr 15 '23 at 18:11
  • Update: It still happens once in a while (every few hours) even after reinstalling R. But restarting R always solves the problem, at least for a few hours, so that is a workaround! – Joe K Apr 19 '23 at 08:09

0 Answers0