-1

I am trying to create a function in R to find a correlation coefficient for n-dimensional non-zero-variance variables using the equation seen at the bottom of this image: Click Here

So far, I have only made the example data-set I am working with:

a <- c(4, 6, 2, 5)
b <- c(8, 1, 3, 7)
c <- c(6, 3, 1, 3)
d <- c(7, 5, 7, 5)
e <- c(9, 2, 6, 1)
f <- c(4, 6, 4, 5)

al <- data.frame(a, b, c, d, e, f)
al

But I am having trouble incorporating the equation into the rest of my code.I know how to make a correlation matrix:

corrmatrix <- cor(al)
head(round(corrmatrix,3))

But I don't know if that should be where I start off or if I can work with the original data-set. I guess the math behind the equation is what is holding me back. For example: How do I use the "det()" function? Is there a function for getting the standardized vector of a variable?

Thank you in advance for any assistance!

Al G
  • 43
  • 2
  • 7
  • 1
    Your example data belong to characters. You need to transform them to numerics, or they won't be computed. In addition, `scale()` can do the standardization. – Darren Tsai Feb 11 '19 at 18:52
  • 1
    I noticed that before posting, but forgot to make the edition. Thank you for pointing it out, but my main issue is bringing the equation from the image I linked into my code. It seems like I definitely will need to use the scale() function, that helps a bit, thank you. – Al G Feb 11 '19 at 19:02

1 Answers1

-1

I think, the problem is with double quotes around the data elements in your vectors.

 a <- c(4, 6, 2, 5)
 b <- c(8, 1, 3, 7)
 c <- c(6, 3, 1, 3)
 d <- c(7, 5, 7, 5)
 e <- c(9, 2, 6, 1)
 f <- c(4, 6, 4, 5)
 al <- data.frame(a, b, c, d, e, f)
 corrmatrix <- cor(al) 
 head(round(corrmatrix,3))    

results

       a      b      c      d      e      f
a  1.000 -0.103  0.355 -0.845 -0.607  0.866
b -0.103  1.000  0.648  0.262  0.368 -0.553
c  0.355  0.648  1.000  0.140  0.459 -0.127
d -0.845  0.262  0.140  1.000  0.937 -0.905
e -0.607  0.368  0.459  0.937  1.000 -0.800
f  0.866 -0.553 -0.127 -0.905 -0.800  1.000
maaniB
  • 595
  • 6
  • 23
  • I noticed that before posting, but forgot to make the edition. Thank you for pointing it out. I have made the edit. But my main issue was applying the equation from the image I linked into my code. – Al G Feb 11 '19 at 20:50