Skip to contents

gscale standardizes variables by dividing them by 2 standard deviations and mean-centering them by default. It contains options for handling binary variables separately. gscale() is a fork of rescale from the arm package---the key feature difference is that gscale() will perform the same functions for variables in svydesign objects. gscale() is also more user-friendly in that it is more flexible in how it accepts input.

Usage

gscale(
  data = NULL,
  vars = NULL,
  binary.inputs = "center",
  binary.factors = FALSE,
  n.sd = 2,
  center.only = FALSE,
  scale.only = FALSE,
  weights = NULL,
  apply.weighted.contrasts = getOption("jtools-weighted.contrasts", FALSE),
  x = NULL,
  messages = FALSE
)

Arguments

data

A data frame or survey design. Only needed if you would like to rescale multiple variables at once. If x = NULL, all columns will be rescaled. Otherwise, x should be a vector of variable names. If x is a numeric vector, this argument is ignored.

vars

If data is a data.frame or similar, you can scale only select columns by providing a vector column names to this argument.

binary.inputs

Options for binary variables. Default is center; 0/1 keeps original scale; -0.5/0.5 rescales 0 as -0.5 and 1 as 0.5; center subtracts the mean; and full subtracts the mean and divides by 2 sd.

binary.factors

Coerce two-level factors to numeric and apply scaling functions to them? Default is FALSE.

n.sd

By how many standard deviations should the variables be divided by? Default for gscale is 2, like arm's rescale. 1 is the more typical standardization scheme.

center.only

A logical value indicating whether you would like to mean -center the values, but not scale them.

scale.only

A logical value indicating whether you would like to scale the values, but not mean-center them.

weights

A vector of weights equal in length to x. If iterating over a data frame, the weights will need to be equal in length to all the columns to avoid errors. You may need to remove missing values before using the weights.

apply.weighted.contrasts

Factor variables cannot be scaled, but you can set the contrasts such that the intercept in a regression model will reflect the true mean (assuming all other variables are centered). If set to TRUE, the argument will apply weighted effects coding to all factors. This is similar to the R default effects coding, but weights according to how many observations are at each level. An adapted version of contr.wec() from the wec package is used to do this. See that package's documentation and/or Grotenhuis et al. (2016) for more info.

x

Deprecated. Pass numeric vectors to data. Pass vectors of column names to vars.

messages

Print messages when variables are not processed due to being non-numeric or all missing? Default is FALSE.

Details

This function is adapted from the rescale function of the arm package. It is named gscale() after the popularizer of this scaling method, Andrew Gelman. By default, it works just like rescale. But it contains many additional options and can also accept multiple types of input without breaking a sweat.

Only numeric variables are altered when in a data.frame or survey design. Character variables, factors, etc. are skipped.

For those dealing with survey data, if you provide a survey.design object you can rest assured that the mean-centering and scaling is performed with help from the svymean() and svyvar() functions, respectively. It was among the primary motivations for creating this function. gscale() will not center or scale the weights variables defined in the survey design unless the user specifically requests them in the x = argument.

References

Gelman, A. (2008). Scaling regression inputs by dividing by two standard deviations. Statistics in Medicine, 27, 2865–2873. http://www.stat.columbia.edu/~gelman/research/published/standardizing7.pdf

Grotenhuis, M. te, Pelzer, B., Eisinga, R., Nieuwenhuis, R., Schmidt-Catran, A., & Konig, R. (2017). When size matters: Advantages of weighted effect coding in observational studies. International Journal of Public Health, 62, 163–167. https://doi.org/10.1007/s00038-016-0901-1 ( open access)

See also

j_summ is a replacement for the summary function for regression models. On request, it will center and/or standardize variables before printing its output.

Other standardization: center_mod(), center(), scale_mod(), standardize()

Author

Jacob Long jacob.long@sc.edu

Examples


x <- rnorm(10, 2, 1)
x2 <- rbinom(10, 1, .5)

