[英]how to calculate the Correlation coefficient using R using a function?
Hello!你好! I'm trying to write a function deriving the formula for Pearson's coefficient of correlation .我正在尝试编写一个函数来推导出 Pearson 相关系数的公式。 I wrote the following code but when I try to pass the values, it returns empty output.我编写了以下代码,但是当我尝试传递值时,它返回空输出。 Please point me to my error, I'm clueless!请指出我的错误,我一无所知! Much appreciated.非常感激。
correlation = function(X, Y, n = length(X)){
sum_X = 0
sum_Y = 0
sum_XY = 0
squareSum_X = 0
squareSum_Y = 0
i = 0
while (i < n ) {
# sum of elements of array X.
sum_X = sum_X + X[i]
# sum of elements of array Y.
sum_Y = sum_Y + Y[i]
# sum of X[i] * Y[i].
sum_XY = sum_XY + X[i] * Y[i]
# sum of square of array elements.
squareSum_X = squareSum_X + X[i] * X[i]
squareSum_Y = squareSum_Y + Y[i] * Y[i]
i =+ 1
}
# combine all into a final formula
final = (n * sum_XY - (sum_X * sum_Y))/ (sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y -
sum_Y * sum_Y)))
return (final)
}
R is a 1-indexed language. R 是 1 索引语言。 Start with i = 1
and change to while(i <= n)
(and fix the iteration counter as noted in the comments: i = i + 1
. Then your function works correctly.从i = 1
开始并更改为while(i <= n)
(并按照注释中的说明修复迭代计数器: i = i + 1
。然后您的函数可以正常工作。
n <- 100
x <- rnorm(n)
y <- rnorm(n)
round(correlation(x, y), 4) == round(cor(x, y), 4) # TRUE
Note, however, that R is also great for vectorized operations, and you can skip the explicit loop altogether.但是请注意,R 也非常适合矢量化操作,您可以完全跳过显式循环。 Something like this is a step towards getting more efficient:像这样的事情是提高效率的一步:
correlation2 <- function(X, Y){
n <- length(X)
sum_X <- sum(X)
sum_Y <- sum(Y)
sum_XY <- sum(X * Y)
squareSum_X <- sum(X * X)
squareSum_Y <- sum(Y * Y)
final <- (n * sum_XY - (sum_X * sum_Y)) / (sqrt((n * squareSum_X - sum_X * sum_X)* (n * squareSum_Y - sum_Y * sum_Y)))
return (final)
}
round(correlation2(x, y), 4) == round(cor(x, y), 4) # TRUE
Or even just:或者甚至只是:
correlation3 <- function(X, Y){
n = length(X)
sum_x = sum(X)
sum_y = sum(Y)
(n * sum(X * Y) - sum_x * sum_y) /
(sqrt((n * sum(x^2) - sum_x^2) * (n * sum(Y^2) - sum_y^2)))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.