Skip to contents

Set the correlation between two or more variables in a simulation.

Usage

set_correlation(list_var, between_var, corr)

Arguments

list_var

A list containing the variables used in the simulation, initialized using init_variable.

between_var

Character vector specifying the names of the variables to set the correlation between.

corr

Numeric value specifying the desired correlation between the variables.

Value

Updated list_var with the specified correlation set between the variables.

Details

The function checks if the variables specified in between_var are declared and initialized in the list_var. It also ensures that at least two variables with provided standard deviation are required to set a correlation in the simulation. The specified correlation value must be within the range (-1, 1). The function computes the corresponding covariance between the variables based on the specified correlation and standard deviations. The correlation information is then added to the list_var in the form of a data frame containing the correlation value and the corresponding covariance value.

Examples

list_var <- init_variable(name = "varA", mu = 0, sd = 5, level = 3) %>%
            init_variable(name = "varB", mu = 0, sd = 25, level = 3)
#> Variable name should not contain digits, spaces, or special characters.
#> If any of these are present, they will be removed from the variable name.
#> Variable name should not contain digits, spaces, or special characters.
#> If any of these are present, they will be removed from the variable name.
list_var <- set_correlation(list_var, between_var = c("varA", "varB"), corr = 0.7)