多窗口范围计算data.table与dplyr

时间:2014-11-11 11:43:18

标签: r data.table dplyr

我在股票回报的多个窗口上进行范围计算(即最大和最小)。 我在dplyr中有我的版本,但很多人发布基准测试,其中使用data.table进行计算要快得多。我已经使用data.table语法创建了版本,但它比dplyr语法慢。 任何人都可以帮我找到更好的方法来使用data.table来加快速度吗? 非常感谢。

library(Quandl)
library(tidyr)
library(dplyr)
library(data.table)
library(microbenchmark)

tickers <- c("GOOG/NASDAQ_AAPL", "GOOG/NASDAQ_MSFT", 
             "GOOG/NYSE_IBM", "GOOG/NASDAQ_GOOG") 

data <- Quandl(tickers,transformation = "rdiff")

returns <- gather(data, stock, value, -Date) %>%
    separate(stock, c("name", "field"), " - ") %>%
    filter(
       field == "Close"
    ) %>%
    select(
       - field
    )

returns_dt <- data.table(returns)

multi_window_range <- function(data) {
    result_1y <- data %>%
        filter(
            Date >= Sys.Date() - 365
        ) %>% 
        group_by(name) %>%
        summarise(
            max_1y = max(value, na.rm = TRUE),
            min_1y = min(value, na.rm = TRUE)
        )
    result_2y <- data %>%
        filter(
            Date >= Sys.Date() - 365 * 2
        ) %>%
        group_by(name) %>%
        summarise(
            max_2y = max(value, na.rm = TRUE),
           min_2y = min(value, na.rm = TRUE)
       )
    result_5y <- data %>%
        filter(
            Date >= Sys.Date() - 365 * 5
        ) %>%
        group_by(name) %>%
        summarise(
            max_5y = max(value, na.rm = TRUE),
            min_5y = min(value, na.rm = TRUE)
        )
    return(inner_join(inner_join(result_1y, result_2y, by = "name"), result_5y, by = "name"))
}

multi_window_range_dt <- function(data) {
    setkey(data, name)
    result_1y <- data[Date >= Sys.Date() - 365,
                      list(
                        max_1y = max(value, na.rm = TRUE),
                        min_1y = min(value, na.rm = TRUE)
                      ), by = "name"]
   result_2y <- data[Date >= Sys.Date() - 365 * 2,
                     list(
                        max_2y = max(value, na.rm = TRUE),
                        min_2y = min(value, na.rm = TRUE)
                     ), by = "name"]
   result_5y <- data[Date >= Sys.Date() - 365 * 5,
                     list(
                        max_5y = max(value, na.rm = TRUE),
                        min_5y = min(value, na.rm = TRUE)
                     ), by = "name"]
   return(result_1y[result_2y][result_5y])
}

microbenchmark(
    multi_window_range(returns),
    multi_window_range_dt(returns_dt)
)


Unit: milliseconds
                              expr      min       lq     mean   median       uq      max neval
       multi_window_range(returns) 6.341532 6.522303 6.915266 6.692666 6.922623 10.16709   100
 multi_window_range_dt(returns_dt) 7.537073 7.738516 8.066579 7.865968 8.073114 12.68021   100 

1 个答案:

答案 0 :(得分:1)

试试这个:

multi_window_range_dt2 <- function(data) {
       data[, {
        rng1 <- range(value[Date > Sys.Date() - 365], na.rm = TRUE)
        rng2 <- range(value[Date > Sys.Date() - 2*365], na.rm = TRUE)
        rng5 <- range(value[Date > Sys.Date() - 5*365], na.rm = TRUE)
        list(max_1y = rng1[2], min_1y = rng1[1],
             max_2y = rng2[2], min_2y = rng2[1],
             max_5y = rng5[2], min_5y = rng5[1])
       }, by = "name"]
}

library(rbenchmark)
benchmark(multi_window_range(returns), multi_window_range_dt2(returns_dt))[1:4]

在我的笔记本电脑上给出了这个:

                                test  replications elapsed relative
1        multi_window_range(returns)           100    2.39    1.189
2 multi_window_range_dt2(returns_dt)           100    2.01    1.000

这表明multi_window_range的时间比multi_window_range_dt2多出18.9%: