[英]how to write R code that calculate correlation coefficient for 2 numbers
If we want to use the formula, the Xbar
is mean
, sigma
is sum
and sqrt
for the corresponding symbol in the formula如果我们要使用公式, Xbar
是mean
, sigma
是sum
, sqrt
是公式中对应的符号
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
cor(X, Y)
#[1] -0.599539
X <- c(2, 3, 5, 10, 8, 5)
Y <- c(7, 6, 9, 1, 6, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.