Skip to main content
added 434 characters in body
Source Link
Sergio
  • 6.1k
  • 2
  • 15
  • 29

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has 0 meanmean=0 and 1 variancevariance=1:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
            [,1]       [,2]         [,3]
[1,] -0.75184545 4557803 0-1.1306994 6174866 0.88685086134224496
[2,]  0.081937197982673 -10.48027991214584 -03.00127638505775038
[3,]  -0.787214212175150 -2 1.63284011449116 -0.94581572407709425
[4,]  0.218120929070137  0.62484874484822 -01.22889928249516553
[5,]  0.12225648 -1.14319248435512 -2.4068282 1.75790172323142658
[6,] -0.260354075840670 -01.50810483760948 -0.78028384437929271
> meanapply(X[X,1] MARGIN=2, FUN=mean)
[1] -0.032888212326054 -0.6547457  1.0971624
> sdapply(X[X,1] MARGIN=2, FUN=sd)
[1] 01.5129346012930 1.360682 1.108703
> X <- scale(X)
> meanapply(X[X,1] MARGIN=2, FUN=mean)
[1] 6 1.938894e127570e-1817 -3.699840e-17  9.483155e-17
> sdapply(X[X,1] MARGIN=2, FUN=sd)
[1] 1 1 1
> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[X)
            [,1],X[        [,2])        [,3]
[1][1,]  1.00000000 -0.0337255105327000 -0.01848098
[2,] -0.05327000  1.00000000 -0.01011558
[3,] -0.01848098 -0.01011558  1.00000000
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[X)
            [,1],X[        [,2])        [,3]
[1][1,] -1.000000000 0.008293499005957725 0.002865598
[2,] 0.005957725 1.000000000 0.008932789
[3,] 0.002865598 0.008932789 1.000000000

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has 0 mean and 1 variance:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
            [,1]       [,2]         [,3]
[1,] -0.75184545  0.1306994  0.886850861
[2,]  0.08193719 -1.4802799 -0.001276385
[3,]  0.78721421 -2.6328401 -0.945815724
[4,]  0.21812092  0.6248487 -0.228899282
[5,]  0.12225648 -1.1431924  1.757901723
[6,] -0.26035407 -0.5081048 -0.780283844
> mean(X[,1])
[1] 0.03288821
> sd(X[,1])
[1] 0.5129346
> X <- scale(X)
> mean(X[,1])
[1] 6.938894e-18
> sd(X[,1])
[1] 1
> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.03372551
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.008293499

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has mean=0 and variance=1:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
           [,1]       [,2]       [,3]
[1,] -0.4557803 -1.6174866 0.34224496
[2,]  0.7982673 -0.1214584 3.05775038
[3,] -0.2175150  1.1449116 0.07709425
[4,]  0.9070137  0.4484822 1.49516553
[5,] -1.8435512 -2.4068282 1.23142658
[6,] -0.5840670 -1.3760948 0.37929271
> apply(X, MARGIN=2, FUN=mean)
[1] -0.2326054 -0.6547457  1.0971624
> apply(X, MARGIN=2, FUN=sd)
[1] 1.012930 1.360682 1.108703
> X <- scale(X)
> apply(X, MARGIN=2, FUN=mean)
[1]  1.127570e-17 -3.699840e-17  9.483155e-17
> apply(X, MARGIN=2, FUN=sd)
[1] 1 1 1
> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X)
            [,1]        [,2]        [,3]
[1,]  1.00000000 -0.05327000 -0.01848098
[2,] -0.05327000  1.00000000 -0.01011558
[3,] -0.01848098 -0.01011558  1.00000000
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X)
            [,1]        [,2]        [,3]
[1,] 1.000000000 0.005957725 0.002865598
[2,] 0.005957725 1.000000000 0.008932789
[3,] 0.002865598 0.008932789 1.000000000
deleted 4 characters in body
Source Link
Sergio
  • 6.1k
  • 2
  • 15
  • 29

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has 0 mean and 1 variance:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
            [,1]       [,2]         [,3]
