Code Base: Personalized Recommender

Author

Andre Thomson

Question and method

I used user-to-user collaborative filtering to predict movie ratings from participants with similar rating patterns. Pearson correlation measures similarity. I tested the system by hiding one rating at a time and predicting it from the other 35 ratings.

I also recalculated the Week 3 Global Baseline Estimate for every same training split. This gives both methods the same 36 test cases and prevents the held-out value from entering either calculation.

Load and verify the data

Show the code
library(tidyverse)
library(knitr)

ratings <- read_csv(
  "movie_ratings_query.csv",
  show_col_types = FALSE
)

required_columns <- c("participant_id", "movie_title", "rating")

stopifnot(
  identical(names(ratings), required_columns),
  nrow(ratings) == 36,
  n_distinct(ratings$participant_id) == 6,
  n_distinct(ratings$movie_title) == 6,
  !anyNA(ratings),
  all(ratings$rating %in% 1:5),
  !anyDuplicated(ratings[c("participant_id", "movie_title")])
)

data_check <- ratings |>
  summarise(
    participants = n_distinct(participant_id),
    movies = n_distinct(movie_title),
    ratings = n(),
    missing_values = sum(is.na(rating)),
    minimum_rating = min(rating),
    maximum_rating = max(rating)
  )

kable(data_check)
participants movies ratings missing_values minimum_rating maximum_rating
6 6 36 0 1 5

The file has 36 ratings from 6 participants for 6 movies. There are no missing values or duplicate participant-movie pairs.

Personalized prediction function

The function first calculates the target participant’s mean from the training data. It then compares the target participant with each other participant who rated the held-out movie. Similarity is based only on movies that both participants rated in the training data.

Show the code
predict_personalized <- function(train, target_user, target_movie) {
  target_ratings <- train |>
    filter(participant_id == target_user)

  target_mean <- mean(target_ratings$rating)

  candidate_ratings <- train |>
    filter(
      movie_title == target_movie,
      participant_id != target_user
    )

  similarities <- map_dfr(
    candidate_ratings$participant_id,
    function(other_user) {
      other_ratings <- train |>
        filter(participant_id == other_user)

      shared <- inner_join(
        target_ratings,
        other_ratings,
        by = "movie_title",
        suffix = c("_target", "_neighbor")
      )

      similarity <- NA_real_

      if (
        nrow(shared) >= 3 &&
        sd(shared$rating_target) > 0 &&
        sd(shared$rating_neighbor) > 0
      ) {
        similarity <- cor(
          shared$rating_target,
          shared$rating_neighbor,
          method = "pearson"
        )
      }

      tibble(
        participant_id = other_user,
        similarity = similarity,
        neighbor_mean = mean(other_ratings$rating)
      )
    }
  )

  usable_neighbors <- candidate_ratings |>
    inner_join(similarities, by = "participant_id") |>
    filter(!is.na(similarity), similarity > 0)

  if (nrow(usable_neighbors) > 0) {
    estimate <- target_mean +
      sum(
        usable_neighbors$similarity *
          (usable_neighbors$rating - usable_neighbors$neighbor_mean)
      ) / sum(usable_neighbors$similarity)

    method <- "Pearson collaborative filtering"
  } else {
    estimate <- target_mean
    method <- "User mean fallback"
  }

  tibble(
    personalized_prediction = pmin(5, pmax(1, estimate)),
    prediction_method = method,
    neighbors_used = nrow(usable_neighbors)
  )
}

Fair Week 3 baseline function

The earlier baseline used the global mean plus participant bias plus movie bias. I use the same formula here, recalculated from each 35-row training set.

Show the code
predict_global_baseline <- function(train, target_user, target_movie) {
  global_mean <- mean(train$rating)

  user_mean <- train |>
    filter(participant_id == target_user) |>
    summarise(value = mean(rating)) |>
    pull(value)

  movie_mean <- train |>
    filter(movie_title == target_movie) |>
    summarise(value = mean(rating)) |>
    pull(value)

  estimate <- global_mean +
    (user_mean - global_mean) +
    (movie_mean - global_mean)

  pmin(5, pmax(1, estimate))
}