# Basic use
gscale(x)
#>  [1] -0.660376779 -0.293748604  0.209033224 -0.721413084  0.661604672
#>  [6]  0.353635365  0.243043906  0.612905496 -0.407397811  0.002713616
# Normal standardization
gscale(x, n.sd = 1)
#>  [1] -1.320753557 -0.587497209  0.418066449 -1.442826169  1.323209343
#>  [6]  0.707270730  0.486087811  1.225810992 -0.814795623  0.005427233
# Scale only
gscale(x, scale.only = TRUE)
#>  [1] 0.4223727 0.7890009 1.2917827 0.3613364 1.7443542 1.4363849 1.3257934
#>  [8] 1.6956550 0.6753517 1.0854631
# Center only
gscale(x, center.only = TRUE)
#>  [1] -1.205314531 -0.536147655  0.381525806 -1.316717519  1.207555672
#>  [6]  0.645452502  0.443601836  1.118670318 -0.743579299  0.004952871
# Binary inputs
gscale(x2, binary.inputs = "0/1")
#>  [1] 0 0 1 0 1 0 0 1 1 1
gscale(x2, binary.inputs = "full") # treats it like a continous var
#>  [1] -0.4743416 -0.4743416  0.4743416 -0.4743416  0.4743416 -0.4743416
#>  [7] -0.4743416  0.4743416  0.4743416  0.4743416
gscale(x2, binary.inputs = "-0.5/0.5") # keep scale, center at zero
#>  [1] -0.5 -0.5  0.5 -0.5  0.5 -0.5 -0.5  0.5  0.5  0.5
gscale(x2, binary.inputs = "center") # mean center it
#>  [1] -0.5 -0.5  0.5 -0.5  0.5 -0.5 -0.5  0.5  0.5  0.5

