dimanche 3 mai 2020

Correlation coefficient from randomised variables in R

my goal is to get a new variable of correlation (spearman's) coefficents where each number corresponds to the correlation between two randomised variables.

e.g.

var1=c(1, 2, 3, 0, 2)
var2=c(3, 6, 0, 1, 2)

I've tried

set.seed(1)
f1=numeric(10000)
for (i in 1:10000) {rand <- replicate(10000, sample(var1))
 rand1 <- replicate(10000, sample(var2))
 f1[i]=cor(rand, rand1, use ="everything", method=c("spearman"))
 } 

which gives me this message: Warning message: In f1[i]=cor(rand, rand1, use = "everything", method = c("spearman")) : number of items to replace is not a multiple of replacement length

I tried this:

cof <- cor((replicate(1000, sample(var1))), (replicate(1000, sample(var2))), use ="everything", method=c("spearman"))

which returns a matrix of correlation coefficients for each value, not each variable

Alternatively, if there's a way to ask R to correlate e.g. row 1 in one data frame with row 1 in another, then on to rows 2 then rows 3 etc I can get matrices of just my randomised variables with this:

set.seed(1)
f1=numeric(10000)
for (i in 1:10000) {rand <- replicate(10000, sample(var1))
  rand1 <- replicate(10000, sample(var2))
  }

which I would then have to correlate with each other

is there a way to calculate the correlation coefficient between each pair of randomised variables as they are generated and then create a new variable made up of the correlation coefficients for each randomisation?

Thanks




Aucun commentaire:

Enregistrer un commentaire