Learn R Programming

DPI (version 2025.11)

sim_data: Simulate data from a multivariate normal distribution.

Description

Simulate data from a multivariate normal distribution.

Usage

sim_data(n, k, cor = NULL, exact = TRUE, seed = NULL)

Value

Return a data.frame of simulated data.

Arguments

n

Number of observations (cases).

k

Number of variables. Will be ignored if cor specifies a correlation matrix.

cor

A correlation value or correlation matrix of the variables. Defaults to NULL that generates completely random data regardless of their empirical correlations.

exact

Ensure the sample correlation matrix to be exact as specified in cor. This argument is passed on to empirical in mvrnorm(). Defaults to TRUE.

seed

Random seed for replicable results. Defaults to NULL.

See Also

cor_matrix()

sim_data_exp()

Examples

Run this code
d1 = sim_data(n=100, k=5, seed=1)
cor_net(d1)

d2 = sim_data(n=100, k=5, cor=0.2, seed=1)
cor_net(d2)

cor.mat = cor_matrix(
  1.0, 0.7, 0.3,
  0.7, 1.0, 0.5,
  0.3, 0.5, 1.0
)
d3 = sim_data(n=100, cor=cor.mat, seed=1)
cor_net(d3)

Run the code above in your browser using DataLab