Get Started

Displaying the results from a lmer model

Data scientists must communicate their work and DataCamp offers courses on the topic. Explaining your work helps your audience understand the results. To do this, match your presentation to your audience's knowledge level and expectations.

For non-technical audiences, describe the important findings from your output. For example, you might say, counties with older mothers tend to have lower birth rates. For technical audiences, include details such as coefficient estimates, confidence intervals, and test statistics. Books such as The Chicago Guide to Writing about Multivariate Analysis provide suggestions for describing regression outputs.

During this exercise, you will extract and plot fixed-effects. Besides plotting the coefficients (with geom_point()) and their 95% confidence intervals (with geom_linerange()), you will add a red-line to the plot to help visualize where zero is located (using geom_hline()). If the 95% confidence intervals do not include zero, the coefficient's estimate differs from zero.

coord_flip() is required because ggplot does not allow for xmin or xmax, only ymin and ymax. And, theme_minimal() changes the theme from the default.

Technical note: Extracting regression coefficients from lmer is tricky (see the discussion between the lmer and broom authors).

This is a part of the course

“Hierarchical and Mixed Effects Models in R”

View Course

Exercise instructions

  • Extract the coefficients from the model out using tidy() from the broom.mixed package. Include the confidence interval.
  • Use the exiting code to filter out the random-effect estimates.
  • Print the coefficient table to the screen.
  • Plot the outputs using ggplot2. Use term for the x-axis, estimate for the y-axis, conf.low for the ymin, and conf.high for the ymax.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Extract out the parameter estimates and confidence intervals
coef_estimates <-
	___(___, ___) %>%
	filter(effect == "fixed")

# Print the new dataframe
print(___)

# Plot the results using ggplot2
ggplot(coef_estimates, aes(x = ___, y = ___,
                     ymin = ___, ymax = ___)) +
    geom_hline( yintercept = 0, color = 'red' ) +
    geom_linerange() + geom_point() + coord_flip() + theme_minimal()
Edit and Run Code

This exercise is part of the course

Hierarchical and Mixed Effects Models in R

AdvancedSkill Level
4.6+
19 reviews

In this course you will learn to fit hierarchical models with random effects.

This chapter providers an introduction to linear mixed-effects models. It covers different types of random-effects, describes how to understand the results for linear mixed-effects models, and goes over different methods for statistical inference with mixed-effects models using crime data from Maryland.

Exercise 1: Linear mixed effect model- Birth rates dataExercise 2: Building a lmer model with random effectsExercise 3: Including a fixed effectExercise 4: Random-effect slopesExercise 5: Uncorrelated random-effect slopeExercise 6: Fixed- and random-effect predictorExercise 7: Understanding and reporting the outputs of a lmerExercise 8: Comparing print and summary outputExercise 9: Extracting coefficientsExercise 10: Displaying the results from a lmer model
Exercise 11: Statistical inference with Maryland crime dataExercise 12: Visualizing Maryland crime dataExercise 13: Rescaling slopesExercise 14: Null hypothesis testingExercise 15: Controversies around P-valuesExercise 16: Model comparison with ANOVA

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free