Reputation: 74
I have X = [2,3,5,10,8,5] Y = [7,6,9,1,6,3] variables I want to calculate correlation coefficient between them but without using "cor(X,Y)" function how can I achieve that?
Upvotes: 0
Views: 520
Reputation: 887501
If we want to use the formula, the Xbar
is mean
, sigma
is sum
and sqrt
for the corresponding symbol in the formula
num1 <- sum((X - mean(X))*(Y - mean(Y)))
den1 <- sqrt(sum((X - mean(X))^2) * sum((Y - mean(Y))^2))
num1/den1
[1] -0.599539
-testing with cor
cor(X, Y)
#[1] -0.599539
X <- c(2, 3, 5, 10, 8, 5)
Y <- c(7, 6, 9, 1, 6, 3)
Upvotes: 1