A Quick Start Guide on Using std_selected()

Shu Fai Cheung

2022-08-07

This Guide Shows to use std_selected() to:

Sample Dataset

library(stdmod)
dat <- sleep_emo_con
head(dat, 3)
#> # A tibble: 3 × 6
#>   case_id sleep_duration  cons  emot   age gender
#>     <int>          <dbl> <dbl> <dbl> <dbl> <chr> 
#> 1       1              6   3.6   3.6    20 female
#> 2       2              4   3.8   2.4    20 female
#> 3       3              7   4.3   2.7    20 female

This dataset has 500 cases, with sleep duration (measured in average hours), conscientiousness, emotional stability, age, and gender (a "female" and "male").

The names of some variables are shortened for readability:

colnames(dat)[2:4] <- c("sleep", "cons", "emot")
head(dat, 3)
#> # A tibble: 3 × 6
#>   case_id sleep  cons  emot   age gender
#>     <int> <dbl> <dbl> <dbl> <dbl> <chr> 
#> 1       1     6   3.6   3.6    20 female
#> 2       2     4   3.8   2.4    20 female
#> 3       3     7   4.3   2.7    20 female

Model

Suppose this is the moderated regression model:

lm() can be used to fit this model:

lm_out <- lm(sleep ~ age + gender + emot * cons,
             dat = dat)
summary(lm_out)
#> 
#> Call:
#> lm(formula = sleep ~ age + gender + emot * cons, data = dat)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -6.0841 -0.7882  0.0089  0.9440  6.1189 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)  1.85154    1.35224   1.369  0.17155   
#> age          0.01789    0.02133   0.838  0.40221   
#> gendermale  -0.26127    0.16579  -1.576  0.11570   
#> emot         1.32151    0.45039   2.934  0.00350 **
#> cons         1.20385    0.37062   3.248  0.00124 **
#> emot:cons   -0.33140    0.13273  -2.497  0.01286 * 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.384 on 494 degrees of freedom
#> Multiple R-squared:  0.0548, Adjusted R-squared:  0.04523 
#> F-statistic: 5.728 on 5 and 494 DF,  p-value: 3.768e-05

The unstandardized moderation effect is significant, B = -0.3314. For each one unit increase of conscientiousness score, the effect of emotional stability decreases by 0.3314.

Correct Standardization For the Moderated Regression

Suppose we want to find the correct standardized solution for the moderated regression, that is, all variables except for categorical variables are standardized. In a moderated regression model, the product term should be formed after standardization.

Instead of doing the standardization ourselves before calling lm(), we can pass the lm() output to std_selected(), and use ~ . for the arguments to_scale and to_center.

lm_stdall <- std_selected(lm_out,
                          to_scale = ~ .,
                          to_center = ~ .)
summary(lm_stdall)
#> 
#> Selected variable(s) are centered by mean and/or scaled by SD
#> - Variable(s) centered: sleep age gender emot cons
#> - Variable(s) scaled: sleep age gender emot cons
#> 
#>        centered_by scaled_by                            Note
#> sleep     6.776333 1.4168291 Standardized (mean = 0, SD = 1)
#> age      22.274000 2.9407857 Standardized (mean = 0, SD = 1)
#> gender          NA        NA Nonnumeric                     
#> emot      2.713200 0.7629613 Standardized (mean = 0, SD = 1)
#> cons      3.343200 0.6068198 Standardized (mean = 0, SD = 1)
#> 
#> Note:
#> - Categorical variables will not be centered or scaled even if requested.
#> 
#> Call:
#> lm(formula = sleep ~ age + gender + emot * cons, data = dat_mod)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -4.2941 -0.5563  0.0063  0.6663  4.3187 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)   
#> (Intercept)  0.05492    0.04883   1.125  0.26124   
#> age          0.03712    0.04428   0.838  0.40221   
#> gendermale  -0.18440    0.11702  -1.576  0.11570   
#> emot         0.11501    0.04493   2.560  0.01076 * 
#> cons         0.13050    0.04517   2.889  0.00403 **
#> emot:cons   -0.10829    0.04337  -2.497  0.01286 * 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.9771 on 494 degrees of freedom
#> Multiple R-squared:  0.0548, Adjusted R-squared:  0.04523 
#> F-statistic: 5.728 on 5 and 494 DF,  p-value: 3.768e-05
#> 
#> Note:
#> - Estimates and their statistics are based on the data after mean-centering, scaling, or standardization.

In this example, the coefficient of the product term, which naturally can be called the
standardized moderation effect, is significant, B = -0.1083. For each one standard deviation increase of conscientiousness score, the standardized effect of emotional stability decreases by 0.1083.

The Arguments

Standardization is equivalent to centering by mean and then scaling by (dividing by) standard deviation.
The argument to_center specifies the variables to be centered by their means, and the argument to_scale specifies the variables to be scaled by their standard deviations. The formula interface of lm() is used in these two arguments, with the variables on the right hand side being the variables to be centered and/or scaled.

The “.” on the right hand side represents all variables in the model, including the outcome variable (sleep duration in this example).

std_selected() will also skip categorical variables automatically skipped because standardizing them will make their coefficients not easy to interpret.

Advantage

Using std_selected minimizes impact on the workflow. Do regression as usual. Get the correct standardized coefficients only when we need to interpret them.

Nonparametric Bootstrap Confidence Intervals

There is one problem with standardized coefficients. The confidence intervals based on ordinary least squares (OLS) fitted to the standardized variables do not take into account the sampling variation of the sample means and standard deviations (Yuan & Chan, 2011). Cheung, Cheung, Lau, Hui, and Vong (2022) suggest using nonparametric bootstrapping, with standardization conducted in each bootstrap sample.

This can be done by std_selected_boot(), a wrapper of std_selected():

set.seed(870432)
lm_stdall_boot <- std_selected_boot(lm_out,
                        to_scale = ~ .,
                        to_center = ~ .,
                        nboot = 5000)

The minimum additional argument is nboot, the number of bootstrap samples.

summary(lm_stdall_boot)
#> 
#> Selected variable(s) are centered by mean and/or scaled by SD
#> - Variable(s) centered: sleep age gender emot cons
#> - Variable(s) scaled: sleep age gender emot cons
#> 
#>        centered_by scaled_by                            Note
#> sleep     6.776333 1.4168291 Standardized (mean = 0, SD = 1)
#> age      22.274000 2.9407857 Standardized (mean = 0, SD = 1)
#> gender          NA        NA Nonnumeric                     
#> emot      2.713200 0.7629613 Standardized (mean = 0, SD = 1)
#> cons      3.343200 0.6068198 Standardized (mean = 0, SD = 1)
#> 
#> Note:
#> - Categorical variables will not be centered or scaled even if requested.
#> - Nonparametric bootstrapping 95% confidence intervals computed.
#> - The number of bootstrap samples is 5000.
#> 
#> Call:
#> lm(formula = sleep ~ age + gender + emot * cons, data = dat_mod)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -4.2941 -0.5563  0.0063  0.6663  4.3187 
#> 
#> Coefficients:
#>              Estimate  CI Lower  CI Upper Std. Error t value Pr(>|t|)   
#> (Intercept)  0.054919  0.007176  0.104459   0.048827   1.125  0.26124   
#> age          0.037125 -0.034742  0.107166   0.044280   0.838  0.40221   
#> gendermale  -0.184402 -0.439171  0.078317   0.117016  -1.576  0.11570   
#> emot         0.115014  0.029085  0.201193   0.044927   2.560  0.01076 * 
#> cons         0.130502  0.028767  0.226530   0.045167   2.889  0.00403 **
#> emot:cons   -0.108292 -0.204263 -0.008952   0.043374  -2.497  0.01286 * 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.9771 on 494 degrees of freedom
#> Multiple R-squared:  0.0548, Adjusted R-squared:  0.04523 
#> F-statistic: 5.728 on 5 and 494 DF,  p-value: 3.768e-05
#> 
#> Note:
#> - Estimates and their statistics are based on the data after mean-centering, scaling, or standardization.
#> - [CI Lower, CI Upper] are bootstrap percentile confidence intervals.
#> - Std. Error are not bootstrap SEs.

The output is similar to that of std_selected(), with additional information on the bootstrapping process.

The 95% bootstrap percentile confidence interval of the standardized moderation effect is -0.2043 to -0.0090.

Standardize Independent Variable (Focal Variable) and Moderator

std_selected() and std_selected_boot() can also be used to standardize only selected variables. There are cases in which we do not want to standardize some continuous variables because they are measured on interpretable units, such as hours.

Suppose we want to standardize only emotional stability and conscientiousness, and do not standardize sleep duration. We just list emot and cons on to_center and to_scale:

lm_std1 <- std_selected(lm_out,
                        to_scale = ~ emot + cons,
                        to_center = ~ emot + cons)
summary(lm_std1)
#> 
#> Selected variable(s) are centered by mean and/or scaled by SD
#> - Variable(s) centered: emot cons
#> - Variable(s) scaled: emot cons
#> 
#>        centered_by scaled_by                            Note
#> sleep       0.0000 1.0000000                                
#> age         0.0000 1.0000000                                
#> gender          NA        NA Nonnumeric                     
#> emot        2.7132 0.7629613 Standardized (mean = 0, SD = 1)
#> cons        3.3432 0.6068198 Standardized (mean = 0, SD = 1)
#> 
#> Note:
#> - Categorical variables will not be centered or scaled even if requested.
#> 
#> Call:
#> lm(formula = sleep ~ age + gender + emot * cons, data = dat_mod)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -6.0841 -0.7882  0.0089  0.9440  6.1189 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  6.45575    0.47828  13.498  < 2e-16 ***
#> age          0.01789    0.02133   0.838  0.40221    
#> gendermale  -0.26127    0.16579  -1.576  0.11570    
#> emot         0.16295    0.06365   2.560  0.01076 *  
#> cons         0.18490    0.06399   2.889  0.00403 ** 
#> emot:cons   -0.15343    0.06145  -2.497  0.01286 *  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.384 on 494 degrees of freedom
#> Multiple R-squared:  0.0548, Adjusted R-squared:  0.04523 
#> F-statistic: 5.728 on 5 and 494 DF,  p-value: 3.768e-05
#> 
#> Note:
#> - Estimates and their statistics are based on the data after mean-centering, scaling, or standardization.

The partially standardized moderation effect is -0.1534. For each one standard deviation increase of conscientiousness score, the partially standardized effect of emotional stability decreases by 0.1534.

Nonparametric Bootstrap Confidence Intervals

The function std_selected_boot() can also be used to form the nonparametric bootstrap confidence interval when only some of the variables are standardized:

set.seed(870432)
lm_std1_boot <- std_selected_boot(lm_out,
                        to_scale = ~ emot + cons,
                        to_center = ~ emot + cons,
                        nboot = 5000)

Again, the only additional argument is nboot.

summary(lm_std1_boot)
#> 
#> Selected variable(s) are centered by mean and/or scaled by SD
#> - Variable(s) centered: emot cons
#> - Variable(s) scaled: emot cons
#> 
#>        centered_by scaled_by                            Note
#> sleep       0.0000 1.0000000                                
#> age         0.0000 1.0000000                                
#> gender          NA        NA Nonnumeric                     
#> emot        2.7132 0.7629613 Standardized (mean = 0, SD = 1)
#> cons        3.3432 0.6068198 Standardized (mean = 0, SD = 1)
#> 
#> Note:
#> - Categorical variables will not be centered or scaled even if requested.
#> - Nonparametric bootstrapping 95% confidence intervals computed.
#> - The number of bootstrap samples is 5000.
#> 
#> Call:
#> lm(formula = sleep ~ age + gender + emot * cons, data = dat_mod)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -6.0841 -0.7882  0.0089  0.9440  6.1189 
#> 
#> Coefficients:
#>             Estimate CI Lower CI Upper Std. Error t value Pr(>|t|)    
#> (Intercept)  6.45575  5.64871  7.27347    0.47828  13.498  < 2e-16 ***
#> age          0.01789 -0.01839  0.05436    0.02133   0.838  0.40221    
#> gendermale  -0.26127 -0.62328  0.11048    0.16579  -1.576  0.11570    
#> emot         0.16295  0.04049  0.28927    0.06365   2.560  0.01076 *  
#> cons         0.18490  0.04153  0.32293    0.06399   2.889  0.00403 ** 
#> emot:cons   -0.15343 -0.29147 -0.01238    0.06145  -2.497  0.01286 *  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 1.384 on 494 degrees of freedom
#> Multiple R-squared:  0.0548, Adjusted R-squared:  0.04523 
#> F-statistic: 5.728 on 5 and 494 DF,  p-value: 3.768e-05
#> 
#> Note:
#> - Estimates and their statistics are based on the data after mean-centering, scaling, or standardization.
#> - [CI Lower, CI Upper] are bootstrap percentile confidence intervals.
#> - Std. Error are not bootstrap SEs.

The 95% bootstrap percentile confidence interval of the partially standardized moderation effect is -0.2915 to -0.0124.

Further Information

A more detailed illustration can be found at vignette("moderation").

vignette("std_selected") illustrates how std_selected() can be used to form nonparametric bootstrap percentile confidence interval for standardized regression coefficients (“betas”) for regression models without a product term.

Further information on the functions can be found in their help pages (std_selected() and std_selected_boot()). For example, parallel computation can be used when doing bootstrapping, if the number of bootstrapping samples request is large.

Reference(s)

Cheung, S. F., Cheung, S.-H., Lau, E. Y. Y., Hui, C. H., & Vong, W. N. (2022) Improving an old way to measure moderation effect in standardized units. Advance online publication. Health Psychology. https://doi.org/10.1037/hea0001188.

Yuan, K.-H., & Chan, W. (2011). Biases and standard errors of standardized regression coefficients. Psychometrika, 76(4), 670-690. https://doi.org/10.1007/s11336-011-9224-6