我拥有的数据集是每周有一个因变量的数据和一个分组变量的独立变量(这是关键变量)。我能得到Dep之间的互相关值。& Ind.使用CCF函数基于分组变量的变量。但是,由于某些原因,当我试图绘制CCF值时,我会得到以下错误:无法确定列value:Dependent.Variable,Independent.Variable。我使用了astsa包中的lag.2 2绘图函数来绘制CCF值。请告诉我哪里做错了
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)谢谢
发布于 2022-02-20 02:06:11
dep = Df$Dependent.Variable
ind = Df$Independent.Variable
# first variable gets lagged (reverse to go other way)
# try the gris-gris version
lag2.plot(ind, dep, 8, pch=Df$Key, cex=1.2, col=2:4, las=0, gg=TRUE, bgl ='transparent' )https://stackoverflow.com/questions/70766939
复制相似问题