-2

I have referred to some of the previous answers such as

  1. How to generate random numbers from a normal distribution with specific mean and variance?
  2. Is there any way to generate uncorrelated random variables using Python?
  3. Generate matrix with iid normal random variables using R

It's still not clear how to generate uncorrelated random normal vectors with a different mean.

The catch is that the number of samples in each vector length should be less (as low as 20, we want 2 (20*1) vectors). Probably this is a bad constraint.

I used the replicate function with rnorm as mentioned in one of the above posts like:

c2=replicate(10000, cor(rnorm(100), rnorm(100)))

For large numbers like 10,000 and above, the correlation is almost 0. But

c2=replicate(20, cor(rnorm(100), rnorm(100)))

gives a positive or negative correlation.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
bespectacled
  • 2,801
  • 4
  • 25
  • 35
  • 2
    You seems to misunderstand what 10000 and 20 mean in `replicate`. They make R calculate the correlation between two randomly generated vectors (`rnorm(100)` and `rnorm(100)`) 10000 and 20 times, respectively. It should have nothing to do with the magnitude of correlations. – ytu Jul 14 '18 at 08:57
  • 1
    BTW, `rnorm(100)` and `rnorm(100)` will have the same mean of 0. Aren't you trying to generate vectors with different means though? – ytu Jul 14 '18 at 08:59
  • @ytu yes, the mean(s) should be different. – bespectacled Jul 14 '18 at 09:01
  • btw, why was my question downvoted ? – bespectacled Jul 14 '18 at 09:03

1 Answers1

3

You can use the mvrnorm function from MASS to do that.

Defining the variables properties:

# Means
m1 <- 5
m2 <- 10
# variance
s1 <- 5
s2 <- 1
# Correlations
X1 <- 0

Creating the variables:

set.seed(123)
dat <- MASS::mvrnorm(20, mu = c(m1, m2),
               Sigma = matrix(c(s1, X1,
                                X1, s2),
                              ncol = 2, byrow = TRUE),
               empirical = TRUE)

Testing for the correlations:

dat %>% 
  cor()

                     [,1]                 [,2]
[1,] 1.000000000000000000 0.000000000000000197
[2,] 0.000000000000000197 1.000000000000000000
DJV
  • 4,743
  • 3
  • 19
  • 34