Description Usage Arguments Details
View source: R/general_functions.R
Transforms any numeric vector onto a continuous distribution.
1 | map_normal(x, MinDim = 2, rescale = TRUE)
|
x |
Numeric Vector that your want to map onto a Normal distribution. |
MinDim |
Numeric Scalar (default = 2). If the number of unique values in a vector is equal to or less than MinDim, then no the function will not transform the variable. This is because it is inadvisable to use this function on dichotmous variables. |
rescale |
Logical (default = TRUE). If true, will scale() the output to ensure a 0 mean and unit variance. |
The function preserves the original order of values, but shifts the location of values so that the overall distribution maps perfectly onto a normal distribution.
This works by first finding the percentile ranks (i.e. rank individuals between 0 and 1), and then use the inverse cumulative distribution function for a standard normal distribution to convert those ranks to the equivalent position on a normal distribution.
Two additional tricks are required.
First, after ranking values from 1 to N (N = the number of values), we minus 1/2 then divide by N to get percentile ranks. This is to avoid having percentile ranks of 0
Second problem is dealing with tied values. The approach here is to initially sort tied values randomly, assign them all a unique normal score, then average across the normal scores for each tied value. This yields a different result than first averaging the percentile rank, and finding a normal score from that.
After transforming percentile ranks to the normal distribution, additional scaling and centering is performed to make sure the vector mean is 0 with unit variance.
Warning 1: The default behavior is that the function will not transform dichotomous variables. It's generally only advisable to use this function for continuous variables with minimal repeated values.
Warning 2: For linear regression your outcome variable does not need to be normally distributed! It may be helpful to use this function for predictor variables, but transforming outcomes may be inadvisable....
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.