-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
446 lines (343 loc) · 14.1 KB
/
script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
rm(list = ls())
library(tidyverse)
library(stringr)
library(caret)
load("C:/Users/DRambaccussing/OneDrive - University of Dundee/HARVARDX/DOORUJRAMBACCUSSING/data1.RData")
# Loading datasets (Using EDX instructions) ------------------------
rm(list=ls())
library(tidyverse)
library(caret)
library(data.table)
# MovieLens 10M dataset:
# https://grouplens.org/datasets/movielens/10m/
# http://files.grouplens.org/datasets/movielens/ml-10m.zip
dl <- tempfile()
download.file("https://files.grouplens.org/datasets/movielens/ml-10m.zip", dl)
ratings <- fread(text = gsub("::", "\t", readLines(unzip(dl, "ml-10M100K/ratings.dat"))),
col.names = c("userId", "movieId", "rating", "timestamp"))
#If Edx code does not work, use the following:
#ratings <- read.table(text = gsub("::", "\t", readLines(unzip(dl, "ml-10M100K/ratings.dat"))),
# col.names = c("userId", "movieId", "rating", "timestamp"))
movies <- str_split_fixed(readLines(unzip(dl, "ml-10M100K/movies.dat")), "\\::", 3)
colnames(movies) <- c("movieId", "title", "genres")
# if using R 4.0 or later:
movies <- as.data.frame(movies) %>% mutate(movieId = as.numeric(movieId),
title = as.character(title),
genres = as.character(genres))
movielens <- left_join(ratings, movies, by = "movieId")
# Validation set will be 10% of MovieLens data
set.seed(1, sample.kind="Rounding") # if using R 3.5 or earlier, use `set.seed(1)`
test_index <- createDataPartition(y = movielens$rating, times = 1, p = 0.1, list = FALSE)
edx <- movielens[-test_index,]
temp <- movielens[test_index,]
# Make sure userId and movieId in validation set are also in edx set
validation <- temp %>%
semi_join(edx, by = "movieId") %>%
semi_join(edx, by = "userId")
# Add rows removed from validation set back into edx set
removed <- anti_join(temp, validation)
edx <- rbind(edx, removed)
rm(dl, ratings, movies, test_index, temp, movielens, removed)
# We save the data locally to improve speed
save.image("~/data1.RData")
##Movie release date
Releaseyear =as.numeric(str_sub(edx$title, start =-5, end =-2))
edx = edx %>% mutate(Release_year = Releaseyear)
head(edx)
Releaseyear =as.numeric(str_sub(validation$title, start =-5, end =-2))
validation = validation %>% mutate(Release_year = Releaseyear)
edx = edx %>% mutate(char_length = nchar(title))
validation = validation %>% mutate(char_length = nchar(title))
edx = edx %>% mutate(MovieAge = 2022 - Release_year)
validation = validation %>% mutate(MovieAge = 2022 - Release_year)
## We can also learn about the movie release date:
Releaseyear =as.numeric(str_sub(edx$title, start =-5, end =-2))
edx = edx %>% mutate(Release_year = Releaseyear)
edx
head(edx)
# Exploring the data ------------------------------------------------------
load("~/data1.RData")
# identify class and change to tibble
class(edx)
edx %>% as_tibble()
# Check for missing observations
which(is.na(edx))
# Check how the data looks like
head(edx)
glimpse(edx)
#Number of Users
length(unique(edx$userId))
#Number of movies
length(unique(edx$movieId))
# Table
table(edx$rating)
# Data Wrangling ----------------------------------------------------------
# create the year of the rating and release date
edx <- edx %>% mutate(timestamp = as.POSIXct(timestamp, origin = "1970-01-01",
tz = "UTC"))
edx$timestamp <- format(edx$timestamp, "%Y")
names(edx)[names(edx) == "timestamp"] <- "Year"
head(edx)
validation <- validation %>% mutate(timestamp = as.POSIXct(timestamp, origin = "1970-01-01",
tz = "UTC"))
validation$timestamp <- format(validation$timestamp, "%Y")
names(edx)[names(validation) == "timestamp"] <- "Year"
head(validation)
## Release year
Releaseyear =as.numeric(str_sub(edx$title, start =-5, end =-2))
edx = edx %>% mutate(Release_year = Releaseyear)
head(edx)
Releaseyear =as.numeric(str_sub(validation$title, start =-5, end =-2))
validation = validation %>% mutate(Release_year = Releaseyear)
edx = edx %>% mutate(MovieAge = 2022 - Release_year)
validation = validation %>% mutate(MovieAge = 2022 - Release_year)
edx = edx %>% mutate(char_length = nchar(title))
validation = validation %>% mutate(char_length = nchar(title))
# Plots -------------------------------------------------------------------
## some plots
movie = edx %>%
group_by(movieId,title) %>%
summarise(n_ratings = n(),
avg_movie_rating = mean(rating)
)
## Scatterplot of number of times rated and average ratings
movie %>%
ggplot(aes(n_ratings,avg_movie_rating))+
geom_point(aes())+
geom_smooth()+
theme_bw()
## Correlation between numebr of times rated and rating scores.
cor(movie$n_ratings,movie$avg_movie_rating)
## Scatterplot of title length and rating
movie %>%
ggplot(aes(nchar(title),avg_movie_rating))+
geom_point(aes())+
geom_smooth()+
theme_bw()
## correlation
cor(nchar(movie_add$title), movie_add$avg_movie_rating)
## Number of words in title
movie$nwords <- str_count(movie$title, "\\w+")
cor(movie$nwords,movie$avg_movie_rating)
## Usually the number of words/characters is positively albeit weakly correlated
# Plots -------------------------------------------------------------------
## Create a new dataframe with a unique genre category representing a row.
genres_df <- edx %>%
separate_rows(genres, sep = "\\|") %>%
group_by(genres) %>%
summarise(number = n(), averageratings = mean(rating))
Genres = genres_df %>%
filter(number > 100000) %>%
mutate(genres = fct_reorder(genres, averageratings)) %>%
ggplot(aes(x = averageratings, y = genres)) +
geom_col(aes(fill=genres), alpha = 0.9) +
labs(y = "Genre",
x = "Average Rating") +
theme_bw() +
scale_x_continuous(limits = c(0, 5),
breaks = 0:5,
labels = 0:5) +
theme(panel.grid.major.x = element_line(linetype = "dashed", color = "blue"))
####
density = edx %>%
group_by(movieId) %>%
summarise(ratings = mean(rating))
mean_rat = mean(density$ratings)
densit= ggplot(density, aes(x=ratings))+
geom_density(fill="dodgerblue", alpha=0.5)+
labs(x="Movie Ratings")+
geom_vline(xintercept=mean_rat, size=1.5, color="red")+
geom_text(aes(x=3.19, label=paste0("Mean\n",3.19), y=1.9))+
theme_bw()
timeseries = edx %>%
group_by(Release_year) %>%
summarise(ratings = mean(rating), count=n())
ggplot(data=timeseries, aes(x=Release_year, y=ratings, group=1)) +
geom_line(linetype = "solid")+
theme(axis.text.x = element_text(angle = 90, hjust = 0.2))+
theme_bw()
###
##partitioning the dataset into test and training samples.
test_index = createDataPartition(y = edx$rating, times= 1, p = 0.2,
list = F
)
test_set <- edx %>% slice(test_index)
train_set <- edx %>% slice(-test_index)
## free memory
rm(density, densit, genres_df, Genres, movie, test_index, timeseries)
#### solving Parameters of alpha (mean), bi(movie) and bu (user),
### bt (release year), bg(genres) and bc(characters in titles)
alpha <- mean(train_set$rating)
bi <- train_set %>%
group_by(movieId) %>%
summarize(b_i = mean(rating - alpha))
bu <- train_set %>%
left_join(bi, by = "movieId") %>%
group_by(userId) %>%
summarize(b_u = mean(rating - alpha - b_i))
bg <- train_set %>%
left_join(bu, by = "userId") %>%
left_join(bi, by = "movieId") %>%
group_by(genres) %>%
summarize(b_g = mean(rating - alpha - b_i - b_u))
bt <- train_set %>%
left_join(bu, by = "userId") %>%
left_join(bi, by = "movieId") %>%
left_join(bg, by = "genres") %>%
group_by(Release_year) %>%
summarize(b_t = mean(rating - alpha - b_i - b_u -b_g))
bc <- train_set %>%
left_join(bu, by = "userId") %>%
left_join(bi, by = "movieId") %>%
left_join(bg, by = "genres") %>%
left_join(bt, by = "Release_year") %>%
group_by(char_length) %>%
summarize(b_c = mean(rating - alpha - b_i - b_u -b_g -b_t))
## Predictions
n_rmse <- RMSE(test_set$rating, alpha)
n_rmse
rmse_results <- data_frame(method = "Average movie rating", RMSE = n_rmse)
pred_bi <- alpha + test_set %>%
left_join(bi, by = "movieId") %>%
pull(b_i)
model1_rmse <- RMSE(pred = pred_bi,
obs = test_set$rating,
na.rm = TRUE)
rmse_results <- bind_rows(rmse_results,
data_frame(method="Movie effect ",
RMSE = model1_rmse ))
pred_bu <- test_set %>%
left_join(bi, by = "movieId") %>%
left_join(bu, by = "userId") %>%
mutate(pred_bu = alpha + b_i + b_u) %>%
pull(pred_bu)
model2_rmse <- RMSE(pred = pred_bu,
obs = test_set$rating,
na.rm = TRUE)
rmse_results <- bind_rows(rmse_results,
data_frame(method="Movie and User effect",
RMSE = model2_rmse))
# Free some space
rm(pred_bi, pred_bu)
pred_bg <- test_set %>%
left_join(bi, by = "movieId") %>%
left_join(bu, by = "userId") %>%
left_join(bg, by = "genres") %>%
mutate(pred_bg = alpha + b_i + b_u + b_g) %>%
pull(pred_bg)
model3_rmse <- RMSE(pred = pred_bg,
obs = test_set$rating,
na.rm = TRUE)
rmse_results <- bind_rows(rmse_results,
data_frame(method="Movie, User and Genre effect",
RMSE = model3_rmse))
pred_bt <- test_set %>%
left_join(bu, by = "userId") %>%
left_join(bi, by = "movieId") %>%
left_join(bg, by = "genres") %>%
left_join(bt, by = "Release_year") %>%
mutate(pred_bt = alpha + b_i + b_u + b_g+ b_t) %>%
pull(pred_bt)
model4_rmse <- RMSE(pred = pred_bt,
obs = test_set$rating,
na.rm = TRUE)
rmse_results <- bind_rows(rmse_results,
data_frame(method="Movie, User, Genre and Release date effect",
RMSE = model4_rmse))
pred_bc <- test_set %>%
left_join(bu, by = "userId") %>%
left_join(bi, by = "movieId") %>%
left_join(bg, by = "genres") %>%
left_join(bt, by = "Release_year") %>%
left_join(bc, by = "char_length") %>%
mutate(pred_bc = alpha + b_i + b_u + b_g+ b_t +b_c) %>%
pull(pred_bc)
model5_rmse <- RMSE(pred = pred_bc,
obs = test_set$rating,
na.rm = TRUE)
rmse_results <- bind_rows(rmse_results,
data_frame(method="Movie, User, Genre, Release date and Character length effect",
RMSE = model4_rmse))
rmse_results %>% knitr::kable()
######################### Validation Dataset
### Simple Mean
mu <- mean(edx$rating)
N_rmse <- RMSE(validation$rating, mu)
rmse_results1 <- data_frame(method = "Average movie rating model", RMSE = N_rmse)
### Movie ID
movie_avgs <- edx %>%
group_by(movieId) %>%
summarize(b_i = mean(rating - mu))
predicted_ratings <- mu + validation %>%
left_join(movie_avgs, by='movieId') %>%
pull(b_i)
model_1_rmse <- RMSE(predicted_ratings, validation$rating)
rmse_results1 <- bind_rows(rmse_results1,
data_frame(method="Movie effect model",
RMSE = model_1_rmse ))
#### User average
user_avgs <- edx %>%
left_join(movie_avgs, by='movieId') %>%
group_by(userId) %>%
summarize(b_u = mean(rating - mu - b_i))
predicted_ratings <- validation%>%
left_join(movie_avgs, by='movieId') %>%
left_join(user_avgs, by='userId') %>%
mutate(pred = mu + b_i + b_u) %>%
pull(pred)
model_2_rmse <- RMSE(predicted_ratings, validation$rating)
rmse_results1 <- bind_rows(rmse_results1,
data_frame(method="Movie and user effect model", RMSE = model_2_rmse))
##########Genre Effect##########
genre_avgs <- edx %>%
left_join(movie_avgs, by='movieId') %>%
left_join(user_avgs, by='userId') %>%
group_by(genres) %>%
summarize(b_g = mean(rating - mu - b_i - b_u))
predicted_ratings <- validation%>%
left_join(movie_avgs, by='movieId') %>%
left_join(user_avgs, by='userId') %>%
left_join(genre_avgs, by='genres') %>%
mutate(pred = mu + b_i + b_u + b_g) %>%
pull(pred)
model_3_rmse <- RMSE(predicted_ratings, validation$rating)
rmse_results1 <- bind_rows(rmse_results1,
data_frame(method="Movie, user and Genre effect model", RMSE = model_3_rmse))
rmse_results1 %>% knitr::kable()
##########################Regularisation###
# lambda is a tuning parameter
# Use cross-validation to choose it.
lambdas <- seq(0, 10, 0.5)
# For each lambda,find b_i & b_u, followed by rating prediction & testing
# note:the below code could take some time
rmses <- sapply(lambdas, function(l){
mu <- mean(edx$rating)
b_i <- edx %>%
group_by(movieId) %>%
summarize(b_i = sum(rating - mu)/(n()+l))
b_u <- edx %>%
left_join(b_i, by="movieId") %>%
group_by(userId) %>%
summarize(b_u = sum(rating - b_i - mu)/(n()+l))
b_g <- edx %>%
left_join(b_i, by="movieId") %>%
left_join(b_u, by="userId") %>%
group_by(genres) %>%
summarize(b_g = sum(rating - b_i - b_u - mu)/(n()+l))
predicted_ratings <-
validation %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_g, by = "genres") %>%
mutate(pred = mu + b_i + b_u + b_g) %>%
pull(pred)
return(RMSE(predicted_ratings, validation$rating))
})
rmse_results1 <- bind_rows(rmse_results1,
data_frame(method="Regularized movie,user and genre effect",
RMSE = min(rmses)))
rmse_results1 %>% knitr::kable()
# Plot rmses vs lambdas to select the optimal lambda
qplot(lambdas, rmses)
#Determine which lambda minimizes the RMSE
lambda <- lambdas[which.min(rmses)]