Final draft of report, ready for others to read.

This commit is contained in:
shelldweller 2021-09-13 12:26:31 -06:00
parent 926da630f1
commit b2a152bff9
2 changed files with 37 additions and 48 deletions

View File

@ -10,11 +10,11 @@ knitr::opts_chunk$set(echo = TRUE)
```
## Introduction and Overview
One common application of machine learning is the creation of recommendation systems. These are mathematical models that take as input the parameters associated with a particular observation to give as output the value of an unknown parameter associated with the observation as a prediction. The more accurate this prediction is, the more useful and valuable it is to specific industries. These industries use these predictions to make recommendations to users based on past behavior and the behavior of other similar users under similar circumstances. For example, video distribution systems might use such a recommendation algorithm to create lists of suggested movies based on what is currently being watched by a particular user.
One common application of machine learning is the creation of recommendation systems. These are mathematical models that take as input parameters associated with a particular observation to give as output the value of an other parameter associated with the observation as a prediction. The more accurate this prediction is, the more useful and valuable it is in specific industries. These predictions are used to make recommendations to users based on past behavior and the behavior of similar users under similar circumstances. For example, an online video distribution network might use such a recommendation algorithm to create lists of suggested movies based on what is currently being watched by a particular user.
### Goal Summary
Our goal is to create such a recommendation algorithm. Our data set containing movie ratings is provided by GroupLens known as the ["10M version of the MovieLens dataset"](https://grouplens.org/datasets/movielens/10m/), which contains 5 base variables per observation. We will measure the accuracy of our model by comparing the ratings predicted for a subset of our data to the actual ratings for those observations, and calculate the root mean square error (RMSE) for the final set of predictions. An RMSE less than 0.86490 will indicate a successful model for the purposes of this report.
Our goal is to create a recommendation algorithm from a standard set of data. Our data set provided by GroupLens is known as the ["10M version of the MovieLens dataset"](https://grouplens.org/datasets/movielens/10m/), which contains 5 base variables per observation. We will measure the accuracy of our model by comparing the ratings predicted for a subset of our data to the actual ratings for those observations, and calculate the root mean square error (RMSE) for the final set of predictions. An RMSE less than 0.86490 will indicate a successful model for the purposes of this report.
### Data
@ -22,7 +22,7 @@ The MovieLens data set contains 10000054 rows, 10677 movies, 797 genres and 6987
* `userId`: Unique user identification number.
* `movieId`: Unique movielens movie identification number.
* `rating`: User-provided ratings on a 5-star scale with half-star increments starting at 0.5
* `rating`: User-provided ratings on a 5-point scale with half-point increments starting at 0.5
* `timestamp`: Time of user-submitted review in epoch time,
* `title`: Movie titles including year of release as identified in [IMDB](http://www.imdb.com)
* `genres`: A pipe-separated list of film genres
@ -30,7 +30,7 @@ The MovieLens data set contains 10000054 rows, 10677 movies, 797 genres and 6987
### Preparation
The main data set is split into a working set called "edx" and a second set representing 10% of the original set, called "validation", which will be only used for checking the model once it has been created, and will not be used at all during the model formation process.
The main data set is split into a working set called `edx` and a second set representing 10% of the original set, called `validation`, which will be only used for checking the model once it has been created, and will not be used at all during the model formation process.
```{r data preparation, message=FALSE, echo=FALSE, warning=FALSE}
@ -88,7 +88,7 @@ rm(dl, ratings, movies, test_index, temp, movielens, removed)
```
The resulting working set "edx" is further split into a training set and a test set which will be used to evaluate various methods. The test set comprises of 20% of the edx working set. No attempt was made to ensure that all the users and movies in the test set are also in the training set. This leads to a fortunate discovery later.
The resulting working set `edx` is further split into a training set and a test set which will be used to evaluate various methods. The test set comprises of 20% of the `edx` working set. No attempt is made to ensure that all the users and movies in the test set are also in the training set. This leads to a fortunate insight later, and thus has been included as part of the discovery process.
## Analysis
@ -98,15 +98,17 @@ The resulting working set "edx" is further split into a training set and a test
2) Movie Effect Model
3) Notice NA's in predictions, need to adjust somehow (leads to insight that is worth mentioning)
3) Notice `NAs` in predictions, need to adjust somehow (leads to an insight that is worth mentioning)
4) Movie and User Effects Model with manual outlier pruning.
4) Movie and User Effects Model with fixed penalization factor lambda.
5) Penalized User and Movie effect with Lambda to the hundredth's place (overkill, but interesting).
5) Penalized User & Movie effect with lambda optimized to the hundredth's place (overkill, but interesting).
6) Simplified Penalized Effect method with integer Lambda and statistical outlier pruning.
6) Simplified Penalized Effect method with integer lambda and manual outlier pruning.
7) RMSE is now in range, test against validation set.
7) Simplified Penalized Effect method with integer lambda and statistical outlier pruning.
8) RMSE now in an acceptable range, test against validation set.
### Exploration
@ -148,7 +150,7 @@ rmse_results <- tibble(method = method_string, RMSE = naive_rmse)
```
#### Movie Effect Model
The next approach was to account for a movie bias effect, since some movies naturally get higher ratings than others. This is when "NA" values started showing up in the predicted ratings. To get past this error for now, the option "na.rm=TRUE" was added to the mean part of the loss function. This effectively throws out any observation that does not have a matching user ID or movie title in the training set. This was only a temporary fix and merited a closer look.
The next refinement to our approach is to account for a movie bias effect, since some movies naturally get higher ratings than others. This is when `NA` values start showing up in the predicted ratings. To get past this error for now, the option `na.rm=TRUE` is added to the `mean()` part of the loss function. This effectively throws out any observation that does not have a matching user ID or movie title in the training set. This is only a temporary fix and merits a closer look.
```{r Notice NAs, echo=FALSE}
# Factor in movie bias the quick way
@ -169,10 +171,12 @@ rmse_results <- bind_rows(rmse_results,
tibble(method="Movie Effect Model",
RMSE = bi_rmse))
# How many are we throwing out?
# How many NA values do we need to get rid of?
nas <- sum(is.na(predicted_ratings))
paste("There are", as.character(nas), "NA values in our predictions, which need to be removed.", sep= " ")
# Plot movie and user histograms to get an idea of what is going on
edx %>% group_by(userId) %>% summarize(reviews = n()) %>% ggplot(aes(reviews)) + geom_histogram(color = "black", bins = 50) + scale_x_log10() + ggtitle("Number of reviews per user") + xlab("Number of reviews (log scale)") +
ylab("Count of users")
@ -181,7 +185,7 @@ edx %>% group_by(movieId) %>% summarize(reviews = n()) %>% ggplot(aes(reviews))
```
Not very many NA values are being generated. The assumption is that movies with only a few ratings affect the mean and therefore the error without contribution much to the overall effect. The same could be said for users who have only rated a few movies. Removing these NA values allowed the RMSEs to be calculated, however a more rigorous approach needs to be formulated as this is only a temporary workaround.
Not very many `NA` values are being generated. This is most likely due to movies and/or users in the training set that are not present in the test set, or vice versa. This was avoided in the original data set by using the `inner_join` function. Instead, we attempt to remove any movies or users with extremely low rating counts. The assumption is that movies with only a few ratings affect the mean and therefore the error without contributing much to the overall effect. The same could be said for users who have only rated a few movies. Removing these low-frequency observations prevents `NA` values in the predicted ratings and allows the RMSEs to be calculated. A more rigorous approach needs to be formulated as this is only a temporary workaround.
````{r remove NAs, echo=FALSE, message=FALSE}
RMSE <- function(true_ratings, predicted_ratings){
@ -205,7 +209,7 @@ rmse_results <- bind_rows(rmse_results,
#### Move and User Effect Model
Similar to the Movie Effect model, user bias can also be accounted for.
Similar to the Movie Effect model, user bias can also be accounted for. The naive average approach is biased for each movie as well as each user, since certain users tend to rate higher or lower than the average user.
````{r more effects, echo=FALSE, message=FALSE}
# Now account for user bias as well, quick method:
@ -235,46 +239,38 @@ rmse_results <- bind_rows(rmse_results,
#### Penalized Movie Effect model.
Now that we have developed a model that performs better than the Naive RMSE approach that we began with, we can start to regularize the effects by penalizing with a parameter we will call lambda. We start by penalizing both the Movie and User effects with the penalization factor lambda arbitrarily fixed at 3.
Now that we have developed a model that performs better than the Naive RMSE approach that we began with, we can start to regularize the effects by penalizing with a parameter we will call lambda. We start by penalizing both the movie and user effects with the penalization factor lambda arbitrarily set to 3.
````{r penalized approach, echo=FALSE, message=FALSE}
# Now try a Penalized Movie and User Effects estimate with lambda = 3
lambda <- 3
#movie_reg_avgs <- train_set %>%
#group_by(movieId) %>%
#summarize(b_i = sum(rating - mu_hat)/(n()+lambda), n_i = n())
b_i <- train_set %>% group_by(movieId) %>% summarize(b_i = sum(rating - mu_hat)/(n()+lambda))
b_u <- train_set %>% left_join(b_i, by="movieId") %>% group_by(userId) %>% summarize(b_u = sum(rating - b_i - mu_hat)/(n()+lambda))
# Check RMSE
#predicted_ratings <- test_set %>%
#left_join(movie_reg_avgs, by = "movieId") %>%
#mutate(pred = mu_hat + b_i) %>%
#pull(pred)
predicted_ratings <- test_set %>% left_join(b_i, by = "movieId") %>% left_join(b_u, by = "userId") %>% mutate(pred = mu_hat + b_i + b_u) %>% pull(pred)
lambda_rmse <- RMSE(predicted_ratings, test_set$rating)
rmse_results <- bind_rows(rmse_results,
tibble(method="Penalized Movie Effect Model, lambda=3",
RMSE = lambda_rmse))
#rmse_results
```
#### Step-Wise Cross-Validation with Lambda optimized to two decimal places.
#### Step-Wise Cross-Validation with lambda optimized to two decimal places.
Iterated cross-validation can be used to quickly optimize the penalization factor lambda to two decimal places of precision. First, lambda is optimized to the nearest integer, then it is optiized again to the nearest tenth, and then again to the nearest hundredth.
Iterated cross-validation can be used to quickly optimize the penalization factor lambda to two decimal places of precision. First, lambda is optimized to the nearest integer, then it is optimized again to the nearest tenth, and then again to the nearest hundredth. This takes 30 tests, instead of the 100 that would be needed to check all values from 1 to 10 at 0.01 increments.
````{r optimized penalized approach, echo=FALSE, message=FALSE}
# Now optimize lambda using step-wise cross-validation
#Optimize lambda to the nearest integer between 1 and 10
lambdas <- seq(1, 10, 1)
RMSEs <- sapply(lambdas, function(l){
b_i <- train_set %>% group_by(movieId) %>% summarize(b_i = sum(rating - mu_hat)/(n()+l))
@ -285,6 +281,8 @@ return(RMSE(predicted_ratings, test_set$rating))
lambda_best <- lambdas[which.min(RMSEs)]
# Optimize lambda to the tenths place
lambdas <- seq(lambda_best - 1 , lambda_best + 1 , 0.1)
RMSEs <- sapply(lambdas, function(l){
b_i <- train_set %>% group_by(movieId) %>% summarize(b_i = sum(rating - mu_hat)/(n()+l))
@ -295,6 +293,7 @@ return(RMSE(predicted_ratings, test_set$rating))
lambda_best <- lambdas[which.min(RMSEs)]
# Optimize lambda to the hundredths place
lambdas <- seq(lambda_best - 0.1 , lambda_best + 0.1 , 0.01)
RMSEs <- sapply(lambdas, function(l){
@ -304,14 +303,12 @@ predicted_ratings <- test_set %>% left_join(b_i, by = "movieId") %>% left_join(b
return(RMSE(predicted_ratings, test_set$rating))
})
lambdas_rmse <- min(RMSEs)
lambda_best <- lambdas[which.min(RMSEs)]
final_method <- paste("Optimized Penalized Movie & User Effect Model, lambda =", as.character(lambda_best), sep=" ")
rmse_results <- bind_rows(rmse_results, tibble(method=final_method, RMSE = lambdas_rmse))
# Optimize lambda to the integer level only
lambdas <- seq(1, 10, 1)
@ -348,24 +345,16 @@ rmse_results <- bind_rows(rmse_results, tibble(method=final_method, RMSE = lambd
```
The final RMSE does not seem to depend greatly on lambda to a high degree of precision. Optimizing lambda to the integer level seems sufficient.
```{r rmse results, echo=FALSE}
#print.data.frame(rmse_results)
```
### Insights
* `NAs in predicted_ratings`: I was getting NAs in the predicted_ratings output. This lead to an investigation of outliers that helped reduce the overall RMSE.
* `NAs in predicted_ratings`: A failure to use the proper join function inadvertently lead to `NA` values in the predictions, which prevents the proper calculation of an RMSE. Attempts to rectify this leads to an investigation of outliers that helps reduce the overall RMSE.
* `Lambda can be refined step-wise`: Lambda can be optimized to two decimal places using 30 tests instead of 100. This ultimately proved to be worthless, but interesting.
* `lambda can be refined step-wise`: Lambda can be optimized to two decimal places using 30 tests instead of 100. This ultimately proved to be worthless, but is interesting. The final RMSE does not seem to depend greatly on lambda to a high degree of precision. Optimizing lambda to the integer level seems sufficient and takes 1/3 the computational time.
### Final Modeling Approach
Ultimately, what helped more than fine-tuning lambda was to throw away the outliers that were adding more noise than was worth in the overall weighted model. I experimented with a few hard-coded values, starting by removing any movie with three or fewer ratings submitted, and removing any user that has reviewed nineteen or fewer movies.
Ultimately, what helps more than fine-tuning lambda is to throw away the outliers that are theoretically adding more noise to the mean than they contribute to the predictive power of the algorithm. Hard-coded values are tested, starting by removing any movie with three or fewer ratings submitted, and removing any user that has reviewed nineteen or fewer movies.
```{r hard-coded outlier filter, message=FALSE, echo=FALSE, warning=FALSE}
edx2 <- edx %>% group_by(movieId) %>% filter(n()>4) %>% ungroup()
@ -417,11 +406,11 @@ print.data.frame(rmse_results)
```
The slight reduction in RMSE was a surprise. The reason for pruning out the lowest number of ratings per movie and per user was to no longer need to remove NA values from the predictions, since that arose from either movies or users with a low count winding up in the training set but not the test set. It was assumed that if every movie has been rated more than three times, and if every user had rated at least nineteen movies, then the possibility of either not being represented in the test set was much lower. It was realized after the fact that this can be controlled during the partition creation by using semi_join, as was done with the original data set. Nonetheless, this attempt at removing the NA values resulted in a reduction of the RMSE value that was unexpected, and merited further investigation.
The slight reduction in RMSE is an unexpected surprise. The motivation for pruning out the lowest number of ratings per movie and per user was to eliminate `NA` values from the predictions, since that arose from either movies or users with a low count winding up in the training set but not the test set. It was assumed that if every movie has been rated more than three times, and if every user had rated at least nineteen movies, then the possibility of either not being represented in the test set was much lower. It was realized after the fact that this can be controlled during the partition creation by using `semi_join`, as was done with the original data set. Nonetheless, this attempt at removing the `NA` values results in an unexpected reduction of the RMSE value, and merits further investigation.
Rather than hard-code arbitrary values, a statistical approach based on ignoring the lowest 10th percentile of both movies and users (in terms of numbers of ratings associated with each unique value) was developed.
Rather than hard-code arbitrary values, a statistical approach based on ignoring the lowest 10th percentile of both movies and users (in terms of numbers of ratings associated with each unique value) can be used.
10% seemed like a good number to start with, although this could be refined more in the future, and is itself an arbitrary cutoff point not based on any deeper investigation.
10% seems like a good number to start with, although this should be refined more in the future, and is itself an arbitrary cutoff point not based on any deeper investigation or theory.
## Results
@ -531,7 +520,7 @@ sep=" ")
### Performance
We can measure the performance of this final algorithm against the validation set (for the first time):
We can measure the performance of this final algorithm against the `validation` set (for the first time):
```{r validation set, echo=FALSE}
# Use this optimized lambda value to test against the validation set.
@ -570,9 +559,9 @@ sep=" ")
### Summary
We began a linear estimate accounting for both movie and user effect as well as a penalized term for both effects. This got us close to our goal, and further exploration was warranted. The original mistake of not using semi_join when generating the training and test sets lead to an investigation of low-value outliers.
We began with a linear estimates accounting for both movie and user effect as well as a penalized term for both effects. This got us close to our goal, and further exploration was warranted. The original mistake of not using `semi_join` when generating the training and test sets lead to an investigation of low-value outliers.
These add noise without contributing to predictability. Our model is based on the mean rating for each movie across userId, which is NOT resistant to outliers. Hence, the users and movies with low frequency add noise to the mean without adding much weight to the predictions. an attempt to remove these (to eliminate NAs in the final predictions) inadvertently lead to a reduction in RMSE, which is what makes this method powerful.
Our model is based on the mean rating for each movie across user ID, which is NOT resistant to outliers. Hence, the users and movies with low frequency add noise to the mean without adding much weight to the predictions. an attempt to remove these (to eliminate `NAs` in the final predictions) inadvertently lead to a reduction in RMSE, which is what makes this method powerful.
### Limitations
@ -580,8 +569,8 @@ This seems to depend on large data sets. Eliminating below a certain percentile
### Future Work
Optimizing the percentile removed from each predictor (movieId and userId) would be interesting, as it is unlikely that the 10th percentile is always the ideal cutoff. It would be nice to keep as much of the original data set as possible while still training a good solid model.
Optimizing the percentile removed from each predictor (`movieId` and `userId`) would be interesting, as it is unlikely that the 10th percentile is always the ideal cutoff. It would be nice to keep as much of the original data set as possible while still training a good solid model.
Many predictors were not considered, such as timestamps or genres. No factor analysis was done of any kind. These could be included in the model to decrease the overall error if desired.
Many predictors were not considered, such as timestamps or genres. No factor importance or influence analysis was done of any kind. These could be included in the model to decrease the overall error further if needed.

Binary file not shown.