# Required libraries
library(recommenderlab)  
library(tidyverse)           
library(ggthemes)
library(kableExtra)
library(skimr)
library(ggrepel)         
library(tictoc)
library(sparklyr)       

Overview

The goal of this project is to practice beginning to work with a distributed recommender system. I leverage my Project 3 Recommender System Spark and Sparklyr to add an additional model to my original analysis. Project 3 originally included UCBF and SVD models, we’ll use Spark to build and add an ALS model to the analysis. Next we will compare the accuracy of the three approaches and discuss the Spark / Sparklyr experience and potential benefits. Part 1 includes the original Project 3 analysis. Part 2 includes the new ALS model built with Spark and Sparklyr. We take a step by step approach with this new tool.

Part 1 - The Original Project 3 Work

The Data Set

The data set is courtesy of MovieLens project and it was downloaded from https://grouplens.org/data sets/movielens/. Please note - I reduced the size of the movie matrix so that my circa 1990 Mac Mini could handle the load. The data set is comprised of two files - rates and titles. We utilized the skimr package to explore the data. SVD models require no missing data. Skimr will let us know where we stand in that regard.

Convert to matrix

First we convert our data into the required format - a realRatingMatrix. The end result is a 150 by 4332 rating matrix with more than 18,000 ratings.

## 150 x 4332 rating matrix of class 'realRatingMatrix' with 18262 ratings.

Split the Data

To train and test our models, we need to split our data into training and testing sets. We utilize an 80-20 split, with given of 7 and a good Ratting set to 3.5.

Build Models

We will build a User-Based Collaborative model and an SVD model. We will compare each model’s performance, based upon RMSE, as well as the time required to build and predict under each methodology.

Table 1. UBCF Performance Results.

x
RMSE 0.8682038
MSE 0.7537779
MAE 0.6706194

Singular Value Decomposition (SVD) Model

Next we create the sVD model. After some tuning, k = 20, was utilized in the final SVD model. Table 2 below set forth the performance of the SVD model.

Table 2. SVD Performance Results

x
RMSE 0.8731257
MSE 0.7623486
MAE 0.6761709

Performance Assessment

The UCBF model outperformed the SVD model by a narrow margin. The RMSE for the UCBF was 0.868 versus 0.873 for the SVD - a virtual tie from the performance perspective. Next we will see have fast the models are trained and how fast they produce predictions. Table 4. below sets forth a comparison of the two alternatives.

####The result of the speed comparison are interesting. The UCBF model was trained 10x faster than the SVD(0.02 seconds vs 0.20 seconds). However, it also took almost 10x longer for the UCBF model predictions (0.82 second vs 0.09 seconds).

These results make a strong argument for the SVD model. This is due to the fact that model are trained infrequently, but are called upon for predictions often. Given the near equal RSME performance and the superior prediction performance the SVD model would be a good choice for a production system.

Run Time
UBCF Model - Training: 0.01 sec elapsed
UBCF Model - Predicting: 0.92 sec elapsed
SVD Model - Training: 0.22 sec elapsed
SVD Model - Predicting: 0.08 sec elapsed

Model Predictions

Now we will make some movie predictions to see if the models produce similar results. Since it’s the 22nd of June, we’ll pick the 22nd user and see how she rated her movies.

Our movie rater appears to be a somewhat generous movie rater or someone who simply likes movies. Of the 22 movies rated 18 were either rated 4 or 5. Dumb & Dumber got a rating of 1, Pulp Fiction and Ace Ventura each earned a 3. This could indicate that our movie rater does like Violent or Comedy movies. There does appear to be a preference for action/suspense, drama and feel good movies.

Movie Rating
Dumb & Dumber (Dumb and Dumber) (1994) 1
Pulp Fiction (1994) 3
Ace Ventura: Pet Detective (1994) 3
Crimson Tide (1995) 4
Waterworld (1995) 4
Interview with the Vampire: The Vampire Chronicles (1994) 4
Shawshank Redemption, The (1994) 4
True Lies (1994) 4
Cliffhanger (1993) 4
Beauty and the Beast (1991) 4
Apollo 13 (1995) 5
Batman Forever (1995) 5
Die Hard: With a Vengeance (1995) 5
Net, The (1995) 5
Outbreak (1995) 5
Stargate (1994) 5
Star Trek: Generations (1994) 5
While You Were Sleeping (1995) 5
Clear and Present Danger (1994) 5
Aladdin (1992) 5
Dances with Wolves (1990) 5
Batman (1989) 5

UCBF vs SVD

Part 2 - ALS With Spark and Sparklyr

ALS Model Using Spark

We already have an UCBF and SVD models, so now we will use spark to create an ALS model. Here are the steps for this analysis.

Establish Connection to Spark Server - local in this case.

Prepare the data

We are using the same data set as used above, just a assigning some spark-inspired names to the variables.

Move to Spark server

This is the key step of copy the data to the spark server for processing. The move to spark is accomplished with a simple copy command (sdf_copy_to)

Display the Spark ALS Model Performance Results

RMSE MSE MAE
Spark ALS 1.026193 1.053071 0.7928117

Compare Results and Discuss Spark

The UBCF and SVD model performed better than the ALS, but that not the headline.

Spark and Sparklyr provide the average R programmer an ability to harness the power and speed of spark while staying in the friendly confines of R. What I experienced was that Spark did not seem to build / calculate the model any faster than RecommenderLabs, however, predictions were significantly faster. This is because the spark approach loads everything to memory, so once you have a working model it just sits there any memory ready to respond. As a result, spark would be a much better platform to deploy a system at scale.

In conclusion, moving to a distributed architecture would seem advisable when data sets are large, processing is computationally demanding and / or minimizing processing time is critical.

RMSE MSE MAE
RecLabs_UBCF 0.8682038 0.7537779 0.6706194
RecLab_SVD 0.8731257 0.7623486 0.6761709
Spark ALS 1.0261927 1.0530714 0.7928117