lagTrim

Purpose

Lags (or leads) a vector a specified number of time periods and removes the incomplete rows.

Format

y = lagTrim(y, t)
Parameters:
  • y (Nx1 vector) – data
  • t (scalar or Px1 vector) – number of time periods.
Returns:

y (NxP matrix) – y lagged t periods.

Examples

Single lag

// Specify number of lags
nlags = 2;

// Define y matrix
y = { 1.4, 2.7, 3.1, 2.9, 3.2, 2.5, 2.8 };

// Lag y nlags number of lags
// and trim missing values
y_lag = lagTrim(y, nlags);

will assign y_lag to equal:

1.4
2.7
3.1
2.9
3.2

Multiple lags

If the number of time periods to lag is a Px1 column vector, then the output matrix with be an NxP matrix where each column contains one of the lags. For example, changing the nlags variable from the example above to be a 3x1 column vector like this:

// Specify to compute the 1, 2, and 3 lags
nlags = { 1, 2, 3 };

// Define y vector
y = { 1.4, 2.7, 3.1, 2.9, 3.2, 2.5, 2.8 };

// Compute the 1, 2, and 3 lags of y
// and trim missing values
y_lag = lagTrim(y, nlags);

will assign lag_mat to equal:

3.1      2.7      1.4
2.9      3.1      2.7
3.2      2.9      3.1
2.5      3.2      2.9

Multiple leads

If the number of time periods to lag is a Px1 column vector, then the output matrix with be an NxP matrix where each column contains one of the lags. For example, changing the nlags variable from the example above to be a 3x1 column vector like this:

// Specify nlags to find
// the 1st and 2nd lead
// and 3rd lag of y
nlags = { -1, -2, 3 };

// Define y vector
y = { 1.4, 2.7, 3.1, 2.9, 3.2, 2.5, 2.8 };

// Lag y using nlags
y_lag = lagTrim(y, nlags);

will assign lag_mat to equal:

3.2      2.5      1.4
2.5      2.8      2.7

Remarks

  • If t is positive, lagTrim() lags y back t time periods, so the first maxc(t) observations of y are removed.

  • If t is negative, lagTrim() leads y forward t time periods, so the last maxc(t) observations of y are removed.

  • lagn() is similar to lagTrim(), but lagn():

    • Fills the first t rows of each column with missing values.
    • Uses more memory and is slower than lagTrim().

See also

Functions lagn()