2012-08-07 49 views
0

我知道這個問題已經在這篇文章中提出(xts error - order.by requires an appropriate time-based object),但問題似乎仍然沒有解決。錯誤xts(new.x,x.index):order.by需要一個適當的基於時間的對象

我的工作相同的代碼如下:R: Error in xts - order.by,一切工作和計算完美,直到我重新啓動我的電腦,現在我面臨這個問題:

我沒有產生的代碼,我發現這本書:http://www.amazon.com/Data-Mining-Learning-Knowledge-Discovery/dp/1439810184/ref=sr_1_1?ie=UTF8&qid=1344349381&sr=8-1&keywords=data+mining+with+r

下面是可再現的例子:

# Packages needed : 
library(xts) 
library(TTR) 
library(randomForest) 
library(DMwR) 

# Time Series : 
myTimeSeries <- 
    structure(c(2787, 2800, 2788, 2803, 2815, 2815, 2812, 2807, 2810, 
    2829, 2830, 2837, 2841, 2840, 2843, 2839, 2835, 2841, 2834, 2838, 
    2827, 2821, 2831, 2811, 2796, 2808, 2814, 2811, 2815, 2803, 2788, 
    2778, 2772, 2777, 2776, 2760, 2732, 2711, 2709, 2707, 2700, 2706, 
    2706, 2690, 2684, 2654, 2637, 2656, 2655, 2670, 2652, 2649, 2621, 
    2622, 2599, 2612, 2632, 2653, 2668, 2654, 2637, 2633, 2639, 2648, 
    2641, 2663, 2657, 2655, 2639, 2639, 2645, 2658, 2658, 2669, 2664, 
    2658, 2667, 2671, 2679, 2670, 2693, 2708, 2715, 2704, 2695, 2697, 
    2688, 2706, 2716, 2719, 2707, 2705, 2706, 2698, 2693, 2699, 2701, 
    2699, 2713, 2716, 2721, 2714, 2710, 2706, 2701, 2691, 2663, 2658, 
    2692, 2694, 2704, 2706, 2714, 2702, 2698, 2688, 2677, 2682, 2677, 
    2688, 2669, 2667, 2674, 2684, 2673, 2682, 2694, 2690, 2691, 2695, 
    2703, 2697, 2699, 2694, 2696, 2679, 2688, 2687, 2688, 2691, 2799, 
    2801, 2804, 2822, 2820, 2819, 2814, 2816, 2836, 2836, 2838, 2846, 
    2842, 2847, 2847, 2842, 2844, 2842, 2840, 2840, 2833, 2834, 2832, 
    2815, 2810, 2819, 2814, 2818, 2815, 2815, 2789, 2779, 2782, 2781, 
    2778, 2764, 2740, 2720, 2713, 2709, 2709, 2707, 2706, 2697, 2688, 
    2661, 2657, 2660, 2672, 2682, 2662, 2651, 2629, 2623, 2614, 2650, 
    2657, 2670, 2671, 2658, 2640, 2640, 2650, 2654, 2664, 2674, 2660, 
    2656, 2646, 2641, 2663, 2663, 2678, 2676, 2664, 2672, 2677, 2684, 
    2689, 2698, 2727, 2723, 2716, 2717, 2703, 2708, 2707, 2726, 2722, 
    2727, 2711, 2713, 2707, 2704, 2705, 2708, 2705, 2704, 2718, 2729, 
    2727, 2719, 2715, 2713, 2704, 2691, 2680, 2698, 2699, 2712, 2711, 
    2710, 2714, 2714, 2705, 2689, 2685, 2685, 2694, 2690, 2677, 2680, 
    2693, 2685, 2689, 2690, 2694, 2704, 2700, 2706, 2704, 2702, 2702, 
    2698, 2697, 2690, 2690, 2690, 2693, 2695, 2785, 2777, 2784, 2802, 
    2808, 2811, 2806, 2805, 2802, 2823, 2828, 2832, 2835, 2837, 2838, 
    2824, 2834, 2826, 2830, 2827, 2816, 2814, 2798, 2796, 2796, 2807, 
    2806, 2808, 2800, 2787, 2768, 2763, 2770, 2772, 2756, 2729, 2709, 
    2694, 2698, 2699, 2697, 2689, 2690, 2653, 2651, 2635, 2634, 2649, 
    2633, 2648, 2632, 2615, 2618, 2597, 2599, 2612, 2632, 2645, 2647, 
    2631, 2627, 2616, 2636, 2630, 2640, 2655, 2652, 2635, 2634, 2622, 
    2643, 2641, 2658, 2658, 2652, 2656, 2664, 2666, 2667, 2652, 2689, 
    2707, 2704, 2703, 2694, 2685, 2686, 2699, 2711, 2704, 2693, 2697, 
    2681, 2689, 2690, 2697, 2696, 2695, 2709, 2707, 2705, 2707, 2683, 
    2689, 2687, 2652, 2655, 2657, 2690, 2691, 2703, 2702, 2700, 2693, 
    2687, 2668, 2669, 2675, 2675, 2658, 2655, 2664, 2674, 2667, 2673, 
    2676, 2677, 2689, 2691, 2694, 2696, 2697, 2693, 2692, 2676, 2678, 
    2686, 2686, 2688, 2690, 2799, 2787, 2803, 2814, 2816, 2812, 2808, 
    2810, 2830, 2830, 2836, 2841, 2840, 2840, 2839, 2836, 2841, 2834, 
    2838, 2827, 2821, 2831, 2811, 2797, 2808, 2813, 2810, 2813, 2803, 
    2788, 2778, 2772, 2777, 2776, 2759, 2732, 2712, 2709, 2706, 2699, 
    2705, 2691, 2690, 2684, 2654, 2637, 2655, 2654, 2671, 2652, 2649, 
    2622, 2622, 2599, 2611, 2646, 2653, 2668, 2654, 2636, 2634, 2639, 
    2648, 2642, 2664, 2658, 2655, 2638, 2640, 2624, 2658, 2659, 2669, 
    2665, 2659, 2667, 2671, 2678, 2670, 2693, 2707, 2715, 2704, 2706, 
    2696, 2688, 2706, 2716, 2720, 2708, 2705, 2705, 2697, 2693, 2699, 
    2701, 2699, 2702, 2716, 2721, 2714, 2710, 2706, 2701, 2690, 2663, 
    2658, 2692, 2694, 2705, 2706, 2708, 2702, 2697, 2688, 2677, 2681, 
    2677, 2687, 2670, 2667, 2674, 2685, 2674, 2682, 2689, 2690, 2691, 
    2696, 2703, 2698, 2699, 2694, 2697, 2680, 2687, 2687, 2690, 2691, 
    2695), .Dim = c(140L, 4L), .Dimnames = list(NULL, c("price.Open", 
    "price.High", "price.Low", "price.Close")), index = structure(c(1265097599, 
    1265101199, 1265104796, 1265108399, 1265111999, 1265115597, 1265119198, 
    1265122799, 1265126399, 1265129992, 1265133591, 1265137199, 1265140797, 
    1265144399, 1265183999, 1265187599, 1265191199, 1265194799, 1265198398, 
    1265201999, 1265205599, 1265209199, 1265212799, 1265216399, 1265219999, 
    1265223580, 1265227194, 1265230799, 1265270399, 1265273999, 1265277595, 
    1265281198, 1265284798, 1265288397, 1265291998, 1265295599, 1265299199, 
    1265302799, 1265306399, 1265309997, 1265313586, 1265317199, 1265356799, 
    1265360399, 1265363999, 1265367599, 1265371199, 1265374799, 1265378399, 
    1265381999, 1265385599, 1265389199, 1265392798, 1265396399, 1265399984, 
    1265403599, 1265615999, 1265619593, 1265623195, 1265626785, 1265630396, 
    1265633999, 1265637599, 1265641199, 1265644799, 1265648398, 1265651994, 
    1265655599, 1265659198, 1265662799, 1265702399, 1265705999, 1265709599, 
    1265713199, 1265716798, 1265720398, 1265723995, 1265727598, 1265731199, 
    1265734799, 1265738399, 1265741975, 1265745594, 1265749199, 1265788799, 
    1265792399, 1265795999, 1265799599, 1265803197, 1265806799, 1265810399, 
    1265813999, 1265817599, 1265821196, 1265824795, 1265828380, 1265831990, 
    1265835598, 1265875199, 1265878799, 1265882399, 1265885999, 1265889599, 
    1265893199, 1265896797, 1265900399, 1265903998, 1265907586, 1265911187, 
    1265914799, 1265918356, 1265921999, 1265961596, 1265965199, 1265968798, 
    1265972399, 1265975998, 1265979599, 1265983196, 1265986798, 1265990399, 
    1265993997, 1265997598, 1266001197, 1266004796, 1266008399, 1266220799, 
    1266224399, 1266227999, 1266231598, 1266235187, 1266238799, 1266242399, 
    1266245996, 1266249598, 1266253195, 1266256799, 1266260385, 1266263956, 
    1266267599), tzone = "", tclass = c("POSIXct", "POSIXt")), 
    tclass = c("POSIXct", "POSIXt"), tzone = "", class = c("xts", "zoo"), 
    .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "") 

