Embed time series

embed_reg(
  x,
  y,
  lags = 1,
  encoding = c("None", "model_matrix", "target_based"),
  testing = FALSE
)

Arguments

model_matrix

Examples


x <- 1:10
set.seed(2020)
df <- data.frame(x1 = x,
                 x2 = c("a", "b", "c", "b", "a",
                        "a", "a", "c", "b", "c"),
                 x3=sort(rnorm(10)))
df$x2 <- as.factor(df$x2)

lags <- 3L

cat("1 - data -----")
#> 1 - data -----
cat("\n")
#> 
print(df)
#>    x1 x2         x3
#> 1   1  a -2.7965343
#> 2   2  b -1.1304059
#> 3   3  c -1.0980232
#> 4   4  b -0.2293777
#> 5   5  a  0.1173668
#> 6   6  a  0.3015484
#> 7   7  a  0.3769721
#> 8   8  c  0.7205735
#> 9   9  b  0.9391210
#> 10 10  c  1.7591313
cat("\n")
#> 

cat("2 - training (model_matrix==FALSE) -----")
#> 2 - training (model_matrix==FALSE) -----
cat("\n")
#> 
cat("\n")
#> 
print(after::embed_reg(y=df$x1, x=df[, c("x2", "x3")],
                       lags = lags))
#> $y
#> [1]  4  5  6  7  8  9 10
#> 
#> $x
#>   y_l1 y_l2 y_l3 x2_l1 x2_l2 x2_l3      x3_l1      x3_l2      x3_l3
#> 1    3    2    1     c     b     a -1.0980232 -1.1304059 -2.7965343
#> 2    4    3    2     b     c     b -0.2293777 -1.0980232 -1.1304059
#> 3    5    4    3     a     b     c  0.1173668 -0.2293777 -1.0980232
#> 4    6    5    4     a     a     b  0.3015484  0.1173668 -0.2293777
#> 5    7    6    5     a     a     a  0.3769721  0.3015484  0.1173668
#> 6    8    7    6     c     a     a  0.7205735  0.3769721  0.3015484
#> 7    9    8    7     b     c     a  0.9391210  0.7205735  0.3769721
#> 
#> $df
#>    y y_l1 y_l2 y_l3 x2_l1 x2_l2 x2_l3      x3_l1      x3_l2      x3_l3
#> 1  4    3    2    1     c     b     a -1.0980232 -1.1304059 -2.7965343
#> 2  5    4    3    2     b     c     b -0.2293777 -1.0980232 -1.1304059
#> 3  6    5    4    3     a     b     c  0.1173668 -0.2293777 -1.0980232
#> 4  7    6    5    4     a     a     b  0.3015484  0.1173668 -0.2293777
#> 5  8    7    6    5     a     a     a  0.3769721  0.3015484  0.1173668
#> 6  9    8    7    6     c     a     a  0.7205735  0.3769721  0.3015484
#> 7 10    9    8    7     b     c     a  0.9391210  0.7205735  0.3769721
#> 
cat("\n")
#> 

cat("2 - training (model_matrix==TRUE) -----")
#> 2 - training (model_matrix==TRUE) -----
cat("\n")
#> 
cat("\n")
#> 
print(after::embed_reg(y=df$x1, x=df[, c("x2", "x3")],
                       lags = lags, encoding = "model_matrix"))
