Hi All,
The dataset I have is a weekly data with one dependent and Independent variable with One grouping Variable(which is the Key variable).
I'm able to get the cross-correlation values between Dep. & Ind. Variables based on the grouping variable using the CCF function.
But for some reason when I try to plot the CCF values i get the below error
Error: Can't determine column value
: Dependent.Variable
, Independent.Variable
.
I have used lag.2 plot function from the astsa package for plotting the CCF values.
Please advise where I'm doing it wrong
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`) %>%
lag2.plot(Independent.Variable,Dependent.Variable)
Thank you