strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science
https://strengejacke.github.io/sjPlot
603 stars 91 forks source link

Different random effects plot from plot_model() when glmmTMB vs lme4 is used to fit the model #894

Open SlavichEve opened 1 year ago

SlavichEve commented 1 year ago

I have used plot_model(Model, type = "re"). When Model is fitted with glmmTMB the intervals are noticeably wider than when I refit the model with lme4. I have replicated this problem on multiple datasets. It feels like the lme4 plots are the correct version and something is wrong with the glmmTMB plots.

Attached is code that demonstrates the problem on publicly available data.

library(dplyr)
library(glmmTMB)
library(lme4)
library(sjPlot)

#I copied this data wrangling code and data from https://cehs-research.github.io/eBook_multilevel/glmm-binary-outcome-#muscatine-obesity.html. 

#This data wrangling is not important- just to get to the point where we can reproduce the problem. 

#read in the data
data_raw <- read.table("https://raw.githubusercontent.com/CEHS-research/data/master/MLM/Muscatine.txt", header=TRUE)

complete_ids <- data_raw %>%
dplyr::filter(obesity %in% c("0", "1")) %>%
dplyr::group_by(id) %>%
dplyr::summarise(n = n()) %>%
dplyr::filter(n == 3) %>%
dplyr::pull(id)

use_ids <- complete_ids %>% sample(350)

data_long <- data_raw %>%
dplyr::filter(id %in% use_ids) %>%
mutate(id       = id     %>% factor) %>%
mutate(gender   = gender %>% factor(levels = 0:1,
labels = c("Male", "Female"))) %>%
mutate(age_base = baseage %>% factor) %>%
mutate(age_curr = currage %>% factor) %>%
mutate(occation = occas   %>% factor) %>%
mutate(obesity  = obesity %>% factor(levels = 0:1,
labels = c("No", "Yes"))) %>%
select(id, gender, age_base, age_curr, occation, obesity)

data_long <- data_long %>%
dplyr::mutate(age_center = age_curr %>% as.character %>% as.numeric -12) %>%
dplyr::mutate(obesity_num = obesity %>% as.numeric - 1)

#The models are the same but the plots are different.
fit_glmm_1 <- glmmTMB(obesity_num ~ age_center*gender + I(age_center^2)*gender + (1|id),data = data_long,
family      = binomial(link = "logit"))

fit_glmm_2 <- lme4::glmer(obesity_num ~ age_center*gender + I(age_center^2)*gender + (1|id),data = data_long,
family      = binomial(link = "logit"))

plot_model(fit_glmm_1, type = "re", transform = NULL )   
plot_model(fit_glmm_2 , type= "re" , transform = NULL )     

image image