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