简体   繁体   中英

how to write R code that calculate correlation coefficient for 2 numbers

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? 在此处输入图像描述

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

data

X <- c(2, 3, 5, 10, 8, 5)
Y <- c(7, 6, 9, 1, 6, 3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM