2014-11-08 46 views
2

我是R的新用戶並嘗試計算每週 2個財務時間序列與ccf函數之間的自動關聯。與每週數據的自動關聯

這裏是我的代碼:

SPX_ImpliedVola_ts<-ts(SPX_ImpliedVola$x, start=c(2005), end=c(2014), freq=52) 
SPX_GSV_ts<-ts(SPX_GSV$x, start=c(2005), end=c(2014), freq=52) 
plot(ccf(SPX_ImpliedVola_ts,SPX_GSV_ts, type= "correlation")) 

的CCF功能有意義的結果,但x軸的標籤是錯誤的。我的滯後應該是幾周。情節函數使用年代替,因此滯後= 1/52。我沒有足夠的信譽點來發布劇情

是否有簡單的方法來格式化x軸,以便1次滯後= 1周?

這裏是我2013年的數據:

SPX_GSV_ts

structure(c(-0.172545978, -0.085914629, -0.051152522, -0.191885526, 
0.10720997, 0.120573931, 0.123062732, -0.073231914, 0.122783425, 
-0.073231914, -0.091330136, -0.108595771, -0.149988456, -0.077412223, 
0.017728767, -0.057991947, -0.04522754, 0.098925304, 0.019744058, 
-0.042403849, 0.097955247, 0.060480747, -0.096910013, 0.04275198, 
-0.111150452, -0.123384909, 0.020203386, 0.02540458, 0.046743404, 
0.046743404, 0.096910013, -0.029289376, -0.020203386, 0.019305155, 
0.124938737, 0.071494417, 0.080655932, 0.032184683, -0.072195125, 
0.08058446, 0.109144469, -0.116215168, -0.003792989, -0.011685758, 
0.033281387, -0.011685758, 0.044203662, -0.137383556, -0.023912157, 
0.023065304, 0.037141808, -0.128799157, -0.036045104), .Tsp = c(2013, 
2014, 52), class = "ts") 

SPX_ImpliedVola_ts:

structure(c(0.1551244, 0.1764986, 0.169477, 0.1509566, 0.14180975, 
0.1455916, 0.1320918, 0.150884, 0.1519094, 0.1670364, 0.1769658, 
0.1491722, 0.14883, 0.13545475, 0.134158, 0.1292596, 0.13465, 
0.14380075, 0.136281, 0.1350982, 0.1384192, 0.1467728, 0.161534, 
0.14764, 0.1332734, 0.1353106, 0.126313, 0.1268324, 0.1200864, 
0.1242202, 0.127857, 0.1382412, 0.1319932, 0.1441192, 0.1316964, 
0.1217246, 0.1262966, 0.11574475, 0.1166192, 0.1231602, 0.119756, 
0.10622025, 0.1133376, 0.1245488, 0.1124368, 0.11566475, 0.1196388, 
0.1003482, 0.0994486, 0.0972232, 0.10798775, 0.1115012, 0.1148464 
), .Tsp = c(2013, 2014, 52), class = "ts") 
+0

請您提供一個可重複的例子。你可以使用'dput'作爲數據的一部分。 – athraa 2014-11-08 21:49:16

+0

嗨@艾哈邁德,謝謝你的快速回復。我剛剛使用dput函數上傳了數據。 – Alexander 2014-11-09 09:03:49

回答

0

看來,ccf不積滯後的數量適當的ts類。您需要將數據的類別更改爲data.frame以在x軸上獲得適當的滯後數。您可以使用下面的代碼:

ccf.results<-ccf(data.frame(SPX_ImpliedVola_ts),data.frame(SPX_GSV_ts), type= "correlation", ylab="Auto-correlation", main="Weekely Auto-corelations") 

您可以刪除type= "correlation",你仍然會得到相同的結果作爲默認typeccfcorrelation

你也從去年的代碼有情節不給接入到每個滯後的具體價值。要檢查每個值滯後,你需要分配ccf結果變量名和薄打印它如下:

ccf.results<-ccf(data.frame(SPX_ImpliedVola_ts),data.frame(SPX_GSV_ts), type= "correlation") 
print(ccf.results) 

您將獲得:

Autocorrelations of series ‘X’, by lag 

    -14 -13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2  -1 
-0.010 0.076 0.011 -0.017 -0.031 -0.057 -0.037 0.059 0.067 0.033 0.105 0.000 -0.242 -0.181 
    0  1  2  3  4  5  6  7  8  9  10  11  12  13 
-0.189 -0.157 -0.079 0.041 0.080 -0.015 -0.098 -0.302 -0.303 -0.355 -0.323 -0.264 -0.222 -0.116 
    14 
-0.121 

注意0在上述結果意味着correlation沒有任何滯後。

enter image description here

+0

非常感謝您的解釋!您提供的代碼完美無瑕:)可悲的是,我沒有足夠的聲望點來「回答」您的答案。但是一旦我有了它,我會投票答覆你的答案! – Alexander 2014-11-09 15:57:22