Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
---
title: Unstandardizing coefficients from a GLMM
author: Ariel Muldoon
date: '2018-03-26'
slug: unstandardizing-coefficients
categories:
- r
- statistics
tags:
- glmm
- lme4
draft: FALSE
description: "Unstandardizing coefficients in order to interpret them on the original scale is often necessary when explanatory variables were standardized to help with model convergence when fitting generalized linear mixed models. Here I show one automated approach to unstandardize coefficients from a generalized linear mixed model fit with lme4."
---
```{r setup, echo = FALSE, message = FALSE, purl = FALSE}
knitr::opts_chunk$set(comment = "#")
devtools::source_gist("2500a85297b742c6f2fb3a14549f5851",
filename = 'render_toc.R')
options(pillar.sigfig = 4, # More #'s in tibble output
scipen = 999) # No scientific notation
```
*This post was last updated on `r format(Sys.Date(), "%Y-%m-%d")`.*
Fitting generalized linear mixed models (GLMM) can be tricky. For example, having explanatory variables with very different magnitudes can cause problems with model convergence. *Standardizing* the explanatory variables by subtracting the mean and dividing by the standard deviation prior to model fitting can often fix this issue. However, the interpretation of standardized coefficients is about the change in mean response for a 1 standard deviation increase in a variable; this is not particularly intuitive. Most often the coefficients need to be converted back to their unstandardized values for interpretation.
It turns out the math for converting the standardized slope estimates to unstandardized ones is fairly straightforward. Coefficients for each variable need to be divided by the standard deviation of that variable (this is only true for slopes, not intercepts). A nice write-up of the math involved is shown in the answer to [this Cross Validated question](https://stats.stackexchange.com/questions/74622/converting-standardized-betas-back-to-original-variables).
The first time I went though the process of unstandardizing many explanatory variables the process felt pretty clunky. Since then I've managed to tidy things up quite a bit to make it more automated. I'll outline that process in this post.
## Table of Contents
```{r toc, echo = FALSE, purl = FALSE}
render_toc("2018-03-26-unstandardizing-coefficients-from-a-glmm.Rmd")
```
# R packages
Model fitting will be done via **lme4**. Data manipulation tools from **dplyr** will be useful for getting results tidied up. I'll also use helper functions from **purrr** to loop through variables and **broom.mixed** for the tidy extraction of fixed-effects coefficients from mixed models.
```{r, message = FALSE, warning = FALSE}
library(lme4) # v. 1.1-23
suppressPackageStartupMessages( library(dplyr) ) # v. 1.0.0
library(purrr) # 0.3.4
library(broom.mixed) # 0.2.6
```
# The dataset
The dataset I'll use is named `cbpp`, and comes with **lme4**. The response variable in this dataset is counted proportions, so the data will be analyzed via a binomial generalized linear mixed model.
```{r}
glimpse(cbpp)
```
This dataset has no continuous explanatory variables in it, so I'll add some to demonstrate standardizing/unstandardizing. I create three new variables with very different ranges.
```{r}
set.seed(16)
cbpp = mutate(cbpp,
y1 = rnorm(56, 500, 100),
y2 = runif(56, 0, 1),
y3 = runif(56, 10000, 20000) )
glimpse(cbpp)
```
# Analysis
## Unstandardized variables
I will first fit the binomial GLMM model without standardizing the explanatory variables to demonstrate the resulting warning messages. The first message tells me that I should consider rescaling the variables since they are on very different scales.
```{r}
fit1 = glmer( cbind(incidence, size - incidence) ~ y1 + y2 + y3 + (1|herd),
data = cbpp, family = binomial)
```
## Standardizing the variables
To get the model to converge without warnings I'll need to standardize the three explanatory variables. You can manually subtract the mean and divide by the standard deviation, but the `scale()` function conveniently does this all at once.
I do the work inside `mutate_at()` so I can standardize all three variables at once. I add "s" as the suffix by assigning a name within the `.funs` list. Adding the suffix allows me to keep the original variables in the dataset. I use `as.numeric()` to convert the matrix that the `scale()` function returns into a vector.
```{r, warning = FALSE}
cbpp = mutate_at(cbpp,
.vars = vars( y1:y3 ),
.funs = list(s = ~as.numeric( scale(.) ) ) )
glimpse(cbpp)
```
## Standardized model
The model with standardized variables converges without any problems.
```{r}
fit2 = glmer( cbind(incidence, size - incidence) ~ y1_s + y2_s + y3_s + (1|herd),
data = cbpp, family = binomial)
```
# Unstandardizing slope coefficients
## Extract coefficients and profile confidence intervals
If I want to use this model for inference I'll need to unstandardize the coefficients and confidence interval limits before reporting them so they are more easily interpretable.
The first step in the process is to get the standardized estimates and confidence intervals from the model `fit2`. I use `tidy()` from package **broom** for this, which returns a data.frame of coefficients, statistical tests, and confidence intervals. The help page is at `?tidy.merMod` if you want to explore some of the options.
Using `tidy()`, I extract the fixed effects along with profile likelihood confidence intervals.
```{r}
coef_st = tidy(fit2,
effects = "fixed",
conf.int = TRUE,
conf.method = "profile")
coef_st
```
## Calculate standard deviations for each variable
Next I'll calculate the standard deviations for each variable. If I do this right I'll be able to put the standard deviations into a data.frame that I can join to `coef_st`. That way dividing the estimated slopes and confidence interval limits by the standard deviation will be straightforward.
I will calculate the standard deviations per variable with `map()` from **purrr**, as it is a convenient way to loop through columns. I pull out the variables I want to calculate standard deviations for via `select()`. An alternative approach would have been to take the variables from columns and put them in rows (i.e., put the data in *long* format), and then summarize by groups.
The output from `map()` returns a list, which can be stacked into a long format data.frame via `utils::stack()`. This results in a two column data.frame, with a column for the standard deviation (called `values`) and a column with the variable names (called `ind`).
```{r}
cbpp %>%
select(y1:y3) %>%
map(sd) %>%
stack()
```
The variables in my model output end with `_s` , so I need to add that suffix to the variable names. I also decided to rename `values` to `sd` so the column name is more useful.
```{r}
sd_all = cbpp %>%
select(y1:y3) %>%
map(sd) %>%
stack() %>%
rename(sd = values) %>%
mutate(ind = paste(ind, "s", sep = "_") )
sd_all
```
## Joining the standard deviations to the coefficients table
Once the names of the variables match between the datasets I can join the "standard deviations" data.frame to the "coefficients" data.frame. I'm not unstandardizing the intercept at this point, so I use `inner_join()` to keep only rows that have a match in both data.frames. Notice that the columns I'm joining by have different names in the two data.frames.
```{r}
coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") )
```
Once everything in one data.frame I can divide `estimate`, `conf.low` and `conf.high` by `sd` via `mutate_at()`. I will round the results, as well, although I am ignoring the vast differences in the variable range when I do this rounding.
```{r, warning = FALSE}
coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") ) %>%
mutate_at( .vars = vars(estimate, conf.low, conf.high),
.funs = list(~round( ./sd, 4) ) )
```
I'll get rid of the extra variables via `select()`, so I end up with only the unstandardized coefficients and confidence interval limits along with the variable names.
```{r}
coef_unst = coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") ) %>%
mutate_at( .vars = vars(estimate, conf.low, conf.high),
.funs = list(~round( ./sd, 4) ) ) %>%
select(-effect, -(std.error:p.value), -sd)
coef_unst
```
## Estimates from the unstandardized model
Note that the estimated coefficients are the same from the model where I manually unstandardize the coefficients (above) and the model fit using unstandardized variables.
```{r}
round( fixef(fit1)[2:4], 4)
```
Given that the estimates are the same, couldn't we simply go back and fit the unstandardized model and ignore the warning message? That would be nice, but unfortunately the convergence issues can cause problems when trying to calculate profile likelihood confidence intervals so this simpler approach doesn't always work.
In this case there are a bunch of warnings (not shown), and the profile likelihood confidence interval limits aren't successfully calculated for some of the coefficients.
```{r, warning = FALSE}
tidy(fit1,
effects = "fixed",
conf.int = TRUE,
conf.method = "profile")
```
# Further (important!) work
These results are all on the scale of log-odds, and I would exponentiate the unstandardized coefficients to the odds scale when reporting.
One thing I didn't discuss in this post that is important to consider is the appropriate and practically interesting unit increase for each variable. Clearly the effect of a 1 unit increase in the variable may not be of interest for at least `y2` (range between 0 and 1) and `y3` (range between 10000 and 20000). In the first case, 1 unit encompasses the entire range of the variable and in the second case 1 unit appears to be much smaller than the scale of the measurement.
The code to calculate the change in odds for a practically interesting increase in each explanatory variable would be similar to what I've done above. I would create a data.frame with the unit increase of interest for each variable in it, join this to the "coefficients" dataset, and multiply all estimates and CI by those values. The multiplication step can occur before or after unstandardizing but *must* happen before doing exponentiation/inverse-linking. I'd report the unit increase used for each variable in any tables of results so the reader can see that the reported estimate is a change in estimated odds/mean for the given practically important increase in the variable.
# Just the code, please
```{r getlabels, echo = FALSE, purl = FALSE}
labs = knitr::all_labels()
labs = labs[!labs %in% c("setup", "toc", "getlabels", "allcode", "makescript")]
```
```{r makescript, include = FALSE, purl = FALSE}
options(knitr.duplicate.label = "allow") # Needed to purl like this
input = knitr::current_input() # filename of input document
output = here::here("static", "script", paste(tools::file_path_sans_ext(input), "R", sep = ".") )
knitr::purl(input, output, documentation = 0, quiet = TRUE)
```
Here's the code without all the discussion. Copy and paste the code below or you can download an R script of uncommented code [from here](/script/2018-03-26-unstandardizing-coefficients-from-a-glmm.R).
```{r allcode, ref.label = labs, eval = FALSE, purl = FALSE}
```