The last post I did about making added variable plots led me to think about other “get model results” topics, such as the one I’m talking about today: unstandardizing coefficients.

I find this comes up particularly for generalized linear mixed models (GLMM), where models don’t always converge if explanatory variables are left unstandardized. The lack of convergence can be caused by explanatory variables with very different magnitudes, and standardizing the variables prior to model fitting can be useful. In such cases, coefficients and confidence interval limits will often need to be converted to their unstandardized values for interpretation. I don’t find thinking about the change in mean response for a 1 standard deviation increase in a variable to be super intuitive, which is the interpretation of a standardized coefficient.

The math for converting the standardized slope estimates to unstandardized ones turns out to be 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). The math is shown here.

The first time I went though this process was quite clunky. Since then I’ve managed to tidy things up quite a bit through work with students, and things are now much more organized.

# R packages

Model fitting will be done via lme4, which is where I’ve most often needed to do this. 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 for the tidy extraction of fixed-effects coefficients from the model.

library(lme4) # v. 1.1-15
suppressPackageStartupMessages( library(dplyr) ) # v. 0.7.4
library(purrr) # 0.2.4
library(broom) # 0.4.3

# The dataset

The dataset I’ll use is named cbpp, and comes with lme4. It is a dataset that has a response variable that is counted proportions, so the data will be analyzed via a binomial generalized linear mixed model.

glimpse(cbpp)
# Observations: 56
# Variables: 4
# $herd <fct> 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5... #$ incidence <dbl> 2, 3, 4, 0, 3, 1, 1, 8, 2, 0, 2, 2, 0, 2, 0, 5, 0, 0...
# $size <dbl> 14, 12, 9, 5, 22, 18, 21, 22, 16, 16, 20, 10, 10, 9,... #$ period    <fct> 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3...

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.

set.seed(16)

cbpp = mutate(cbpp, y1 = rnorm(56, 500, 100),
y2 = runif(56, 0, 1),
y3 = runif(56, 10000, 20000) )

glimpse(cbpp)
# Observations: 56
# Variables: 7
# $herd <fct> 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5... #$ incidence <dbl> 2, 3, 4, 0, 3, 1, 1, 8, 2, 0, 2, 2, 0, 2, 0, 5, 0, 0...
# $size <dbl> 14, 12, 9, 5, 22, 18, 21, 22, 16, 16, 20, 10, 10, 9,... #$ period    <fct> 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3...
# $y1 <dbl> 547.6413, 487.4620, 609.6216, 355.5771, 614.7829, 45... #$ y2        <dbl> 0.87758754, 0.33596115, 0.11495285, 0.26466003, 0.99...
# control$checkConv, : Model failed to converge with max|grad| = 0.947357 # (tol = 0.001, component 1) # Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue # - Rescale variables?;Model is nearly unidentifiable: large eigenvalue ratio # - Rescale variables? ## Standardizing the variables I’ll now standardize the three explanatory variables, which involves subtracting the mean and then dividing by the standard deviation. The scale() function is one way to do this in R. I do the work inside mutate_at(), which allows me to choose the three variables I want to standardize by name and add “s” as the suffix by using a name in funs(). Adding the suffix allows me to keep the original variables, as I will need them later. I use as.numeric() to convert the matrix that the scale() function returns into a vector. cbpp = mutate_at(cbpp, vars( y1:y3 ), funs(s = as.numeric( scale(.) ) ) ) glimpse(cbpp) # Observations: 56 # Variables: 10 #$ herd      <fct> 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5...
# $incidence <dbl> 2, 3, 4, 0, 3, 1, 1, 8, 2, 0, 2, 2, 0, 2, 0, 5, 0, 0... #$ size      <dbl> 14, 12, 9, 5, 22, 18, 21, 22, 16, 16, 20, 10, 10, 9,...
# $period <fct> 1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3... #$ y1        <dbl> 547.6413, 487.4620, 609.6216, 355.5771, 614.7829, 45...
# $y2 <dbl> 0.87758754, 0.33596115, 0.11495285, 0.26466003, 0.99... #$ y3        <dbl> 14481.07, 11367.88, 14405.16, 18497.73, 17955.66, 10...
# $y1_s <dbl> 0.34007250, -0.32531152, 1.02536895, -1.78352139, 1.... #$ y2_s      <dbl> 1.4243017, -0.4105502, -1.1592536, -0.6520950, 1.837...
# \$ y3_s      <dbl> -0.2865770, -1.3443309, -0.3123665, 1.0781427, 0.893...

