Create a lag matrix for a time series (or suite of time series) using given E and tau values. Like in fitGP, either a data frame and column names or vectors/matrices can be provided. Use forecast=TRUE to generate a forecast matrix. Use vtimestep=TRUE to use the variable timestep method.

makelags(
  data = NULL,
  y,
  pop = NULL,
  E,
  tau,
  yname = NULL,
  forecast = FALSE,
  vtimestep = FALSE,
  x = NULL,
  time = NULL,
  augment = FALSE,
  Tdiff_max = NULL,
  Tdiff_fore = NULL,
  nreps = NULL,
  append = FALSE
)

Arguments

data

A data frame, or matrix with named columns.

y

A vector containing a time series (usually the response variable). Alternatively, a matrix or data frame where each column is a time series (usually the response variable and covariates). In the case of multiple time series, lags will be generated for each variable. If data is supplied, the column names or indices.

pop

A vector of populations (optional, if not supplied defaults to 1 population). If data is supplied, the column name or index.

E

Embedding dimension. Required.

tau

Time delay. Required.

yname

Optional, name of the variable if y is input as a vector, or variables if y is a matrix with unnamed columns. Otherwise not used. Defaults to "var" if NULL.

forecast

Produce a forecast matrix instead of the standard training/test matrix.

vtimestep

Use variable timestep method.

x

If using vtimestep=TRUE, put the response variable under use y, and covariates under x (see details). If vtimestep=FALSE, will be appended to y.

time

Used to generate forecast time if forecast=TRUE and to calculate Tdiff if vtimestep=TRUE. If not supplied and vtimestep=TRUE, a numeric index is used.

augment

If vtimestep=TRUE, produce augmentation lag matrix.

Tdiff_max

If vtimestep=TRUE, the maximum Tdiff value to allow in production of the lag matrix or augmentation lag matrix.

Tdiff_fore

If vtimestep=TRUE and forecast=TRUE, vector of time units to forecast beyond the last timestep. Defaults to the minimum time difference between consecutive timepoints, multipled by tau.

nreps

If augment=TRUE, the max number of delay vectors for each Tdiff value.

append

Return data with the lags appended (defaults to FALSE). Only relevant if forecast=FALSE and augment=FALSE.

Value

A matrix with named columns, the appended number indicating the time lag. If y has named columns, named columns in the lag matrix will match. If generating forecasts, the output matrix will include a column for population if there is more than one, and will include a time column if time is supplied. If using the variable timestep method, the output matrix will include lags of Tdiff (time difference).

Details

When using the standard (fixed timestep) method, the response variable and covariates can all be input under y, and lags will be generated for all variables.

When using the variable timestep method, it is necessary to differentiate between the response variable and covariates, as they are handled differently. The response variable should go under y and the covariates should go under x. The output matrix will include lags of Tdiff (time difference).

An augmentation matrix for use with the variable timestep method can be generated by setting vtimestep=TRUE and augment=TRUE. Under default behavior, the augmentation matrix will include only the Tdiff combinations observed in the original vtimestep matrix, up to nreps. If you supply a vector Tdiff_fore, then the augmentation matrix will include or all possible combinations of the Tdiff values supplied in Tdiff_fore, even if they weren't in the original vtimestep matrix, up to nreps.

If generating forecasts, the output matrix will include a column for population if there is more than one, and will include a time column if time is supplied. The time increment is based on the minimum difference between timepoints. If generating forecasts using the variable timestep method, a vector of time units to forecast beyond the last timestep can be provided under Tdiff_fore.

Examples

