0

有没有一种有效的方法将 ar 数据框中的行展开到列?这是我遇到的一个反复出现的问题,当我从 SQL 脚本获得的数据应该被解包成几列时。例如,对于我没有 rnn 的时间序列预测,我的目标是使用标准神经网络。我计划事先展平数据,而不是重复使用网络,以便网络接收 t-1、t-2、t-3 等的行。请参阅下面我的精美绘画工作

在此处输入图像描述

基本上,对于每一行,我想在右侧连接 n 之前的行,其中 n 取决于我们想要使用多少之前的时间步来预测当前行中的值。

大多数情况下,我正在寻找一种智能且有效的方法,最好使用 r 中现有的库/函数。我可以用多种语言对其进行编程,但旨在找到一个解决方案。我之前在 Java 中(非常快)和在 r 中(花了 ~forever=1 小时)完成了此操作。

目前我有超过 3000 行和 10 列。例如,如果我想使用 15 个之前的时间步,我们会得到 10+15*10 列。我不知道 15 是否是一个好的选择,因此我需要能够快速测试例如 n = 5、10、15、20、25、50。

编辑

老实说,我是 r 的初学者,因此我寻求帮助,而不是再次为此编写自定义函数。

dput 给出:

structure(list(Date = structure(c(10L, 9L, 8L, 7L, 6L, 5L, 4L, 
3L, 2L, 1L), .Label = c("6/10/2016", "6/13/2016", "6/14/2016", 
"6/15/2016", "6/16/2016", "6/17/2016", "6/20/2016", "6/21/2016", 
"6/22/2016", "6/23/2016"), class = "factor"), Bid = c(5.04, 4.97, 
4.96, 4.93, 4.84, 5.09, 5.05, 4.96, 5.08, 5), Ask = c(5.04, 4.97, 
4.96, 4.94, 4.84, 5.09, 5.06, 4.97, 5.08, 5.01), Opening.price = c(4.98, 
4.97, 4.95, 4.94, 4.92, 5.01, 5.01, 5.01, 4.95, 5.05), High.price = c(5.07, 
4.98, 4.97, 4.99, 4.93, 5.14, 5.06, 5.1, 5.13, 5.09), Low.price = c(4.94, 
4.91, 4.89, 4.92, 4.81, 5.01, 4.94, 4.94, 4.89, 4.97), Closing.price = c(5.04, 
4.97, 4.95, 4.94, 4.86, 5.08, 5.05, 4.94, 5.06, 4.98), Average.price = c(5.02, 
4.96, 4.94, 4.94, 4.87, 5.08, 5.01, 5, 5.01, 5.01), Total.volume = c(18997216L, 
17969939L, 21430529L, 20725035L, 66884495L, 32994371L, 24600829L, 
24439514L, 26540825L, 24756699L), Turnover = c(95382241.29, 89106913.2, 
105823382.96, 102379207.58, 325592595.95, 167697936.93, 123243137.11, 
122189815.88, 133063486.77, 124080799.95), Trades = c(9220L, 
9317L, 10075L, 10230L, 16446L, 13544L, 11888L, 10923L, 11981L, 
9696L)), .Names = c("Date", "Bid", "Ask", "Opening.price", "High.price", 
"Low.price", "Closing.price", "Average.price", "Total.volume", 
"Turnover", "Trades"), class = "data.frame", row.names = c(NA, 
-10L))

当 n = 2 时的结果(在右侧附加 2 个先前的时间步长):

structure(list(Date = structure(c(8L, 7L, 6L, 5L, 4L, 3L, 2L, 
1L), .Label = c("6/14/2016", "6/15/2016", "6/16/2016", "6/17/2016", 
"6/20/2016", "6/21/2016", "6/22/2016", "6/23/2016"), class = "factor"), 
Bid = c(5.04, 4.97, 4.96, 4.93, 4.84, 5.09, 5.05, 4.96), 
Ask = c(5.04, 4.97, 4.96, 4.94, 4.84, 5.09, 5.06, 4.97), 
Opening.price = c(4.98, 4.97, 4.95, 4.94, 4.92, 5.01, 5.01, 
5.01), High.price = c(5.07, 4.98, 4.97, 4.99, 4.93, 5.14, 
5.06, 5.1), Low.price = c(4.94, 4.91, 4.89, 4.92, 4.81, 5.01, 
4.94, 4.94), Closing.price = c(5.04, 4.97, 4.95, 4.94, 4.86, 
5.08, 5.05, 4.94), Average.price = c(5.02, 4.96, 4.94, 4.94, 
4.87, 5.08, 5.01, 5), Total.volume = c(18997216L, 17969939L, 
21430529L, 20725035L, 66884495L, 32994371L, 24600829L, 24439514L
), Turnover = c(95382241.29, 89106913.2, 105823382.96, 102379207.58, 
325592595.95, 167697936.93, 123243137.11, 122189815.88), 
Trades = c(9220L, 9317L, 10075L, 10230L, 16446L, 13544L, 
11888L, 10923L), X1_Bid = c(4.97, 4.96, 4.93, 4.84, 5.09, 
5.05, 4.96, 5.08), X1_Ask = c(4.97, 4.96, 4.94, 4.84, 5.09, 
5.06, 4.97, 5.08), X1_Opening.price = c(4.97, 4.95, 4.94, 
4.92, 5.01, 5.01, 5.01, 4.95), X1_High.price = c(4.98, 4.97, 
4.99, 4.93, 5.14, 5.06, 5.1, 5.13), X1_Low.price = c(4.91, 
4.89, 4.92, 4.81, 5.01, 4.94, 4.94, 4.89), X1_Closing.price = c(4.97, 
4.95, 4.94, 4.86, 5.08, 5.05, 4.94, 5.06), X1_Average.price = c(4.96, 
4.94, 4.94, 4.87, 5.08, 5.01, 5, 5.01), X1_Total.volume = c(17969939L, 
21430529L, 20725035L, 66884495L, 32994371L, 24600829L, 24439514L, 
26540825L), X1_Turnover = c(89106913.2, 105823382.96, 102379207.58, 
325592595.95, 167697936.93, 123243137.11, 122189815.88, 133063486.77
), X1_Trades = c(9317L, 10075L, 10230L, 16446L, 13544L, 11888L, 
10923L, 11981L), X2_Bid = c(4.96, 4.93, 4.84, 5.09, 5.05, 
4.96, 5.08, 5), X2_Ask = c(4.96, 4.94, 4.84, 5.09, 5.06, 
4.97, 5.08, 5.01), X2_Opening.price = c(4.95, 4.94, 4.92, 
5.01, 5.01, 5.01, 4.95, 5.05), X2_High.price = c(4.97, 4.99, 
4.93, 5.14, 5.06, 5.1, 5.13, 5.09), X2_Low.price = c(4.89, 
4.92, 4.81, 5.01, 4.94, 4.94, 4.89, 4.97), X2_Closing.price = c(4.95, 
4.94, 4.86, 5.08, 5.05, 4.94, 5.06, 4.98), X2_Average.price = c(4.94, 
4.94, 4.87, 5.08, 5.01, 5, 5.01, 5.01), X2_Total.volume = c(21430529L, 
20725035L, 66884495L, 32994371L, 24600829L, 24439514L, 26540825L, 
24756699L), X2_Turnover = c(105823382.96, 102379207.58, 325592595.95, 
167697936.93, 123243137.11, 122189815.88, 133063486.77, 124080799.95
), X2_Trades = c(10075L, 10230L, 16446L, 13544L, 11888L, 
10923L, 11981L, 9696L)), .Names = c("Date", "Bid", "Ask", 
"Opening.price", "High.price", "Low.price", "Closing.price", 
"Average.price", "Total.volume", "Turnover", "Trades", "X1_Bid", 
"X1_Ask", "X1_Opening.price", "X1_High.price", "X1_Low.price", 
"X1_Closing.price", "X1_Average.price", "X1_Total.volume",  "X1_Turnover", 
"X1_Trades", "X2_Bid", "X2_Ask", "X2_Opening.price", "X2_High.price", 
"X2_Low.price", "X2_Closing.price", "X2_Average.price", "X2_Total.volume", 
"X2_Turnover", "X2_Trades"), class = "data.frame", row.names = c(NA, 
-8L))

编辑 2

有人对 rnns 以及直接输入前一个时间点时它们与经典网络有何不同有疑问。 rnn vs classic net 问题的第 1 部分解释了我希望如何在我想要预测的数据的右侧为每一行获取 x(t-1), x(t-2)...。

4

1 回答 1

0
# I put your dataframe into x. 
# Then I extract just the numeric columns into x1,
# because operations on matrices tend to be faster 
# than operations on dataframes.
    x1<-as.matrix(x[,-1])
# Now I increase the size to a 3000 row matrix to match your comment
    x2<-x1[sample(1:10,3000,replace=TRUE),]
# record some sizes:
    N<-50; n<-nrow(x2); J<-1:(n-(N-1))
# Create the new numeric matrix:
    z<-x2[J,]; for (i in 1:(N-1)){z<-cbind(z,x2[i+J,])}
#This took an imperceptible length of time (< 1 second) on my MacBook Pro
    dim(z)
    [1] 2951  500
# Note that looping is inelegant at times but here it's just fine.
# appending columns to a matrix is fast because it goes 
# in the order in which matrices are stored.

# You could run your test cases with z[,1:50], z[,1:100] ... 
# to test the right number of past entries
于 2016-06-28T20:01:46.377 回答