Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created July 25, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njtierney/8d235ec8ab0db7d544f3157062439fdb to your computer and use it in GitHub Desktop.
Save njtierney/8d235ec8ab0db7d544f3157062439fdb to your computer and use it in GitHub Desktop.
library(lme4)
# broom tidies up model summaries
library(broom) # install.packages("broom")
# fit a mixed model
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
# print the model output
fm1
# Linear mixed model fit by REML ['lmerMod']
# Formula: Reaction ~ Days + (Days | Subject)
# Data: sleepstudy
# REML criterion at convergence: 1743.628
# Random effects:
# Groups Name Std.Dev. Corr
# Subject (Intercept) 24.740
# Days 5.922 0.07
# Residual 25.592
# Number of obs: 180, groups: Subject, 18
# Fixed Effects:
# (Intercept) Days
# 251.41 10.47
# show the tidy model summary
tidy(fm1)
# term estimate
# 1 (Intercept) 251.40510485
# 2 Days 10.46728596
# 3 sd_(Intercept).Subject 24.74044768
# 4 sd_Days.Subject 5.92213326
# 5 cor_(Intercept).Days.Subject 0.06555134
# 6 sd_Observation.Residual 25.59181589
# std.error statistic group
# 1 6.824556 36.838310 fixed
# 2 1.545789 6.771485 fixed
# 3 NA NA Subject
# 4 NA NA Subject
# 5 NA NA Subject
# 6 NA NA Residual
# show the fixed effects
fixef(fm1)
# (Intercept) Days
# 251.40510 10.46729
# pull the fixed effects function directly from lme4.
lme4::fixef(fm1)
# (Intercept) Days
# 251.40510 10.46729
# if this doesn't work, try this:
fixed_effects <- function(x){ # x is a model
# get the names of the coefficients
Term <- x %>% fixef %>% names
# get the values of the coefficients
Estimate <- x %>% fixef %>% as.numeric
# put it into a dataframe
fixed_effects_df <- data_frame(Term, Estimate)
fixed_effects_df
}
library(dplyr) # install.packages("dplyr")
fixed_effects(fm1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment