Skip to contents

This is brain-dead standardization of all variables in the design matrix. It mimics the silly output of SPSS, which standardizes all regressors, even if they represent categorical variables.

Usage

standardize(model)

# S3 method for class 'lm'
standardize(model)

Arguments

model

a fitted lm object

Value

an lm fitted with the standardized variables

a standardized regression object

See also

meanCenter which will center or re-scale only numberic variables

Author

Paul Johnson pauljohn@ku.edu

Examples


library(rockchalk)
N <- 100
dat <- genCorrelatedData(N = N, means = c(100,200), sds = c(20,30), rho = 0.4, stde = 10)
dat$x3 <- rnorm(100, m = 40, s = 4)

m1 <- lm(y ~ x1 + x2 + x3, data = dat)
summary(m1)
#> 
#> Call:
#> lm(formula = y ~ x1 + x2 + x3, data = dat)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -19.7411  -6.2368   0.3172   6.5262  19.4610 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) -3.27732   12.15321  -0.270  0.78800    
#> x1           0.16938    0.04708   3.598  0.00051 ***
#> x2           0.22327    0.03126   7.142 1.77e-10 ***
#> x3          -0.02077    0.24607  -0.084  0.93292    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 8.868 on 96 degrees of freedom
#> Multiple R-squared:  0.4891,	Adjusted R-squared:  0.4731 
#> F-statistic: 30.63 on 3 and 96 DF,  p-value: 5.575e-14
#> 

m1s <- standardize(m1)
summary(m1s)
#> All variables in the model matrix and the dependent variable
#> were centered. The centered variables have the letter "s" appended to their
#> non-centered counterparts, even constructed
#> variables like `x1:x2` and poly(x1,2). We agree, that's probably
#> ill-advised, but you asked for it by running standardize().
#> 
#> The rockchalk function meanCenter is a smarter option, probably. 
#> 
#> The summary statistics of the variables in the design matrix. 
#>     mean std.dev.
#> ys     0        1
#> x1s    0        1
#> x2s    0        1
#> x3s    0        1
#> 
#> Call:
#> lm(formula = ys ~ -1 + x1s + x2s + x3s, data = stddat)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -1.61580 -0.51048  0.02596  0.53416  1.59287 
#> 
#> Coefficients:
#>      Estimate Std. Error t value Pr(>|t|)    
#> x1s  0.279140   0.077189   3.616 0.000476 ***
#> x2s  0.556095   0.077459   7.179 1.42e-10 ***
#> x3s -0.006223   0.073354  -0.085 0.932572    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.7221 on 97 degrees of freedom
#> Multiple R-squared:  0.4891,	Adjusted R-squared:  0.4733 
#> F-statistic: 30.95 on 3 and 97 DF,  p-value: 4.005e-14
#> 



m2 <- lm(y ~ x1 * x2 + x3, data = dat)
summary(m2)
#> 
#> Call:
#> lm(formula = y ~ x1 * x2 + x3, data = dat)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -19.6403  -6.2700   0.3927   6.5660  19.3667 
#> 
#> Coefficients:
#>               Estimate Std. Error t value Pr(>|t|)
#> (Intercept)  0.6156143 31.3667874   0.020    0.984
#> x1           0.1280068  0.3106719   0.412    0.681
#> x2           0.2027410  0.1555218   1.304    0.196
#> x3          -0.0174722  0.2485387  -0.070    0.944
#> x1:x2        0.0002087  0.0015492   0.135    0.893
#> 
#> Residual standard error: 8.914 on 95 degrees of freedom
#> Multiple R-squared:  0.4892,	Adjusted R-squared:  0.4677 
#> F-statistic: 22.74 on 4 and 95 DF,  p-value: 3.364e-13
#> 

m2s <- standardize(m2)
summary(m2s)
#> All variables in the model matrix and the dependent variable
#> were centered. The centered variables have the letter "s" appended to their
#> non-centered counterparts, even constructed
#> variables like `x1:x2` and poly(x1,2). We agree, that's probably
#> ill-advised, but you asked for it by running standardize().
#> 
#> The rockchalk function meanCenter is a smarter option, probably. 
#> 
#> The summary statistics of the variables in the design matrix. 
#>          mean std.dev.
#> ys          0        1
#> x1s         0        1
#> x2s         0        1
#> x3s         0        1
#> `x1:x2s`    0        1
#> 
#> Call:
#> lm(formula = ys ~ -1 + x1s + x2s + x3s + `x1:x2s`, data = stddat)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -1.60754 -0.51319  0.03214  0.53742  1.58515 
#> 
#> Coefficients:
#>           Estimate Std. Error t value Pr(>|t|)
#> x1s       0.210955   0.509313   0.414    0.680
#> x2s       0.504975   0.385341   1.310    0.193
#> x3s      -0.005236   0.074087  -0.071    0.944
#> `x1:x2s`  0.098182   0.724827   0.135    0.893
#> 
#> Residual standard error: 0.7258 on 96 degrees of freedom
#> Multiple R-squared:  0.4892,	Adjusted R-squared:  0.4679 
#> F-statistic: 22.98 on 4 and 96 DF,  p-value: 2.429e-13
#> 

m2c <- meanCenter(m2)
summary(m2c)
#> These variables were mean-centered before any transformations were made on the design matrix.
#> [1] "x1c" "x2c"
#> The centers and scale factors were 
#>            x1c      x2c
#> mean  99.94682 200.9469
#> scale  1.00000   1.0000
#> The summary statistics of the variables in the design matrix (after centering). 
#>             mean std.dev.
#> y        57.6796  12.2176
#> x1c       0.0000  20.1346
#> x2c       0.0000  30.4309
#> x3       40.2900   3.6611
#> x1c:x2c 198.0943 587.2567
#> 
#> The following results were produced from: 
#> meanCenter.default(model = m2)
#> 
#> Call:
#> lm(formula = y ~ x1c * x2c + x3, data = stddat)
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -19.6403  -6.2700   0.3927   6.5660  19.3667 
#> 
#> Coefficients:
#>               Estimate Std. Error t value Pr(>|t|)    
#> (Intercept) 58.3421697 10.0879012   5.783 9.32e-08 ***
#> x1c          0.1699542  0.0475148   3.577 0.000549 ***
#> x2c          0.2236047  0.0315222   7.094 2.32e-10 ***
#> x3          -0.0174722  0.2485387  -0.070 0.944103    
#> x1c:x2c      0.0002087  0.0015492   0.135 0.893096    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 8.914 on 95 degrees of freedom
#> Multiple R-squared:  0.4892,	Adjusted R-squared:  0.4677 
#> F-statistic: 22.74 on 4 and 95 DF,  p-value: 3.364e-13
#>