运行相关(gtools)

时间:2017-07-24 02:26:31

标签: r csv

我是R新手,试图做简单的事情。 我想检查两个时间序列(两个CSV文件)之间的运行相关性。 下面是我的代码,在加载gtools包之后:

v1<-read.csv("var1.csv", header = FALSE)

v2<-read.csv("var2.csv", header = FALSE)

running(v1,v2,fun=cor, width=5)

我收到以下错误消息: 命名列表()

然后我再次尝试分配一个变量:

p1<-running(v1,v2,fun=cor, width=5)

plot(p1)

我收到以下错误消息:

  

xy.coords(x,y,xlabel,ylabel,log)中的错误:&#39; x&#39;是一个列表,但是   没有组件&#39; x&#39;和&#39;

我错过了什么?

如何创建一个显示运行相关性的图和表示95%置信区间的线?

谢谢!

v1v2如下:

v1 = structure(list(var1 = c(-0.888829723, -0.638363898, -0.820331055, -0.711637919, -3.631666745, 0.528082315, -0.888551728, 3.670203445, -0.406498322, 1.185030346, 1.427746793, -0.393369446, 2.905055593, -0.401353407, -0.563123881, 1.140042632, 7.078661195, 2.556181809, 0.888551728, -3.670203445, 0.406498322, -1.185030346, -1.427746793, 0.393369446, -2.016225871, 1.039717305, 1.383454936, -0.428404714, -3.44699445, -3.084264124)), .Names = "var1", class = "data.frame", row.names = c(NA, -30L))

v2 = structure(list(var2 = c(0.008871463, -0.218818955, 1.055065334, 1.353131909, -1.021284981, -2.153524661, 1.825212612, 0.460388983, 1.48721711, -1.78249802, 0.46047233, -0.894777526, -0.852226438, 0.136373161, -0.248409748, -0.411183561, 0.912205699, -1.856740048, -1.825212612, -0.460388983, -1.48721711, 1.78249802, -0.46047233, 0.894777526, 0.843354976, 0.082445794, -0.806655586, -0.941948347, 0.109079282, 4.010264709)), .Names = "var2", class = "data.frame", row.names = c(NA, -30L))

1 个答案:

答案 0 :(得分:0)

假设你想要进行5年滚动相关,你的给定时间是1988-2017(30年):

v1<-read.csv("var1.csv", header = FALSE)
v2<-read.csv("var2.csv", header = FALSE)

v1 = structure(list(var1 = c(-0.888829723, -0.638363898, -0.820331055, -0.711637919, -3.631666745, 0.528082315, -0.888551728, 3.670203445, -0.406498322, 1.185030346, 1.427746793, -0.393369446, 2.905055593, -0.401353407, -0.563123881, 1.140042632, 7.078661195, 2.556181809, 0.888551728, -3.670203445, 0.406498322, -1.185030346, -1.427746793, 0.393369446, -2.016225871, 1.039717305, 1.383454936, -0.428404714, -3.44699445, -3.084264124)), .Names = "var1", class = "data.frame", row.names = c(NA, -30L))
v1 = as.vector(v1$var1)
v2 = structure(list(var2 = c(0.008871463, -0.218818955, 1.055065334, 1.353131909, -1.021284981, -2.153524661, 1.825212612, 0.460388983, 1.48721711, -1.78249802, 0.46047233, -0.894777526, -0.852226438, 0.136373161, -0.248409748, -0.411183561, 0.912205699, -1.856740048, -1.825212612, -0.460388983, -1.48721711, 1.78249802, -0.46047233, 0.894777526, 0.843354976, 0.082445794, -0.806655586, -0.941948347, 0.109079282, 4.010264709)), .Names = "var2", class = "data.frame", row.names = c(NA, -30L))
v2 = as.vector(v2$var2)

rc <- running(v1, v2, fun = cor, width = 5)
length(rc)
plot((2017-length(rc) + 1):2017, rc, type="l")

这应该给你滚动的相关图。