The error loop may be used to correct the final correlation of simulated variables to be within a user-specified precision value (epsilon
) of the target correlation. It updates the pairwise intermediate MVN correlation iteratively in a loop until either the maximum error is less than epsilon
or the number of iterations exceeds the maximum number set by the user (maxit
). It uses SimMultiCorrData::error_vars
to simulate the pair of variables in each iteration. The function modifies Barbiero and Ferrari (2015) ordcont
function in the GenOrd
package in the following ways:
The initial positive-definite correlation check has been removed because the intermediate correlation Sigma
from rcorrvar
or rcorrvar2
has already been used to generate variables (and therefore is positive-definite).
Matrix::nearPD
functionextra_correct
= “TRUE”)Allowing specifications for the sample size and the seed for reproducibility.
The error loop does increase simulation time, but it can improve accuracy in most situations.
Below is a schematic of the algorithm. In the figure, rho_calc
is the calculated final correlation matrix updated in each iteration, rho_calcold
is the final correlation matrix before the error loop, rho0
is the target final correlation matrix, Sigmaold
is the intermediate correlation matrix from the previous iteration, it
is the iteration number, q
is the row number, and r
is the column number. In addition, extra_correct
refers to the setting in the simulation functions (see rcorrvar
and rcorrvar2
).
Overview of Error Loop.
Barbiero, A, and PA Ferrari. 2015. GenOrd: Simulation of Discrete Random Variables with Given Correlation Matrix and Marginal Distributions. https://CRAN.R-project.org/package=GenOrd.