How to Simulate Multivariate Random Variables in R (Example Code)
On this page you’ll learn how to create multivariate random variables in the R programming language.
Example: Create Multivariate Random Data Set Using the mvrnorm() Function of the MASS Package
install.packages("MASS") # Install & load MASS library("MASS") |
install.packages("MASS") # Install & load MASS library("MASS")
cor_mat <- matrix(c(1, 0.1, 0.7, 0.5, # Create correlation matrix 0.1, 1, 0.6, 0.2, 0.7, 0.6, 1, 0.3, 0.5, 0.2, 0.3, 1), nrow = 4) cor_mat # Print correlation matrix # [,1] [,2] [,3] [,4] # [1,] 1.0 0.1 0.7 0.5 # [2,] 0.1 1.0 0.6 0.2 # [3,] 0.7 0.6 1.0 0.3 # [4,] 0.5 0.2 0.3 1.0 |
cor_mat <- matrix(c(1, 0.1, 0.7, 0.5, # Create correlation matrix 0.1, 1, 0.6, 0.2, 0.7, 0.6, 1, 0.3, 0.5, 0.2, 0.3, 1), nrow = 4) cor_mat # Print correlation matrix # [,1] [,2] [,3] [,4] # [1,] 1.0 0.1 0.7 0.5 # [2,] 0.1 1.0 0.6 0.2 # [3,] 0.7 0.6 1.0 0.3 # [4,] 0.5 0.2 0.3 1.0
set.seed(56832) # Set random seed |
set.seed(56832) # Set random seed
df_rand <- mvrnorm(n = 5000, # Generate random multivariate data mu = 1:4, Sigma = cor_mat) head(df_rand) # Show random data # [,1] [,2] [,3] [,4] # [1,] -0.05704288 0.9567854 2.057415 4.527190 # [2,] 0.91550241 1.0366073 3.197887 4.539859 # [3,] -0.05532218 1.0616700 2.022091 2.635634 # [4,] 2.03027930 2.4729577 2.860940 4.342003 # [5,] 2.29906767 3.3044178 4.608380 4.837060 # [6,] 0.82928454 2.8524927 2.742915 3.803645 |
df_rand <- mvrnorm(n = 5000, # Generate random multivariate data mu = 1:4, Sigma = cor_mat) head(df_rand) # Show random data # [,1] [,2] [,3] [,4] # [1,] -0.05704288 0.9567854 2.057415 4.527190 # [2,] 0.91550241 1.0366073 3.197887 4.539859 # [3,] -0.05532218 1.0616700 2.022091 2.635634 # [4,] 2.03027930 2.4729577 2.860940 4.342003 # [5,] 2.29906767 3.3044178 4.608380 4.837060 # [6,] 0.82928454 2.8524927 2.742915 3.803645
cor(df_rand) # Print correlation matrix of random data # [,1] [,2] [,3] [,4] # [1,] 1.00000000 0.08922849 0.6950656 0.5073786 # [2,] 0.08922849 1.00000000 0.5849754 0.2226230 # [3,] 0.69506560 0.58497538 1.0000000 0.3092777 # [4,] 0.50737862 0.22262299 0.3092777 1.0000000 |
cor(df_rand) # Print correlation matrix of random data # [,1] [,2] [,3] [,4] # [1,] 1.00000000 0.08922849 0.6950656 0.5073786 # [2,] 0.08922849 1.00000000 0.5849754 0.2226230 # [3,] 0.69506560 0.58497538 1.0000000 0.3092777 # [4,] 0.50737862 0.22262299 0.3092777 1.0000000