我有以下data
数据点的时间序列(请参阅dput()
下面的输出以获取可重复的序列)。
data
2012-03-13 0.0099809886
2012-03-14 -0.0011633318
2012-03-15 0.0021057557
2012-03-16 -0.0039516504
2012-03-19 -0.0006950880
2012-03-20 -0.0064935065
2012-03-21 -0.0016389604
2012-03-22 0.0089264740
2012-03-23 0.0061047194
2012-03-26 -0.0032664489
2012-03-27 0.0016199954
2012-03-28 0.0123198512
2012-03-29 -0.0018399264
2012-03-30 0.0013828071
2012-04-02 -0.0134335155
2012-04-03 -0.0038999771
2012-04-04 0.0057816836
2012-04-05 0.0041695622
2012-04-10 0.0039627040
2012-04-11 -0.0007045561
2012-04-12 0.0063261481
2012-04-13 0.0030106531
2012-04-16 0.0004650081
2012-04-17 -0.0057924004
2012-04-18 0.0055337791
2012-04-19 0.0009157509
2012-04-20 -0.0004576659
2012-04-23 -0.0038857143
2012-04-24 0.0029960820
2012-04-26 -0.0074779062
我想尝试获得一个 n 周期滚动分位数的时间序列。
例如,要获得整个系列的上四分位数,只需:
> quantile(se,.75)
75%
0.004117848
但我想要的是有效地添加data$rolling_quantile
,这样我就可以有一个滚动的 n 周期窗口,它构成了
我原以为apply.rolling
(in Performance Analytics
) 或 roll.apply (in zoo
) 会解决问题,但在尝试计算 10 天滚动上四分位数时出现以下错误:
> rolling_quantile <- apply.rolling(data,width=10,FUN="quantile",.75)
Error in xts(calcs[-1], order.by = dates[steps]) :
NROW(x) must match length(order.by)
tracback
也没有给出太多线索:
> traceback()
3: stop("NROW(x) must match length(order.by)")
2: xts(calcs[-1], order.by = dates[steps])
1: apply.rolling(data, width = 10, FUN = "quantile", 0.75)
roll.apply
似乎有效,但它似乎在data
. 如有必要,我会将该电话作为更新发布,但我apply.rolling
认为无论如何都是合适的解决方案。
当然,在 excel 中执行此操作非常简单,但我想在 R 中得到答案。但作为解决方案的指南,这是我想要得到的结果(在 Excel 中生成):
data rolling_qt
3/13/2012 0.009980989
3/14/2012 -0.001163332
3/15/2012 0.002105756
3/16/2012 -0.00395165
3/19/2012 -0.000695088
3/20/2012 -0.006493506
3/21/2012 -0.00163896
3/22/2012 0.008926474
3/23/2012 0.006104719
3/26/2012 -0.003266449 0.005104978
3/27/2012 0.001619995 0.001984316
3/28/2012 0.012319851 0.005104978
3/29/2012 -0.001839926 0.004983538
3/30/2012 0.001382807 0.004983538
4/2/2012 -0.013433515 0.004983538
4/3/2012 -0.003899977 0.004983538
4/4/2012 0.005781684 0.00602396
4/5/2012 0.004169562 0.005378653
4/10/2012 0.003962704 0.004117848
4/11/2012 -0.000704556 0.004117848
4/12/2012 0.006326148 0.005378653
4/13/2012 0.003010653 0.004117848
4/16/2012 0.000465008 0.004117848
4/17/2012 -0.0057924 0.004117848
4/18/2012 0.005533779 0.005192725
4/19/2012 0.000915751 0.005192725
4/20/2012 -0.000457666 0.004117848
4/23/2012 -0.003885714 0.003724691
4/24/2012 0.002996082 0.00300701
4/26/2012 -0.007477906 0.00300701
任何帮助,一如既往,非常感谢。
> dput(se)
structure(c(0.00998098859315588, -0.00116333178222428, 0.00210575573233496,
-0.00395165039516499, -0.000695088044485592, -0.00649350649350644,
-0.00163896043081246, 0.00892647404275304, 0.00610471941770374,
-0.00326644890340644, 0.00161999537144175, 0.0123198512319851,
-0.00183992640294384, 0.00138280709840988, -0.0134335154826958,
-0.0038999770589585, 0.00578168362627207, 0.00416956219596942,
0.00396270396270393, -0.000704556129638378, 0.00632614807872534,
0.00301065308012971, 0.000465008137642497, -0.0057924003707136,
0.00553377910998387, 0.000915750915750912, -0.000457665903890181,
-0.00388571428571427, 0.00299608204655444, -0.00747790618626787
), .indexCLASS = "Date", .indexTZ = "", na.action = structure(c(5L,
6L, 10L, 14L, 17L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 47L, 49L, 51L, 54L, 55L, 56L, 57L, 58L, 59L, 60L,
61L, 62L, 63L, 64L, 65L, 66L, 68L, 69L, 70L, 71L, 72L, 73L, 74L,
75L, 76L, 77L, 78L, 79L, 80L, 82L, 83L, 84L, 85L, 86L, 87L, 88L,
89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 98L, 99L, 100L, 101L,
102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 112L, 113L,
114L, 115L, 116L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L,
126L, 127L, 128L, 129L, 132L, 133L, 135L, 136L, 137L, 138L, 139L,
140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L,
151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L,
162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L,
245L, 252L, 285L, 426L, 448L, 509L, 686L, 704L, 796L, 942L, 959L,
1057L, 1257L, 1311L, 1531L, 1565L, 1726L, 1787L, 1820L, 1982L,
2032L, 2044L, 2045L, 2075L, 2076L, 2218L, 2221L, 2237L, 2238L,
2330L, 2493L, 2555L, 2590L, 2749L, 2811L, 2845L, 3005L, 3067L
), class = "omit", index = c(957708000, 957794400, 958312800,
958658400, 959090400, 959263200, 959522400, 959608800, 959695200,
959868000, 960127200, 960213600, 960300000, 960386400, 960472800,
960732000, 960818400, 960904800, 960991200, 961077600, 961336800,
961423200, 961509600, 961596000, 961682400, 961941600, 962028000,
962114400, 962200800, 962287200, 962719200, 962892000, 963237600,
963496800, 963756000, 963842400, 963928800, 964015200, 964101600,
964360800, 964447200, 964533600, 964620000, 964706400, 964965600,
965052000, 965224800, 965311200, 965570400, 965656800, 965743200,
965829600, 965916000, 966175200, 966261600, 966348000, 966434400,
966520800, 966780000, 966952800, 967039200, 967125600, 967381200,
967467600, 967554000, 967640400, 967726800, 967986000, 968072400,
968158800, 968245200, 968331600, 968590800, 968677200, 968850000,
968936400, 969195600, 969282000, 969368400, 969454800, 969541200,
969800400, 969886800, 969973200, 970059600, 970146000, 970405200,
970578000, 970664400, 970750800, 971010000, 971096400, 971269200,
971355600, 971614800, 971701200, 971787600, 971874000, 971960400,
972219600, 972306000, 972392400, 972478800, 972565200, 972997200,
973083600, 973429200, 973515600, 973602000, 973688400, 973774800,
974034000, 974120400, 974206800, 974293200, 974379600, 974638800,
974725200, 974811600, 974898000, 974984400, 975243600, 975330000,
975416400, 975502800, 975589200, 975848400, 975934800, 976021200,
976107600, 976194000, 976453200, 976539600, 976626000, 976712400,
976798800, 977058000, 977144400, 977230800, 977317200, 977403600,
977835600, 977922000, 978008400, 987343200, 988120800, 992181600,
1009285200, 1012136400, 1019656800, 1041339600, 1043586000, 1055080800,
1072875600, 1075035600, 1087135200, 1111932000, 1118584800, 1145887200,
1150034400, 1169730000, 1177423200, 1181484000, 1201438800, 1207663200,
1209045600, 1209304800, 1212933600, 1213020000, 1230469200, 1230814800,
1232888400, 1232974800, 1244383200, 1264424400, 1272204000, 1276437600,
1295960400, 1303740000, 1307887200, 1327496400, 1335276000)), index = structure(c(1331557200,
1331643600, 1331730000, 1331816400, 1332075600, 1332162000, 1332248400,
1332334800, 1332421200, 1332680400, 1332766800, 1332853200, 1332939600,
1333026000, 1333288800, 1333375200, 1333461600, 1333548000, 1333980000,
1334066400, 1334152800, 1334239200, 1334498400, 1334584800, 1334671200,
1334757600, 1334844000, 1335103200, 1335189600, 1335362400), tzone = "", tclass = "Date"), .Dim = c(30L,
1L), .Dimnames = list(NULL, "data"), class = c("xts", "zoo"))