> norm = function(vec) {
+
return(sqrt(sum(vec^2)))
+ }
> rvcn = sweep(rvc, 1, STATS = apply(rvc, 1, norm), FUN = "/")
(d) Compute the cosine similarity between all 100 vectors (2 by 2).
> cos.sim = function(p, q) {
+
return(sum(p * q))
+ }
> cossv = rep(0, n * (n - 1)/2)
> for (i in 1:(n - 1)) {
+
for (j in (i + 1):n) {
+
cossv[n * (i - 1) - i * (i - 1)/2 + j - i] = cos.sim(rvcn[i,
+
], rvcn[j, ])
+
}
+ }
> cossm = matrix(0, n, n)
> for (i in 1:n) {
+
for (j in i:n) {
+
cossm[i, j] = cos.sim(rvcn[i, ], rvcn[j, ])
+
cossm[j, i] = cossm[i, j]
+
}
+ }
(e) Compute the correlation between all 100 vectors of length 10 (2 by 2).
> corrm = cor(t(rvcn))
> corr.forvec = as.vector(as.dist(corrm))
(f) Show these two similarities against each other in a scatter plot.
> library(ggplot2)
> print(qplot(c(as.vector(as.dist(corrm, diag = TRUE)), diag(corrm)),
+
c(as.vector(as.dist(cossm,
diag = TRUE)), diag(corrm)), alpha = I(1/100),
+
main = "Matrix Method Comparison of Cos.sim
+
and Correlation (with diagonal)"))
2