Compute the correlation of x and y by hand:
Obs: | 1 | 2 | 3 | 4 | 5 |
x: | 1 | 2 | 3 | 4 | 5 |
y: | 1 | 3 | 2 | 5 | 4 |
Verify your answer using R.
Answer:
x = (1 + 2 + 3 + 4 + 5) / 5 = 3,
x = (1 + 3 + 2 + 5 + 4) / 5 = 3,
SDx = √((1-3)2 + (2-3)2 + (3-3)2 +
(4-3)2 + (5-3)2) / 5) = √2
SDy = √((1-3)2 + (3-3)2 + (2-3)2 +
(5-3)2 + (4-3)2) / 5) = √2
Now compute the z-scores for both x and y:
zxi = (xi - x) / SDx
and zyi = (yi - y) / SDy
for i = 1, ... , n.
Then the correlation is the average of the products of the z-scores:
xi yi
zxi zyi
zxi*zyi
-----+-----+---------+---------+-----------
1 1 -2/√(2) -2/√(2)
+4/2 = 2
2 3 -1/√(2)
0 0 = 0
3
2 0 -1/√(2)
0 = 0
4 5
+1/√(2) +2/√(2) +2/2 = 1
5
4 +2/√(2) +1/√(2) +2/2 = 1
The
average of
the products is (2 + 0 + 0 + 1 + 1) / 5 = 0.8.
Verify your calculation with
R:
> x <- 1:5
[1] 1 2 3 4 5
> y <- c(1, 3, 2, 5, 4)
[1] 1 3 2 5 4
> cor(x, y)
[1] 0.8