Reputation: 21
I need to create a set of 100 random (x,y) points in R that are Gaussian. How do I do this?
Upvotes: 2
Views: 179
Reputation: 61154
Take a look at mvrnorm
function from MASS package
library(MASS)
Sigma <- matrix(c(10,3,3,2),2,2) # Covariance Matrix
set.seed(1) # For the example to be reproducible
Random_XY <- mvrnorm(n=100, c(0, 0), Sigma) # Random (x,y) from a Gaussian distr.
head(Random_XY)
[,1] [,2]
[1,] 2.3299984 -0.4196921
[2,] -0.2261965 -1.2474779
[3,] 2.3538800 1.7025069
[4,] -4.9527947 -1.8730622
[5,] -1.0148272 -0.4114252
[6,] 2.0557678 2.4378417
EDIT
Since a gaussian process has mean 0 and variance 1 and zero correlation, the correct answer should be:
mvrnorm(n=100, c(0, 0), diag(c(1,1)))
Where the vector of means is c(0,0)
and a unitary covariance matrix diag(c(1,1))
As @Ben Bolker pointed out, the fastest way to go (using R Base function) is:
data.frame(x=rnorm(100),y=rnorm(100))
Upvotes: 5