How to Use the forecastML Package for Multi-Step-Ahead Forecasting

Dec 5, 2021 | Data Science

The forecastML package offers a robust suite of functions and visualizations aimed at simplifying multi-step-ahead forecasting using standard machine learning algorithms. In this article, we will walk you through the steps to utilize this powerful tool effectively.

Getting Started

This package is designed with flexibility in mind, allowing users to choose any machine learning algorithm from various R or Python packages. Let’s dive into how you can use forecastML for your forecasting needs!

Installation

  • Install from CRAN:
  • install.packages("forecastML")
  • Install from GitHub:
  • remotes::install_github("nredell/forecastML")

Setting Up the Forecasting Process

1. Preparing Your Data

The first step in using forecastML is preparing your dataset for modeling. This involves creating a lagged dataset for your dependent variable:

data_train <- forecastML::create_lagged_df(data_seatbelts, type = "train", outcome_col = 1, lookback = 1:15, horizons = 1:12)

This line of code prepares the training data for forecasting by incorporating lagged features, which are essential for time series analysis.

2. Building a Model

Next, you'll want to define your model. Let’s say we choose a LASSO regression model:

model_fn <- function(data) {
  x <- as.matrix(data[, -1, drop = FALSE])
  y <- as.matrix(data[, 1, drop = FALSE])
  model <- glmnet::cv.glmnet(x, y)
  return(model)
}

Here, we are creating a function that trains a LASSO regression model on our data. Think of this as a chef mixing all the right ingredients to bake the perfect cake! If the ingredients are good (i.e., your features), the cake (your predictions) will turn out delicious.

3. Training the Model

Once the model function is defined, it's time to train the model:

model_results <- forecastML::train_model(data_train, windows, model_name = "LASSO", model_function = model_fn)

This command trains the model using the training data we prepared earlier.

4. Making Predictions

After model training, you can now forecast future values:

data_forecasts <- predict(model_results, prediction_function = list(predict_fn), data = data_forecast)

Here, we utilize our trained model to generate future forecasts based on new data. It's like having a crystal ball that predicts the future based on patterns in your data!

5. Plotting the Results

Finally, visualize the predictions with the actual data to assess performance:

plot(data_forecasts, data_seatbelts[-(1:160), ], actual_indices = (1:nrow(data_seatbelts))[-(1:160)], interval_alpha = seq(.1, .2, length.out = 10))

This plot will help you see how well your forecasts align with the actual outcomes.

Troubleshooting Common Issues

  • If you encounter issues with missing data, ensure that you fill the gaps in your dataset using fill_gaps() before training your model.
  • For unexpected results in predictions, revisit your lagged feature generation and ensure correct parameters were supplied.
  • If you need further help or insights, consider checking out the documentation or, for additional insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Now that you have a basic understanding of how to utilize the forecastML package, you can start making accurate and valuable forecasts on your datasets! Happy forecasting!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox