Hi,
This is two part query regarding the Correlation plot
Note- I use astsa package to generate cross correlation plot for the two variable series. The two series data has multiple groups
- Is it possible to specify a lag value in the lag2.plot function because the positive lag values are not available on the plot if I use lag2.plot
- My data has two series but it has been grouped by one Key variable which has multiple groups- Is it possible to generate a CCF plot for multiple groups in a single shot rather than generating it for one group at a time
Attached my reproducible code
library(tidyverse)
library(lubridate)
library(tibble)
library(tsibble)
library(norm)
library(fpp3)
library(norm)
library(ISOweek)
library(tseries)
library(astsa)
Df<-structure(list(Week = c("201901", "201901", "201901", "201902",
"201902", "201902", "201903", "201903", "201903", "201904", "201904",
"201904", "201905", "201905", "201905", "201906", "201906", "201906",
"201907", "201907", "201907", "201908", "201908", "201908", "201909",
"201909", "201909", "201910", "201910", "201910", "201911", "201911",
"201911", "201912", "201912", "201912", "201913", "201913", "201913"
), Key = c("A", "B", "C", "A", "B", "C", "A", "B", "C", "A",
"B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B",
"C", "A", "B", "C", "A", "B", "C", "A", "B", "C", "A", "B", "C",
"A", "B", "C"), Dependent.Variable = c(19, 11, 11, 98, 127, 44,
135, 148, 34, 112, 108, 39, 77, 141, 50, 89, 151, 41, 86, 120,
70, 149, 102, 62, 89, 94, 60, 146, 128, 66, 91, 119, 106, 160,
132, 56, 143, 152, 75), Independent.Variable = c(794, 794, 794,
1400, 1400, 1400, 1505, 1505, 1505, 1055, 1055, 1055, 1396, 1396,
1396, 1331, 1331, 1331, 1461, 1461, 1461, 1623, 1623, 1623, 1513,
1513, 1513, 1667, 1667, 1667, 1737, 1737, 1737, 1264, 1264, 1264,
1722, 1722, 1722)), row.names = c(NA, 39L), class = "data.frame")
#Converting to Week format
Df <- Df %>%
mutate(
isoweek =stringr::str_replace(Week, "^(\\d{4})(\\d{2})$", "\\1-W\\2-1"),
date = ISOweek::ISOweek2date(isoweek)
)
Df <- Df %>%
mutate(Week.1 = yearweek(ISOweek::ISOweek(date))) %>%
group_by(`Key`,Week.1) %>%
dplyr::select(-Week,-date,-isoweek) %>%
as_tsibble(key = `Key`,index = Week.1)
has_gaps(Df,.full = TRUE)
#Generating Cross-Correlation values
ccfvalues<- Df %>%
CCF(Independent.Variable,Dependent.Variable,type = "correlation")
#Plot the CCF values using astsa package
Plot<-Df %>% group_by(`Key`) %>% filter(Key == 'A') %>%
lag2.plot(Independent.Variable,Dependent.Variable)