Description Usage Arguments Details Examples
View source: R/forecast_functions.R
Methods for forecasting regular time series data based on a linear regression model
1 2 3 4 5 6 7 8 9 10 11 12 13 |
input |
A tsibble or ts object |
y |
A character, the column name of the depended variable of the input object, required (and applicable) only when the input is tsibble object |
x |
A character, the column names of the independent variable of the input object, applicable when using tsibble object with regressors |
seasonal |
A character, optional, create categorical variable/s to model the single or multiple seasonal components. Supporting the following frequencies structure: 'quarter' - for quarterly frequency 'month' - for monthly frequency 'week' - for weekly frequency 'yday' - for daily frequency 'wday' - for day of the week frequency 'hour' - for hourly frequency The argument supports multiple seasonality cases such as daily and day of the week effect |
trend |
A list, define the trend structure. Possible arguments - 'linear', a boolean variable, if set to TRUE defines a linear trend (e.g., index of 1,2,3,...,t, for a series with t observations) 'power“, a numeric value, defines the polynomial degree of the series index (for example, a power = 0.5 define a square root index and power = 2 represents a squared index). By default set to NULL 'exponential', a boolean variable, if set to TRUE defines an exponential trend 'log', a boolean variable, if set to TRUE defines a log transformation for the trend. By default, the 'trend' argument is set to a linear trend (i.e., power = 1) |
lags |
A positive integer, defines the series lags to be used as input to the model (equivalent to AR process) |
splines |
A list, optional, provides the ability to model structural shifts or breaks of the series trend with the use of spline or piecewise regression. The argument enables to define either single or multiple splines /piecewise regression by setting for each one as a list with the following two arguments: type - defines the spline feature. For model a linear trend for each segments, set the type argument as 'linear'. For a structural break or shift of the trend, set the type argument as 'break'. knots - a single or sequence of dates/time that represents the breaking point of the trend (either slope shift or structural break). Note when using 'tsibble“ object as input, the dates or time inputs must align with the series index class. When using 'ts' object, the input class should align with the converted index of the 'ts“ object. For example, if the frequency of the 'ts' object is 12 (i.e., monthly), you should use 'Date“ objects to set the knots |
events |
A list, optional, create hot encoding variables based on date/time objects, where the date/time objects must align with the input object index class (may not work when the input object is 'ts') |
scale |
A character, scaling options of the series, methods available - c("log", "normal", "standard") for log transformation, normalization, or standardization of the series, respectively. If set to NULL (default), no transformation will occur |
step |
A boolean, if set to TRUE will use apply the stepwise function for variable selection using the |
... |
Optional, a list of arguments to pass to the |
The trainLM function provides a flexible framework for forecasting regular time-series data with the linear regression model with the lm
function.
The function arguments enable a fast extraction and generation of new features from the input series, such as seasonal, trend, lags, outliers, and special events.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | data(ny_gas)
head(ny_gas)
# Fitting basic model with linear trend
md1 <- trainLM(input = ny_gas,
y = "y",
trend = list(linear = TRUE))
# Getting the regression summary
summary(md1$model)
# Plotting the residauls of the model
plot_res(md1)
# Adding monthly seasonal component
md2 <- trainLM(input = ny_gas,
y = "y",
trend = list(linear = TRUE),
seasonal = "month")
plot_res(md2)
# Adding the first and seasonal lags
md3 <- trainLM(input = ny_gas,
y = "y",
trend = list(linear = TRUE),
seasonal = "month",
lags = c(1, 12))
plot_res(md3)
# Adding more lags and using stepwise regression for variable selection
md4 <- trainLM(input = ny_gas,
y = "y",
trend = list(linear = TRUE),
seasonal = "month",
lags = c(1:12),
step = TRUE)
summary(md4$model)
plot_res(md4)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.