{ "cells": [ { "cell_type": "markdown", "id": "75fd9cc6", "metadata": { "id": "75fd9cc6" }, "source": [ "# **🤖 Benchmarking & Modeling**" ] }, { "cell_type": "markdown", "id": "fb807724", "metadata": { "id": "fb807724" }, "source": [ "## **1.** 📦 Setup" ] }, { "cell_type": "code", "execution_count": null, "id": "d40cd131", "metadata": { "id": "d40cd131" }, "outputs": [], "source": [ "\n", "# Uncomment the next line once:\n", "install.packages(c(\"readr\",\"dplyr\",\"stringr\",\"tidyr\",\"lubridate\",\"ggplot2\",\"forecast\",\"broom\",\"jsonlite\"), repos=\"https://cloud.r-project.org\")\n", "\n", "suppressPackageStartupMessages({\n", " library(readr)\n", " library(dplyr)\n", " library(stringr)\n", " library(tidyr)\n", " library(lubridate)\n", " library(ggplot2)\n", " library(forecast)\n", " library(broom)\n", " library(jsonlite)\n", "})" ] }, { "cell_type": "markdown", "id": "f01d02e7", "metadata": { "id": "f01d02e7" }, "source": [ "## **2.** ✅️ Load & inspect inputs" ] }, { "cell_type": "code", "execution_count": null, "id": "29e8f6ce", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "29e8f6ce", "outputId": "5a1bda1c-c58d-43d0-c85e-db5041c8bc49" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Loaded: 1000 rows (title-level), 18 rows (monthly)\n" ] } ], "source": [ "\n", "must_exist <- function(path, label) {\n", " if (!file.exists(path)) stop(paste0(\"Missing \", label, \": \", path))\n", "}\n", "\n", "TITLE_PATH <- \"synthetic_title_level_features.csv\"\n", "MONTH_PATH <- \"synthetic_monthly_revenue_series.csv\"\n", "\n", "must_exist(TITLE_PATH, \"TITLE_PATH\")\n", "must_exist(MONTH_PATH, \"MONTH_PATH\")\n", "\n", "df_title <- read_csv(TITLE_PATH, show_col_types = FALSE)\n", "df_month <- read_csv(MONTH_PATH, show_col_types = FALSE)\n", "\n", "cat(\"Loaded:\", nrow(df_title), \"rows (title-level),\", nrow(df_month), \"rows (monthly)\n", "\")" ] }, { "cell_type": "code", "execution_count": null, "id": "9fd04262", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9fd04262", "outputId": "5f031538-96be-4758-904d-9201ec3c3ea7" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\u001b[90m# A tibble: 1 × 6\u001b[39m\n", " n na_avg_revenue na_price na_rating na_share_pos na_share_neg\n", " \u001b[3m\u001b[90m\u001b[39m\u001b[23m \u001b[3m\u001b[90m\u001b[39m\u001b[23m \u001b[3m\u001b[90m\u001b[39m\u001b[23m \u001b[3m\u001b[90m\u001b[39m\u001b[23m \u001b[3m\u001b[90m\u001b[39m\u001b[23m \u001b[3m\u001b[90m\u001b[39m\u001b[23m\n", "\u001b[90m1\u001b[39m \u001b[4m1\u001b[24m000 0 0 \u001b[4m1\u001b[24m000 0 0\n", "Monthly rows after parsing: 18 \n" ] } ], "source": [ "\n", "# ---------- helpers ----------\n", "safe_num <- function(x) {\n", " # strips anything that is not digit or dot\n", " suppressWarnings(as.numeric(str_replace_all(as.character(x), \"[^0-9.]\", \"\")))\n", "}\n", "\n", "parse_rating <- function(x) {\n", " # Accept: 4, \"4\", \"4.0\", \"4/5\", \"4 out of 5\", \"⭐⭐⭐⭐\", etc.\n", " x <- as.character(x)\n", " x <- str_replace_all(x, \"⭐\", \"\")\n", " x <- str_to_lower(x)\n", " x <- str_replace_all(x, \"stars?\", \"\")\n", " x <- str_replace_all(x, \"out of\", \"/\")\n", " x <- str_replace_all(x, \"\\\\s+\", \"\")\n", " x <- str_replace_all(x, \"[^0-9./]\", \"\")\n", " suppressWarnings(as.numeric(str_extract(x, \"^[0-9.]+\")))\n", "}\n", "\n", "parse_month <- function(x) {\n", " x <- as.character(x)\n", " # try YYYY-MM-DD, then YYYY-MM\n", " out <- suppressWarnings(ymd(x))\n", " if (mean(is.na(out)) > 0.5) out <- suppressWarnings(ymd(paste0(x, \"-01\")))\n", " na_idx <- which(is.na(out))\n", " if (length(na_idx) > 0) out[na_idx] <- suppressWarnings(ymd(paste0(x[na_idx], \"-01\")))\n", " out\n", "}\n", "\n", "# ---------- normalize keys ----------\n", "df_title <- df_title %>% mutate(title = str_squish(as.character(title)))\n", "df_month <- df_month %>% mutate(month = as.character(month))\n", "\n", "# ---------- parse numeric columns defensively ----------\n", "need_cols_title <- c(\"title\",\"avg_revenue\",\"total_revenue\",\"price\",\"rating\",\"share_positive\",\"share_negative\",\"share_neutral\")\n", "missing_title <- setdiff(need_cols_title, names(df_title))\n", "if (length(missing_title) > 0) stop(paste0(\"df_title missing columns: \", paste(missing_title, collapse=\", \")))\n", "\n", "df_title <- df_title %>%\n", " mutate(\n", " avg_revenue = safe_num(avg_revenue),\n", " total_revenue = safe_num(total_revenue),\n", " price = safe_num(price),\n", " rating = parse_rating(rating),\n", " share_positive = safe_num(share_positive),\n", " share_negative = safe_num(share_negative),\n", " share_neutral = safe_num(share_neutral)\n", " )\n", "\n", "# basic sanity stats\n", "hyg <- df_title %>%\n", " summarise(\n", " n = n(),\n", " na_avg_revenue = sum(is.na(avg_revenue)),\n", " na_price = sum(is.na(price)),\n", " na_rating = sum(is.na(rating)),\n", " na_share_pos = sum(is.na(share_positive)),\n", " na_share_neg = sum(is.na(share_negative))\n", " )\n", "\n", "print(hyg)\n", "\n", "# monthly parsing\n", "need_cols_month <- c(\"month\",\"total_revenue\")\n", "missing_month <- setdiff(need_cols_month, names(df_month))\n", "if (length(missing_month) > 0) stop(paste0(\"df_month missing columns: \", paste(missing_month, collapse=\", \")))\n", "\n", "df_month2 <- df_month %>%\n", " mutate(\n", " month = parse_month(month),\n", " total_revenue = safe_num(total_revenue)\n", " ) %>%\n", " filter(!is.na(month)) %>%\n", " arrange(month)\n", "\n", "cat(\"Monthly rows after parsing:\", nrow(df_month2), \"\\n\")" ] }, { "cell_type": "markdown", "id": "b8971bc4", "metadata": { "id": "b8971bc4" }, "source": [ "## **3.** 💾 Folder for R outputs for Hugging Face" ] }, { "cell_type": "code", "execution_count": null, "id": "dfaa06b1", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dfaa06b1", "outputId": "73f6437a-39f4-4968-f88a-99f10a3fd8ae" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "R outputs will be written to: /content/artifacts/r \n" ] } ], "source": [ "\n", "ART_DIR <- \"artifacts\"\n", "R_FIG_DIR <- file.path(ART_DIR, \"r\", \"figures\")\n", "R_TAB_DIR <- file.path(ART_DIR, \"r\", \"tables\")\n", "\n", "dir.create(R_FIG_DIR, recursive = TRUE, showWarnings = FALSE)\n", "dir.create(R_TAB_DIR, recursive = TRUE, showWarnings = FALSE)\n", "\n", "cat(\"R outputs will be written to:\", normalizePath(file.path(ART_DIR, \"r\"), winslash = \"/\"), \"\n", "\")" ] }, { "cell_type": "markdown", "id": "f880c72d", "metadata": { "id": "f880c72d" }, "source": [ "## **4.** 🔮 Forecast book sales benchmarking with `accuracy()`" ] }, { "cell_type": "markdown", "source": [ "We benchmark **three** models on a holdout window (last *h* months):\n", "- ARIMA + Fourier (seasonality upgrade)\n", "- ETS\n", "- Naive baseline\n", "\n", "Then we export:\n", "- `accuracy_table.csv`\n", "- `forecast_compare.png`\n", "- `rmse_comparison.png`" ], "metadata": { "id": "R0JZlzKegmzW" }, "id": "R0JZlzKegmzW" }, { "cell_type": "code", "execution_count": null, "id": "62e87992", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "62e87992", "outputId": "73b36487-a25d-4bb9-cf80-8d5a654a2f0d" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "✅ Saved: artifacts/r/tables/accuracy_table.csv\n", "✅ Saved: artifacts/r/figures/rmse_comparison.png\n" ] }, { "output_type": "display_data", "data": { "text/html": [ "agg_record_872216040: 2" ], "text/markdown": "**agg_record_872216040:** 2", "text/latex": "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_872216040:} 2", "text/plain": [ "agg_record_872216040 \n", " 2 " ] }, "metadata": {} }, { "output_type": "stream", "name": "stdout", "text": [ "✅ Saved: artifacts/r/figures/forecast_compare.png\n" ] } ], "source": [ "\n", "# Build monthly ts\n", "start_year <- year(min(df_month2$month, na.rm = TRUE))\n", "start_mon <- month(min(df_month2$month, na.rm = TRUE))\n", "\n", "y <- ts(df_month2$total_revenue, frequency = 12, start = c(start_year, start_mon))\n", "\n", "# holdout size: min(6, 20% of series), at least 1\n", "h_test <- min(6, max(1, floor(length(y) / 5)))\n", "train_ts <- head(y, length(y) - h_test)\n", "test_ts <- tail(y, h_test)\n", "\n", "# Model A: ARIMA + Fourier\n", "K <- 2\n", "xreg_train <- fourier(train_ts, K = K)\n", "fit_arima <- auto.arima(train_ts, xreg = xreg_train)\n", "xreg_future <- fourier(train_ts, K = K, h = h_test)\n", "fc_arima <- forecast(fit_arima, xreg = xreg_future, h = h_test)\n", "\n", "# Model B: ETS\n", "fit_ets <- ets(train_ts)\n", "fc_ets <- forecast(fit_ets, h = h_test)\n", "\n", "# Model C: Naive baseline\n", "fc_naive <- naive(train_ts, h = h_test)\n", "\n", "# accuracy() tables\n", "acc_arima <- as.data.frame(accuracy(fc_arima, test_ts))\n", "acc_ets <- as.data.frame(accuracy(fc_ets, test_ts))\n", "acc_naive <- as.data.frame(accuracy(fc_naive, test_ts))\n", "\n", "accuracy_tbl <- bind_rows(\n", " acc_arima %>% mutate(model = \"ARIMA+Fourier\"),\n", " acc_ets %>% mutate(model = \"ETS\"),\n", " acc_naive %>% mutate(model = \"Naive\")\n", ") %>% relocate(model)\n", "\n", "write_csv(accuracy_tbl, file.path(R_TAB_DIR, \"accuracy_table.csv\"))\n", "cat(\"✅ Saved: artifacts/r/tables/accuracy_table.csv\\n\")\n", "\n", "# RMSE bar chart\n", "p_rmse <- ggplot(accuracy_tbl, aes(x = reorder(model, RMSE), y = RMSE)) +\n", " geom_col() +\n", " coord_flip() +\n", " labs(title = \"Forecast model comparison (RMSE on holdout)\", x = \"\", y = \"RMSE\") +\n", " theme_minimal()\n", "\n", "ggsave(file.path(R_FIG_DIR, \"rmse_comparison.png\"), p_rmse, width = 8, height = 4, dpi = 160)\n", "cat(\"✅ Saved: artifacts/r/figures/rmse_comparison.png\\n\")\n", "\n", "# Side-by-side forecast plots (simple, no extra deps)\n", "png(file.path(R_FIG_DIR, \"forecast_compare.png\"), width = 1200, height = 500)\n", "par(mfrow = c(1, 3))\n", "plot(fc_arima, main = \"ARIMA + Fourier\", xlab = \"Time\", ylab = \"Total revenue\"); lines(test_ts, col = \"black\")\n", "plot(fc_ets, main = \"ETS\", xlab = \"Time\", ylab = \"Total revenue\"); lines(test_ts, col = \"black\")\n", "plot(fc_naive, main = \"Naive\", xlab = \"Time\", ylab = \"Total revenue\"); lines(test_ts, col = \"black\")\n", "dev.off()\n", "cat(\"✅ Saved: artifacts/r/figures/forecast_compare.png\\n\")" ] }, { "cell_type": "markdown", "id": "30bc017b", "metadata": { "id": "30bc017b" }, "source": [ "## **5.** 💾 Some R metadata for Hugging Face" ] }, { "cell_type": "code", "execution_count": null, "id": "645cb12b", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "645cb12b", "outputId": "c00c26da-7d27-4c78-a296-aa33807495d4" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "✅ Saved: artifacts/r/tables/r_meta.json\n", "DONE. R artifacts written to: artifacts/r \n" ] } ], "source": [ "# =========================================================\n", "# Metadata export (aligned with current notebook objects)\n", "# =========================================================\n", "\n", "meta <- list(\n", "\n", " # ---------------------------\n", " # Dataset footprint\n", " # ---------------------------\n", " n_titles = nrow(df_title),\n", " n_months = nrow(df_month2),\n", "\n", " # ---------------------------\n", " # Forecasting info\n", " # (only if these objects exist in your forecasting section)\n", " # ---------------------------\n", " forecasting = list(\n", " holdout_h = h_test,\n", " arima_order = forecast::arimaorder(fit_arima),\n", " ets_method = fit_ets$method\n", " )\n", ")\n", "\n", "jsonlite::write_json(\n", " meta,\n", " path = file.path(R_TAB_DIR, \"r_meta.json\"),\n", " pretty = TRUE,\n", " auto_unbox = TRUE\n", ")\n", "\n", "cat(\"✅ Saved: artifacts/r/tables/r_meta.json\\n\")\n", "cat(\"DONE. R artifacts written to:\", file.path(ART_DIR, \"r\"), \"\\n\")\n" ] } ], "metadata": { "colab": { "provenance": [], "collapsed_sections": [ "f01d02e7", "b8971bc4", "f880c72d", "30bc017b" ] }, "kernelspec": { "name": "ir", "display_name": "R" }, "language_info": { "name": "R" } }, "nbformat": 4, "nbformat_minor": 5 }