tidyverts / feasts

Feature Extraction And Statistics for Time Series
https://feasts.tidyverts.org/
291 stars 23 forks source link

CCF function differ from standard R use #144

Closed espher1987 closed 2 years ago

espher1987 commented 2 years ago

Hi to everyone, i'm new using the feast package but i wonder if this correct, i'm trying to verify a cross correlation so i have:

At this point i define y = e and x = prod

When i set this same variables to stats::ccf and forecasts::Ccf and compare to feasts::CCF

This first two plots give the contrary direction to feasts::CCF, Why this happens?

library(vars)
#> Loading required package: MASS
#> Loading required package: strucchange
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: sandwich
#> Loading required package: urca
#> Loading required package: lmtest
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following object is masked from 'package:zoo':
#> 
#>     index
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, union
library(feasts)
#> Loading required package: fabletools
library(tidyr)
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo

data("Canada")

df <- Canada %>% 
  diff %>% 
  as_tsibble() %>% 
  pivot_wider(id_cols = index,names_from = key,values_from = value)

with(as.data.frame(diff(Canada)),
     ccf(y = ts(e),
         x = ts(prod),lag.max = 4))

with(as.data.frame(diff(Canada)),
     Ccf(y = ts(e),
         x = ts(prod),lag.max = 4))

df %>% 
  CCF(y = e,
      x = prod,lag_max = 4) %>% 
  autoplot()

Created on 2021-11-22 by the reprex package (v2.0.1)

Thanks for this amazing project. I appreciate this!

Info: R version 4.0.4 (2021-02-15) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Debian GNU/Linux 11 (bullseye) feasts_0.2.2
tsibble_1.0.1 fabletools_0.3.1 forecast_8.13

EDIT: I edit this post to use reprex package

mitchelloharawild commented 2 years ago

Thanks for raising this issue - it's a bug. The x-axis is reversed here because x was passed to y and vice-versa, since CCF() uses CCF(y,x) compared to ccf(x,y).

I've now fixed this.

library(vars)
#> Loading required package: MASS
#> Loading required package: strucchange
#> Loading required package: zoo
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: sandwich
#> Loading required package: urca
#> Loading required package: lmtest
library(tidyr)
library(feasts)
#> Loading required package: fabletools

data("Canada")

df <- Canada %>% 
  diff %>% 
  as_tsibble() %>% 
  pivot_wider(id_cols = index,names_from = key,values_from = value)

with(as.data.frame(diff(Canada)),
     ccf(y = ts(e),
         x = ts(prod),lag.max = 4))

df %>% 
  CCF(y = e,
      x = prod,lag_max = 4) %>% 
  autoplot()

Created on 2022-08-25 by the reprex package (v2.0.1)