Testing recommender systems in R

Recommender systems are pervasive. You have encountered them while buying a book on barnesandnoble, renting a movie on Netflix, listening to music on Pandora, to finding the bar visit (FourSquare). Saar for Revolution Analytics, had demonstrated how to get started with some techniques for R here.

We will build some using Michael Hahsler’s excellent package – recommenderlab. But to build something we have to learn to recognize when it is good. For this reason we will talk about some metrics quickly –

– RMSE  (Root Mean Squared Error) : Here we measure far were real ratings from the ones we predicted. Mathematically, we can write it out as

RMSE = \sqrt\frac{\sum_{(i,j) \in \kappa}(r_{(i,j)} - \hat {r}_{(i,j)})^2}{|\kappa|}

where \kappa is the set of all user-item pairings (i, j) for which we have a predicted rating \hat r_{(i,j)}  and a known rating r_{(i,j)}  which was not used to learn the recommendation model.

Here at sane.a.lytics, I will talk about when an analysis makes sense and when it doesn’t. RMSE is a great metric if you are measuring how good your predicted ratings are. But if you want to know how many people clicked on your recommendation, I have a different metric for you.

– Precision/Recall/f-value/AUC: Precision tells us how good the predictions are. In other words, how many were a hit.

Recall tells us how many of the hits were accounted for, or the coverage of the desirable outcome.

Precision and recall usually have an inverse relationship. This becomes an even bigger issue for rare issue phenomenon like recommendations. To tackle this problem, we will use f-value. This is nothing but the harmonic mean of precision and recall.

Another popular measure is AUC. This is roughly analogous. We will go ahead and use this for now for our comparisons of recommendation effectiveness.

– ARHR (Hit Rate): Karypis likes this metric.

ARHR = \frac{1}{\#users} \sum_{i=1}^{\#hits} \frac{1}{p_i}

where p is the position of the item in a ranked list.

OK, on to the fun stuff.

They are a few different ways to build a recommender system

Collaborative Filtering : If my friend Jimmy tells me that he liked the movie “Drive”, I might like it too since we have similar tastes. However if Paula tells me she liked “The Notebook”, I might avoid it. This is called UBCF (User-based collaborative filtering). Another way to think about it is that this is soft-clustering. We find Users with similar tastes (neighbourhood) and use their preferences to build yours.

Another flavour of this is IBCF (Item Based Collaborative Filtering). If I watched “Darjeeling Limited”, I might be inclined to watch “The Royal Tannenbaums” but not necessarily “Die Hard”. This is because the first two are more similar in the users who have watched/rated them. This is a rather simple to compute as all we need is the covariance between products to find out what this might be.

Let’s compare both approaches on some real data (thanks R)

Distribution of ratings sample

Histogram of ratings

Rating histogram, normalized

Movies rated

avg movie rating

AUC

prec/recall

It seems like UBCF did better than IBCF. Then why would you use IBCF? The answer lies is when and how are you generating recommendations. UBCF saves the whole matrix and then generates the recommendation at predict by finding the closest user. IBCF saves only k closest items in the matrix and doesn’t have to save everything. It is pre-calculated and predict simply reads off the closest items.

Predictably, RANDOM is the worst but perhaps surprisingly it seems, its hard to beat POPULAR. I guess we are not so different, you and I.

In the next post I will go over some other algorithms that are out there and how to use them in R. I would also recommend reading Michael’s documentation on recommenderlab for more details.

Also added this to r-bloggers. Please check it out for more R goodies.

Testing recommender systems in R

9 thoughts on “Testing recommender systems in R

  1. thai says:

    results <- evaluate(scheme, algorithms, n=c(1, 3, 5, 10, 15, 20))
    RANDOM run
    1 [0sec/1.64sec] POPULAR run
    1 [0.18sec/0.36sec] UBCF run
    1 [0.16sec/5.99sec] IBCF2 run
    1 Timing stopped at: 0 0 0
    Error in .local(data, …) :
    Recommender method IBCF2 not implemented for data type realRatingMatrix .

    Please help me fix this error. Thanks!

  2. Sorry about the delayed response. I have submitted to recommenderlab codebase and IBCF’s bug has been fixed. Please go ahead and use IBCF instead of IBCF2.

  3. Christos says:

    Hi and thanks for your code contribution. I am trying to get the recommenderlabbrats but I get an error:

    Downloading GitHub repo sanealytics/recommenderlabrats@master
    Installing recommenderlabrats
    ‘/Library/Frameworks/R.framework/Resources/bin/R’ –no-site-file –no-environ –no-save –no-restore \
    CMD INSTALL \
    ‘/private/var/folders/57/xggd_p_s1g70kf37yp67b6t80000gn/T/Rtmp8bTtut/devtools1058c7c56f105/sanealytics-recommenderlabrats-2d2b52c’ \
    –library=’/Library/Frameworks/R.framework/Versions/3.2/Resources/library’ –install-tests

    * installing *source* package ‘recommenderlabrats’ …
    ** libs
    clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I”/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include” -I”/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/include” -fPIC -Wall -mtune=core2 -g -O2 -c RcppExports.cpp -o RcppExports.o
    clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -I”/Library/Frameworks/R.framework/Versions/3.2/Resources/library/Rcpp/include” -I”/Library/Frameworks/R.framework/Versions/3.2/Resources/library/RcppArmadillo/include” -fPIC -Wall -mtune=core2 -g -O2 -c als.cpp -o als.o
    als.cpp:2:10: fatal error: ‘omp.h’ file not found
    #include
    ^
    1 error generated.
    make: *** [als.o] Error 1
    ERROR: compilation failed for package ‘recommenderlabrats’
    * removing ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library/recommenderlabrats’
    Error: Command failed (1)

    Do you have any ideas on that?

    Thanks,

    Christos

    1. Thank you for using the package.

      This error happened because I had a hard dependancy on OpenMP BLAS. I just changed it so that you can install it on a single core machine too.
      Sorry I couldn’t test it because my machine has OpenMP. Can you let me know if it works for you now?

  4. Filip says:

    hey, could you give me a hint what the parameter given exactly does. as I see, with user less item ratings than given the evaluation fails

    secondly… the UBCF and IBCF seems to be extremely slow

    my data set is like 50000 x 3000 big

  5. Subhasree Chatterje says:

    My Rstudio hangs whenever I try to run IBRS. Has anyone else faced similar problem? Is there a way to get rid of this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s