CenterScale: Centering and Scaling

Description Usage Arguments Details Value References See Also Examples

View source: R/CenterScale.R

Description

This function either centers and scales a continuous variable and provides options for binary variables, or returns an untransformed variable from a centered and scaled variable.

Usage

1
CenterScale(x, Binary="none", Inverse=FALSE, mu, sigma, Range, Min)

Arguments

x

This is a vector to be centered and scaled, or to be untransformed if Inverse=TRUE.

Binary

This argument indicates how binary variables will be treated, and defaults to "none", which keeps the original scale, or transforms the variable to the 0-1 range, if not already there. With "center", it will center the binary variable by subtracting the mean. With "center0", it centers the binary variable at zero, recoding a 0 to -0.5, and a 1 to 0.5. Finally, "centerscale" will center and scale the binary variable, subtracting the mean and dividing by two standard deviations.

Inverse

Logical. If TRUE, then a centered and scaled variable x will be transformed to its original, un-centered and un-scaled state. This defaults to FALSE.

mu, sigma, Range, Min

These arguments are required only when Inverse=TRUE, where mu is the mean, sigma is the standard deviation, Range is the range, and Min is the minimum of the original x. Range and Min are used only when Binary="none" or Binary="center0".

Details

Gelman (2008) recommends centering and scaling continuous predictors to facilitate MCMC convergence and enable comparisons between coefficients of centered and scaled continuous predictors with coefficients of untransformed binary predictors. A continuous predictor is centered and scaled as follows: x.cs <- (x - mean(x)) / (2*sd(x)). This is an improvement over the usual practice of standardizing predictors, which is x.z <- (x - mean(x)) / sd(x), where coefficients cannot be validly compared between binary and continuous predictors.

In MCMC, such as in LaplacesDemon, a centered and scaled predictor often results in a higher effective sample size (ESS), and therefore the chain mixes better. Centering and scaling is a method of re-parameterization to improve mixing.

Griffin and Brown (2013) also assert that the user may not want to scale predictors that are measured on the same scale, since scaling in this case may increase noisy, low signals. In this case, centering (without scaling) is recommended. To center a predictor, subtract its mean.

Value

The CenterScale function returns a centered and scaled vector, or the untransformed vector.

References

Gelman, A. (2008). "Scaling Regression Inputs by Dividing by Two Standard Devations". Statistics in Medicine, 27, p. 2865–2873.

Griffin, J.E. and Brown, P.J. (2013) "Some Priors for Sparse Regression Modelling". Bayesian Analysis, 8(3), p. 691–702.

See Also

ESS, IterativeQuadrature, LaplaceApproximation, LaplacesDemon, and PMC.

Examples

1
2
3
4
5
### See the LaplacesDemon function for an example in use.
library(LaplacesDemon)
x <- rnorm(100,10,1)
x.cs <- CenterScale(x)
x.orig <- CenterScale(x.cs, Inverse=TRUE, mu=mean(x), sigma=sd(x))

LaplacesDemon documentation built on July 9, 2021, 5:07 p.m.