【问题标题】:CCF vs ccf: What;s the Difference?CCF 与 ccf:有什么区别?
【发布时间】:2021-05-11 13:30:04
【问题描述】:

我试图了解feasts::CCFbase::ccf 之间的区别以及它们在reprex 中产生不同结果的原因(NA 是否与此有关?)

## Data
df <- structure(list(date = structure(c(1590919200, 1590922800, 1590926400, 
1590930000, 1590933600, 1590937200, 1590940800, 1590944400, 1590948000, 
1590951600, 1590955200, 1590958800, 1590962400, 1590966000, 1590969600, 
1590973200, 1590976800, 1590980400, 1590984000, 1590987600, 1590991200, 
1590994800, 1590998400, 1591002000), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), x = c(12.61, 14.2, 13.37, 16.68, 13.35, 11.42, 16.51, 
11.78, 12.18, 13.67, 14.12, 13.2, 11.24, 10.76, 12.93, 16.48, 
20.65, 14.55, NA, NA, NA, NA, NA, NA), y = c(459.07, 496.83, 
511.17, 510.99, 511.22, 511.16, 511.22, 511.08, 511.14, 511.24, 
511.21, 511.03, 511.13, 511.23, 511.1, 511.11, 511.34, 510.98, 
511.18, 509.62, 511.09, 510.89, 505.53, 497.52)), class = "data.frame", row.names = c(NA, 
-24L), spec = structure(list(cols = list(date = structure(list(
    format = ""), class = c("collector_datetime", "collector"
)), cp.sum = structure(list(), class = c("collector_double", 
"collector")), vru_gc_h2s_ppmv = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"))

## Base R
ccf(df$x, df$y, lag.max = 12, plot = TRUE, na.action = na.pass)

## Feasts
library(tsibble)
library(feasts)

df  %>% 
  as_tsibble(index = date) %>%
  fill_gaps() %>% 
  CCF(x, y, lag_max = 12) %>%
  autoplot() 

【问题讨论】:

  • 看起来 ccf(na.action = na.omit, ...)acf 返回与 CCF 相同的值。所以我猜 CCF 默认会省略 NA。
  • feasts:::build_cf 使用 na.contiguous 而不是 na.pass。在您的示例中,df &lt;- na.omit(df) 会给出相同的结果
  • 不要在 cmets 中回答,人们

标签: r time-series tidyverse tsibble


【解决方案1】:

是的,结果取决于 NA 的处理方式。如果使用na.omit 而不是na.passccf 的结果与CCF 相同。

编辑

但是 - 如果您查看 CCF() 的源代码,在 RStudio 中使用 例如 View(CCF),您会发现它使用基址 ccf

compute_ccf <- function(.data, value1, value2, ...) {
  value1 <- enexpr(value1)
  value2 <- enexpr(value2)
  ccf <- ccf(x = eval_tidy(value1, data = .data), y = eval_tidy(value2, 
    data = .data), plot = FALSE, ...)
  lag <- as.numeric(ccf$lag)
  tibble(lag = lag, ccf = as.numeric(ccf$acf))
}

因此,您可以将 CCF() 保存为新函数(使用新名称)并对其进行编辑以将 na.action = na.pass 添加到 ccf 调用中。

ccf_napass <- ccf(df$x, df$y, lag.max = 12, na.action = na.pass, plot = FALSE)
ccf_naomit <- ccf(df$x, df$y, lag.max = 12, na.action = na.omit, plot = FALSE)

CCF_feasts <- df %>% 
  tsibble::as_tsibble(index = date) %>%
  tsibble::fill_gaps() %>% 
  feasts::CCF(x, y, lag_max = 12) %>% 
  dplyr::pull(ccf)

data.frame(ccf_napass = ccf_napass$acf, ccf_naomit = ccf_naomit$acf, CCF_feasts)

     ccf_napass   ccf_naomit   CCF_feasts
1  -0.001339325 -0.010819818 -0.010819818
2   0.031219857  0.005546548  0.005546548
3   0.041227368 -0.005382620 -0.005382620
4  -0.010115336 -0.019916634 -0.019916634
5  -0.091096987 -0.022542801 -0.022542801
6  -0.169062715 -0.017967752 -0.017967752
7  -0.069318178 -0.023075644 -0.023075644
8  -0.007847907 -0.043090132 -0.043090132
9  -0.008233668 -0.064749235 -0.064749235
10 -0.018144372 -0.070301161 -0.070301161
11 -0.002914065 -0.054108236 -0.054108236
12  0.035133485  0.028140497  0.028140497
13  0.133520812  0.118747777  0.118747777
14 -0.011428760 -0.010055235 -0.010055235
15 -0.021989205 -0.019475560 -0.019475560
16 -0.289253595 -0.257125853 -0.257125853
17  0.122887924  0.109172907  0.109172907
18  0.186626990  0.165904850  0.165904850
19 -0.217882808 -0.193637291 -0.193637291
20  0.280088738  0.249009757  0.249009757
21  0.204005890  0.181525595  0.181525595
22  0.036459851  0.032663381  0.032663381
23  0.017956562  0.016224802  0.016224802
24  0.179025309  0.159451149  0.159451149
25  0.418716273  0.372680241  0.372680241

【讨论】:

  • 似乎没有在feasts::CCF() 中使用na.pass 的选项,这正是我想要的
  • 我会用一个可能的解决方案来编辑​​答案。
  • 正确,feasts::CCF() 中的默认 NA 处理是 na.contiguous。感谢您的彻底调查。此行为是为了与forecast::Ccf() 保持一致,默认情况下也使用na.action = na.contiguous。我已更新 ACF/PACF/CCF 以将所有适当的输入传递给 acf()pacf()ccf() - 特别是您现在可以更改 na.action 行为:github.com/tidyverts/feasts/commit/… 我还为改进这些函数的... 行为:github.com/tidyverts/feasts/issues/124
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 2020-12-09
相关资源
最近更新 更多