bsLag: Lagged Time Series

View source: R/bsLag.r

bsLagR Documentation

Lagged Time Series

Description

Generate a set of lagged time series for time series data.

Usage

bsLag(h, lag, prefix = "", var.name, suffix = ".t_", 
    include.orig = TRUE, by.lag = FALSE, ...)

Arguments

h

time series data

lag

number of lags

prefix

prefix for the name of lagged time series.

var.name

varible name of the lagged time series.

suffix

suffix of the name of lagged time series.

include.orig

logical value (default of TRUE) of whether to include the original series (i.e., lag zero) in the final output.

by.lag

logical value (default of FALSE) of whether to order the column by variable (FALSE) or by lag (TRUE).

...

additional arguments to be passed.

Details

The input data can be a single time series or a set of multiple time series data. The output is a set of lagged time series with the specified lag dimension. All the series are aligned with the shortest window so the loss of observations is equal to lag. The original series (e.g., without lag but just loss of beginning observations) can be included or excluded by setting the logical value of include.org.

The name of the output data is composed of four parts: prefix, var.name, suffix, and an index number of lag. Users can control the first three parts only because the lag number is added automatically. prefix and suffix can be fixed for all the output series. var.name provides some flexibility when bsLag is used within a function and the variable name is unknown a priori.

The column of the output can be ordered either by the variable name (e.g., diff.GA.t_0, diff.GA.t_1, diff.ND.t_0, diff.ND.t_1), or by the lag order ((e.g., diff.GA.t_0, diff.ND.t_0, diff.GA.t_1, diff.ND.t_1).

Value

Return a multiple time series object.

Author(s)

Changyou Sun (edwinsun258@gmail.com)

Examples

# simple example
h1 <- ts(data=cbind(1:24), start=c(2001, 1), frequency=12)
h2 <- ts(data=cbind(1:24, 25:48), start=c(2001, 1), frequency=12)
h3 <- ts(data=cbind(1:4, 5:8, 9:12), start=c(2001, 1), frequency=4)
colnames(h2) <- c("aa", "bb")
colnames(h3) <- c("cc", "dd", "ee")
h1; h2; h3

bsLag(h=h1, lag=0, prefix="", suffix=".t_")
bsLag(h=h1, lag=2, prefix="price.", var.name="fl", suffix=".t_")
bsLag(h=h1, lag=2, prefix="price.", var.name="fl", suffix=".t_", by.lag=TRUE)
bsLag(h=h1, lag=23, prefix="price.", suffix=".t_", include.orig=FALSE)

bsLag(h=h2, lag=4, prefix="", suffix=".t_", include.orig = TRUE)
bsLag(h=h2, lag=4, prefix="", suffix=".t_", include.orig = FALSE)
bsLag(h=h2, lag=4, prefix="", suffix=".t_", include.orig = FALSE, by.lag=TRUE)
bsLag(h=h2, lag=0, prefix="", var.name=c("nc", "sc"), suffix=".t_")

bsLag(h=h3, lag=2, prefix="", suffix=".t_", include.orig=FALSE)
bsLag(h=h3, lag=1, prefix="", var.name=c("nd", "sd", "mi"), suffix=".lag.")
bsLag(h=h3, lag=2, prefix="NY.", suffix=".t_", by.lag=TRUE)
bsLag(h=h3, lag=3, prefix="NY.", suffix=".t_", include.orig=FALSE)

# with real data
data(daBedRaw)
small <- daBedRaw[, c("vCN", "qCN")]
(lag.small <- bsLag(h=small, lag=4))
colnames(lag.small)

resid <- residuals(lm(qCN ~ vCN, data = small))
res <- ts(resid, start=start(small), end=end(small),
    frequency=tsp(small)[3])
lag.res <- bsLag(h=res, lag=2, prefix="resid.", var.name="china")
str(lag.res)
headts(lag.res)
tailts(lag.res)

erer documentation built on Sept. 26, 2024, 5:06 p.m.