2012-10-30 35 views
3

請把下面的數據結構爲R:PerformanceAnalytics在處理基於時間的對象時出錯?

ret.tot <- structure(c(NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.0176629385269872, 
-0.00598540500471782, 0.000397233717450593, 0.0306832492016377, 
0.017489053560066, 0.00269955471373475, -0.0320201783372234, 
-0.0275430853903852, -0.000590271106063863, 0.0129459406811023, 
0.0149953072553982, -0.0129087888171287, -0.00130280102489988, 
-0.0377404713075129, 0.0236191282922097, -0.0119020468768697, 
-0.115096461748588, 0.100199681314344, 0.0911308738334283, -0.110863725885523, 
-0.0727448946117171, 0.0474370051015683, -0.0261582369412903, 
0.0555183596655775, 0.00860003940385565, 0.0346875742874423, 
0.0165600647484871, 0.0289458251073716, -0.0200260791601897, 
0.0762373770534174, -0.0221922758574445, -0.0462536669889334, 
0.0145014986918377, 0.00785273903343038, 0.0278941165111914, 
-0.0147065715501423, 0.0153730326847651, 0.0144617698521356, 
0.0197478508710501, 0.0251269321439706, -0.0103666325237731, 
-0.00157067125918319, 0.0115368085910558, -0.0200792001115183, 
0.0309994626763721, -0.00718247947811835, 0.0175666609422171, 
0.00657518046680547, -0.0281804455661145, 0.0239339965633073, 
-0.0040811434351766, -0.00861507762926691, 0.0492982380847687, 
0.0101687467126706, 0.0134748887393316, 0.0258425088138871, 0.00378545928098804, 
-0.0150120910386031, 0.00362726110322858, -0.00593835966763433, 
0.0312603603825787, 0.0111363934978518, 0.00793289404006547, 
0.00807089833865603, -0.0118743825996689), .Dim = c(78L, 1L), .Dimnames = list(
    NULL, NULL), index = structure(c(1146441600, 1149120000, 
1151712000, 1154390400, 1157068800, 1159660800, 1162339200, 1164931200, 
1167609600, 1170288000, 1172707200, 1175385600, 1177977600, 1180656000, 
1183248000, 1185926400, 1188604800, 1191196800, 1193875200, 1196467200, 
1199145600, 1201824000, 1204329600, 1207008000, 1209600000, 1212278400, 
1214870400, 1217548800, 1220227200, 1222819200, 1225497600, 1228089600, 
1230768000, 1233446400, 1235865600, 1238544000, 1241136000, 1243814400, 
1246406400, 1249084800, 1251763200, 1254355200, 1257033600, 1259625600, 
1262304000, 1264982400, 1267401600, 1270080000, 1272672000, 1275350400, 
1277942400, 1280620800, 1283299200, 1285891200, 1288569600, 1291161600, 
1293840000, 1296518400, 1298937600, 1301616000, 1304208000, 1306886400, 
1309478400, 1312156800, 1314835200, 1317427200, 1320105600, 1322697600, 
1325376000, 1328054400, 1330560000, 1333238400, 1335830400, 1338508800, 
1341100800, 1343779200, 1346457600, 1349049600), tzone = "", tclass = "yearmon"), .indexCLASS = "yearmon", tclass = "yearmon", .indexTZ = "", tzone = "", class = c("xts", 
"zoo")) 

我的R對話:

R version 2.15.1 (2012-06-22) 
Platform: i386-pc-mingw32/i386 (32-bit) 

locale: 
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C      
[5] LC_TIME=English_Australia.1252  

attached base packages: 
[1] stats  graphics grDevices datasets utils  methods base  

other attached packages: 
[1] quantmod_0.3-17  TTR_0.21-1   xts_0.8-6    
[4] zoo_1.7-8    Defaults_1.1-1  rmgarch_0.97   
[7] Matrix_1.0-9   lattice_0.20-10  Kendall_2.2   
[10] spd_1.7    KernSmooth_2.23-8  rugarch_1.0-11  
[13] Rsolnp_1.12   truncnorm_1.0-6  chron_2.3-42   
[16] numDeriv_2012.3-1  MASS_7.3-18   RcppArmadillo_0.3.4.2 
[19] Rcpp_0.9.13   rcom_2.2-5   rscproxy_2.0-5  