## Standardized model

The model with standardized variables converges without any problems.

fit2 = glmer( cbind(incidence, size - incidence) ~ y1_s + y2_s + y3_s + (1|herd),
data = cbpp, family = binomial)

# Unstandardizing slope coefficients

## Get coefficients and profile confidence intervals from model

If I want to use this model for inference I need to unstandardize the coefficients before reporting them to make them more easily interpretable.

The first step in the process is to get the standardized estimates and confidence intervals from the model. 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.

I use tidy() to extract the fixed effects along with profile likelihood confidence intervals.

coef_st = tidy(fit2, effects = "fixed",
conf.int = TRUE,
conf.method = "profile")
# Computing profile confidence intervals ...
coef_st
# # A tibble: 4 x 7
#   term        estimate std.error statistic  p.value conf.low conf.high
#   <chr>          <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
# 1 (Intercept)   -2.10      0.216    -9.70  3.04e-22 -2.57       -1.65
# 2 y1_s           0.264     0.140     1.89  5.85e- 2 -0.00931     0.547
# 3 y2_s           0.103     0.129     0.797 4.26e- 1 -0.151       0.364
# 4 y3_s           0.157     0.123     1.28  2.02e- 1 -0.0863      0.403

## Calculate standard deviations for each variable

I need the standard deviations for each variable in order to unstandardize the coefficients. If I do this right, I can get the standard deviations into a data.frame that I can then join to coef_st. Once that is done, 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).

map( select(cbpp, y1:y3), sd) %>%
stack()
#         values ind
# 1   90.4430192  y1
# 2    0.2951881  y2
# 3 2943.2098667  y3

The variables in my model and in my output end with _s , so I’ll need to add that suffix to the variable names in the “standard deviations” dataset prior to joining the two data.frames together.

sd_all = map( select(cbpp, y1:y3), sd) %>%
stack() %>%
mutate(ind = paste(ind, "s", sep = "_") )

sd_all
#         values  ind
# 1   90.4430192 y1_s
# 2    0.2951881 y2_s
# 3 2943.2098667 y3_s

## 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’ll 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.

coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") )
# # A tibble: 3 x 8
#   term  estimate std.error statistic p.value conf.low conf.high   values
#   <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>    <dbl>
# 1 y1_s     0.264     0.140     1.89   0.0585 -0.00931     0.547   90.4
# 2 y2_s     0.103     0.129     0.797  0.426  -0.151       0.364    0.295
# 3 y3_s     0.157     0.123     1.28   0.202  -0.0863      0.403 2943.

With everything in one data.frame I can easily divide estimate, conf.low and conf.high by the standard deviation in values via mutate_at(). I will round the results, as well, although I’m ignoring the vast differences in the variable range when I do this rounding.

coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") ) %>%
mutate_at( vars(estimate, conf.low, conf.high), funs(round( ./values, 4) ) )
# # A tibble: 3 x 8
#   term  estimate std.error statistic p.value conf.low conf.high   values
#   <chr>    <dbl>     <dbl>     <dbl>   <dbl>    <dbl>     <dbl>    <dbl>
# 1 y1_s   0.00290     0.140     1.89   0.0585  -0.0001    0.0061   90.4
# 2 y2_s   0.350       0.129     0.797  0.426   -0.513     1.23      0.295
# 3 y3_s   0.0001      0.123     1.28   0.202    0         0.0001 2943.

