2
$\begingroup$

In a matrix with user ratings I want to calculate similarities between the first ten users and the rest of the users. I use the method "jaccard" for the similarity calculation in recommenderlab.

My matrix looks like this:

   users v1 v2 v3 v4 v5
1      1  0  1  0  0  1
2      2  0  1  0  0  1
3      3  0  1  0  0  1
4      4  0  1  0  1  1
5      5  0  1  0  1  1
6      6  0  1  0  1  0
7      7  0  1  1  1  1
8      8  1  1  1  1  1
9      9  0  1  0  1  1
10    10  0  1  0  1  1
11    11  0  1  1  0  1
12    12  0  0  0  1  1
13    13  0  1  0  1  1
14    14  0  1  1  1  1
15    15  0  1  0  0  0
16    16  0  1  1  0  1
17    17  0  1  0  0  1
18    18  0  1  0  1  1
19    19  1  1  0  1  1
20    20  0  1  0  1  1

First I make two datafiles

datafile1 <- datafile[1:10,]
datafile2 <- datafile[2:20,]

Now I try to calculatie similarities with the similarity function

library(recommenderlab)
datafile1 <- as(datafile1, "realRatingMatrix")
datafile2 <- as(datafile2, "realRatingMatrix")
similarity(datafile2,datafile1, method = "jaccard", which="users")

This is the result:

   1 10 2 3 4 5 6 7 8 9
11 0  1 0 0 1 0 0 0 0 1
12 0  1 0 0 1 0 0 0 0 1
13 1  0 0 1 0 0 0 0 0 0
14 0  0 1 0 0 1 0 1 0 0
15 0  0 0 0 0 0 1 0 0 0
16 0  1 0 0 1 0 0 0 0 1
17 0  1 0 0 1 0 0 0 0 1
18 0  1 0 0 1 0 0 0 0 1
19 0  0 0 0 0 0 0 0 1 0
20 1  0 0 1 0 0 0 0 0 0

I think this is not what I expected because I only see 0's and 1's.
Am I doing something wrong here?

Many thanks in advance!

$\endgroup$
1
  • 5
    $\begingroup$ Actually belongs to stackoverflow (purely programming related). Why is the off-topic-belongs-other-site-button gone ? $\endgroup$
    – steffen
    Nov 29, 2013 at 6:44

1 Answer 1

2
$\begingroup$

As far as I can see, your datafile is a data.frame, not a matrix.

Given this data in a csv-file

id,users,v1,v2,v3,v4,v5
1,1,0,1,0,0,1
2,2,0,1,0,0,1
3,3,0,1,0,0,1
4,4,0,1,0,1,1
5,5,0,1,0,1,1
6,6,0,1,0,1,0
7,7,0,1,1,1,1
8,8,1,1,1,1,1
9,9,0,1,0,1,1
10,10,0,1,0,1,1
11,11,0,1,1,0,1
12,12,0,0,0,1,1
13,13,0,1,0,1,1
14,14,0,1,1,1,1
15,15,0,1,0,0,0
16,16,0,1,1,0,1
17,17,0,1,0,0,1
18,18,0,1,0,1,1
19,19,1,1,0,1,1
20,20,0,1,0,1,1

the following code works ...

dat <- read.csv("<source-Dir>/userData.csv",sep=",",header=T)
dat <- dat[,-1]
datafile <- as(dat,"matrix")

datafile1 <- datafile[1:10,]
datafile2 <- datafile[11:20,]
datafile1 <- as(datafile1, "realRatingMatrix")
datafile2 <- as(datafile2, "realRatingMatrix")
similarity(datafile2,datafile1, method = "jaccard", which="users")

print(datafile1)
10 x 6 rating matrix of class ‘realRatingMatrix’ with 60 ratings. # right

but converting a data.frame to a real rating matrix does not.

datafile1 <- as(dat[1:10,], "realRatingMatrix")
datafile2 <- as(dat[2:20,], "realRatingMatrix")
similarity(datafile2,datafile1, method = "jaccard", which="users")
print(datafile1)
10 x 2 rating matrix of class ‘realRatingMatrix’ with 10 ratings. # wrong !

I do not know the implementation details of recommenderlab, hence I cannot provide an explanation for this strange behavior.

Aside: Keep the user-ids out of the columns or it will be also treated as rating (for the item "users"). My code is wrong too (intentionally, otherwise similarity won't work .. see for yourself if you use dat[,-1] instead of dat in the latter part of the code)

$\endgroup$
1
  • $\begingroup$ sorry for the delete/un-delete confusion. Had to check the result of the jaccard - metric (it is actually tanimoto for real-values, see en.wikipedia.org/wiki/…) $\endgroup$
    – steffen
    Aug 9, 2013 at 13:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.