-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.R
488 lines (384 loc) · 15.7 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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# =============================================================================.
# Capstone Project I: MovieLens
# Applying Machine Learning Algorithms to predict movie ratings
# =============================================================================.
# Clean environment
rm(list=ls()); invisible(gc())
start_time <- Sys.time()
# Load required packages
if(!require(tidyverse)) install.packages("tidyverse", repos = "http://cran.us.r-project.org")
if(!require(caret)) install.packages("caret", repos = "http://cran.us.r-project.org")
if(!require(data.table)) install.packages("data.table", repos = "http://cran.us.r-project.org")
if(!require(lubridate)) install.packages("lubridate", repos = "http://cran.us.r-project.org")
if(!require(recosystem)) install.packages("recosystem", repos = "http://cran.us.r-project.org")
library(tidyverse)
library(caret)
library(data.table)
library(lubridate)
library(recosystem)
# Define functions used in script
RMSE <- function(true_ratings, predicted_ratings){
sqrt(mean((true_ratings - predicted_ratings)^2, na.rm=T))
}
round_any = function(x, accuracy, f=round){f(x/ accuracy) * accuracy}
# Define objects used in script
rmse_results <- data.frame(model = character(), rmse = numeric())
# =============================================================================.
# 1. Create edx set and validation set (as provied by course staff) ----
# =============================================================================.
# -----------------------------------------------------------------------------.
# 1.1 download file ----
# -----------------------------------------------------------------------------.
cat("\nDownload data...")
dl <- tempfile()
download.file("http://files.grouplens.org/datasets/movielens/ml-10m.zip", dl)
# -----------------------------------------------------------------------------.
# 1.2 wrangle downloaded data ----
# -----------------------------------------------------------------------------.
cat("\nWrangle data...")
# get rating data
ratings <- fread(text = gsub("::", "\t", readLines(unzip(dl, "ml-10M100K/ratings.dat"))),
col.names = c("userId", "movieId", "rating", "timestamp"))
# get movie data
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))
# if using R 3.6 or earlier:
# movies <- as.data.frame(movies) %>% mutate(movieId = as.numeric(levels(movieId))[movieId],
# title = as.character(title),
# genres = as.character(genres))
# merge rating and movie data
movielens <- left_join(ratings, movies, by = "movieId")
# -----------------------------------------------------------------------------.
# 1.3 create edx set and validation set ----
# -----------------------------------------------------------------------------.
# if using R 3.6 or later:
set.seed(1, sample.kind = "Rounding")
# if using R 3.5 or earlier:
# set.seed(1)
# create validation set (10% of MovieLens data)
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)
# clean up
rm(dl, ratings, movies, test_index, temp, movielens, removed)
# =============================================================================.
# 2. Develop the algorithm ----
# =============================================================================.
# -----------------------------------------------------------------------------.
# 2.1 prepare data ----
# -----------------------------------------------------------------------------.
# 2.1.1 further data manipulation ----
# add column for decade
edx <- edx %>%
mutate(year_of_pub = str_extract(string = title, pattern = "\\((1|2)(9|0|1|2)\\d{2}\\)")) %>%
mutate(year_of_pub = as.numeric(str_extract(string = year_of_pub, pattern = "\\d{4}"))) %>%
mutate(decade = (year_of_pub-1)-(year_of_pub-1)%%10)
validation <- validation %>%
mutate(year_of_pub = str_extract(string = title, pattern = "\\((1|2)(9|0|1|2)\\d{2}\\)")) %>%
mutate(year_of_pub = as.numeric(str_extract(string = year_of_pub, pattern = "\\d{4}"))) %>%
mutate(decade = (year_of_pub-1)-(year_of_pub-1)%%10)
# add column for year when rating was made
edx <- edx %>%
mutate(year_of_rating = as_datetime(timestamp)) %>%
mutate(year_of_rating = format(year_of_rating, format = "%Y")) %>%
mutate(year_of_rating = round_any(as.numeric(year_of_rating), 5, f = ceiling))
validation <- validation %>%
mutate(year_of_rating = as_datetime(timestamp)) %>%
mutate(year_of_rating = format(year_of_rating, format = "%Y")) %>%
mutate(year_of_rating = round_any(as.numeric(year_of_rating), 5, f = ceiling))
# 2.1.2 build train and test set ----
test_index <- createDataPartition(y = edx$rating, times = 1, p = 0.1, list = FALSE)
train_set <- edx[-test_index,]
test_set <- edx[test_index,]
# -----------------------------------------------------------------------------.
# 2.2 develop the model ----
# -----------------------------------------------------------------------------.
cat("\nTrain linear models...")
# 2.2.1 benchmark model ----
# overall average, here estimate for rating
mu <- mean(train_set$rating)
# calculate rmse
rmse_benchmark <- RMSE(mu, test_set$rating)
# add rmse to df
rmse_results <- rbind(rmse_results,
data.frame(model = "benchmark model", rmse = rmse_benchmark))
# 2.2.2 movie effects ----
# movie specific average
b_i <- train_set %>%
group_by(movieId) %>%
summarize(b_i = mean(rating - mu))
# estimation
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
mutate(prediction = mu + b_i) %>%
pull(prediction)
# calculate rmse
rmse_bi <- RMSE(test_set$rating, predicted_ratings)
# add rmse to df
rmse_results <- rbind(rmse_results,
data.frame(model = "movie effect", rmse = rmse_bi))
# 2.2.3 movie and user effects ----
# movie+user specific average
b_u <- train_set %>%
left_join(b_i, by = "movieId") %>%
group_by(userId) %>%
summarize(b_u = mean(rating - mu - b_i))
# estimation
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
mutate(prediction = mu + b_i + b_u) %>%
pull(prediction)
# calculate rmse
rmse_bi_bu <- RMSE(test_set$rating, predicted_ratings)
# add rmse to df
rmse_results <- rbind(rmse_results,
data.frame(model = "movie+user effect", rmse = rmse_bi_bu))
# 2.2.4 movie, user and decade effect ----
# movie+user+decade specific average
b_d <- train_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
group_by(decade) %>%
summarize(b_d = mean(rating - mu - b_i - b_u))
# estimation
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_d, by = "decade") %>%
mutate(prediction = mu + b_i + b_u + b_d) %>%
pull(prediction)
# calculate rmse
rmse_bi_bu_bd <- RMSE(test_set$rating, predicted_ratings)
# add rmse to df
rmse_results <- rbind(rmse_results,
data.frame(model = "movie+user+decade effect", rmse = rmse_bi_bu_bd))
# 2.2.5 movie, user, decade and genre effect ----
# movie+user+decade+genre specific average
b_g <- train_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_d, by = "decade") %>%
group_by(genres) %>%
summarize(b_g = mean(rating - mu - b_i - b_u - b_d))
# estimation
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_d, by = "decade") %>%
left_join(b_g, by = "genres") %>%
mutate(prediction = mu + b_i + b_u + b_d + b_g) %>%
pull(prediction)
# calculate rmse
rmse_bi_bu_bd_bg <- RMSE(test_set$rating, predicted_ratings)
# add rmse to df
rmse_results <- rbind(rmse_results,
data.frame(model = "movie+user+decade+genre effect", rmse = rmse_bi_bu_bd_bg))
# rename some objects to later use in RMD
b_i_train <- b_i
b_u_train <- b_u
b_d_train <- b_d
b_g_train <- b_g
# -----------------------------------------------------------------------------.
# 2.3 build regularized models ----
# -----------------------------------------------------------------------------.
cat("\nTrain regularized models...")
lambdas <- seq(0, 10, 0.1)
# 2.3.1 regularized movie effects ----
# train and predict movie effects with all lambdas
rmse_bi_reg <- sapply(lambdas, function(lambda){
b_i <- train_set %>%
group_by(movieId) %>%
summarize(b_i = sum(rating - mu)/(n() + lambda))
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
mutate(prediction = mu + b_i) %>%
pull(prediction)
return(RMSE(test_set$rating, predicted_ratings))
})
qplot(lambdas, rmse_bi_reg)
# chose the lambda which minimizes the rmse
lambda_bi <- lambdas[which.min(rmse_bi_reg)]
# add rmse to results
rmse_results <- rbind(
rmse_results,
data.frame(
model = "regularized movie effect",
rmse = min(rmse_bi_reg))
)
# 2.3.2 regularized movie and user effects ----
rmse_bi_bu_reg <- sapply(lambdas, function(lambda){
b_i <- train_set %>%
group_by(movieId) %>%
summarize(b_i = sum(rating - mu)/(n() + lambda))
b_u <- train_set %>%
left_join(b_i, by = "movieId") %>%
group_by(userId) %>%
summarize(b_u = sum(rating - mu - b_i)/(n() + lambda))
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
mutate(prediction = mu + b_i + b_u) %>%
pull(prediction)
return(RMSE(test_set$rating, predicted_ratings))
})
qplot(lambdas, rmse_bi_bu_reg)
lambda_bi_bu <- lambdas[which.min(rmse_bi_bu_reg)]
rmse_results <- rbind(
rmse_results,
data.frame(
model = "regularized movie+user effect",
rmse = min(rmse_bi_bu_reg))
)
# 2.3.3 regularized movie, user and decade effect ----
rmse_bi_bu_bd_reg <- sapply(lambdas, function(lambda){
b_i <- train_set %>%
group_by(movieId) %>%
summarize(b_i = sum(rating - mu)/(n() + lambda))
b_u <- train_set %>%
left_join(b_i, by = "movieId") %>%
group_by(userId) %>%
summarize(b_u = sum(rating - mu - b_i)/(n() + lambda))
b_d <- train_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
group_by(decade) %>%
summarize(b_d = sum(rating - mu - b_i - b_u)/(n() + lambda))
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_d, by = "decade") %>%
mutate(prediction = mu + b_i + b_u + b_d) %>%
pull(prediction)
return(RMSE(test_set$rating, predicted_ratings))
})
qplot(lambdas, rmse_bi_bu_bd_reg)
lambda_bi_bu_bd <- lambdas[which.min(rmse_bi_bu_bd_reg)]
rmse_results <- rbind(
rmse_results,
data.frame(
model = "regularized movie+user+decade effect",
rmse = min(rmse_bi_bu_bd_reg))
)
# 2.3.4 regularized movie, user, decade and genre effect ----
rmse_bi_bu_bd_bg_reg <- sapply(lambdas, function(lambda){
b_i <- train_set %>%
group_by(movieId) %>%
summarize(b_i = sum(rating - mu)/(n() + lambda))
b_u <- train_set %>%
left_join(b_i, by = "movieId") %>%
group_by(userId) %>%
summarize(b_u = sum(rating - mu - b_i)/(n() + lambda))
b_d <- train_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
group_by(decade) %>%
summarize(b_d = sum(rating - mu - b_i - b_u)/(n() + lambda))
b_g <- train_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_d, by = "decade") %>%
group_by(genres) %>%
summarize(b_g = sum(rating - mu - b_i - b_u - b_d)/(n() + lambda))
predicted_ratings <- test_set %>%
left_join(b_i, by = "movieId") %>%
left_join(b_u, by = "userId") %>%
left_join(b_d, by = "decade") %>%
left_join(b_g, by = "genres") %>%
mutate(prediction = mu + b_i + b_u + b_d + b_g) %>%
pull(prediction)
return(RMSE(test_set$rating, predicted_ratings))
})
qplot(lambdas, rmse_bi_bu_bd_bg_reg)
lambda_bi_bu_bd_bg <- lambdas[which.min(rmse_bi_bu_bd_bg_reg)]
rmse_results <- rbind(
rmse_results,
data.frame(
model = "regularized movie+user+decade+genre effect",
rmse = min(rmse_bi_bu_bd_bg_reg))
)
# -----------------------------------------------------------------------------.
# 2.4 Matrix factorization ----
# -----------------------------------------------------------------------------.
cat("\nTrain matrix factorization model...")
# 2.4.1 Preprocessing ----
# construct a recommender system object
r <- Reco()
# specify data source for training as object of class DataSource
train_reco <- data_memory(
user_index = train_set$userId,
item_index = train_set$movieId,
rating = train_set$rating,
index1 = TRUE
)
# specify data source for testing as object of class DataSource
test_reco <- data_memory(
user_index = test_set$userId,
item_index = test_set$movieId,
rating = test_set$rating,
index1 = TRUE
)
# specify data source to validate as object of class DataSource
validation_reco <- data_memory(
user_index = validation$userId,
item_index = validation$movieId,
index1 = TRUE
)
# 2.4.2 Tune model parameters ----
tune_options <- r$tune(train_reco, opts = list(
dim = c(10, 20),
costp_l1 = c(0.01, 0.1),
costp_l2 = c(0.01, 0.1),
costq_l1 = c(0.01, 0.1),
costq_l2 = c(0.01, 0.1),
lrate = c(0.01, 0.1),
nthread = 4,
niter = 20,
verbose = TRUE)
)
# 2.4.3 Train the recommender model ----
r$train(train_reco, opts = c(tune_options$min, niter = 100, nthread = 4))
# 2.4.4 Make recommender model predictions ----
predicted_ratings <- r$predict(test_reco, out_memory())
rmse_mf <- RMSE(test_set$rating, predicted_ratings)
rmse_results <- rbind(rmse_results,
data.frame(model = "matrix factorization", rmse = rmse_mf))
# =============================================================================.
# 3. apply the developed model ----
# =============================================================================.
message("\nApply selected model on validation set...")
# -----------------------------------------------------------------------------.
# 3.1 use selected model with validation set ----
# -----------------------------------------------------------------------------.
validation$prediction <- r$predict(validation_reco, out_memory())
predicted_ratings <- validation$prediction
rmse_validation <- RMSE(validation$rating, predicted_ratings)
rmse_validation
# =============================================================================.
# 4. End of script ----
# =============================================================================.
# -----------------------------------------------------------------------------.
# 4.1 save relevant data ----
# -----------------------------------------------------------------------------.
cat("\nSave some data...")
save(list = c("edx", "rmse_results", "b_i", "b_i_train", "b_u", "b_u_train",
"b_d", "b_d_train", "b_g", "b_g_train", "tune_options",
"rmse_validation"),
file = "rmd-input.RData")
# =============================================================================.
# End of code
# =============================================================================.
message("\nScript successfully finished")
end_time <- Sys.time()
end_time - start_time