rdatacode.com provides online training course for R and Python, click here for more info !

In simulation and Bayesian statistics, it is often needed to draw data coming from multivariate variables. R provides a function draw.d.variate.dist() from package ‘MultiRNG’ for such purpose. For example, draw.d.variate.normal() are used to draw multivariate random data coming from variables have joint normal distributions. In the following example, 200 random sample data are drawn from 3 variables which follow joint normal distributions.

#load the required library
library(MultiRNG)
#create mean vector for three variables
m_vec <- c(32, 69, 19)
#create variance-covariance matrix for three random variables
s_mtx <- matrix(c(600, 400, 200,
                    400, 1000, -200, 
                    200, -200, 300), nrow=3, ncol=3) 
#draw 200 random samples from joint normal distributions
multidf <- draw.d.variate.normal(200, 3, m_vec, s_mtx) 
#tranfrom matrix to data frame
multidf <- as.data.frame(multidf) 
#show first 15 sample data
head(multidf,15)
#output 
      V1    V2    V3
1  27.765 118.3 12.78
2  13.605 101.2 -2.31
3  25.342  34.4 29.25
4  41.945  97.4 14.67
5   7.668  28.3 47.67
6  60.555  82.2 28.60
7  38.371  67.6  8.65
8  33.889 106.6 15.70
9  -0.971 -22.1 29.69
10 34.631  72.5 12.37
11 33.310  94.6 11.15
12 63.333 121.4 23.68
13 28.631  60.4 31.86
14 22.792  48.0 18.52
15 33.293  53.6 32.18

You can preview and you learn more R functions from our YouTube channel.


0 Comments

Leave a Reply

Avatar placeholder