[1,] -0.75184545  0.1306994  0.886850861
[2,]  0.08193719 -1.4802799 -0.001276385
[3,]  0.78721421 -2.6328401 -0.945815724
[4,]  0.21812092  0.6248487 -0.228899282
[5,]  0.12225648 -1.1431924  1.757901723
[6,] -0.26035407 -0.5081048 -0.780283844
> mean(X[,1])
[1] 0.03288821
> sd(X[,1])
[1] 0.5129346
> X <- scale(X)
> mean(X[,1])
[1] 6.938894e-18
> sd(X[,1])
[1] 1

The columns are correlated when $n$ is small, but the correlation decreases (as expected, because sigma is an identity matrix) as $n$ grows:

> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.03372551
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.008293499

You can generate heteroscedastic and autocorrrelatedcorrrelated data by changing sigma.

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has 0 mean and 1 variance:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
            [,1]       [,2]         [,3]
[1,] -0.75184545  0.1306994  0.886850861
[2,]  0.08193719 -1.4802799 -0.001276385
[3,]  0.78721421 -2.6328401 -0.945815724
[4,]  0.21812092  0.6248487 -0.228899282
[5,]  0.12225648 -1.1431924  1.757901723
[6,] -0.26035407 -0.5081048 -0.780283844
> mean(X[,1])
[1] 0.03288821
> sd(X[,1])
[1] 0.5129346
> X <- scale(X)
> mean(X[,1])
[1] 6.938894e-18
> sd(X[,1])
[1] 1

The columns are correlated when $n$ is small, but the correlation decreases (as expected, because sigma is an identity matrix) as $n$ grows:

> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.03372551
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.008293499

You can generate heteroscedastic and autocorrrelated data by changing sigma.

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has 0 mean and 1 variance:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
            [,1]       [,2]         [,3]
[1,] -0.75184545  0.1306994  0.886850861
[2,]  0.08193719 -1.4802799 -0.001276385
[3,]  0.78721421 -2.6328401 -0.945815724
[4,]  0.21812092  0.6248487 -0.228899282
[5,]  0.12225648 -1.1431924  1.757901723
[6,] -0.26035407 -0.5081048 -0.780283844
> mean(X[,1])
[1] 0.03288821
> sd(X[,1])
[1] 0.5129346
> X <- scale(X)
> mean(X[,1])
[1] 6.938894e-18
> sd(X[,1])
[1] 1

The columns are correlated when $n$ is small, but the correlation decreases (as expected, because sigma is an identity matrix) as $n$ grows:

> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.03372551
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.008293499

You can generate heteroscedastic and corrrelated data by changing sigma.

Source Link
Sergio
  • 6.1k
  • 2
  • 15
  • 29

The question is about R syntax, as BruceET says, but I think that data simulation is an important topic, and that you could (should?) generate your data in another way. You can use the mvtnorm package to generate random multivariate matrices and the scale() function to ensure that each column has 0 mean and 1 variance:

> library(mvtnorm)
> n <- 6
> p <- 3
> mean <- rep(0, p)
> sigma <- diag(p)              # identity matrix
> X <- rmvnorm(n, mean=mean, sigma=sigma)
> X
            [,1]       [,2]         [,3]
[1,] -0.75184545  0.1306994  0.886850861
[2,]  0.08193719 -1.4802799 -0.001276385
[3,]  0.78721421 -2.6328401 -0.945815724
[4,]  0.21812092  0.6248487 -0.228899282
[5,]  0.12225648 -1.1431924  1.757901723
[6,] -0.26035407 -0.5081048 -0.780283844
> mean(X[,1])
[1] 0.03288821
> sd(X[,1])
[1] 0.5129346
> X <- scale(X)
> mean(X[,1])
[1] 6.938894e-18
> sd(X[,1])
[1] 1

The columns are correlated when $n$ is small, but the correlation decreases (as expected, because sigma is an identity matrix) as $n$ grows:

> X <- scale(rmvnorm(1000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.03372551
> X <- scale(rmvnorm(10000, mean=mean, sigma=sigma))
> cor(X[,1],X[,2])
[1] -0.008293499

You can generate heteroscedastic and autocorrrelated data by changing sigma.