Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.6 kB
1
Indexable
Never
library(tidyverse)
library(lme4)
library(ggeffects)

lmm_data <- read.csv(file = 'lmm_data.csv')
lmm_data


## only intercept variable (means are different)
mixed.lmer2 <- lmer(LVEF ~ BMI + (1| ethnicity), data=lmm_data)
summary(mixed.lmer2)

## intercept and slope (generally different and progresses at different rates)
mixed.ranslope <- lmer(LVEF ~ BMI + (1 + BMI | ethnicity), data = lmm_data) 
summary(mixed.ranslope)

library(ggeffects)
# Extract the prediction data frame
pred.mm <- ggpredict(mixed.ranslope, terms = c("BMI"))  # this gives overall predictions for the model

# Plot the predictions 

(ggplot(pred.mm) + 
    geom_line(aes(x = x, y = predicted)) +          # slope
    geom_ribbon(aes(x = x, ymin = predicted - std.error, ymax = predicted + std.error), 
                fill = "lightgrey", alpha = 0.5) +  # error band
    geom_point(data = lmm_data,                      # adding the raw data (scaled values)
               aes(x = BMI, y = LVEF, colour = ethnicity)) + 
    labs(x = "BMI", y = "LVEF", 
         title = "Body length does not affect intelligence in dragons") + 
    theme_minimal()
)
ggpredict(mixed.ranslope, terms = c("BMI", "ethnicity"), type = "re", ci.lvl = NA) %>% 
  plot() +
  scale_fill_manual(values=c("#CC0000", "#006600", "#669999", "#00CCCC", 
                             "#660099", "#CC0066", "#FF9999", "#FF9900", 
                             "black", "orange", "green", "yellow", "blue",
                             "brown", "red", "cyan", "black", "black", "black","black")) + 
  labs(x = "BMI", y = "LVEF", title = "Effect of body size on intelligence in dragons") + 
  theme_minimal()