# lagTrim#

## Purpose#

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

## Format#

y = lagTrim(y, t)#
Parameters:
• y (Nx1 vector, NxK matrix or dataframe) – data

• t (scalar or Px1 vector) – number of time periods. NOTE: If Y has multiple columns, T must be a scalar.

Returns:

y (NxP matrix) – y lagged t periods.

## Examples#

### Single lag vector#

```// 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 with a vector#

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
```

### Single lag with multi-column dataframe#

```// Get the first 10 observations of 'price' and 'rep78'
// from the auto dataset
auto = loadd(getGAUSSHome() \$+ "examples/auto2.dta");
auto = auto[1:10,"price" "rep78"];

// Specify number of lags
nlags = 2;

// Lag both variables in 'auto',
// nlags number of lags
// and trim missing values
auto_lag = lagTrim(auto, nlags);
```

will assign auto_lag to equal:

```    price            rep78
4099.0000          Average
4749.0000          Average
3799.0000                .
4816.0000          Average
7827.0000             Good
5788.0000          Average
4453.0000                .
5189.0000          Average
```

## Remarks#

See also

Functions `lagn()`