PerformanceAnalytics处理基于时间的对象时出错?

时间:2012-10-30 17:25:00

标签: r time-series xts zoo

请将以下数据结构放入R:

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()相同)。

1 个答案:

答案 0 :(得分:4)

我可以重现你的问题,但使用完全不同的系统(见下文)。因此,我认为您的问题不是由于您安装的任何软件包造成的。

我的猜测是PerformanceAnalytics中的一些功能不期望有一个年级日期类型,所以尝试将其转换为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. charts.PerformanceSummary()适用于as.POSIXctas.Date个对象。也许再试一次,有一个新的会议?

  2. 对于table.Returns我通过给列命名来修复它!这有点奇怪,没有文档,所以你可以提交错误报告。但只要给它一个名字:

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

  3. 这是我的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