Search code examples
rcsvmatrixcorrelationsimilarity

Computing correlation of matrix


I have a matrix with some documents and all the words in them. The number indicates the number of occurrences of this word in a document.

| topic    | word1 | word2 | word3 | word4 | word5 |...
|----------|-------|-------|-------|-------|-------|
| politics | 5     | 2     | 4     | 0     | 1     |
| sports   | 2     | 0     | 1     | 1     | 6     |
| music    | 2     | 3     | 1     | 3     | 6     |
| movies   | 0     | 3     | 2     | 6     | 1     |
| history  | 4     | 6     | 2     | 3     | 3     |
|...

I want to compute and visualize their correlations. So say I would like to see if the document about music and more similar to the document about movies, or polities, etc.

When doing:

csv <- read.csv("documents.csv")
matrix <- data.matrix(csv)
cor(matrix)

I get:

            topic       word1       word2       word3      word4      word5
topic  1.00000000  0.08111071 -0.94812244  0.00000000 -0.6868028  0.3779645
word1  0.08111071  1.00000000  0.21296184  0.62828086 -0.7687575 -0.1635038
word2 -0.94812244  0.21296184  1.00000000  0.09415545  0.4307761 -0.3032248
word3  0.00000000  0.62828086  0.09415545  1.00000000 -0.3546635 -0.8132501
word4 -0.68680282 -0.76875749  0.43077610 -0.35466345  1.0000000 -0.2249755
word5  0.37796447 -0.16350382 -0.30322482 -0.81325006 -0.2249755  1.0000000

Actually I'm not sure if I got the right results and how to interpret them.

Update:

> dput(csv)
structure(list(topic = structure(c(4L, 5L, 3L, 2L, 1L), .Label = c("history", 
"movies", "music", "politics", "sports"), class = "factor"), 
    word1 = c(5L, 2L, 2L, 0L, 4L), word2 = c(2L, 0L, 3L, 3L, 
    6L), word3 = c(4L, 1L, 1L, 2L, 2L), word4 = c(0L, 1L, 3L, 
    6L, 3L), word5 = c(1, 6, 6, 1, 3)), .Names = c("topic", "word1", 
"word2", "word3", "word4", "word5"), class = "data.frame", row.names = c(NA, 
-5L))


> dput(matrix)
structure(c(4, 5, 3, 2, 1, 5, 2, 2, 0, 4, 2, 0, 3, 3, 6, 4, 1, 
1, 2, 2, 0, 1, 3, 6, 3, 1, 6, 6, 1, 3), .Dim = 5:6, .Dimnames = list(
    NULL, c("topic", "word1", "word2", "word3", "word4", "word5"
    )))

Solution

  • You probably want to remove the first column and work on the transposed matrix:

    csv <- read.csv("documents.csv")
    
    row.names(csv) <- csv[,1]
    
    csv <- csv[,-1]
    
    matrix <- as.matrix(csv)
    cor(t(matrix))