richard — Jun 11, 2014, 5:35 AM
ad <- read.csv("AliceDirections.csv") # Alice directions file
head(ad)
i x y
1 1 0.96093 0.2768
2 2 0.64485 -0.7643
3 3 0.96758 -0.2526
4 4 0.08544 -0.9963
5 5 -0.83315 -0.5530
6 6 0.97774 0.2098
u <- rbind(ad$x, ad$y) # just the x and y coordinates as a 2xN matrix
rownames(u) <- c("x", "y")
bd <- read.csv("BobDirections.csv") # Bob directions file
head(bd)
i x y
1 1 -0.96093 -0.2768
2 2 -0.64485 0.7643
3 3 -0.96758 0.2526
4 4 -0.08544 0.9963
5 5 0.83315 0.5530
6 6 -0.97774 -0.2098
nrow(ad) == nrow(bd) # both files same length?
[1] TRUE
nrow(ad) # N
[1] 10000
v <- rbind(bd$x, bd$y) # just the x and y coordinates as a 2xN matrix
rownames(v) <- c("x", "y")
## The data came "row-wise" (each observation a different row)
## But for technical reasons I wanted it column-wise.
## The function "t" transposes a matrix
head(t(u)) # the first few directions in u
x y
[1,] 0.96093 0.2768
[2,] 0.64485 -0.7643
[3,] 0.96758 -0.2526
[4,] 0.08544 -0.9963
[5,] -0.83315 -0.5530
[6,] 0.97774 0.2098
tail(t(u)) # the last few directions in u
x y
[9995,] -0.887422 -0.4610
[9996,] -0.907657 0.4197
[9997,] -0.008629 -1.0000
[9998,] -0.991182 0.1325
[9999,] 0.968047 0.2508
[10000,] -0.600912 0.7993
head(t(v)) # the first few directions in v
x y
[1,] -0.96093 -0.2768
[2,] -0.64485 0.7643
[3,] -0.96758 0.2526
[4,] -0.08544 0.9963
[5,] 0.83315 0.5530
[6,] -0.97774 -0.2098
tail(t(v)) # the last few directions in v
x y
[9995,] 0.887422 0.4610
[9996,] 0.907657 -0.4197
[9997,] 0.008629 1.0000
[9998,] 0.991182 -0.1325
[9999,] -0.968047 -0.2508
[10000,] 0.600912 -0.7993
# E(0, 45)
alpha <- 0 * pi / 180
beta <- 45 * pi / 180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
rho11 <- mean(sign(colSums(a*u))*sign(colSums(b*v))) # E(0, 45)
rho11 # E(0, 45)
[1] -0.4944
# E(0, 135)
alpha <- 0 * pi / 180
beta <- 135 * pi / 180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
rho12 <- mean(sign(colSums(a*u))*sign(colSums(b*v)))
rho12 # E(0, 135)
[1] 0.497
# E(90, 45)
alpha <- 90 * pi / 180
beta <- 45 * pi / 180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
rho21 <- mean(sign(colSums(a*u))*sign(colSums(b*v)))
rho21 # E(90, 45)
[1] -0.4942
# E(90, 135)
alpha <- 90 * pi / 180
beta <- 135 * pi / 180
a <- c(cos(alpha), sin(alpha))
b <- c(cos(beta), sin(beta))
rho22 <- mean(sign(colSums(a*u))*sign(colSums(b*v)))
rho22 # E(90, 135)
[1] -0.5144
### To summarize, here are the four correlations:
corrs <- c(rho11, rho12, rho21, rho22)
names(corrs) <- c("E(0, 45)", "E(0, 135)", "E(90, 45)", "E(90, 135)")
corrs
E(0, 45) E(0, 135) E(90, 45) E(90, 135)
-0.4944 0.4970 -0.4942 -0.5144