# Data frame as input
# loops through each numeric column
gscale(data = mtcars, binary.inputs = "-0.5/0.5")
#>                             mpg        cyl        disp          hp        drat
#> Mazda RX4            0.07544241 -0.0524939 -0.28530991 -0.26754642  0.28375684
#> Mazda RX4 Wag        0.07544241 -0.0524939 -0.28530991 -0.26754642  0.28375684
#> Datsun 710           0.22477172 -0.6124289 -0.49509105 -0.39152023  0.23699979
#> Hornet 4 Drive       0.10862670 -0.0524939  0.11004685 -0.26754642 -0.48305877
#> Hornet Sportabout   -0.11536726  0.5074411  0.52154061  0.20647109 -0.41759890
#> Valiant             -0.16514370 -0.0524939 -0.02308349 -0.30400931 -0.78230388
#> Duster 360          -0.48039447  0.5074411  0.52154061  0.71695148 -0.36149044
#> Merc 240D            0.35750889 -0.6124289 -0.33896547 -0.61759012  0.08737724
#> Merc 230             0.22477172 -0.6124289 -0.36276756 -0.37693508  0.30245966
#> Merc 280            -0.07388690 -0.0524939 -0.25464959 -0.17274292  0.30245966
#> Merc 280C           -0.19003192 -0.0524939 -0.25464959 -0.17274292  0.30245966
#> Merc 450SE          -0.30617694  0.5074411  0.18185654  0.24293397 -0.49241018
#> Merc 450SL          -0.23151228  0.5074411  0.18185654  0.24293397 -0.49241018
#> Merc 450SLC         -0.40572981  0.5074411  0.18185654  0.24293397 -0.49241018
#> Cadillac Fleetwood  -0.80394131  0.5074411  0.97337691  0.42524840 -0.62332991
#> Lincoln Continental -0.80394131  0.5074411  0.92496588  0.49817417 -0.55787004
#> Chrysler Imperial   -0.44721018  0.5074411  0.84428082  0.60756282 -0.34278762
#> Fiat 128             1.02119472 -0.6124289 -0.61329465 -0.58841981  0.45208222
#> Honda Civic          0.85527326 -0.6124289 -0.62539740 -0.69051589  1.24695206
#> Toyota Corolla       1.14563581 -0.6124289 -0.64395497 -0.59571239  0.58300196
#> Toyota Corona        0.11692278 -0.6124289 -0.44627659 -0.36234992  0.09672865
#> Dodge Challenger    -0.38084159  0.5074411  0.35210200  0.02415666 -0.78230388
#> AMC Javelin         -0.40572981  0.5074411  0.29562247  0.02415666 -0.41759890
#> Camaro Z28          -0.56335520  0.5074411  0.48119809  0.71695148  0.12478288
#> Pontiac Firebird    -0.07388690  0.5074411  0.68291072  0.20647109 -0.48305877
#> Fiat X1-9            0.59809500 -0.6124289 -0.61208437 -0.58841981  0.45208222
#> Porsche 914-2        0.49024605 -0.6124289 -0.44546974 -0.40610538  0.77938156
#> Lotus Europa         0.85527326 -0.6124289 -0.54713290 -0.24566869  0.16218851
#> Ford Pantera L      -0.35595337  0.5074411  0.48523234  0.85551044  0.58300196
#> Ferrari Dino        -0.03240653 -0.0524939 -0.34582370  0.20647109  0.02191737
#> Maserati Bora       -0.42232196  0.5074411  0.28351971  1.37328341 -0.05289391
#> Volvo 142E           0.10862670 -0.6124289 -0.44264576 -0.27483900  0.48013645
#>                               wt         qsec   vs   am       gear        carb
#> Mazda RX4           -0.305199784 -0.388582573 -0.5  0.5  0.2117771  0.36760154
#> Mazda RX4 Wag       -0.174892635 -0.231890410 -0.5  0.5  0.2117771  0.36760154
#> Datsun 710          -0.458502312  0.213003408  0.5  0.5  0.2117771 -0.56107604
#> Hornet 4 Drive      -0.001149769  0.445243578  0.5 -0.5 -0.4659096 -0.56107604
#> Hornet Sportabout    0.113827127 -0.231890410 -0.5 -0.5 -0.4659096 -0.25151684
#> Valiant              0.124047296  0.663493376  0.5 -0.5 -0.4659096 -0.56107604
#> Duster 360           0.180258223 -0.562063181 -0.5 -0.5 -0.4659096  0.36760154
#> Merc 240D           -0.013924980  0.601935740  0.5 -0.5  0.2117771 -0.25151684
#> Merc 230            -0.034365317  1.413377296  0.5 -0.5  0.2117771 -0.25151684
#> Merc 280             0.113827127  0.126263104  0.5 -0.5  0.2117771  0.36760154
#> Merc 280C            0.113827127  0.294147564  0.5 -0.5  0.2117771  0.36760154
#> Merc 450SE           0.435762437 -0.125563586 -0.5 -0.5 -0.4659096  0.05804235
#> Merc 450SL           0.262019572 -0.069602099 -0.5 -0.5 -0.4659096  0.05804235
#> Merc 450SLC          0.287569993  0.042320874 -0.5 -0.5 -0.4659096  0.05804235
#> Cadillac Fleetwood   1.038752382  0.036724726 -0.5 -0.5 -0.4659096  0.36760154
#> Lincoln Continental  1.127667849 -0.008044464 -0.5 -0.5 -0.4659096  0.36760154
#> Chrysler Imperial    1.087298183 -0.119967437 -0.5 -0.5 -0.4659096  0.36760154
#> Fiat 128            -0.519823324  0.453637801  0.5  0.5  0.2117771 -0.56107604
#> Honda Civic         -0.818763254  0.187820739  0.5  0.5  0.2117771 -0.25151684
#> Toyota Corolla      -0.706341400  0.573954997  0.5  0.5  0.2117771 -0.56107604
#> Toyota Corona       -0.384406090  0.604733815  0.5 -0.5 -0.4659096 -0.56107604
#> Dodge Challenger     0.154707802 -0.273861525 -0.5 -0.5 -0.4659096 -0.25151684
#> AMC Javelin          0.111272085 -0.153544329 -0.5 -0.5 -0.4659096 -0.25151684
#> Camaro Z28           0.318230499 -0.682380377 -0.5 -0.5 -0.4659096  0.36760154
#> Pontiac Firebird     0.320785541 -0.223496187 -0.5 -0.5 -0.4659096 -0.25151684
#> Fiat X1-9           -0.655240557  0.294147564  0.5  0.5  0.2117771 -0.56107604
#> Porsche 914-2       -0.550483829 -0.321428789 -0.5  0.5  0.8894638 -0.25151684
#> Lotus Europa        -0.870886114 -0.265467302  0.5  0.5  0.8894638 -0.25151684
#> Ford Pantera L      -0.024145148 -0.937005142 -0.5  0.5  0.8894638  0.36760154
#> Ferrari Dino        -0.228548520 -0.657197709 -0.5  0.5  0.8894638  0.98671992
#> Maserati Bora        0.180258223 -0.909024398 -0.5  0.5  0.8894638  1.60583831
#> Volvo 142E          -0.223438435  0.210205334  0.5  0.5  0.2117771 -0.25151684

# Specified vars in data frame
gscale(mtcars, vars = c("hp", "wt", "vs"), binary.inputs = "center")
#>                      mpg cyl  disp          hp drat           wt  qsec      vs
#> Mazda RX4           21.0   6 160.0 -0.26754642 3.90 -0.305199784 16.46 -0.4375
#> Mazda RX4 Wag       21.0   6 160.0 -0.26754642 3.90 -0.174892635 17.02 -0.4375
#> Datsun 710          22.8   4 108.0 -0.39152023 3.85 -0.458502312 18.61  0.5625
#> Hornet 4 Drive      21.4   6 258.0 -0.26754642 3.08 -0.001149769 19.44  0.5625
#> Hornet Sportabout   18.7   8 360.0  0.20647109 3.15  0.113827127 17.02 -0.4375
#> Valiant             18.1   6 225.0 -0.30400931 2.76  0.124047296 20.22  0.5625
#> Duster 360          14.3   8 360.0  0.71695148 3.21  0.180258223 15.84 -0.4375
#> Merc 240D           24.4   4 146.7 -0.61759012 3.69 -0.013924980 20.00  0.5625
#> Merc 230            22.8   4 140.8 -0.37693508 3.92 -0.034365317 22.90  0.5625
#> Merc 280            19.2   6 167.6 -0.17274292 3.92  0.113827127 18.30  0.5625
#> Merc 280C           17.8   6 167.6 -0.17274292 3.92  0.113827127 18.90  0.5625
#> Merc 450SE          16.4   8 275.8  0.24293397 3.07  0.435762437 17.40 -0.4375
#> Merc 450SL          17.3   8 275.8  0.24293397 3.07  0.262019572 17.60 -0.4375
#> Merc 450SLC         15.2   8 275.8  0.24293397 3.07  0.287569993 18.00 -0.4375
#> Cadillac Fleetwood  10.4   8 472.0  0.42524840 2.93  1.038752382 17.98 -0.4375
#> Lincoln Continental 10.4   8 460.0  0.49817417 3.00  1.127667849 17.82 -0.4375
#> Chrysler Imperial   14.7   8 440.0  0.60756282 3.23  1.087298183 17.42 -0.4375
#> Fiat 128            32.4   4  78.7 -0.58841981 4.08 -0.519823324 19.47  0.5625
#> Honda Civic         30.4   4  75.7 -0.69051589 4.93 -0.818763254 18.52  0.5625
#> Toyota Corolla      33.9   4  71.1 -0.59571239 4.22 -0.706341400 19.90  0.5625
#> Toyota Corona       21.5   4 120.1 -0.36234992 3.70 -0.384406090 20.01  0.5625
#> Dodge Challenger    15.5   8 318.0  0.02415666 2.76  0.154707802 16.87 -0.4375
#> AMC Javelin         15.2   8 304.0  0.02415666 3.15  0.111272085 17.30 -0.4375
#> Camaro Z28          13.3   8 350.0  0.71695148 3.73  0.318230499 15.41 -0.4375
#> Pontiac Firebird    19.2   8 400.0  0.20647109 3.08  0.320785541 17.05 -0.4375
#> Fiat X1-9           27.3   4  79.0 -0.58841981 4.08 -0.655240557 18.90  0.5625
#> Porsche 914-2       26.0   4 120.3 -0.40610538 4.43 -0.550483829 16.70 -0.4375
#> Lotus Europa        30.4   4  95.1 -0.24566869 3.77 -0.870886114 16.90  0.5625
#> Ford Pantera L      15.8   8 351.0  0.85551044 4.22 -0.024145148 14.50 -0.4375
#> Ferrari Dino        19.7   6 145.0  0.20647109 3.62 -0.228548520 15.50 -0.4375
#> Maserati Bora       15.0   8 301.0  1.37328341 3.54  0.180258223 14.60 -0.4375
#> Volvo 142E          21.4   4 121.0 -0.27483900 4.11 -0.223438435 18.60  0.5625
#>                     am gear carb
#> Mazda RX4            1    4    4
#> Mazda RX4 Wag        1    4    4
#> Datsun 710           1    4    1
#> Hornet 4 Drive       0    3    1
#> Hornet Sportabout    0    3    2
#> Valiant              0    3    1
#> Duster 360           0    3    4
#> Merc 240D            0    4    2
#> Merc 230             0    4    2
#> Merc 280             0    4    4
#> Merc 280C            0    4    4
#> Merc 450SE           0    3    3
#> Merc 450SL           0    3    3
#> Merc 450SLC          0    3    3
#> Cadillac Fleetwood   0    3    4
#> Lincoln Continental  0    3    4
#> Chrysler Imperial    0    3    4
#> Fiat 128             1    4    1
#> Honda Civic          1    4    2
#> Toyota Corolla       1    4    1
#> Toyota Corona        0    3    1
#> Dodge Challenger     0    3    2
#> AMC Javelin          0    3    2
#> Camaro Z28           0    3    4
#> Pontiac Firebird     0    3    2
#> Fiat X1-9            1    4    1
#> Porsche 914-2        1    5    2
#> Lotus Europa         1    5    2
#> Ford Pantera L       1    5    4
#> Ferrari Dino         1    5    6
#> Maserati Bora        1    5    8
#> Volvo 142E           1    4    2

