Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

forecast.lm

Forecast a linear model with possible time series components


Description

forecast.lm is used to predict linear models, especially those involving trend and seasonality components.

Usage

## S3 method for class 'lm'
forecast(
  object,
  newdata,
  h = 10,
  level = c(80, 95),
  fan = FALSE,
  lambda = object$lambda,
  biasadj = NULL,
  ts = TRUE,
  ...
)

Arguments

object

Object of class "lm", usually the result of a call to lm or tslm.

newdata

An optional data frame in which to look for variables with which to predict. If omitted, it is assumed that the only variables are trend and season, and h forecasts are produced.

h

Number of periods for forecasting. Ignored if newdata present.

level

Confidence level for prediction intervals.

fan

If TRUE, level is set to seq(51,99,by=3). This is suitable for fan plots.

lambda

Box-Cox transformation parameter. If lambda="auto", then a transformation is automatically selected using BoxCox.lambda. The transformation is ignored if NULL. Otherwise, data transformed before model is estimated.

biasadj

Use adjusted back-transformed mean for Box-Cox transformations. If transformed data is used to produce forecasts and fitted values, a regular back transformation will result in median forecasts. If biasadj is TRUE, an adjustment will be made to produce mean forecasts and fitted values.

ts

If TRUE, the forecasts will be treated as time series provided the original data is a time series; the newdata will be interpreted as related to the subsequent time periods. If FALSE, any time series attributes of the original data will be ignored.

...

Other arguments passed to predict.lm().

Details

forecast.lm is largely a wrapper for predict.lm() except that it allows variables "trend" and "season" which are created on the fly from the time series characteristics of the data. Also, the output is reformatted into a forecast object.

Value

An object of class "forecast".

The function summary is used to obtain and print a summary of the results, while the function plot produces a plot of the forecasts and prediction intervals.

The generic accessor functions fitted.values and residuals extract useful features of the value returned by forecast.lm.

An object of class "forecast" is a list containing at least the following elements:

model

A list containing information about the fitted model

method

The name of the forecasting method as a character string

mean

Point forecasts as a time series

lower

Lower limits for prediction intervals

upper

Upper limits for prediction intervals

level

The confidence values associated with the prediction intervals

x

The historical data for the response variable.

residuals

Residuals from the fitted model. That is x minus fitted values.

fitted

Fitted values

Author(s)

Rob J Hyndman

See Also

tslm, lm.

Examples

y <- ts(rnorm(120,0,3) + 1:120 + 20*sin(2*pi*(1:120)/12), frequency=12)
fit <- tslm(y ~ trend + season)
plot(forecast(fit, h=20))

forecast

Forecasting Functions for Time Series and Linear Models

v8.14
GPL-3
Authors
Rob Hyndman [aut, cre, cph] (<https://orcid.org/0000-0002-2140-5352>), George Athanasopoulos [aut], Christoph Bergmeir [aut] (<https://orcid.org/0000-0002-3665-9021>), Gabriel Caceres [aut], Leanne Chhay [aut], Mitchell O'Hara-Wild [aut] (<https://orcid.org/0000-0001-6729-7695>), Fotios Petropoulos [aut] (<https://orcid.org/0000-0003-3039-4955>), Slava Razbash [aut], Earo Wang [aut], Farah Yasmeen [aut] (<https://orcid.org/0000-0002-1479-5401>), R Core Team [ctb, cph], Ross Ihaka [ctb, cph], Daniel Reid [ctb], David Shaub [ctb], Yuan Tang [ctb] (<https://orcid.org/0000-0001-5243-233X>), Zhenyu Zhou [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.