首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用astsa软件包绘制互相关值时的误差

在R中使用astsa软件包绘制互相关值时的误差
EN

Stack Overflow用户
提问于 2022-01-19 07:46:52
回答 1查看 59关注 0票数 0

我拥有的数据集是每周有一个因变量的数据和一个分组变量的独立变量(这是关键变量)。我能得到Dep之间的互相关值。& Ind.使用CCF函数基于分组变量的变量。但是,由于某些原因,当我试图绘制CCF值时,我会得到以下错误:无法确定列valueDependent.VariableIndependent.Variable。我使用了astsa包中的lag.2 2绘图函数来绘制CCF值。请告诉我哪里做错了

代码语言:javascript
复制
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)

谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-02-20 02:06:11

代码语言:javascript
复制
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' )
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70766939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档