T.ind <- function(quotes,tgt.margin=0.005,n.days=5){ 
    v <- apply(HLC(quotes),1,mean)  

    r <- matrix(NA,ncol=n.days,nrow=NROW(quotes)) 
    for(x in 1:n.days) r[,x] <- Next(Delt(v,k=x),x)  

    x <- apply(r,1,function(x) sum(x[x > tgt.margin | x < -tgt.margin])) 
    if (is.xts(quotes)) xts(x,time(quotes)) else x 
} 

# Candle Chart and new indicator created above : 
candleChart(last(myTimeSeries,"2 days")) 
avgPrice<-function(p) apply(HLC(p),1,mean) 
addAvgPrice<-newTA(FUN=avgPrice,col=1,legend="Average Price") 
addT.indicator<-newTA(FUN=T.indicator,col="red",legend="Target") 
addAvgPrice(on=1) 
addT.indicator() 

和將R sessionInfo():

R version 2.15.0 (2012-03-30) 
Platform: i386-pc-mingw32/i386 (32-bit) 

locale: 
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C     LC_TIME=French_France.1252  

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

other attached packages: 
[1] boot_1.3-4   car_2.0-12   nnet_7.3-4   dynamo_0.1.3   fda_2.2.8   Matrix_1.0-6   Formula_1.1-0  gstat_1.0-13   lmtest_0.9-30  spatial_7.3-3  spacetime_0.7-1  sp_0.9-99   sm_2.2-4.1   tseries_0.10-28  
[15] quadprog_1.5-4  DMwR_0.2.3   lattice_0.20-6  cluster_1.14.2  abind_1.4-0   rpart_3.1-52   class_7.3-3   ROCR_1.0-4   gplots_2.11.0  KernSmooth_2.23-7 caTools_1.13   bitops_1.0-4.1  gdata_2.11.0   gtools_2.7.0   
[29] quantmod_0.3-17  Defaults_1.1-1  randomForest_4.6-6 fExtremes_2100.77 fTrading_2100.76  fGarch_2110.80.1  fBasics_2160.81  timeSeries_2160.94 TTR_0.21-1   fractal_1.1-1  scatterplot3d_0.3-33 akima_0.5-7   wmtsa_1.1-1   sapa_1.1-0   
[43] ifultools_1.1-2  MASS_7.3-17   splus2R_1.1-1  chron_2.3-42   RTAQ_0.2    timeDate_2160.95  xts_0.8-6   zoo_1.7-7   rattle_2.6.20  

