Get Started

Calculating odds-ratios

In the previous exercise, we saw how to compare the effects of a friend's recommendation on sales. However, regression outputs can be hard to describe and sometimes odds-ratios can be easier to use. Using the outputs from the previous exercise, we're going to calculate odds-ratios.

Refresher on odds-ratios:

  • If an odds-ratio is 1.0, then both events have an equal chance of occurring. For example, if the odds-ratio for a friend's recommendation was 1.0, then a friend would have no influence on a purchase decision.
  • If an odds-ratio is less than 1, then a friend's recommendation would decrease the chance of a purchase occurring. For example, an odds-ratio of 0.5 would mean a friend's recommendation has odds of 1:2 or 1 purchase occurring for every 2 passes.
  • If an odds-ratio is greater than 1, then a friend's recommendation would increase the chance of a purchase occurring. For example, an odds-ratio of 3.0 would mean a friend's recommendation has odds of 3:1 or 3 purchases occurring for every 1 passes.

Note on course code: Since this course launched, the broom package has dropped support for lme4::lmer() models. If you try to repeat this on your own, you will need the broom.mixed package, which is on cran.

This is a part of the course

“Hierarchical and Mixed Effects Models in R”

View Course

Exercise instructions

  • Look at the summary() of model_out.
  • Extract the coefficients from model_out with fixef() and then convert to an odds-ratio by taking exponential. Repeat with confint() to get the confidence intervals.
  • Calculate the confidence intervals and then exponentiate the effect of friends on a purchase using tidy(). Make sure to set the conf.int and exponentiate parameters.

Hands-on interactive exercise

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

# Run the code to see how to calculate odds ratios
summary( ___) 
exp(___(model_out))
exp(___(model_out))

# Create the tidied output
tidy(model_out, conf.int = ___, exponentiate = ___)

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 extends linear mixed-effects models to include non-normal error terms using generalized linear mixed-effects models. By altering the model to include a non-normal error term, you are able to model more kinds of data with non-linear responses. After reviewing generalized linear models, the chapter examines binomial data and count data in the context of mixed-effects models.

Exercise 1: Crash course on GLMsExercise 2: Logistic regressionExercise 3: Poisson RegressionExercise 4: Plotting GLMsExercise 5: Binomial dataExercise 6: Toxicology dataExercise 7: Marketing exampleExercise 8: Calculating odds-ratios
Exercise 9: Count dataExercise 10: Internet click-throughsExercise 11: Chlamydia by age-group and countyExercise 12: Displaying chlamydia results

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