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|
| 14 |
+
"cell_type": "markdown",
|
| 15 |
+
"id": "fb807724",
|
| 16 |
+
"metadata": {
|
| 17 |
+
"id": "fb807724"
|
| 18 |
+
},
|
| 19 |
+
"source": [
|
| 20 |
+
"## **1.** 📦 Setup"
|
| 21 |
+
]
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"cell_type": "code",
|
| 25 |
+
"execution_count": null,
|
| 26 |
+
"id": "d40cd131",
|
| 27 |
+
"metadata": {
|
| 28 |
+
"id": "d40cd131"
|
| 29 |
+
},
|
| 30 |
+
"outputs": [],
|
| 31 |
+
"source": [
|
| 32 |
+
"\n",
|
| 33 |
+
"# Uncomment the next line once:\n",
|
| 34 |
+
"install.packages(c(\"readr\",\"dplyr\",\"stringr\",\"tidyr\",\"lubridate\",\"ggplot2\",\"forecast\",\"broom\",\"jsonlite\"), repos=\"https://cloud.r-project.org\")\n",
|
| 35 |
+
"\n",
|
| 36 |
+
"suppressPackageStartupMessages({\n",
|
| 37 |
+
" library(readr)\n",
|
| 38 |
+
" library(dplyr)\n",
|
| 39 |
+
" library(stringr)\n",
|
| 40 |
+
" library(tidyr)\n",
|
| 41 |
+
" library(lubridate)\n",
|
| 42 |
+
" library(ggplot2)\n",
|
| 43 |
+
" library(forecast)\n",
|
| 44 |
+
" library(broom)\n",
|
| 45 |
+
" library(jsonlite)\n",
|
| 46 |
+
"})"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "markdown",
|
| 51 |
+
"id": "f01d02e7",
|
| 52 |
+
"metadata": {
|
| 53 |
+
"id": "f01d02e7"
|
| 54 |
+
},
|
| 55 |
+
"source": [
|
| 56 |
+
"## **2.** ✅️ Load & inspect inputs"
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"cell_type": "code",
|
| 61 |
+
"execution_count": null,
|
| 62 |
+
"id": "29e8f6ce",
|
| 63 |
+
"metadata": {
|
| 64 |
+
"colab": {
|
| 65 |
+
"base_uri": "https://localhost:8080/"
|
| 66 |
+
},
|
| 67 |
+
"id": "29e8f6ce",
|
| 68 |
+
"outputId": "5a1bda1c-c58d-43d0-c85e-db5041c8bc49"
|
| 69 |
+
},
|
| 70 |
+
"outputs": [
|
| 71 |
+
{
|
| 72 |
+
"output_type": "stream",
|
| 73 |
+
"name": "stdout",
|
| 74 |
+
"text": [
|
| 75 |
+
"Loaded: 1000 rows (title-level), 18 rows (monthly)\n"
|
| 76 |
+
]
|
| 77 |
+
}
|
| 78 |
+
],
|
| 79 |
+
"source": [
|
| 80 |
+
"\n",
|
| 81 |
+
"must_exist <- function(path, label) {\n",
|
| 82 |
+
" if (!file.exists(path)) stop(paste0(\"Missing \", label, \": \", path))\n",
|
| 83 |
+
"}\n",
|
| 84 |
+
"\n",
|
| 85 |
+
"TITLE_PATH <- \"synthetic_title_level_features.csv\"\n",
|
| 86 |
+
"MONTH_PATH <- \"synthetic_monthly_revenue_series.csv\"\n",
|
| 87 |
+
"\n",
|
| 88 |
+
"must_exist(TITLE_PATH, \"TITLE_PATH\")\n",
|
| 89 |
+
"must_exist(MONTH_PATH, \"MONTH_PATH\")\n",
|
| 90 |
+
"\n",
|
| 91 |
+
"df_title <- read_csv(TITLE_PATH, show_col_types = FALSE)\n",
|
| 92 |
+
"df_month <- read_csv(MONTH_PATH, show_col_types = FALSE)\n",
|
| 93 |
+
"\n",
|
| 94 |
+
"cat(\"Loaded:\", nrow(df_title), \"rows (title-level),\", nrow(df_month), \"rows (monthly)\n",
|
| 95 |
+
"\")"
|
| 96 |
+
]
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"cell_type": "code",
|
| 100 |
+
"execution_count": null,
|
| 101 |
+
"id": "9fd04262",
|
| 102 |
+
"metadata": {
|
| 103 |
+
"colab": {
|
| 104 |
+
"base_uri": "https://localhost:8080/"
|
| 105 |
+
},
|
| 106 |
+
"id": "9fd04262",
|
| 107 |
+
"outputId": "5f031538-96be-4758-904d-9201ec3c3ea7"
|
| 108 |
+
},
|
| 109 |
+
"outputs": [
|
| 110 |
+
{
|
| 111 |
+
"output_type": "stream",
|
| 112 |
+
"name": "stdout",
|
| 113 |
+
"text": [
|
| 114 |
+
"\u001b[90m# A tibble: 1 × 6\u001b[39m\n",
|
| 115 |
+
" n na_avg_revenue na_price na_rating na_share_pos na_share_neg\n",
|
| 116 |
+
" \u001b[3m\u001b[90m<int>\u001b[39m\u001b[23m \u001b[3m\u001b[90m<int>\u001b[39m\u001b[23m \u001b[3m\u001b[90m<int>\u001b[39m\u001b[23m \u001b[3m\u001b[90m<int>\u001b[39m\u001b[23m \u001b[3m\u001b[90m<int>\u001b[39m\u001b[23m \u001b[3m\u001b[90m<int>\u001b[39m\u001b[23m\n",
|
| 117 |
+
"\u001b[90m1\u001b[39m \u001b[4m1\u001b[24m000 0 0 \u001b[4m1\u001b[24m000 0 0\n",
|
| 118 |
+
"Monthly rows after parsing: 18 \n"
|
| 119 |
+
]
|
| 120 |
+
}
|
| 121 |
+
],
|
| 122 |
+
"source": [
|
| 123 |
+
"\n",
|
| 124 |
+
"# ---------- helpers ----------\n",
|
| 125 |
+
"safe_num <- function(x) {\n",
|
| 126 |
+
" # strips anything that is not digit or dot\n",
|
| 127 |
+
" suppressWarnings(as.numeric(str_replace_all(as.character(x), \"[^0-9.]\", \"\")))\n",
|
| 128 |
+
"}\n",
|
| 129 |
+
"\n",
|
| 130 |
+
"parse_rating <- function(x) {\n",
|
| 131 |
+
" # Accept: 4, \"4\", \"4.0\", \"4/5\", \"4 out of 5\", \"⭐⭐⭐⭐\", etc.\n",
|
| 132 |
+
" x <- as.character(x)\n",
|
| 133 |
+
" x <- str_replace_all(x, \"⭐\", \"\")\n",
|
| 134 |
+
" x <- str_to_lower(x)\n",
|
| 135 |
+
" x <- str_replace_all(x, \"stars?\", \"\")\n",
|
| 136 |
+
" x <- str_replace_all(x, \"out of\", \"/\")\n",
|
| 137 |
+
" x <- str_replace_all(x, \"\\\\s+\", \"\")\n",
|
| 138 |
+
" x <- str_replace_all(x, \"[^0-9./]\", \"\")\n",
|
| 139 |
+
" suppressWarnings(as.numeric(str_extract(x, \"^[0-9.]+\")))\n",
|
| 140 |
+
"}\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"parse_month <- function(x) {\n",
|
| 143 |
+
" x <- as.character(x)\n",
|
| 144 |
+
" # try YYYY-MM-DD, then YYYY-MM\n",
|
| 145 |
+
" out <- suppressWarnings(ymd(x))\n",
|
| 146 |
+
" if (mean(is.na(out)) > 0.5) out <- suppressWarnings(ymd(paste0(x, \"-01\")))\n",
|
| 147 |
+
" na_idx <- which(is.na(out))\n",
|
| 148 |
+
" if (length(na_idx) > 0) out[na_idx] <- suppressWarnings(ymd(paste0(x[na_idx], \"-01\")))\n",
|
| 149 |
+
" out\n",
|
| 150 |
+
"}\n",
|
| 151 |
+
"\n",
|
| 152 |
+
"# ---------- normalize keys ----------\n",
|
| 153 |
+
"df_title <- df_title %>% mutate(title = str_squish(as.character(title)))\n",
|
| 154 |
+
"df_month <- df_month %>% mutate(month = as.character(month))\n",
|
| 155 |
+
"\n",
|
| 156 |
+
"# ---------- parse numeric columns defensively ----------\n",
|
| 157 |
+
"need_cols_title <- c(\"title\",\"avg_revenue\",\"total_revenue\",\"price\",\"rating\",\"share_positive\",\"share_negative\",\"share_neutral\")\n",
|
| 158 |
+
"missing_title <- setdiff(need_cols_title, names(df_title))\n",
|
| 159 |
+
"if (length(missing_title) > 0) stop(paste0(\"df_title missing columns: \", paste(missing_title, collapse=\", \")))\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"df_title <- df_title %>%\n",
|
| 162 |
+
" mutate(\n",
|
| 163 |
+
" avg_revenue = safe_num(avg_revenue),\n",
|
| 164 |
+
" total_revenue = safe_num(total_revenue),\n",
|
| 165 |
+
" price = safe_num(price),\n",
|
| 166 |
+
" rating = parse_rating(rating),\n",
|
| 167 |
+
" share_positive = safe_num(share_positive),\n",
|
| 168 |
+
" share_negative = safe_num(share_negative),\n",
|
| 169 |
+
" share_neutral = safe_num(share_neutral)\n",
|
| 170 |
+
" )\n",
|
| 171 |
+
"\n",
|
| 172 |
+
"# basic sanity stats\n",
|
| 173 |
+
"hyg <- df_title %>%\n",
|
| 174 |
+
" summarise(\n",
|
| 175 |
+
" n = n(),\n",
|
| 176 |
+
" na_avg_revenue = sum(is.na(avg_revenue)),\n",
|
| 177 |
+
" na_price = sum(is.na(price)),\n",
|
| 178 |
+
" na_rating = sum(is.na(rating)),\n",
|
| 179 |
+
" na_share_pos = sum(is.na(share_positive)),\n",
|
| 180 |
+
" na_share_neg = sum(is.na(share_negative))\n",
|
| 181 |
+
" )\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"print(hyg)\n",
|
| 184 |
+
"\n",
|
| 185 |
+
"# monthly parsing\n",
|
| 186 |
+
"need_cols_month <- c(\"month\",\"total_revenue\")\n",
|
| 187 |
+
"missing_month <- setdiff(need_cols_month, names(df_month))\n",
|
| 188 |
+
"if (length(missing_month) > 0) stop(paste0(\"df_month missing columns: \", paste(missing_month, collapse=\", \")))\n",
|
| 189 |
+
"\n",
|
| 190 |
+
"df_month2 <- df_month %>%\n",
|
| 191 |
+
" mutate(\n",
|
| 192 |
+
" month = parse_month(month),\n",
|
| 193 |
+
" total_revenue = safe_num(total_revenue)\n",
|
| 194 |
+
" ) %>%\n",
|
| 195 |
+
" filter(!is.na(month)) %>%\n",
|
| 196 |
+
" arrange(month)\n",
|
| 197 |
+
"\n",
|
| 198 |
+
"cat(\"Monthly rows after parsing:\", nrow(df_month2), \"\\n\")"
|
| 199 |
+
]
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"cell_type": "markdown",
|
| 203 |
+
"id": "b8971bc4",
|
| 204 |
+
"metadata": {
|
| 205 |
+
"id": "b8971bc4"
|
| 206 |
+
},
|
| 207 |
+
"source": [
|
| 208 |
+
"## **3.** 💾 Folder for R outputs for Hugging Face"
|
| 209 |
+
]
|
| 210 |
+
},
|
| 211 |
+
{
|
| 212 |
+
"cell_type": "code",
|
| 213 |
+
"execution_count": null,
|
| 214 |
+
"id": "dfaa06b1",
|
| 215 |
+
"metadata": {
|
| 216 |
+
"colab": {
|
| 217 |
+
"base_uri": "https://localhost:8080/"
|
| 218 |
+
},
|
| 219 |
+
"id": "dfaa06b1",
|
| 220 |
+
"outputId": "73f6437a-39f4-4968-f88a-99f10a3fd8ae"
|
| 221 |
+
},
|
| 222 |
+
"outputs": [
|
| 223 |
+
{
|
| 224 |
+
"output_type": "stream",
|
| 225 |
+
"name": "stdout",
|
| 226 |
+
"text": [
|
| 227 |
+
"R outputs will be written to: /content/artifacts/r \n"
|
| 228 |
+
]
|
| 229 |
+
}
|
| 230 |
+
],
|
| 231 |
+
"source": [
|
| 232 |
+
"\n",
|
| 233 |
+
"ART_DIR <- \"artifacts\"\n",
|
| 234 |
+
"R_FIG_DIR <- file.path(ART_DIR, \"r\", \"figures\")\n",
|
| 235 |
+
"R_TAB_DIR <- file.path(ART_DIR, \"r\", \"tables\")\n",
|
| 236 |
+
"\n",
|
| 237 |
+
"dir.create(R_FIG_DIR, recursive = TRUE, showWarnings = FALSE)\n",
|
| 238 |
+
"dir.create(R_TAB_DIR, recursive = TRUE, showWarnings = FALSE)\n",
|
| 239 |
+
"\n",
|
| 240 |
+
"cat(\"R outputs will be written to:\", normalizePath(file.path(ART_DIR, \"r\"), winslash = \"/\"), \"\n",
|
| 241 |
+
"\")"
|
| 242 |
+
]
|
| 243 |
+
},
|
| 244 |
+
{
|
| 245 |
+
"cell_type": "markdown",
|
| 246 |
+
"id": "f880c72d",
|
| 247 |
+
"metadata": {
|
| 248 |
+
"id": "f880c72d"
|
| 249 |
+
},
|
| 250 |
+
"source": [
|
| 251 |
+
"## **4.** 🔮 Forecast book sales benchmarking with `accuracy()`"
|
| 252 |
+
]
|
| 253 |
+
},
|
| 254 |
+
{
|
| 255 |
+
"cell_type": "markdown",
|
| 256 |
+
"source": [
|
| 257 |
+
"We benchmark **three** models on a holdout window (last *h* months):\n",
|
| 258 |
+
"- ARIMA + Fourier (seasonality upgrade)\n",
|
| 259 |
+
"- ETS\n",
|
| 260 |
+
"- Naive baseline\n",
|
| 261 |
+
"\n",
|
| 262 |
+
"Then we export:\n",
|
| 263 |
+
"- `accuracy_table.csv`\n",
|
| 264 |
+
"- `forecast_compare.png`\n",
|
| 265 |
+
"- `rmse_comparison.png`"
|
| 266 |
+
],
|
| 267 |
+
"metadata": {
|
| 268 |
+
"id": "R0JZlzKegmzW"
|
| 269 |
+
},
|
| 270 |
+
"id": "R0JZlzKegmzW"
|
| 271 |
+
},
|
| 272 |
+
{
|
| 273 |
+
"cell_type": "code",
|
| 274 |
+
"execution_count": null,
|
| 275 |
+
"id": "62e87992",
|
| 276 |
+
"metadata": {
|
| 277 |
+
"colab": {
|
| 278 |
+
"base_uri": "https://localhost:8080/"
|
| 279 |
+
},
|
| 280 |
+
"id": "62e87992",
|
| 281 |
+
"outputId": "73b36487-a25d-4bb9-cf80-8d5a654a2f0d"
|
| 282 |
+
},
|
| 283 |
+
"outputs": [
|
| 284 |
+
{
|
| 285 |
+
"output_type": "stream",
|
| 286 |
+
"name": "stdout",
|
| 287 |
+
"text": [
|
| 288 |
+
"✅ Saved: artifacts/r/tables/accuracy_table.csv\n",
|
| 289 |
+
"✅ Saved: artifacts/r/figures/rmse_comparison.png\n"
|
| 290 |
+
]
|
| 291 |
+
},
|
| 292 |
+
{
|
| 293 |
+
"output_type": "display_data",
|
| 294 |
+
"data": {
|
| 295 |
+
"text/html": [
|
| 296 |
+
"<strong>agg_record_872216040:</strong> 2"
|
| 297 |
+
],
|
| 298 |
+
"text/markdown": "**agg_record_872216040:** 2",
|
| 299 |
+
"text/latex": "\\textbf{agg\\textbackslash{}\\_record\\textbackslash{}\\_872216040:} 2",
|
| 300 |
+
"text/plain": [
|
| 301 |
+
"agg_record_872216040 \n",
|
| 302 |
+
" 2 "
|
| 303 |
+
]
|
| 304 |
+
},
|
| 305 |
+
"metadata": {}
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"output_type": "stream",
|
| 309 |
+
"name": "stdout",
|
| 310 |
+
"text": [
|
| 311 |
+
"✅ Saved: artifacts/r/figures/forecast_compare.png\n"
|
| 312 |
+
]
|
| 313 |
+
}
|
| 314 |
+
],
|
| 315 |
+
"source": [
|
| 316 |
+
"\n",
|
| 317 |
+
"# Build monthly ts\n",
|
| 318 |
+
"start_year <- year(min(df_month2$month, na.rm = TRUE))\n",
|
| 319 |
+
"start_mon <- month(min(df_month2$month, na.rm = TRUE))\n",
|
| 320 |
+
"\n",
|
| 321 |
+
"y <- ts(df_month2$total_revenue, frequency = 12, start = c(start_year, start_mon))\n",
|
| 322 |
+
"\n",
|
| 323 |
+
"# holdout size: min(6, 20% of series), at least 1\n",
|
| 324 |
+
"h_test <- min(6, max(1, floor(length(y) / 5)))\n",
|
| 325 |
+
"train_ts <- head(y, length(y) - h_test)\n",
|
| 326 |
+
"test_ts <- tail(y, h_test)\n",
|
| 327 |
+
"\n",
|
| 328 |
+
"# Model A: ARIMA + Fourier\n",
|
| 329 |
+
"K <- 2\n",
|
| 330 |
+
"xreg_train <- fourier(train_ts, K = K)\n",
|
| 331 |
+
"fit_arima <- auto.arima(train_ts, xreg = xreg_train)\n",
|
| 332 |
+
"xreg_future <- fourier(train_ts, K = K, h = h_test)\n",
|
| 333 |
+
"fc_arima <- forecast(fit_arima, xreg = xreg_future, h = h_test)\n",
|
| 334 |
+
"\n",
|
| 335 |
+
"# Model B: ETS\n",
|
| 336 |
+
"fit_ets <- ets(train_ts)\n",
|
| 337 |
+
"fc_ets <- forecast(fit_ets, h = h_test)\n",
|
| 338 |
+
"\n",
|
| 339 |
+
"# Model C: Naive baseline\n",
|
| 340 |
+
"fc_naive <- naive(train_ts, h = h_test)\n",
|
| 341 |
+
"\n",
|
| 342 |
+
"# accuracy() tables\n",
|
| 343 |
+
"acc_arima <- as.data.frame(accuracy(fc_arima, test_ts))\n",
|
| 344 |
+
"acc_ets <- as.data.frame(accuracy(fc_ets, test_ts))\n",
|
| 345 |
+
"acc_naive <- as.data.frame(accuracy(fc_naive, test_ts))\n",
|
| 346 |
+
"\n",
|
| 347 |
+
"accuracy_tbl <- bind_rows(\n",
|
| 348 |
+
" acc_arima %>% mutate(model = \"ARIMA+Fourier\"),\n",
|
| 349 |
+
" acc_ets %>% mutate(model = \"ETS\"),\n",
|
| 350 |
+
" acc_naive %>% mutate(model = \"Naive\")\n",
|
| 351 |
+
") %>% relocate(model)\n",
|
| 352 |
+
"\n",
|
| 353 |
+
"write_csv(accuracy_tbl, file.path(R_TAB_DIR, \"accuracy_table.csv\"))\n",
|
| 354 |
+
"cat(\"✅ Saved: artifacts/r/tables/accuracy_table.csv\\n\")\n",
|
| 355 |
+
"\n",
|
| 356 |
+
"# RMSE bar chart\n",
|
| 357 |
+
"p_rmse <- ggplot(accuracy_tbl, aes(x = reorder(model, RMSE), y = RMSE)) +\n",
|
| 358 |
+
" geom_col() +\n",
|
| 359 |
+
" coord_flip() +\n",
|
| 360 |
+
" labs(title = \"Forecast model comparison (RMSE on holdout)\", x = \"\", y = \"RMSE\") +\n",
|
| 361 |
+
" theme_minimal()\n",
|
| 362 |
+
"\n",
|
| 363 |
+
"ggsave(file.path(R_FIG_DIR, \"rmse_comparison.png\"), p_rmse, width = 8, height = 4, dpi = 160)\n",
|
| 364 |
+
"cat(\"✅ Saved: artifacts/r/figures/rmse_comparison.png\\n\")\n",
|
| 365 |
+
"\n",
|
| 366 |
+
"# Side-by-side forecast plots (simple, no extra deps)\n",
|
| 367 |
+
"png(file.path(R_FIG_DIR, \"forecast_compare.png\"), width = 1200, height = 500)\n",
|
| 368 |
+
"par(mfrow = c(1, 3))\n",
|
| 369 |
+
"plot(fc_arima, main = \"ARIMA + Fourier\", xlab = \"Time\", ylab = \"Total revenue\"); lines(test_ts, col = \"black\")\n",
|
| 370 |
+
"plot(fc_ets, main = \"ETS\", xlab = \"Time\", ylab = \"Total revenue\"); lines(test_ts, col = \"black\")\n",
|
| 371 |
+
"plot(fc_naive, main = \"Naive\", xlab = \"Time\", ylab = \"Total revenue\"); lines(test_ts, col = \"black\")\n",
|
| 372 |
+
"dev.off()\n",
|
| 373 |
+
"cat(\"✅ Saved: artifacts/r/figures/forecast_compare.png\\n\")"
|
| 374 |
+
]
|
| 375 |
+
},
|
| 376 |
+
{
|
| 377 |
+
"cell_type": "markdown",
|
| 378 |
+
"id": "30bc017b",
|
| 379 |
+
"metadata": {
|
| 380 |
+
"id": "30bc017b"
|
| 381 |
+
},
|
| 382 |
+
"source": [
|
| 383 |
+
"## **5.** 💾 Some R metadata for Hugging Face"
|
| 384 |
+
]
|
| 385 |
+
},
|
| 386 |
+
{
|
| 387 |
+
"cell_type": "code",
|
| 388 |
+
"execution_count": null,
|
| 389 |
+
"id": "645cb12b",
|
| 390 |
+
"metadata": {
|
| 391 |
+
"colab": {
|
| 392 |
+
"base_uri": "https://localhost:8080/"
|
| 393 |
+
},
|
| 394 |
+
"id": "645cb12b",
|
| 395 |
+
"outputId": "c00c26da-7d27-4c78-a296-aa33807495d4"
|
| 396 |
+
},
|
| 397 |
+
"outputs": [
|
| 398 |
+
{
|
| 399 |
+
"output_type": "stream",
|
| 400 |
+
"name": "stdout",
|
| 401 |
+
"text": [
|
| 402 |
+
"✅ Saved: artifacts/r/tables/r_meta.json\n",
|
| 403 |
+
"DONE. R artifacts written to: artifacts/r \n"
|
| 404 |
+
]
|
| 405 |
+
}
|
| 406 |
+
],
|
| 407 |
+
"source": [
|
| 408 |
+
"# =========================================================\n",
|
| 409 |
+
"# Metadata export (aligned with current notebook objects)\n",
|
| 410 |
+
"# =========================================================\n",
|
| 411 |
+
"\n",
|
| 412 |
+
"meta <- list(\n",
|
| 413 |
+
"\n",
|
| 414 |
+
" # ---------------------------\n",
|
| 415 |
+
" # Dataset footprint\n",
|
| 416 |
+
" # ---------------------------\n",
|
| 417 |
+
" n_titles = nrow(df_title),\n",
|
| 418 |
+
" n_months = nrow(df_month2),\n",
|
| 419 |
+
"\n",
|
| 420 |
+
" # ---------------------------\n",
|
| 421 |
+
" # Forecasting info\n",
|
| 422 |
+
" # (only if these objects exist in your forecasting section)\n",
|
| 423 |
+
" # ---------------------------\n",
|
| 424 |
+
" forecasting = list(\n",
|
| 425 |
+
" holdout_h = h_test,\n",
|
| 426 |
+
" arima_order = forecast::arimaorder(fit_arima),\n",
|
| 427 |
+
" ets_method = fit_ets$method\n",
|
| 428 |
+
" )\n",
|
| 429 |
+
")\n",
|
| 430 |
+
"\n",
|
| 431 |
+
"jsonlite::write_json(\n",
|
| 432 |
+
" meta,\n",
|
| 433 |
+
" path = file.path(R_TAB_DIR, \"r_meta.json\"),\n",
|
| 434 |
+
" pretty = TRUE,\n",
|
| 435 |
+
" auto_unbox = TRUE\n",
|
| 436 |
+
")\n",
|
| 437 |
+
"\n",
|
| 438 |
+
"cat(\"✅ Saved: artifacts/r/tables/r_meta.json\\n\")\n",
|
| 439 |
+
"cat(\"DONE. R artifacts written to:\", file.path(ART_DIR, \"r\"), \"\\n\")\n"
|
| 440 |
+
]
|
| 441 |
+
}
|
| 442 |
+
],
|
| 443 |
+
"metadata": {
|
| 444 |
+
"colab": {
|
| 445 |
+
"provenance": [],
|
| 446 |
+
"collapsed_sections": [
|
| 447 |
+
"f01d02e7",
|
| 448 |
+
"b8971bc4",
|
| 449 |
+
"f880c72d",
|
| 450 |
+
"30bc017b"
|
| 451 |
+
]
|
| 452 |
+
},
|
| 453 |
+
"kernelspec": {
|
| 454 |
+
"name": "ir",
|
| 455 |
+
"display_name": "R"
|
| 456 |
+
},
|
| 457 |
+
"language_info": {
|
| 458 |
+
"name": "R"
|
| 459 |
+
}
|
| 460 |
+
},
|
| 461 |
+
"nbformat": 4,
|
| 462 |
+
"nbformat_minor": 5
|
| 463 |
+
}
|