Get up and running with forecastbaselines in 5 minutes!
# 1. Install remotes, which pulls in the juliabridge dependency
install.packages("remotes")
# 2. Install forecastbaselines
remotes::install_github("epiforecasts/forecastbaselines")
# 3. Load and setup (this may take a few minutes the first time)
library(forecastbaselines)
setup_ForecastBaselines()Note: Make sure you have Julia installed first! See INSTALL.md for details.
library(forecastbaselines)
setup_ForecastBaselines()
# Your data
data <- c(10, 12, 13, 14, 15, 16, 18, 19, 20, 22)
# Create model → Fit → Forecast
model <- ARMAModel(p = 1)
fitted <- fit_baseline(data, model)
forecasts <- point_forecast(fitted, horizon = 1:5)
print(forecasts)# Generate probabilistic forecast with prediction intervals
fc <- forecast(
fitted,
interval_method = EmpiricalInterval(n_trajectories = 1000),
horizon = 1:5,
levels = c(0.80, 0.95)
)
print(fc)# Add true observed values
truth <- c(23, 24, 25, 26, 27)
fc_eval <- add_truth(fc, truth)
# Compute error metrics
scores <- score(fc_eval)
cat("MAE:", scores$ae_point, "\n")
cat("RMSE:", sqrt(scores$se_point), "\n")Choose the model that fits your data:
# Simple baselines
ConstantModel() # Last value
MarginalModel(p = 10) # Mean of last 10 obs
# Trend/Seasonal
LSDModel(s = 12) # Seasonal pattern
STLModel(s = 12) # STL decomposition
OLSModel(degree = 2) # Polynomial trend
# Time series models
ARMAModel(p = 1, q = 1) # ARMA
ETSModel("A", "A", "N") # Exponential smoothing
INARCHModel(p = 1) # For count data# Monthly data with yearly seasonality
model <- STLModel(s = 12)
fitted <- fit_baseline(monthly_data, model)
fc <- forecast(fitted, horizon = 1:12,
interval_method = EmpiricalInterval())# Weekly data with 52-week seasonality
model <- LSDModel(s = 52)
fitted <- fit_baseline(weekly_data, model)
fc <- forecast(fitted, horizon = 1:4)# For integer counts (e.g., number of events)
model <- INARCHModel(p = 1)
fitted <- fit_baseline(count_data, model)
fc <- forecast(fitted,
interval_method = EmpiricalInterval(
positivity_correction = "post_clip"
))# Apply log transformation (useful for multiplicative trends)
model <- ARMAModel(p = 1)
trans <- LogTransform()
model_trans <- transform_model(model, trans)
fitted <- fit_baseline(data, model_trans)
# Forecasts are automatically back-transformed!
fc <- forecast(fitted, horizon = 1:10)# Compare multiple models
models <- list(
Naive = ConstantModel(),
ARMA = ARMAModel(p = 1, q = 1),
STL = STLModel(s = 12)
)
results <- lapply(names(models), function(name) {
fitted <- fit_baseline(data, models[[name]])
fc <- forecast(fitted, horizon = 1:12, truth = truth)
fc_scores <- score(fc)
data.frame(
Model = name,
MAE = fc_scores$ae_point,
RMSE = sqrt(fc_scores$se_point)
)
})
comparison <- do.call(rbind, results)
print(comparison)Choose how to quantify uncertainty:
# No intervals (point forecast only)
NoInterval()
# Bootstrap from historical errors (non-parametric)
EmpiricalInterval(n_trajectories = 1000, seed = 42)
# Model-based parametric intervals
ParametricInterval()
# Simulate from the model
ModelTrajectoryInterval(n_trajectories = 1000)Evaluate forecast quality:
# Get all metrics at once
scores <- score(forecast)
# Access specific metrics
mae <- scores$ae_point # Mean Absolute Error
rmse <- sqrt(scores$se_point) # Root Mean Squared Error
ape <- scores$ape # Absolute Percentage Error
# For probabilistic forecasts (requires quantile/interval forecasts)
wis <- scores$wis # Weighted Interval Score
bias <- scores$bias # Forecast bias# Standard workflow
fitted <- fit_baseline(data, model)
fc <- forecast(fitted, interval_method = EmpiricalInterval(),
horizon = 1:h, truth = truth)
scores <- score(fc)
mae <- scores$ae_point# Rolling window validation
n <- length(data)
window <- 50
errors <- numeric(n - window - 12)
for (i in 1:(n - window - 12)) {
train <- data[i:(i + window - 1)]
test <- data[(i + window):(i + window + 11)]
fitted <- fit_baseline(train, model)
fc <- forecast(fitted, horizon = 1:12, truth = test)
errors[i] <- score(fc)$ae_point
}
cat("Average MAE:", mean(errors), "\n")# Combine multiple models
models <- list(ARMAModel(p=1), STLModel(s=12), ETSModel("A","A","N"))
forecasts <- lapply(models, function(m) {
fitted <- fit_baseline(data, m)
point_forecast(fitted, 1:12)
})
# Simple average ensemble
ensemble <- rowMeans(do.call(cbind, forecasts))-
First Time Setup: The first run of
setup_ForecastBaselines()may take a few minutes to install Julia packages. -
Speed: Julia uses JIT compilation, so the first forecast will be slower. Subsequent forecasts are fast!
-
Model Selection: Start simple (ConstantModel, MarginalModel) then try more complex models (ARMA, ETS, STL).
-
Seasonality: If your data has clear seasonal patterns, use models with
sparameter (STL, LSD, ARMA with s). -
Intervals: EmpiricalInterval is robust and works well in most cases. Use more trajectories (2000+) for smoother intervals.
-
Transformations: Use LogTransform or Box-Cox for data with multiplicative trends or heteroscedasticity.
- Full Examples: getting started for a first forecast, the models guide for every model
- Documentation: See
README.mdfor complete API documentation - Installation Help: See
INSTALL.mdif you encounter setup issues
# Get help on any function
?ARMAModel
?forecast
?score
# Check if setup is complete
is_setup()Copy-paste this to test your installation:
library(forecastbaselines)
setup_ForecastBaselines()
set.seed(123)
data <- cumsum(rnorm(50)) + 10
model <- ARMAModel(p = 1)
fitted <- fit_baseline(data, model)
fc <- forecast(fitted, interval_method = EmpiricalInterval(),
horizon = 1:5, levels = 0.95)
print(fc)If this runs without errors, you're all set! 🎉