loaded via a namespace (and not attached): 
[1] stabledist_0.6-4 tools_2.15.0 
+0

請提供_minimal_ [重​​復的例子(HTTP://計算器。 COM /問題/ 5963269 /如何對做 - 一個偉大-R重現-例子)。即我們需要'價格'對象的_minimal_樣本,以便我們重現您的錯誤。我強調_minimal_因爲幾天的10分鐘數據將是巨大的。如果你可以在你的問題中編輯'sessionInfo'的輸出,那也不錯。 – 2012-08-07 13:42:11

+0

我真的不理解,因爲在重新啓動我的電腦之前,一切都很完美。 – marino89 2012-08-07 13:52:19

回答

9

for循環是沒有意義的:

for (x in 1:n.periods) { 
    r[, x] <- Next(Delt(v, k = x), x) 
    x <- apply(r, 1, function(x) {sum(x[x > target | x < -target])}) 
} 

x是迭代器,你在你的apply呼叫分配給,參數匿名函數的對象,您在sum調用中的子集對象,target比較的對象。我甚至不想考慮這可能如何工作。

總之,你需要更好的變量名稱/定義。

如果您試圖更改x(迭代器)的值,則不能。請參閱?"for"的詳細信息部分中的第三段。

UPDATE:

下面是從Luis的代碼你說你正在使用的功能。你周圍添加額外的括號for循環,然後包括函數在for循環的第二到最後一行...

T.ind <- function(quotes,tgt.margin=0.025,n.days=10) { 
    v <- apply(HLC(quotes),1,mean) 

    r <- matrix(NA,ncol=n.days,nrow=NROW(quotes)) 
    for(x in 1:n.days) r[,x] <- Next(Delt(v,k=x),x) 

    x <- apply(r,1,function(x) sum(x[x > tgt.margin | x < -tgt.margin])) 
    if (is.xts(quotes)) xts(x,time(quotes)) else x 
} 
+0

對不起,我知道它可能看起來很愚蠢,但一切正常,我的意思是我的T.indcator函數正在工作並輸出一個時間序列。實際上,我沒有編寫代碼,我在本書中找到了它:http://www.amazon.com/Data-Mining-Learning-Knowledge-Discovery/dp/1439810184/ref=sr_1_1?ie=UTF8&qid=1344348700&sr=8 -1&keywords = data + mining + with + R – marino89 2012-08-07 14:08:53

+3

@ marino89:我不懷疑它是「正在工作」(即沒有拋出錯誤),但我真的懷疑它給了你_correct_結果。 Luis書中的所有代碼都在[他的網站上](http://www.liaad.up.pt/~ltorgo/DataMiningWithR)。請告訴我他在哪裏發佈了您正在使用的代碼。 – 2012-08-07 14:12:06

+0

當我看到x被用作for循環迭代器和變量時,我很驚訝。 – marino89 2012-08-07 14:17:06

相關問題