Learn R Programming

DPI (version 2025.11)

sim_data_exp: Simulate experiment-like data with independent binary Xs.

Description

Simulate experiment-like data with independent binary Xs.

Usage

sim_data_exp(
  n,
  r.xy,
  approx = TRUE,
  tol = 0.01,
  max.iter = 30,
  verbose = FALSE,
  seed = NULL
)

Value

Return a data.frame of simulated data.

Arguments

n

Number of observations (cases).

r.xy

A vector of expected correlations of each X (binary independent variable: 0 or 1) with Y.

approx

Make the sample correlation matrix approximate more to values as specified in r.xy, using the method of orthogonal decomposition of residuals (i.e., making residuals more independent of Xs). Defaults to TRUE.

tol

Tolerance of absolute difference between specified and empirical correlations. Defaults to 0.01.

max.iter

Maximum iterations for approximation. More iterations produce more approximate correlations, but the absolute differences will be convergent after about 30 iterations. Defaults to 30.

verbose

Print information about iterations that satisfy tolerance. Defaults to FALSE.

seed

Random seed for replicable results. Defaults to NULL.

See Also

sim_data()

Examples

Run this code
data = sim_data_exp(n=1000, r.xy=c(0.5, 0.3), seed=1)
cor(data)  # tol = 0.01

data = sim_data_exp(n=1000, r.xy=c(0.5, 0.3), seed=1,
                    verbose=TRUE)
cor(data)  # print iteration information

data = sim_data_exp(n=1000, r.xy=c(0.5, 0.3), seed=1,
                    verbose=TRUE, tol=0.001)
cor(data)  # more approximate, though not exact

data = sim_data_exp(n=1000, r.xy=c(0.5, 0.3), seed=1,
                    approx=FALSE)
cor(data)  # far less exact

Run the code above in your browser using DataLab