Simple interfaces to the forecasting API | R-bloggers
- Updated on: 2023-01-04
- Read original article here
Simple interfaces to the forecasting API
Posted on November 22, 2022 by T. Moudiki in R bloggers | 0 Comments
[This article was first published on T. Moudiki's Webpage - R , and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here )
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
So far, as of 2022-11-23, this API contains four methods for univariate time series forecasting (with prediction intervals):
mean a (not so naïve) benchmark method, whose prediction is the sample mean.
rw a (not so naïve) benchmark method, whose prediction is the last value of the input time series.
theta is the forecasting method described in [1] and [2], which won the M3 competition.
prophet is a popular model described in [3].
In this post, I’ll present two packages, one implemented in R and one in Python, which are designed for smoothing users’ interaction with the API. You can create similar high-level packages in other programming languages, by using this tool and this page .
Content
0 – Install packages in R or Python
1 – Create an account with create_account
2 – Get a token for authentication using get_token
3 – Requests for forecasts with get_forecast
0 – Install packages in R or Python:
In Python
1 – Create an account with create_account:
In Python
import forecastingapi as fapi res_create_account = fapi.create_account(username=" [email protected] ", password="pwd") # choose a better password print(res_create_account)
In R
forecastingAPI::create_account(username = " [email protected] ", password = "pwd") # choose a better password
2 – Get a token for authentication using get_token
In Python
token = fapi.get_token(username = " [email protected] ", password = "pwd") print(token)
In R
token <- forecastingAPI::get_token(username = " [email protected] ", password = "pwd")
The token is valid for 5 minutes. After 5 minutes, it must be renewed, using get_token.
3 - Requests for forecasts with get_forecast:
In Python
path_to_file = 'https://j5e2t5e4.stackpathcdn.com/Users/t/Documents/datasets/time_series/univariate/USAccDeaths.csv' # (examples:https://github.com/Techtonique/datasets/tree/main/time_series/univariate) res_get_forecast = fapi.get_forecast(file=path_to_file, token=token) print(res_get_forecast) res_get_forecast2 = fapi.get_forecast(file=path_to_file, token=token, start_training = 2, n_training = 7, h = 4, level = 90) print(res_get_forecast2) res_get_forecast3 = fapi.get_forecast(file=path_to_file, token=token, date_formatting="ms", start_training = 2, n_training = 7, h = 4, level = 90) print(res_get_forecast3) res_get_forecast4 = fapi.get_forecast(file=path_to_file, token=token, method = "prophet") print(res_get_forecast4)
In R
path_to_file <- 'https://j5e2t5e4.stackpathcdn.com/Users/t/Documents/datasets/time_series/univariate/USAccDeaths.csv' # (examples:https://github.com/Techtonique/datasets/tree/main/time_series/univariate) f_theta <- forecastingAPI::get_forecast(file = path_to_file, token = token, method = "theta", h=10, level = 95) f_mean <- forecastingAPI::get_forecast(file = path_to_file, token = token, method = "mean", h=10, level = 95) f_rw <- forecastingAPI::get_forecast(file = path_to_file, token = token, method = "rw", h=10, level = 95) f_prophet <- forecastingAPI::get_forecast(file = path_to_file, token = token, method = "prophet", h=10, level = 95)
[1] Assimakopoulos, V., & Nikolopoulos, K. (2000). The theta model: a decomposition approach to forecasting. International journal of forecasting, 16(4), 521-530.
[2] Hyndman, R. J., & Billah, B. (2003). Unmasking the Theta method. International Journal of Forecasting, 19(2), 287-290.
[3] Taylor, S. J., & Letham, B. (2018). Forecasting at scale. The American Statistician, 72(1), 37-45.
Related