Leave-one-out evaluation

Each loop removes exactly one row. Both models receive the same remaining rows and predict the same held-out rating.

Show the code
predictions <- map_dfr(
  seq_len(nrow(ratings)),
  function(i) {
    held_out <- ratings[i, ]
    training <- ratings[-i, ]

    personalized <- predict_personalized(
      training,
      held_out$participant_id,
      held_out$movie_title
    )

    baseline <- predict_global_baseline(
      training,
      held_out$participant_id,
      held_out$movie_title
    )

    bind_cols(held_out, personalized) |>
      mutate(
        baseline_prediction = baseline,
        personalized_error = rating - personalized_prediction,
        personalized_absolute_error = abs(personalized_error),
        personalized_squared_error = personalized_error^2,
        baseline_error = rating - baseline_prediction,
        baseline_absolute_error = abs(baseline_error),
        baseline_squared_error = baseline_error^2
      )
  }
)

stopifnot(
  nrow(predictions) == 36,
  all(between(predictions$personalized_prediction, 1, 5)),
  all(between(predictions$baseline_prediction, 1, 5)),
  !anyNA(predictions)
)

write_csv(
  predictions,
  "results/leave_one_out_predictions.csv"
)

kable(
  predictions |>
    select(
      participant_id,
      movie_title,
      rating,
      personalized_prediction,
      baseline_prediction,
      prediction_method,
      neighbors_used
    ),
  digits = 3
)
participant_id movie_title rating personalized_prediction baseline_prediction prediction_method neighbors_used
Participant 1 Puss in Boots: The Last Wish 5 5.000 5.000 Pearson collaborative filtering 3
Participant 1 The Wild Robot 5 5.000 5.000 Pearson collaborative filtering 3
Participant 1 Inside Out 2 5 3.298 2.800 Pearson collaborative filtering 3
Participant 1 Moana 2 5 4.152 3.600 Pearson collaborative filtering 3
Participant 1 Dog Man 3 2.800 3.943 Pearson collaborative filtering 1
Participant 1 The Super Mario Bros. Movie 4 5.000 5.000 Pearson collaborative filtering 3
Participant 2 Puss in Boots: The Last Wish 5 4.708 4.400 Pearson collaborative filtering 4
Participant 2 The Wild Robot 5 4.708 4.400 Pearson collaborative filtering 3
Participant 2 Inside Out 2 3 3.776 2.943 Pearson collaborative filtering 3
Participant 2 Moana 2 5 3.022 3.000 Pearson collaborative filtering 4
Participant 2 Dog Man 2 3.518 3.714 Pearson collaborative filtering 5
Participant 2 The Super Mario Bros. Movie 4 4.890 5.000 Pearson collaborative filtering 4
Participant 3 Puss in Boots: The Last Wish 3 3.347 3.943 Pearson collaborative filtering 2
Participant 3 The Wild Robot 2 4.471 4.314 Pearson collaborative filtering 3
Participant 3 Inside Out 2 1 1.712 2.486 Pearson collaborative filtering 2
Participant 3 Moana 2 2 2.236 2.914 Pearson collaborative filtering 2
Participant 3 Dog Man 5 1.383 1.400 Pearson collaborative filtering 4
Participant 3 The Super Mario Bros. Movie 5 3.933 3.600 Pearson collaborative filtering 1
Participant 4 Puss in Boots: The Last Wish 3 4.994 4.743 Pearson collaborative filtering 4
Participant 4 The Wild Robot 5 3.384 4.000 Pearson collaborative filtering 3
Participant 4 Inside Out 2 2 2.451 2.914 Pearson collaborative filtering 3
Participant 4 Moana 2 3 2.912 3.343 Pearson collaborative filtering 4
Participant 4 Dog Man 4 2.747 2.571 Pearson collaborative filtering 4
Participant 4 The Super Mario Bros. Movie 5 4.992 4.400 Pearson collaborative filtering 4
Participant 5 Puss in Boots: The Last Wish 5 2.785 3.000 Pearson collaborative filtering 5
Participant 5 The Wild Robot 4 3.375 3.371 Pearson collaborative filtering 5
Participant 5 Inside Out 2 1 2.384 2.286 Pearson collaborative filtering 5
Participant 5 Moana 2 1 3.106 3.086 Pearson collaborative filtering 5
Participant 5 Dog Man 1 2.922 2.686 Pearson collaborative filtering 4
Participant 5 The Super Mario Bros. Movie 5 2.988 3.400 Pearson collaborative filtering 4
Participant 6 Puss in Boots: The Last Wish 5 4.528 4.200 Pearson collaborative filtering 4
Participant 6 The Wild Robot 5 4.508 4.200 Pearson collaborative filtering 5
Participant 6 Inside Out 2 3 2.947 2.743 Pearson collaborative filtering 4
Participant 6 Moana 2 3 3.736 3.543 Pearson collaborative filtering 4
Participant 6 Dog Man 2 3.829 3.514 Pearson collaborative filtering 4
Participant 6 The Super Mario Bros. Movie 5 4.377 4.600 Pearson collaborative filtering 4

Accuracy results

Show the code
n_cases <- nrow(predictions)

model_metrics <- tibble(
  model = c(
    "Personalized recommender",
    "Week 3 global baseline"
  ),
  MAE = c(
    mean(predictions$personalized_absolute_error),
    mean(predictions$baseline_absolute_error)
  ),
  RMSE = c(
    sqrt(mean(predictions$personalized_squared_error)),
    sqrt(mean(predictions$baseline_squared_error))
  ),
  predictions = n_cases
)

method_counts <- predictions |>
  count(prediction_method, name = "predictions")

write_csv(model_metrics, "results/model_metrics.csv")
write_csv(method_counts, "results/prediction_method_counts.csv")

kable(model_metrics, digits = 3)
model MAE RMSE predictions
Personalized recommender 1.051 1.353 36
Week 3 global baseline 1.161 1.383 36
Show the code
kable(method_counts)
prediction_method predictions
Pearson collaborative filtering 36

The personalized system had an MAE of 1.051 and an RMSE of 1.353. The same-split Global Baseline Estimate had an MAE of 1.161 and an RMSE of 1.383.

The Pearson calculation was available for 36 predictions. The user-mean fallback was used for 0 predictions. The model with the lower MAE was Personalized recommender.

Show the code
metric_plot_data <- model_metrics |>
  pivot_longer(
    cols = c(MAE, RMSE),
    names_to = "metric",
    values_to = "error"
  )

metric_plot <- ggplot(
  metric_plot_data,
  aes(x = metric, y = error, fill = model)
) +
  geom_col(
    position = position_dodge(width = 0.75),
    width = 0.65,
    color = "black"
  ) +
  geom_text(
    aes(label = sprintf("%.3f", error)),
    position = position_dodge(width = 0.75),
    vjust = -0.35,
    size = 4
  ) +
  scale_fill_manual(values = c("#0072B2", "#E69F00")) +
  coord_cartesian(ylim = c(0, max(metric_plot_data$error) * 1.18)) +
  labs(
    title = "Leave-One-Out Error on the Same 36 Ratings",
    x = "Error metric",
    y = "Rating points",
    fill = "Model"
  ) +
  theme_minimal(base_size = 13) +
  theme(legend.position = "bottom")

ggsave(
  "results/model_error_comparison.png",
  metric_plot,
  width = 8,
  height = 5,
  dpi = 300
)

metric_plot

Lower MAE and RMSE indicate predictions closer to the actual ratings. Values are printed above each bar.

Actual and predicted ratings

Show the code
actual_plot_data <- predictions |>
  select(
    actual_rating = rating,
    `Personalized recommender` = personalized_prediction,
    `Week 3 global baseline` = baseline_prediction
  ) |>
  pivot_longer(
    cols = -actual_rating,
    names_to = "model",
    values_to = "predicted_rating"
  )