I’ll get rid of the extra variables via select(), so I end up with the unstandardized coefficients and confidence interval limits along with the variable name. I could also get the variable names cleaned up, possibly removing the suffix and/or capitalizing and adding units, etc., although I don’t do that with these fake variables today.

coef_unst = coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") ) %>%
mutate_at( vars(estimate, conf.low, conf.high), funs(round( ./values, 4) ) ) %>%
select(-(std.error:p.value), -values)

coef_unst
# # A tibble: 3 x 4
#   term  estimate conf.low conf.high
#   <chr>    <dbl>    <dbl>     <dbl>
# 1 y1_s   0.00290  -0.0001    0.0061
# 2 y2_s   0.350    -0.513     1.23
# 3 y3_s   0.0001    0         0.0001

## 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.

round( fixef(fit1)[2:4], 4)
#     y1     y2     y3
# 0.0029 0.3495 0.0001

Given that the estimates are the same, couldn’t we simply go back and fit the unstandardized model and ignored the warning message? Unfortunately, the convergence issues can cause problems when trying to calculate profile likelihood confidence intervals, so the 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.

tidy(fit1, effects = "fixed",
conf.int = TRUE,
conf.method = "profile")
# Computing profile confidence intervals ...
# # A tibble: 4 x 7
#   term          estimate std.error statistic   p.value   conf.low conf.high
#   <chr>            <dbl>     <dbl>     <dbl>     <dbl>      <dbl>     <dbl>
# 1 (Intercept) -4.58      1.09         -4.19  0.0000285   NA       NA
# 2 y1           0.00292   0.00152       1.92  0.0549      NA       NA
# 3 y2           0.350     0.437         0.800 0.424       NA       NA
# 4 y3           0.0000533 0.0000405     1.32  0.188       -2.93e-5  0.000137

# Further (important!) work

These results are all on the scale of log-odds, and I would exponentiate the unstandardized coefficients to the odds scale for reporting and interpretation.

Along these same lines, one thing I didn’t discuss in this post that is important to consider is the appropriate and interesting unit increase for each variable. Clearly the effect of a “1 unit” increase in the variable is likely not 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 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

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.

library(lme4) # v. 1.1-15
suppressPackageStartupMessages( library(dplyr) ) # v. 0.7.4
library(purrr) # 0.2.4
library(broom) # 0.4.3

glimpse(cbpp)

set.seed(16)

cbpp = mutate(cbpp, y1 = rnorm(56, 500, 100),
y2 = runif(56, 0, 1),
y3 = runif(56, 10000, 20000) )

glimpse(cbpp)

fit1 = glmer( cbind(incidence, size - incidence) ~ y1 + y2 + y3 + (1|herd),
data = cbpp, family = binomial)

cbpp = mutate_at(cbpp, vars( y1:y3 ), funs(s = as.numeric( scale(.) ) ) )

glimpse(cbpp)

fit2 = glmer( cbind(incidence, size - incidence) ~ y1_s + y2_s + y3_s + (1|herd),
data = cbpp, family = binomial)

coef_st = tidy(fit2, effects = "fixed",
conf.int = TRUE,
conf.method = "profile")

coef_st

map( select(cbpp, y1:y3), sd) %>%
stack()

sd_all = map( select(cbpp, y1:y3), sd) %>%
stack() %>%
mutate(ind = paste(ind, "s", sep = "_") )

sd_all

coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") )

coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") ) %>%
mutate_at( vars(estimate, conf.low, conf.high), funs(round( ./values, 4) ) )

coef_unst = coef_st %>%
inner_join(., sd_all, by = c("term" = "ind") ) %>%
mutate_at( vars(estimate, conf.low, conf.high), funs(round( ./values, 4) ) ) %>%
select(-(std.error:p.value), -values)

coef_unst

round( fixef(fit1)[2:4], 4)

tidy(fit1, effects = "fixed",
conf.int = TRUE,
conf.method = "profile")