# Weighted inputs

wts <- runif(10, 0, 1)
gscale(x, weights = wts)
#>  [1] -0.83725905 -0.40511328  0.18751724 -0.90920274  0.72096462  0.35796024
#>  [7]  0.22760574  0.66356275 -0.53907195 -0.05567233
# If using a weights column of data frame, give its name
mtcars$weights <- runif(32, 0, 1)
gscale(mtcars, weights = weights) # will skip over mtcars$weights
#>                             mpg         cyl        disp          hp        drat
#> Mazda RX4            0.12275845 -0.08359883 -0.30677868 -0.27800924  0.31941113
#> Mazda RX4 Wag        0.12275845 -0.08359883 -0.30677868 -0.27800924  0.31941113
#> Datsun 710           0.29621831 -0.67451751 -0.53248500 -0.39909849  0.26970797
#> Hornet 4 Drive       0.16130508 -0.08359883  0.11859092 -0.27800924 -0.49572065
#> Hornet Sportabout   -0.09888471  0.50731985  0.56132254  0.18497907 -0.42613623
#> Valiant             -0.15670467 -0.08359883 -0.02464578 -0.31362373 -0.81382086
#> Duster 360          -0.52289771  0.50731985  0.56132254  0.68358187 -0.36649244
#> Merc 240D            0.45040486 -0.67451751 -0.36450741 -0.61990830  0.11065787
#> Merc 230             0.29621831 -0.67451751 -0.39011640 -0.38485270  0.33929239
#> Merc 280            -0.05070142 -0.08359883 -0.27379083 -0.18541158  0.33929239
#> Merc 280C           -0.18561464 -0.08359883 -0.27379083 -0.18541158  0.33929239
#> Merc 450SE          -0.32052787  0.50731985  0.19585193  0.22059355 -0.50566128
#> Merc 450SL          -0.23379794  0.50731985  0.19585193  0.22059355 -0.50566128
#> Merc 450SLC         -0.43616778  0.50731985  0.19585193  0.22059355 -0.50566128
#> Cadillac Fleetwood  -0.89872741  0.50731985  1.04745922  0.39866598 -0.64483012
#> Lincoln Continental -0.89872741  0.50731985  0.99537315  0.46989495 -0.57524570
#> Chrysler Imperial   -0.48435107  0.50731985  0.90856303  0.57673841 -0.34661118
#> Fiat 128             1.22133758 -0.67451751 -0.65966183 -0.59141672  0.49834249
#> Honda Civic          1.02860440 -0.67451751 -0.67268334 -0.69113728  1.34329616
#> Toyota Corolla       1.36588747 -0.67451751 -0.69264967 -0.59853961  0.63751133
#> Toyota Corona        0.17094174 -0.67451751 -0.47996487 -0.37060691  0.12059850
#> Dodge Challenger    -0.40725780  0.50731985  0.37902128  0.00690664 -0.81382086
#> AMC Javelin         -0.43616778  0.50731985  0.31825420  0.00690664 -0.42613623
#> Camaro Z28          -0.61926430  0.50731985  0.51791748  0.68358187  0.15042039
#> Pontiac Firebird    -0.05070142  0.50731985  0.73494278  0.18497907 -0.49572065
#> Fiat X1-9            0.72986797 -0.67451751 -0.65835967 -0.59141672  0.49834249
#> Porsche 914-2        0.60459140 -0.67451751 -0.47909677 -0.41334429  0.84626459
#> Lotus Europa         1.02860440 -0.67451751 -0.58847753 -0.25664055  0.19018292
#> Ford Pantera L      -0.37834782  0.50731985  0.52225799  0.81891691  0.63751133
#> Ferrari Dino        -0.00251812 -0.08359883 -0.37188627  0.18497907  0.04107345
#> Maserati Bora       -0.45544110  0.50731985  0.30523268  1.32464260 -0.03845160
#> Volvo 142E           0.16130508 -0.67451751 -0.47605842 -0.28513214  0.52816439
#>                                wt        qsec         vs         am      gear
#> Mazda RX4           -0.3433674983 -0.38733400 -0.4234893  0.6135692  0.274789
#> Mazda RX4 Wag       -0.1965244117 -0.21422623 -0.4234893  0.6135692  0.274789
#> Datsun 710          -0.5161240707  0.27727618  0.5765107  0.6135692  0.274789
#> Hornet 4 Drive      -0.0007336296  0.53384662  0.5765107 -0.3864308 -0.422220
#> Hornet Sportabout    0.1288337997 -0.21422623 -0.4234893 -0.3864308 -0.422220
#> Valiant              0.1403509045  0.77496101  0.5765107 -0.3864308 -0.422220
#> Duster 360           0.2036949811 -0.57898903 -0.4234893 -0.3864308 -0.422220
#> Merc 240D           -0.0151300107  0.70695438  0.5765107 -0.3864308  0.274789
#> Merc 230            -0.0381642203  1.60340532  0.5765107 -0.3864308  0.274789
#> Merc 280             0.1288337997  0.18144866  0.5765107 -0.3864308  0.274789
#> Merc 280C            0.1288337997  0.36692127  0.5765107 -0.3864308  0.274789
#> Merc 450SE           0.4916226018 -0.09676025 -0.4234893 -0.3864308 -0.422220
#> Merc 450SL           0.2958318197 -0.03493605 -0.4234893 -0.3864308 -0.422220
#> Merc 450SLC          0.3246245818  0.08871236 -0.4234893 -0.3864308 -0.422220
#> Cadillac Fleetwood   1.1711317866  0.08252994 -0.4234893 -0.3864308 -0.422220
#> Lincoln Continental  1.2713305986  0.03307058 -0.4234893 -0.3864308 -0.422220
#> Chrysler Imperial    1.2258380346 -0.09057783 -0.4234893 -0.3864308 -0.422220
#> Fiat 128            -0.5852266996  0.54312025  0.5765107  0.6135692  0.274789
#> Honda Civic         -0.9221020159  0.24945529  0.5765107  0.6135692  0.274789
#> Toyota Corolla      -0.7954138627  0.67604228  0.5765107  0.6135692  0.274789
#> Toyota Corona       -0.4326250607  0.71004559  0.5765107 -0.3864308 -0.422220
#> Dodge Challenger     0.1749022190 -0.26059438 -0.4234893 -0.3864308 -0.422220
#> AMC Javelin          0.1259545235 -0.12767235 -0.4234893 -0.3864308 -0.422220
#> Camaro Z28           0.3591758963 -0.71191106 -0.4234893 -0.3864308 -0.422220
#> Pontiac Firebird     0.3620551725 -0.20495260 -0.4234893 -0.3864308 -0.422220
#> Fiat X1-9           -0.7378283386  0.36692127  0.5765107  0.6135692  0.274789
#> Porsche 914-2       -0.6197780141 -0.31314496 -0.4234893  0.6135692  0.971798
#> Lotus Europa        -0.9808392505 -0.25132075  0.5765107  0.6135692  0.971798
#> Ford Pantera L      -0.0266471155 -0.99321118 -0.4234893  0.6135692  0.971798
#> Ferrari Dino        -0.2569892120 -0.68409017 -0.4234893  0.6135692  0.971798
#> Maserati Bora        0.2036949811 -0.96229908 -0.4234893  0.6135692  0.971798
#> Volvo 142E          -0.2512306596  0.27418497  0.5765107  0.6135692  0.274789
#>                            carb     weights
#> Mazda RX4            0.31118624  0.66578850
#> Mazda RX4 Wag        0.31118624  0.12315108
#> Datsun 710          -0.53734007 -0.80797650
#> Hornet 4 Drive      -0.53734007  0.54688053
#> Hornet Sportabout   -0.25449797 -0.27194229
#> Valiant             -0.53734007 -0.48451564
#> Duster 360           0.31118624  0.23153917
#> Merc 240D           -0.25449797 -1.04038028
#> Merc 230            -0.25449797 -1.04867862
#> Merc 280             0.31118624 -0.33750834
#> Merc 280C            0.31118624  0.45781845
#> Merc 450SE           0.02834414  0.33079087
#> Merc 450SL           0.02834414 -0.76318095
#> Merc 450SLC          0.02834414 -0.88364256
#> Cadillac Fleetwood   0.31118624 -1.05662557
#> Lincoln Continental  0.31118624 -0.60447757
#> Chrysler Imperial    0.31118624 -0.16185384
#> Fiat 128            -0.53734007 -0.57824620
#> Honda Civic         -0.25449797 -0.75271687
#> Toyota Corolla      -0.53734007 -0.39319043
#> Toyota Corona       -0.53734007  0.66059745
#> Dodge Challenger    -0.25449797 -0.45603002
#> AMC Javelin         -0.25449797 -0.39811197
#> Camaro Z28           0.31118624 -0.57895336
#> Pontiac Firebird    -0.25449797 -0.08540564
#> Fiat X1-9           -0.53734007 -0.67497260
#> Porsche 914-2       -0.25449797 -0.25672336
#> Lotus Europa        -0.25449797 -0.77568943
#> Ford Pantera L       0.31118624 -0.78148459
#> Ferrari Dino         0.87687045 -0.92030170
#> Maserati Bora        1.44255466  0.11838070
#> Volvo 142E          -0.25449797  0.13911936
# If using a weights column of data frame, can still select variables
gscale(mtcars, vars = c("hp", "wt", "vs"), weights = weights)
#>                      mpg cyl  disp          hp drat            wt  qsec
#> Mazda RX4           21.0   6 160.0 -0.27800924 3.90 -0.3433674983 16.46
#> Mazda RX4 Wag       21.0   6 160.0 -0.27800924 3.90 -0.1965244117 17.02
#> Datsun 710          22.8   4 108.0 -0.39909849 3.85 -0.5161240707 18.61
#> Hornet 4 Drive      21.4   6 258.0 -0.27800924 3.08 -0.0007336296 19.44
#> Hornet Sportabout   18.7   8 360.0  0.18497907 3.15  0.1288337997 17.02
#> Valiant             18.1   6 225.0 -0.31362373 2.76  0.1403509045 20.22
#> Duster 360          14.3   8 360.0  0.68358187 3.21  0.2036949811 15.84
#> Merc 240D           24.4   4 146.7 -0.61990830 3.69 -0.0151300107 20.00
#> Merc 230            22.8   4 140.8 -0.38485270 3.92 -0.0381642203 22.90
#> Merc 280            19.2   6 167.6 -0.18541158 3.92  0.1288337997 18.30
#> Merc 280C           17.8   6 167.6 -0.18541158 3.92  0.1288337997 18.90
#> Merc 450SE          16.4   8 275.8  0.22059355 3.07  0.4916226018 17.40
#> Merc 450SL          17.3   8 275.8  0.22059355 3.07  0.2958318197 17.60
#> Merc 450SLC         15.2   8 275.8  0.22059355 3.07  0.3246245818 18.00
#> Cadillac Fleetwood  10.4   8 472.0  0.39866598 2.93  1.1711317866 17.98
#> Lincoln Continental 10.4   8 460.0  0.46989495 3.00  1.2713305986 17.82
#> Chrysler Imperial   14.7   8 440.0  0.57673841 3.23  1.2258380346 17.42
#> Fiat 128            32.4   4  78.7 -0.59141672 4.08 -0.5852266996 19.47
#> Honda Civic         30.4   4  75.7 -0.69113728 4.93 -0.9221020159 18.52
#> Toyota Corolla      33.9   4  71.1 -0.59853961 4.22 -0.7954138627 19.90
#> Toyota Corona       21.5   4 120.1 -0.37060691 3.70 -0.4326250607 20.01
#> Dodge Challenger    15.5   8 318.0  0.00690664 2.76  0.1749022190 16.87
#> AMC Javelin         15.2   8 304.0  0.00690664 3.15  0.1259545235 17.30
#> Camaro Z28          13.3   8 350.0  0.68358187 3.73  0.3591758963 15.41
#> Pontiac Firebird    19.2   8 400.0  0.18497907 3.08  0.3620551725 17.05
#> Fiat X1-9           27.3   4  79.0 -0.59141672 4.08 -0.7378283386 18.90
#> Porsche 914-2       26.0   4 120.3 -0.41334429 4.43 -0.6197780141 16.70
#> Lotus Europa        30.4   4  95.1 -0.25664055 3.77 -0.9808392505 16.90
#> Ford Pantera L      15.8   8 351.0  0.81891691 4.22 -0.0266471155 14.50
#> Ferrari Dino        19.7   6 145.0  0.18497907 3.62 -0.2569892120 15.50
#> Maserati Bora       15.0   8 301.0  1.32464260 3.54  0.2036949811 14.60
#> Volvo 142E          21.4   4 121.0 -0.28513214 4.11 -0.2512306596 18.60
#>                             vs am gear carb    weights
#> Mazda RX4           -0.4234893  1    4    4 0.98108497
#> Mazda RX4 Wag       -0.4234893  1    4    4 0.68196326
#> Datsun 710           0.5765107  1    4    1 0.16869149
#> Hornet 4 Drive       0.5765107  0    3    1 0.91553852
#> Hornet Sportabout   -0.4234893  0    3    2 0.46417326
#> Valiant              0.5765107  0    3    1 0.34699501
#> Duster 360          -0.4234893  0    3    4 0.74171077
#> Merc 240D            0.5765107  0    4    2 0.04058197
#> Merc 230             0.5765107  0    4    2 0.03600762
#> Merc 280             0.5765107  0    4    4 0.42803084
#> Merc 280C            0.5765107  0    4    4 0.86644423
#> Merc 450SE          -0.4234893  0    3    3 0.79642195
#> Merc 450SL          -0.4234893  0    3    3 0.19338444
#> Merc 450SLC         -0.4234893  0    3    3 0.12698157
#> Cadillac Fleetwood  -0.4234893  0    3    4 0.03162697
#> Lincoln Continental -0.4234893  0    3    4 0.28086758
#> Chrysler Imperial   -0.4234893  0    3    4 0.52485806
#> Fiat 128             0.5765107  1    4    1 0.29532728
#> Honda Civic          0.5765107  1    4    2 0.19915262
#> Toyota Corolla       0.5765107  1    4    1 0.39733682
#> Toyota Corona        0.5765107  0    3    1 0.97822348
#> Dodge Challenger    -0.4234893  0    3    2 0.36269733
#> AMC Javelin         -0.4234893  0    3    2 0.39462389
#> Camaro Z28          -0.4234893  0    3    4 0.29493747
#> Pontiac Firebird    -0.4234893  0    3    2 0.56699912
#> Fiat X1-9            0.5765107  1    4    1 0.24200813
#> Porsche 914-2       -0.4234893  1    5    2 0.47256249
#> Lotus Europa         0.5765107  1    5    2 0.18648930
#> Ford Pantera L      -0.4234893  1    5    4 0.18329480
#> Ferrari Dino        -0.4234893  1    5    6 0.10677370
#> Maserati Bora       -0.4234893  1    5    8 0.67933365
#> Volvo 142E           0.5765107  1    4    2 0.69076557

# Survey designs
if (requireNamespace("survey")) {
  library(survey)
  data(api)
  ## Create survey design object
  dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw,
                       data = apistrat, fpc=~fpc)
  # Creating test binary variable
  dstrat$variables$binary <- rbinom(200, 1, 0.5)

  gscale(data = dstrat, binary.inputs = "-0.5/0.5")
  gscale(data = dstrat, vars = c("api00","meals","binary"),
         binary.inputs = "-0.5/0.5")
}
#> Stratified Independent Sampling design
#> dstrat <- svydesign(id = ~1, strata = ~stype, weights = ~pw,
#>                        data = apistrat, fpc=~fpc)