Learn R Programming

dLagM (version 1.0.2)

dlmForecast: Compute forecasts for finite distributed lag model

Description

A function that computes forecasts for finite distributed lag model with one or multiple predictor(s).

Usage

dlmForecast(model , x , h = 1 , interval = TRUE , alpha = 0.05 , nSim = 500)

Arguments

model

An object of class lm including the fitted model with dlm() function.

x

A vector or matrix including the new observations of independent time series. This is not restricted to ts objects. See the details for specification of the values of x.

h

The number of ahead forecasts.

interval

If TRUE, \((1-\alpha)\%\) confidence intervals for forecasts are displayed along with forecasts.

alpha

Significance level of confidence interval.

nSim

An integer showing the number of Monte Carlo simulations used to compute confidence intervals for forecasts.

Value

forecasts

A vector including forecasts.

Details

This function directly uses the model formula and estimates of model coefficients to find forecasts one-by-one starting from the one-step ahead forecast.

Confidence intervals are found by the Monte Carlo approach using a Gaussian error distribution with zero mean and empirical variance of the dependent series.

When the model argument includes multiple independent series, x must be entered as a matrix including the new observations of each independent series in its rows. The number of columns of x must be equal to the forecast horizon h and the rows of x are matched with the independent series in the order they appear in the data.

This function can still be used when some of the lags of independent series are removed from the model.

Examples

Run this code
# NOT RUN {
# Only one independent series
data(warming)
model.dlm = dlm(x = warming$NoMotorVehicles , 
y = warming$Warming , q = 2 ,  show.summary = TRUE)
dlmForecast(model = model.dlm , x = c(95 , 98, 101) , h = 3)


# Multiple independent series
data(M1Germany)
data = M1Germany[1:144,]
model.dlm  = dlm(formula = logprice ~ interest + logm1, 
          data = data.frame(data) , q = 4 , show.summary = TRUE)

x.new =  matrix(c(0.07 , 9.06 , 0.071 , 9.09), 
                ncol = 2, nrow = 2)
dlmForecast(model = model.dlm , x = x.new , h = 2 , 
            interval = FALSE)
dlmForecast(model = model.dlm , x = x.new , h = 2 , 
            interval = TRUE, nSim = 100)

# Some lags are removed:
# Remove lag 2 from "interest" and 
# lags 1 and 3 from "logm1"
removed = matrix(c(2 , NA , 1 , 3), nrow = 2, ncol = 2 , 
                byrow = TRUE)
model.dlm  = dlm(formula = logprice ~ interest + logm1 , 
              data = data.frame(data) , q = 4 , remove = removed ,
              show.summary = TRUE)

x.new =  matrix(c(0.07 , 9.06 , 0.071 , 9.09), 
                ncol = 2, nrow = 2)
dlmForecast(model = model.dlm , x = x.new , h = 2 , 
            interval = FALSE)
dlmForecast(model = model.dlm , x = x.new , h = 2 , 
            interval = TRUE, nSim = 100)
  
x.new =  matrix(c(0.07 , 9.06 , 0.071 , 9.09, 0.08 , 9.12), ncol = 3, 
                nrow = 2)
dlmForecast(model = model.dlm , x = x.new , h = 3,  interval = FALSE)
dlmForecast(model = model.dlm , x = x.new , h = 3,  interval = TRUE, 
            nSim = 100)

# }

Run the code above in your browser using DataLab