Data transformation

There are many transformations we can do to a continuous variable, but the most common ones are mean-centering, scaling, and z-scoring (z-scoring is also called standardising). These transformations can help to aid interpretability of our statistical models.

Mean-centering

Centering simply means moving the entire distribution to be centered on some new value. We achieve this by subtracting our desired center from each value of a variable. A common option is to mean-centre, by subtracting the variable’s mean from each observation.

data_name <- data_name |>
  mutate(
   mc_variable = variable - mean(variable)
    )

The resulting transformed variable will have a mean of 0. Values that are negative are below the average, and values that are positive are above the average.

Scaling

Scaling changes the units of the variable, and we do this by dividing the observations by some value. E.g., moving from “36 months” to “3 years” involves multiplying (scaling) the value by 1/12.

The most common transformation that involves scaling is z-scoring (aka standardising).

z-scoring

Transforming values into z-scores involves subtracting the mean from each individual observation (i.e., calculating individual deviations) and then dividing by the standard deviation. So, z-scoring centres the variable on the sample mean and scales it by the sample standard deviation. The result is a standardised variable with a mean of 0 and a standard deviation of 1.

z-score formula for \(x\):

\[ z_{x_i} = \frac{x_i - \bar{x}}{s_x} \]

In words: The z-scored version of the observation \(x_i\) equals the observed value \(x_i\), minus the mean of all the \(x\) values \(\bar{x}\), with that difference divided by the standard deviation of the \(x\) values \(s_x\).

In R:

data_name <- data_name |>
  mutate(
    z_variable = (variable - mean(variable)) / sd(variable)
  )

Standardising variables for linear modelling

If both \(x\) and \(y\) are standardised, our model coefficients (\(\beta\)’s) are standardised too.

When we standardise variables in a regression model, it means we can talk about all our coefficients in terms of “standard deviation units”. To the extent that it is possible to do so, this puts our coefficients on scales of the similar magnitude. This means we can qualitatively compare effect sizes more clearly than if the predictors are on different scales.

We tend to refer to coefficients using standardised variables as “standardised coefficients”.

There are two main ways that people construct standardised coefficients. One of which standardises just the predictor, and the other of which standardises both predictor and outcome:

predictor outcome pseudocode coefficient interpretation
standardised raw y ~ zscored(x) \(\beta = b \cdot s_x\) “difference in Y for a 1 SD increase in X”
standardised standardised zscored(y) ~ zscored(x) \(\beta = b \cdot \frac{s_x}{s_y}\) “difference in SD of Y for a 1 SD increase in X”