loaded via a namespace (and not attached): 
[1] boot_1.3-7     grid_2.15.1     
[3] PerformanceAnalytics_1.0.4.4 timeDate_2160.95 
[5] timeSeries_2160.94   tools_2.15.1 

這使我的問題代碼:

op <- par(no.readonly = TRUE) 
library(PerformanceAnalytics) 
charts.PerformanceSummary(R = ret.tot, Rf = .0) 
table.AnnualizedReturns(R = ret.tot, scale = 12, Rf = 0) 
table.Returns(R = ret.tot) 
detach(package:PerformanceAnalytics) 
par(op) 

上面的代碼返回以下錯誤:

> table.AnnualizedReturns(R = ret.tot, scale = 12, Rf = 0) 
Error in xts(rep(Rf, length(indexseries)), order.by = indexseries) : 
    order.by requires an appropriate time-based object 
> table.Returns(R = ret.tot) 
Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : 
    'data' must be of a vector type 

我猜PerformanceAnalytics包等之間的一些衝突,但我不明白誰是有罪的......

ret.tot <- xts(ret.tot, order.by = as.POSIXct(index(ret.tot))) 

等等...

> charts.PerformanceSummary(R = ret.tot, Rf = .0) 
Error in UseMethod("first") : 
    no applicable method for 'first' applied to an object of class "c('xts', 'zoo')" 

> table.AnnualizedReturns(R = ret.tot, scale = 12, Rf = 0) 
           NA 
Annualized Return   0.0335 
Annualized Std Dev  0.1144 
Annualized Sharpe (Rf=0%) 0.2928 

> table.Returns(R = ret.tot) 
Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, : 
    'data' must be of a vector type 

哎呀!

(與as.Date()相同)。

+2

做,如果你改變「RET的'index'您的任何問題得到解決。 tot「改爲'as.Date',而不是將它作爲'yearmon'? 'index(ret.tot)< - as.Date(index(ret.tot))'? – A5C1D2H2I1M1N2O1R2T1

回答

4

我可以重現你的問題,但有相當不同的系統(見下文)。所以我不認爲你的問題是由於你已經安裝的任何包。

我的猜測是PerformanceAnalytics的一些功能並不期待yearmon日期類型,因此請嘗試將其轉換爲POSIXct。是的,我只是證實:

z=xts(coredata(ret.tot), order.by=as.POSIXct(index(ret.tot))) 
table.AnnualizedReturns(R = z, scale=12,Rf=0) 

給出了這樣的:

       NA 
Annualized Return   0.0335 
Annualized Std Dev  0.1144 
Annualized Sharpe (Rf=0%) 0.2928 

更新您的更新

  1. ​​作品對我來說,對於as.POSIXctas.Date對象。也許再試一次,重新開一次會議?

  2. 對於table.Returns我通過給該列命名來解決它!這有點奇怪,而且沒有文檔,所以你可以提交一個錯誤報告。但是,只要給它一個名字:

    colnames(ret.tot)=c('something') 
    

這裏是我的sessionInfo()

R version 2.15.1 (2012-06-22) 
Platform: x86_64-pc-linux-gnu (64-bit) 

locale: 
[1] LC_CTYPE=en_US.utf8  LC_NUMERIC=C    LC_TIME=en_US.utf8  LC_COLLATE=en_US.utf8  LC_MONETARY=en_US.utf8 LC_MESSAGES=en_US.utf8 
[7] LC_PAPER=C    LC_NAME=C     LC_ADDRESS=C    LC_TELEPHONE=C   LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] PerformanceAnalytics_1.0.4.4 xts_0.8-6     zoo_1.7-7     

loaded via a namespace (and not attached): 
[1] grid_2.15.1  lattice_0.20-10 tools_2.15.1 
+0

由於每次更改後都會出現新的錯誤,因此這變得相當令人沮喪。請參閱編輯的問題。 –

+0

@JohnGay看到我的更新。 –

+0

好吧,它有一個新的R會話,並且設置表列名稱的技巧很好。我真的不知道可能會導致這樣一個問題,我會進一步調查。順便說一句,非常感謝你的幫助:)這可能與'timeSeries'和'timeDate'的衝突 –