English 中文(简体)
如何将滚动qu带应用到R的xts时段?
原标题:How to apply rolling quantiles to an xts timeseries in R?

我有以下<条码>数据,这是各数据点的系列(见<条码>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

我愿尝试和获得一段时期的滚动板。

例如,获得整个系列的上半部分只是:

> quantile(se,.75)
    75% 
0.004117848

但是,我想要有效地增加<代码>数据输入——数量<>/代码>。 因此,我可以有一个滚动的单期窗口,即构成什么内容。

我想到apply.rolling (in Performance Analysis)或滚动(载于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)

:

> 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似乎是在发挥作用的,但似乎比预计的<代码>数据的头和尾版数据更多。 我将在必要时把这一呼吁列为最新情况,但我的数字是<编码>应用程序>. 滚动无论如何都是适当的解决办法。

当然,用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

Any help, as ever, very much appreciated.

> 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"))
最佳回答

我认为,这只是一小块混淆。 这项工作:

rollapply(x, width = 10, FUN = "quantile", p = .75)

我指的是你想要达到零功能的参数。

BTW,我获得的产量与你的Excel样本相同,但时间为1天。 如果是这样的话,这将相应调整:

res <- rollapply(x, width = 10, FUN = "quantile", p = .75)
index(res) <- index(res) + 1
问题回答

暂无回答




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签