lundi 9 février 2015

R: Create dataset with specific correlation in r

How can i create a dataset with two columns having a specific correlation to each other? I want to be able to define the number of values which will be created and specify the correlation the output should have.


The question is similar to this one: Generate numbers with specific correlation


One of the answers was to use:



out <- mvrnorm(10, mu = c(0,0), Sigma = matrix(c(1,0.56,0.56,1),, ncol = 2),
mpirical = TRUE)


Producing an output like this:



[,1] [,2]
[1,] -0.4152618 0.033311146
[2,] 0.7617759 -0.181852441
[3,] -1.6393045 -1.054752469
[4,] -1.7872420 -0.605214425
[5,] 0.9581152 2.511000955
[6,] 0.5048160 -0.278329145
[7,] 0.8656220 0.483521747
[8,] -0.1385699 0.017395548
[9,] 0.3261103 -0.932889606
[10,] 0.5639388 0.007808691


with the following correlation table cor(out):



[,1] [,2]
[1,] 1.00 0.56
[2,] 0.56 1.00


But i want the data set to contain higher, no negative and more far away numbers for example:



x y
1 5 5
2 20 20
3 30 30
4 100 100


having a correlation of 1:



x y
x 1 1
y 1 1


With more far away i mean "more" random and bigger in their value like in my sample above.


Is there are (easy) way to archive something like that?





Aucun commentaire:

Enregistrer un commentaire