滞后时间序列数据 [英] Lagging time series data
问题描述
我一直在寻找一种用于预测的神经网络模型.我正在尝试使数据具有图片中显示的格式,以便模型可以根据给定的先前值(2天前,1天前,今天)进行预测.这些值将在第二天针对下一个预测进行调整,例如如图中所示,从第一个输入开始的1天前变为在第二个天的2天前.我使用滞后函数滞后时间序列的数据,并使用矩阵以这种格式设置数据,但是我对此感到非常困惑和困惑.如何使用滞后函数将图像中所示的数据滞后?
I was looking to build a Neural network model for prediction. I am trying to get my data to be in the format that is shown in the image so the model can predict given the previous values of (2 days ago, 1 day ago, today). These values will be adjusted the next day for the next prediction e.g. 1 day ago from first input becomes 2 days ago in the second as seen in the image. I am using the lag function to lag the data for time series and a matrix to set up the data in this format but I am quite confused and struggling with this for sometime. How could I lag the data as shown in the image using the lag function?
我当前的代码:
data <-
structure(
list(
`USD/EUR` = c(
1.373,
1.386,
1.3768,
1.3718,
1.3774,
1.3672,
1.3872,
1.3932,
1.3911,
1.3838,
1.4171,
1.4164,
1.3947,
1.3675,
1.3801,
1.3744,
1.3759,
1.3743,
1.3787,
1.3595,
1.3599,
1.3624,
1.3523,
1.3506,
1.3521
)
),
row.names = c(NA,-25L),
class = c("tbl_df",
"tbl", "data.frame")
)
#Lag the data
lagData <- c(lag(data$`USD/EUR`,k = 1))
lagData
#store data into matrix to feed to neural net
matrixForm <- matrix(lagData, nrow = 25, ncol = 4, byrow = TRUE)
matrixForm
推荐答案
1)使用嵌入.不使用任何软件包.
1) Use embed. No packages are used.
embed(data[[1]], 4)[, 4:1]
给出此矩阵:
[,1] [,2] [,3] [,4]
[1,] 1.3730 1.3860 1.3768 1.3718
[2,] 1.3860 1.3768 1.3718 1.3774
[3,] 1.3768 1.3718 1.3774 1.3672
[4,] 1.3718 1.3774 1.3672 1.3872
...snip...
2)另一种可能性是折叠包中的标志(快速滞后):
2) Another possibility is flag (fast lag) in the collapse package:
na_omit(flag(data[[1]], 3:0))
给予:
L3 L2 L1 --
[1,] 1.3730 1.3860 1.3768 1.3718
[2,] 1.3860 1.3768 1.3718 1.3774
[3,] 1.3768 1.3718 1.3774 1.3672
[4,] 1.3718 1.3774 1.3672 1.3872
...snip...
3)动物园以与标记类似的方式支持多个延迟,但它使用与R中相同的方向.确保dplyr未加载 ,因为它会覆盖延迟
3) zoo supports multiple lags in a similar manner to flag except it uses the same orientation as in R. Be sure that dplyr is not loaded since it overwrites lag.
library(zoo)
na.omit(lag(zoo(data[[1]]), -3:0))
给予该动物园对象:
lag-3 lag-2 lag-1 lag0
4 1.3730 1.3860 1.3768 1.3718
5 1.3860 1.3768 1.3718 1.3774
6 1.3768 1.3718 1.3774 1.3672
7 1.3718 1.3774 1.3672 1.3872
...snip...
这篇关于滞后时间序列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!