actual_plot <- ggplot(
  actual_plot_data,
  aes(
    x = actual_rating,
    y = predicted_rating,
    color = model,
    shape = model
  )
) +
  geom_jitter(width = 0.08, height = 0.08, alpha = 0.72, size = 2.7) +
  geom_abline(
    slope = 1,
    intercept = 0,
    linetype = "dashed",
    color = "black"
  ) +
  scale_color_manual(values = c("#0072B2", "#E69F00")) +
  scale_shape_manual(values = c(16, 17)) +
  coord_equal(xlim = c(1, 5), ylim = c(1, 5)) +
  scale_x_continuous(breaks = 1:5) +
  scale_y_continuous(breaks = 1:5) +
  labs(
    title = "Actual and Predicted Movie Ratings",
    x = "Actual rating",
    y = "Predicted rating",
    color = "Model",
    shape = "Model"
  ) +
  theme_minimal(base_size = 13) +
  theme(legend.position = "bottom")

ggsave(
  "results/actual_vs_predicted.png",
  actual_plot,
  width = 8,
  height = 6,
  dpi = 300
)

actual_plot

The dashed line represents a perfect prediction. Shape and color both identify the model.

Simulated top-three output

Because every person already rated every movie, these rankings show the three highest leave-one-out predictions for each participant. They demonstrate the requested recommendation output, but they are not new viewing suggestions.

Show the code
recommendations <- predictions |>
  group_by(participant_id) |>
  arrange(
    desc(personalized_prediction),
    movie_title,
    .by_group = TRUE
  ) |>
  slice_head(n = 3) |>
  mutate(rank = row_number()) |>
  ungroup() |>
  select(
    participant_id,
    rank,
    movie_title,
    personalized_prediction,
    prediction_method
  )

write_csv(
  recommendations,
  "results/simulated_top3_recommendations.csv"
)

kable(recommendations, digits = 3)
participant_id rank movie_title personalized_prediction prediction_method
Participant 1 1 Puss in Boots: The Last Wish 5.000 Pearson collaborative filtering
Participant 1 2 The Super Mario Bros. Movie 5.000 Pearson collaborative filtering
Participant 1 3 The Wild Robot 5.000 Pearson collaborative filtering
Participant 2 1 The Super Mario Bros. Movie 4.890 Pearson collaborative filtering
Participant 2 2 Puss in Boots: The Last Wish 4.708 Pearson collaborative filtering
Participant 2 3 The Wild Robot 4.708 Pearson collaborative filtering
Participant 3 1 The Wild Robot 4.471 Pearson collaborative filtering
Participant 3 2 The Super Mario Bros. Movie 3.933 Pearson collaborative filtering
Participant 3 3 Puss in Boots: The Last Wish 3.347 Pearson collaborative filtering
Participant 4 1 Puss in Boots: The Last Wish 4.994 Pearson collaborative filtering
Participant 4 2 The Super Mario Bros. Movie 4.992 Pearson collaborative filtering
Participant 4 3 The Wild Robot 3.384 Pearson collaborative filtering
Participant 5 1 The Wild Robot 3.375 Pearson collaborative filtering
Participant 5 2 Moana 2 3.106 Pearson collaborative filtering
Participant 5 3 The Super Mario Bros. Movie 2.988 Pearson collaborative filtering
Participant 6 1 Puss in Boots: The Last Wish 4.528 Pearson collaborative filtering
Participant 6 2 The Wild Robot 4.508 Pearson collaborative filtering
Participant 6 3 The Super Mario Bros. Movie 4.377 Pearson collaborative filtering

Conclusion

The comparison shows whether this small personalized model improves on the earlier baseline. The evaluation covers every rating, uses the same held-out cases, and reports fallback use instead of treating every prediction as Pearson-based.

The main limitation is the size and structure of the survey. There are only six participants, and everyone rated the same six movies. These are simulated predictions of known ratings, not recommendations for movies the participants have not seen. More participants, more movies, and a naturally sparse ratings table would provide a stronger test.

Tools and AI assistance

I used R and Quarto to run the calculations and create the report. AI assisted with code review, editing, and packaging.

Sources