set.seed(1)
yrand <- rnorm(20)
site <- rep(c("a","b"),each=10)
dfrand <- data.frame(firstvar=rnorm(20),secondvar=rnorm(20))
makelags(y=yrand,E=3,tau=1)
#>             var_1       var_2       var_3
#>  [1,]          NA          NA          NA
#>  [2,] -0.62645381          NA          NA
#>  [3,]  0.18364332 -0.62645381          NA
#>  [4,] -0.83562861  0.18364332 -0.62645381
#>  [5,]  1.59528080 -0.83562861  0.18364332
#>  [6,]  0.32950777  1.59528080 -0.83562861
#>  [7,] -0.82046838  0.32950777  1.59528080
#>  [8,]  0.48742905 -0.82046838  0.32950777
#>  [9,]  0.73832471  0.48742905 -0.82046838
#> [10,]  0.57578135  0.73832471  0.48742905
#> [11,] -0.30538839  0.57578135  0.73832471
#> [12,]  1.51178117 -0.30538839  0.57578135
#> [13,]  0.38984324  1.51178117 -0.30538839
#> [14,] -0.62124058  0.38984324  1.51178117
#> [15,] -2.21469989 -0.62124058  0.38984324
#> [16,]  1.12493092 -2.21469989 -0.62124058
#> [17,] -0.04493361  1.12493092 -2.21469989
#> [18,] -0.01619026 -0.04493361  1.12493092
#> [19,]  0.94383621 -0.01619026 -0.04493361
#> [20,]  0.82122120  0.94383621 -0.01619026
makelags(y=dfrand,E=2,tau=2)
#>        firstvar_2  firstvar_4 secondvar_2 secondvar_4
#>  [1,]          NA          NA          NA          NA
#>  [2,]          NA          NA          NA          NA
#>  [3,]  0.91897737          NA  -0.1645236          NA
#>  [4,]  0.78213630          NA  -0.2533617          NA
#>  [5,]  0.07456498  0.91897737   0.6969634  -0.1645236
#>  [6,] -1.98935170  0.78213630   0.5566632  -0.2533617
#>  [7,]  0.61982575  0.07456498  -0.6887557   0.6969634
#>  [8,] -0.05612874 -1.98935170  -0.7074952   0.5566632
#>  [9,] -0.15579551  0.61982575   0.3645820  -0.6887557
#> [10,] -1.47075238 -0.05612874   0.7685329  -0.7074952
#> [11,] -0.47815006 -0.15579551  -0.1123462   0.3645820
#> [12,]  0.41794156 -1.47075238   0.8811077   0.7685329
#> [13,]  1.35867955 -0.47815006   0.3981059  -0.1123462
#> [14,] -0.10278773  0.41794156  -0.6120264   0.8811077
#> [15,]  0.38767161  1.35867955   0.3411197   0.3981059
#> [16,] -0.05380504 -0.10278773  -1.1293631  -0.6120264
#> [17,] -1.37705956  0.38767161   1.4330237   0.3411197
#> [18,] -0.41499456 -0.05380504   1.9803999  -1.1293631
#> [19,] -0.39428995 -1.37705956  -0.3672215   1.4330237
#> [20,] -0.05931340 -0.41499456  -1.0441346   1.9803999
makelags(y=dfrand,pop=site,E=2,tau=1)
#>        firstvar_1  firstvar_2 secondvar_1 secondvar_2
#>  [1,]          NA          NA          NA          NA
#>  [2,]  0.91897737          NA  -0.1645236          NA
#>  [3,]  0.78213630  0.91897737  -0.2533617  -0.1645236
#>  [4,]  0.07456498  0.78213630   0.6969634  -0.2533617
#>  [5,] -1.98935170  0.07456498   0.5566632   0.6969634
#>  [6,]  0.61982575 -1.98935170  -0.6887557   0.5566632
#>  [7,] -0.05612874  0.61982575  -0.7074952  -0.6887557
#>  [8,] -0.15579551 -0.05612874   0.3645820  -0.7074952
#>  [9,] -1.47075238 -0.15579551   0.7685329   0.3645820
#> [10,] -0.47815006 -1.47075238  -0.1123462   0.7685329
#> [11,]          NA          NA          NA          NA
#> [12,]  1.35867955          NA   0.3981059          NA
#> [13,] -0.10278773  1.35867955  -0.6120264   0.3981059
#> [14,]  0.38767161 -0.10278773   0.3411197  -0.6120264
#> [15,] -0.05380504  0.38767161  -1.1293631   0.3411197
#> [16,] -1.37705956 -0.05380504   1.4330237  -1.1293631
#> [17,] -0.41499456 -1.37705956   1.9803999   1.4330237
#> [18,] -0.39428995 -0.41499456  -0.3672215   1.9803999
#> [19,] -0.05931340 -0.39428995  -1.0441346  -0.3672215
#> [20,]  1.10002537 -0.05931340   0.5697196  -1.0441346
makelags(y=yrand,pop=site,E=2,tau=3,forecast = TRUE, yname="SomeName",time=c(1:10,1:10))
#>   time pop SomeName_3  SomeName_6
#> 1   11   a  0.7383247  0.32950777
#> 2   12   a  0.5757814 -0.82046838
#> 3   13   a -0.3053884  0.48742905
#> 4   11   b  0.9438362  1.12493092
#> 5   12   b  0.8212212 -0.04493361
#> 6   13   b  0.5939013 -0.01619026
dfrand2 <- cbind.data.frame(Time=c(1:10,1:10),Site=site,dfrand)
makelags(data=dfrand2, y=c("firstvar","secondvar"),pop="Site",E=2,tau=3)
#>        firstvar_3  firstvar_6 secondvar_3 secondvar_6
#>  [1,]          NA          NA          NA          NA
#>  [2,]          NA          NA          NA          NA
#>  [3,]          NA          NA          NA          NA
#>  [4,]  0.91897737          NA  -0.1645236          NA
#>  [5,]  0.78213630          NA  -0.2533617          NA
#>  [6,]  0.07456498          NA   0.6969634          NA
#>  [7,] -1.98935170  0.91897737   0.5566632  -0.1645236
#>  [8,]  0.61982575  0.78213630  -0.6887557  -0.2533617
#>  [9,] -0.05612874  0.07456498  -0.7074952   0.6969634
#> [10,] -0.15579551 -1.98935170   0.3645820   0.5566632
#> [11,]          NA          NA          NA          NA
#> [12,]          NA          NA          NA          NA
#> [13,]          NA          NA          NA          NA
#> [14,]  1.35867955          NA   0.3981059          NA
#> [15,] -0.10278773          NA  -0.6120264          NA
#> [16,]  0.38767161          NA   0.3411197          NA
#> [17,] -0.05380504  1.35867955  -1.1293631   0.3981059
#> [18,] -1.37705956 -0.10278773   1.4330237  -0.6120264
#> [19,] -0.41499456  0.38767161   1.9803999   0.3411197
#> [20,] -0.39428995 -0.05380504  -0.3672215  -1.1293631
makelags(data=dfrand2, y=c("firstvar","secondvar"),pop="Site",E=2,tau=3,
forecast = TRUE,time="Time")
#>   Time Site firstvar_3  firstvar_6 secondvar_3 secondvar_6
#> 1   11    a -1.4707524  0.61982575   0.7685329  -0.6887557
#> 2   12    a -0.4781501 -0.05612874  -0.1123462  -0.7074952
#> 3   13    a  0.4179416 -0.15579551   0.8811077   0.3645820
#> 4   11    b -0.0593134 -1.37705956  -1.0441346   1.4330237
#> 5   12    b  1.1000254 -0.41499456   0.5697196   1.9803999
#> 6   13    b  0.7631757 -0.39428995  -0.1350546  -0.3672215