library(faux) library(ggplot2) library(ggExtra) library(truncnorm)
sim_design() only simulates multivariate normal distributions. You can use the conversion functions to convert values between distributions.
Imagine you’re trying to simulate each participant’s mean score on 50 trials with 1-7 Likert responses and you only have the mean and SD from a previous paper to go on. simulating from a normal distribution is likely to give you impossible values, but you can convert to a truncated normal distribution.
Convert between a normal distribution with a mean of 3.5 and SD of 2.5 and a truncated normal distribution ranging from 1 to 7 with the same mean and sd. The red values below are outside the truncated range on the unbounded normal scale.
= 3.5 mu = 2.5 sd = 1 min = 7 max <- rnorm(1000, mu, sd) x_n <- norm2trunc(x_n, min, max) y_t <- rtruncnorm(1000, min, max, mu, sd) x_t <- trunc2norm(x_t, min , max)y_n
If you omit the min and max values for
trunc2norm(), it will set them outside the min and max of the data, but this can be really inaccurate, so set them yourself.
You can convert a normal distribution to any sample distribution if you can specify the probability of each level. You can specify probability as counts or proportions. If you use a named vector, the output will be a factor with those named levels.
= 0 mu = 1 sd = c('very low' = 50, prob 'low' = 80, 'medium' = 100, 'high' = 40, 'very high' = 20) <- rnorm(1000, mu, sd) x_n <- norm2likert(x_n, prob)y_l
Convert between a normal distribution with a mean of 0 and SD of 1 and a uniform distribution ranging from 0 to 10. If you omit the min and max values for
unif2norm(), it will set them slightly outside the min and max of the data.
= 0 mu = 1 sd = 0 min = 100 max <- rnorm(1e3, mu, sd) x_n <- norm2unif(x_n, min, max) y_u <- runif(1e3, min, max) x_u <- unif2norm(x_u, mu, sd) y_n #> min was set to 0.0992686796709895 #> max was set to 99.7016261954084
By default, the function will try to guess the relevant parameters of the input distribution, but you can also specify them. This is especially useful if you know the actual min and max values, but don’t have enough data for any of the observations to be near them.
# cheating a little to make sure no values near min and max <- runif(1e4, 0.1, 0.9) x <- unif2norm(x, mu = 100, sd = 10) y_guess #> min was set to 0.100089273990691 #> max was set to 0.900074888563156 <- unif2norm(x, mu = 100, sd = 10, min = 0, max = 1)y_spec
Convert between a normal distribution with a mean of 0 and SD of 1 to a binomial distribution with size of 20 and probability of 0.75 (e.g., sum scores on a 20-trial test with a mean of 75% correct).
Converting from binomial to normal can produce odd-looking distributions of the size is small or the probability is very high or low. If you don’t specify the probability, it will be estimated from your data. If you don’t specify the size, it will be set to the maximum value of x (which can be wrong, so set it yourself).
= 0 mu = 1 sd = 20 size = 0.75 prob <- rnorm(1e4, mu, sd) x_n <- norm2binom(x_n, size, prob) y_b <- rbinom(1e4, size, prob) x_b <- binom2norm(x_b, mu, sd, size) y_n #> prob was set to 0.74942
Convert between a normal distribution with a mean of 0 and SD of 1 and a beta distribution with shape parameters of 2 and 5. The function
beta2norm() will try to guess the shape parameters from your data if you don’t specify them.
= 0 mu = 1 sd = 2 shape1 = 5 shape2 <- rnorm(1e4, mu, sd) x_n <- norm2beta(x_n, shape1, shape2) y_b <- rbeta(1e4, shape1, shape2) x_b <- beta2norm(x_b, mu, sd) y_n #> shape1 was set to 1.95666779213503 #> shape2 was set to 4.95063569233446
Convert between a normal distribution with a mean of 0 and SD of 1 and a gamma distribution with a shape parameter of 2 and a rate of 1. The function
gamma2norm() will try to guess the shape and rate parameters from your data if you don’t specify them.
= 0 mu = 1 sd = 2 shape = 1 rate <- rnorm(1e4, mu, sd) x_n <- norm2gamma(x_n, shape, rate) y_g <- rgamma(1e4, shape, rate) x_g <- gamma2norm(x_g, mu, sd) y_n #> shape was set to 1.97250941176025