Basically, I have the following data
x <- structure(c(0.609973095295898, 0.731967714355078, 0.243989238118359,
1.46393542871016, 0.609973095295898, 0.243989238118359, 0.12199461905918,
1.34194080965098, 1.82991928588769, 0.609973095295898, 2.92787085742031,
0.243989238118359, 0.12199461905918, 0.365983857177539, 0.243989238118359,
0.365983857177539, 0.243989238118359, 0.243989238118359, 0.609973095295898,
0.243989238118359, 3.41584933365703, 1.09795157153262, 2.19590314306523,
1.95191390494687, 0.975956952473437, 0.487978476236719, 0.975956952473437,
0.243989238118359, 0, 0.243989238118359, 0.731967714355078, 0.12199461905918,
0.365983857177539, 0.487978476236719, 0.365983857177539, 0.609973095295898,
0.487978476236719, 0, 0.365983857177539, 1.46393542871016, 5.00177938142637,
1.58593004776934, 0.975956952473437, 0.731967714355078, 0.12199461905918,
1.09795157153262, 0.609973095295898, 0.12199461905918, 1.09795157153262,
0), .Dim = c(50L, 1L), .indexCLASS = c("POSIXct", "POSIXt"), .indexTZ = "", tclass = c("POSIXct",
"POSIXt"), tzone = "", class = c("xts", "zoo"), index = structure(c(1519867559.7265,
1519867560.9555, 1519867561.9165, 1519867562.9545, 1519867563.9795,
1519867564.7125, 1519867565.7165, 1519867566.8635, 1519867567.9235,
1519867568.8315, 1519867569.9905, 1519867570.7575, 1519867572.3225,
1519867573.8155, 1519867574.9315, 1519867575.4905, 1519867576.2835,
1519867577.8485, 1519867578.8205, 1519867579.8385, 1519867580.7705,
1519867581.8485, 1519867582.9695, 1519867583.9445, 1519867584.7765,
1519867585.9805, 1519867586.8755, 1519867587.7145, 1519867588.6735,
1519867589.8995, 1519867590.8525, 1519867591.8665, 1519867592.7175,
1519867593.9575, 1519867594.7415, 1519867595.9385, 1519867596.8065,
1519867597.9895, 1519867598.7925, 1519867599.8625, 1519867600.9995,
1519867601.9075, 1519867602.9505, 1519867603.9515, 1519867604.9975,
1519867605.8405, 1519867606.8505, 1519867607.9255, 1519867608.9765,
1519867609.5645), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dimnames = list(
NULL, "random"))
It is just a column xts object.
Now I am trying to work out the rolling quantile such that the starting point of width is always at row 1, i.e. the range of the window always start from 1. So, if I am looking at row 35, then the range of rolling quantile window for row 35 would be from 1 to 35. And if I am looking at row 49, then the range of rolling quantile window for row 49 would be from 1 to 49.
rollapply(x[,"random"],width="from row 1 to current row",FUN="quantile",probs=0.95))
But I cannot figure out how to write the code do it efficiently ?
Most likely your are looking for:
lapply(seq_along(x), function(i){
quantile(x[1:i], probs = 0.95)
})
for each index i in x, subset x from 1 to i and return quantile.
The output will be a list, you can convert it to vector:
unlist(lapply(seq_along(x), function(i){
quantile(x[1:i], probs=0.95)
}))
or better yer (as @Rui Barradas suggested in the comments) use sapply:
sapply(seq_along(x), function(i){
quantile(x[1:i], probs=0.95)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With