rstatisticsmixed-modelsr-package# 'mixed' and 'lmer' in R provide different estimates for variable coefficients

I was fitting mixed-effects models in a longitudinal dataset and noticed that specifying the same structure led to different outputs depending on if I used 'mixed' (from package afex) or lme4. I have provided reproducible code below (R version 4.2.2)

```
library(lme4)
library(afex)
library(tidyverse)
library(effectsize)
# Pull in toy data and convert vars to factors
dset_test1 <- mtcars %>%
mutate(car_id = row.names(mtcars)) %>%
mutate(vs = as.factor(vs)) %>%
mutate(am = as.factor(am))
# Create simulated longitudinal dataset
increase_longitudinal <- mtcars %>%
dplyr::select(am, wt) %>%
mutate(car_id = row.names(mtcars)) %>%
mutate(wt2 = (wt)*2)
# Merge and make long form
dset_test = merge(dset_test1, increase_longitudinal, on='car_id') %>%
pivot_longer(cols = c(wt, wt2), names_to = 'year', values_to = 'weight') %>%
mutate(year = as.factor(year))
# Set reference levels
dset_test$year <- relevel(dset_test$year, "wt")
# fit model with mixed
mix1 <- mixed(mpg~year * weight + hp +(1 | car_id), data=dset_test)
summary(mix1)
parameters::standardize_parameters(mix1)
# fit model with lme4
lmer1 <- lmer(mpg~year * weight + hp +(1 | car_id), data=dset_test)
summary(lmer1)
parameters::standardize_parameters(lmer1)
```

This produces the following standardized output:

```
# model with mixed:
Parameter | Std. Coef. | 95% CI
------------------------------------------
(Intercept) | -0.29 | [-0.40, -0.18]
year1 | -0.81 | [-0.98, -0.63]
weight | -1.12 | [-1.36, -0.88]
hp | -0.39 | [-0.53, -0.25]
year1:weight | -0.37 | [-0.45, -0.29]
# Model with lme4:
Parameter | Std. Coef. | 95% CI
--------------------------------------------
(Intercept) | -1.05 | [-1.29, -0.81]
yearwt2 | 1.54 | [ 1.21, 1.86]
weight | -1.42 | [-1.72, -1.12]
hp | -0.40 | [-0.54, -0.26]
yearwt2:weight | 0.71 | [ 0.56, 0.86]
```

As you can see, the model coefficients are quite different, particularly for the interaction term, where the sign of the coefficient even is different. Thanks for any insight into what might be going on here!

Solution

`afex::mixed()`

automatically uses sum-to-zero contrasts by default, since these often make it easier to interpret main effects in models with interactions (but R's default is to use *treatment* contrasts).

If you set `options(contrasts = c("contr.sum", "contr.poly"))`

before running the `lmer()`

fit, you'll get identical coefficients. (There are other ways to set contrasts but this is probably the simplest.)

I hope by the way that this is not representative of your real data -- I got **lots** of warnings when I ran this code, and lots of red flags in the output (e.g. the residual standard error was very close to zero, as was the raw `yearwt2`

estimate in the original `lmer`

fit). If you can it's generally best to generate more sensible test data so that people (like me) don't get distracted/think the problem might be some unidentifiability issue ...

- Installing R on Linux: configure: error: libcurl >= 7.28.0 library and headers are required with support for https
- How to do ensembles with time series using AICc?
- planes3d expands and draws the area based on the sphere's radius
- How to extract tag code itself using R, rvest
- How to Display or Print Contents of Environment in R
- How to use Windows user credentials for proxy authentication in R/RStudio
- R reticulate specifying python executable to use
- Replace multiple Instances of a variable name in an R function and save the modified function
- Standardizing address formatting in R
- How to fix "failed to load cairo DLL" in R?
- Using grepl to filter columns names in specific range of columns
- changing the legends in ggplot2 to have groups of similar labels
- How to keep only unique rows but ignore a column?
- convert string date to R Date FAST for all dates
- Add subgroup text to plotly pie chart
- R Shiny : adjust height of DT datatable when fillContainer=TRUE,
- Why do R external pointers' "unusual copying semantics" mean they should not be used stand-alone?
- How to extract somo character after a string with a number of word which can change in R
- What does `se` stand for in geom_smooth(..., se = FALSE)?
- How to find number of rows greater than any values in R
- Align text and reduce space between text and parentheses in plotly hover info box
- Remove outer box of geom_bar plot with broken y-axis
- How to use lag/lead in mutate with an initial value?
- Is it possible to have a Shiny ConditionalPanel whose condition is a global variable?
- counting elements in one list in another list
- How to vectorize nested loops in R?
- Replace NA values with an incrementing sequence starting from the previous non-NA value
- How can I calculate the number of uniques in a row within a species matrix?
- How to perform operations on pairs of rows, based on a "distinguishing" column's values
- Mutate variable based on previous observations