Build a base R toolkit that compares naive, mean, drift and SES forecasts by RMSE
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement a forecasting toolkit in base R that splits a univariate series into train and test parts, forecasts the test period with four baseline methods, scores each forecast, and ranks the methods. Every forecaster returns a flat numeric vector of length `h`.
- `train_test_split(y, test_size)` returns `list(train = ..., test = ...)`, where `test` is the last `test_size` elements of `y` and `train` is the rest, both in original order.
- `naive_forecast(train, h)` repeats the last training value `h` times. `mean_forecast(train, h)` repeats `mean(train)` `h` times.
- `drift_forecast(train, h)` returns `forecast[i] = train[n] + slope * i` for `i` in `1..h`, with `slope = (train[n] - train[1]) / (n - 1)`.
- `ses_forecast(train, alpha, h)` applies simple exponential smoothing: start with `l = train[1]`, update `l = alpha * train[t] + (1 - alpha) * l` for `t` in `2..n`, and repeat the final level `h` times.
- `accuracy_metrics(actual, predicted)` returns `list(MAE, RMSE, MAPE)` computed as shown below.
- `compare_forecasts(y, test_size, alpha)` runs all four methods with `h = test_size` and returns a `data.frame` with columns `method`, `MAE`, `RMSE` and `MAPE`. `method` is a character column holding exactly `"naive"`, `"mean"`, `"drift"` and `"ses"`. Rows are sorted ascending by `RMSE`, ties are broken by `method` in alphabetical order, and row names are `1..4`.
```r
e <- actual - predicted
MAE <- mean(abs(e))
RMSE <- sqrt(mean(e^2))
MAPE <- mean(abs(e / actual)) * 100
```
Example, with metric values rounded to 4 decimals:
```r
y <- c(10, 12, 11, 13, 14, 16)
compare_forecasts(y, test_size = 2, alpha = 0.5)
# method MAE RMSE MAPE
# 1 drift 0.5000 0.7071 3.1250
# 2 naive 2.0000 2.2361 12.9464
# 3 ses 3.0000 3.1623 19.6429
# 4 mean 3.5000 3.6401 22.9911
```
The split gives `train = c(10, 12, 11, 13)` and `test = c(14, 16)`. Drift forecasts 14 and 15 (slope 1), naive forecasts 13 twice, SES ends at level 12, and the mean forecast is 11.5.
```hint Small cases first
Write out the train and test indices for a series of length 3 with `test_size = 2`, then check what each forecaster does with a single training value.
```
```hint Ranking
Base R's `order()` accepts more than one sort key.
```
### Constraints & Assumptions
- `test_size` is a positive integer smaller than `length(y)`, and every `h` is a positive integer.
- `actual` contains no zeros, so MAPE is always defined.
- `alpha` is a smoothing weight between 0 and 1.
- Base R only: no `forecast` package, no `stats::HoltWinters`, no other packages.
### Clarifying Questions to Ask
- What should `drift_forecast()` return when `train` has a single value and the slope formula divides by zero?
- Should the metrics come back at full precision or rounded?
- Should the functions validate `test_size` and `alpha`, or trust the stated constraints?
### What a Strong Answer Covers
- Each forecaster follows its formula and returns a numeric vector of length `h` that uses only the training data.
- The three metrics follow the given formulas, with the assumption that keeps MAPE defined stated.
- `compare_forecasts()` scores all methods on one shared split, uses the exact method labels, sorts by `RMSE` with the alphabetical tie rule, and resets row names.
- Edge cases are handled: a one-value training set, exact ties, and the off-by-one traps in the split and the smoothing loop.
### Follow-up Questions
- How would you extend `ses_forecast()` to a damped-trend method?
- Why prefer `RMSE` over `MAPE` for ranking here?
- How would you make the comparison more reliable than a single hold-out split?
Overview: Implement a base R toolkit that splits a time series, forecasts the holdout with naive, mean, drift and simple exponential smoothing baselines, and ranks them by RMSE. It tests faithful translation of forecasting formulas, error metrics, index handling and deterministic tie-breaking.