#> $y
#> [1]  4  5  6  7  8  9 10
#> 
#> $x
#>   y_l1 y_l2 y_l3 x2b_l1 x2b_l2 x2b_l3 x2c_l1 x2c_l2 x2c_l3      x3_l1
#> 1    3    2    1      0      1      0      1      0      0 -1.0980232
#> 2    4    3    2      1      0      1      0      1      0 -0.2293777
#> 3    5    4    3      0      1      0      0      0      1  0.1173668
#> 4    6    5    4      0      0      1      0      0      0  0.3015484
#> 5    7    6    5      0      0      0      0      0      0  0.3769721
#> 6    8    7    6      0      0      0      1      0      0  0.7205735
#> 7    9    8    7      1      0      0      0      1      0  0.9391210
#>        x3_l2      x3_l3
#> 1 -1.1304059 -2.7965343
#> 2 -1.0980232 -1.1304059
#> 3 -0.2293777 -1.0980232
#> 4  0.1173668 -0.2293777
#> 5  0.3015484  0.1173668
#> 6  0.3769721  0.3015484
#> 7  0.7205735  0.3769721
#> 
#> $df
#>    y y_l1 y_l2 y_l3 x2b_l1 x2b_l2 x2b_l3 x2c_l1 x2c_l2 x2c_l3      x3_l1
#> 1  4    3    2    1      0      1      0      1      0      0 -1.0980232
#> 2  5    4    3    2      1      0      1      0      1      0 -0.2293777
#> 3  6    5    4    3      0      1      0      0      0      1  0.1173668
#> 4  7    6    5    4      0      0      1      0      0      0  0.3015484
#> 5  8    7    6    5      0      0      0      0      0      0  0.3769721
#> 6  9    8    7    6      0      0      0      1      0      0  0.7205735
#> 7 10    9    8    7      1      0      0      0      1      0  0.9391210
#>        x3_l2      x3_l3
#> 1 -1.1304059 -2.7965343
#> 2 -1.0980232 -1.1304059
#> 3 -0.2293777 -1.0980232
#> 4  0.1173668 -0.2293777
#> 5  0.3015484  0.1173668
#> 6  0.3769721  0.3015484
#> 7  0.7205735  0.3769721
#> 
cat("\n")
#> 

cat("3 - training target-based -----")
#> 3 - training target-based -----
cat("\n")
#> 
cat("\n")
#> 
print(after::embed_reg(y=df$x1, x=df[, c("x2", "x3")],
                       lags = lags, encoding = "target_based"))
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================================================| 100%$y
#> [1]  4  5  6  7  8  9 10
#> 
#> $x
#>   y_l1 y_l2 y_l3    x2_l1    x2_l2    x2_l3      x3_l1      x3_l2      x3_l3
#> 1    3    2    1 20.84799 14.15348 20.74479 -1.0980232 -1.1304059 -2.7965343
#> 2    4    3    2 14.15348 20.84799 14.15348 -0.2293777 -1.0980232 -1.1304059
#> 3    5    4    3 20.74479 14.15348 20.84799  0.1173668 -0.2293777 -1.0980232
#> 4    6    5    4 20.74479 20.74479 14.15348  0.3015484  0.1173668 -0.2293777
#> 5    7    6    5 20.74479 20.74479 20.74479  0.3769721  0.3015484  0.1173668
#> 6    8    7    6 20.84799 20.74479 20.74479  0.7205735  0.3769721  0.3015484
#> 7    9    8    7 14.15348 20.84799 20.74479  0.9391210  0.7205735  0.3769721
#> 
#> $df
#>    y y_l1 y_l2 y_l3    x2_l1    x2_l2    x2_l3      x3_l1      x3_l2      x3_l3
#> 1  4    3    2    1 20.84799 14.15348 20.74479 -1.0980232 -1.1304059 -2.7965343
#> 2  5    4    3    2 14.15348 20.84799 14.15348 -0.2293777 -1.0980232 -1.1304059
#> 3  6    5    4    3 20.74479 14.15348 20.84799  0.1173668 -0.2293777 -1.0980232
#> 4  7    6    5    4 20.74479 20.74479 14.15348  0.3015484  0.1173668 -0.2293777
#> 5  8    7    6    5 20.74479 20.74479 20.74479  0.3769721  0.3015484  0.1173668
#> 6  9    8    7    6 20.84799 20.74479 20.74479  0.7205735  0.3769721  0.3015484
#> 7 10    9    8    7 14.15348 20.84799 20.74479  0.9391210  0.7205735  0.3769721
#> 
#> $codes
#> $codes$x2
#> $codes$x2$a
#> [1] 20.74479
#> 
#> $codes$x2$b
#> [1] 14.15348
#> 
#> $codes$x2$c
#> [1] 20.84799
#> 
#> 
#> 
cat("\n")
#>