main.R 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # (c) Deben Oldert
  2. # Checks the difference between multiple movie rating from different websites
  3. # Load libraries
  4. suppressMessages(library(dplyr))
  5. suppressMessages(library(stringr))
  6. # Set global vars
  7. wd <- getwd()
  8. # Set common functions
  9. returnPath <- function(pth) {
  10. return(paste(wd, pth, sep = "/"))
  11. }
  12. # 1. IMDB
  13. # 2. GroupLens
  14. # 3. Netflix
  15. set_names <- c("IMDB", "GroupLens", "Netflix")
  16. # Loading .CSV's
  17. print("Prepairing IMDB...")
  18. imdb <- read.csv(returnPath("datasets/imdb/imdb.csv"), row.names=1)
  19. imdb <- select(tbl_df(imdb), title, rating, year)
  20. imdb <- mutate(imdb, title = paste(title, " (", year, ")", sep = ""))
  21. print("DONE")
  22. print("Prepairing GroupLens...")
  23. groupLens_movie <- read.csv(returnPath("datasets/groupLens/movies_frmt.csv"))
  24. groupLens_movie <- select(tbl_df(groupLens_movie), movieId, title, year)
  25. # Extract the year
  26. #groupLens_movie <- mutate(groupLens_movie, year = as.integer(str_match(title, "([0-9]{4})")[,1]))
  27. groupLens_rating <- read.csv(returnPath("datasets/groupLens/ratings.csv"))
  28. groupLens_rating <- tbl_df(groupLens_rating)
  29. groupLens_rating <- group_by(groupLens_rating, movieId)
  30. groupLens_rating <- summarise(groupLens_rating, rating = mean(rating, na.rm = TRUE))
  31. groupLens <- merge(groupLens_movie, groupLens_rating, by = intersect(names(groupLens_movie), names(groupLens_rating)), all = TRUE)
  32. # Cleanup
  33. remove(groupLens_rating)
  34. remove(groupLens_movie)
  35. print("DONE")
  36. print("Prepairing Netflix...")
  37. # Gonna be a clusterfuck w/ 2GB data (±100 million reviews)
  38. netflix_movie <- read.csv(returnPath("datasets/netflix/movie_titles.csv"))
  39. netflix_movie <- select(tbl_df(netflix_movie), movieId, title, year)
  40. netflix_movie <- mutate(netflix_movie, title_frmt = paste(title, " (", year, ")", sep = ""))
  41. # Create new and empty dataframe for final results
  42. netflix <- data.frame(title_frmt = character(0), year = integer(0), rating = numeric(0))
  43. # We need to loop through every movieId to find its .csv file
  44. # Then we calculate the average rating for the movie and store it in a new data.frame
  45. for (i in 1:nrow(netflix_movie)) {
  46. row <- netflix_movie[i,]
  47. print(i)
  48. # Format the file name
  49. # E.G:
  50. # mv_0000001.csv
  51. # mv_0027640.csv
  52. csv_ <- "mv_"
  53. for(j in nchar(row$movieId):6){
  54. csv_ <- paste(csv_, "0", sep = "")
  55. }
  56. csv_ <- paste(csv_, row$movieId, ".csv", sep = "")
  57. # Prepend the filesystem location (working directory)
  58. csv_ <- paste(wd, "datasets/netflix", csv_, sep = "/")
  59. netflix_rating <- read.csv(csv_)
  60. if(is.null(netflix_rating)) { # If csv is empty => skip it
  61. print(paste("Empty:", csv_))
  62. next
  63. }
  64. netflix_rating <- tbl_df(netflix_rating)
  65. netflix_rating <- summarise(netflix_rating, ratings = mean(rating, na.rm = TRUE))
  66. # Append result to the netflix table
  67. netflix <- bind_rows(netflix, data.frame(title_frmt = as.character(row$title_frmt), year = as.integer(as.character(row$year)), rating = netflix_rating$ratings))
  68. # Cleanup
  69. remove(csv_)
  70. remove(netflix_rating)
  71. remove(j)
  72. }
  73. # Cleanup
  74. remove(netflix_movie)
  75. remove(row)
  76. remove(i)
  77. remove(wd)
  78. print("DONE")
  79. print("Done Loading")
  80. print("Working on question no. 1...")
  81. # Define ranges
  82. y = 10
  83. x_min <- min(min(imdb$year, na.rm = TRUE), min(groupLens$year, na.rm = TRUE), min(netflix$year, na.rm = TRUE))
  84. x_max <- max(max(imdb$year, na.rm = TRUE), max(groupLens$year, na.rm = TRUE), max(netflix$year, na.rm = TRUE))
  85. # Define colors
  86. color <- c("blue", "red", "green")
  87. imdb_year_avg <- imdb %>%
  88. group_by(year) %>%
  89. summarise(rating = mean(rating, na.rm = TRUE))
  90. groupLens_year_avg <- groupLens %>%
  91. group_by(year) %>%
  92. summarise(rating = mean(rating, na.rm = TRUE) * 2)
  93. netflix_year_avg <- netflix %>%
  94. group_by(year) %>%
  95. summarise(rating = mean(rating, na.rm = TRUE) * 2)
  96. plot(imdb_year_avg,
  97. type = "l",
  98. ylim = c(0, 10),
  99. col = color[1],
  100. axes = F,
  101. ann = T,
  102. xlab = "Years",
  103. ylab = "Avg. rating",
  104. cex.lab=0.8, lwd=2)
  105. #text(axTicks(1), par("usr")[3] - 2, srt=45, adj = 1, labels = x_min:x_max, xpd=T, cex=0.8)
  106. box()
  107. lines(groupLens_year_avg, type = "l", lty = 2, lwd = 2, col = color[2])
  108. lines(netflix_year_avg, type = "l", lty = 3, lwd = 2, col = color[3])
  109. legend("topleft", set_names, cex = 0.8, col = color, lty=1:3, lwd = 2, bty="n")