Preserve timestamp after decomposing xts in R

冷暖自知 提交于 2020-06-11 05:15:12

问题


I have an xts timeseries called hourplot in R with a period of 24 (hourly data) over two weeks, indexed by timestamp objects of POSIXlt class, like the following:

> dput(hourplot)

    structure(c(1, 1, 1, 1, 1, 1, 1.11221374045802, 1.3368, 1.18, 
1.0032, 1, 1, 1, 1, 1, 1, 1.0736, 1.2536, 1, 1.0032, 1.1856, 
1.0048, 1, 1, 1, 1, 1, 1, 1, 1, 1.04045801526718, 1.20229007633588, 
1.00229007633588, 1, 1, 1, 1, 1, 1, 1, 1.1152, 1.008, 1, 1, 1.2648, 
1.1832, 1, 1, 1, 1, 1, 1, 1, 1.0424, 1.2952, 1.6496, 1.1208, 
1.0216, 1, 1, 1, 1, 1, 1, 1.1256, 1, 1, 1, 1.0192, 1.3056, 1, 
1.008, 1, 1, 1, 1, 1, 1, 1.0192, 1.0976, 1.0016, 1, 1, 1, 1, 
1, 1, 1.0016, 1.6512, 1.8112, 1, 1, 1.2712, 1.0288, 1.0248, 1.1984, 
1.0736, 1, 1, 1, 1, 1, 1.112, 1.336, 1.6224, 1.38, 2.2848, 1.628, 
1, 1, 1, 1.0464, 1.4328, 1.6088, 1, 1, 1.0208, 1.2088, 1.02666666666667, 
1.0784, 1.16, 1, 1.0064, 1.0616, 1.008, 1.0768, 1.4304, 1.3112, 
1.0416, 1.00133333333333, 1.028, 1, 1, 1.1736, 1, 1.1264, 2.6744, 
1.4696, 1, 1, 1.262, 1.2576, 1.0288, 1.1112, 1.008, 1, 1.01866666666667, 
1.01733333333333, 1, 1, 1.294, 1.5744, 1.264, 1.182, 1.008, 1, 
1, 1, 1, 1, 1, 1, 1.072, 1.8, 1.6424, 1.488, 1.1176, 1, 1, 1, 
1, 1, 1.012, 1.2904, 1, 1, 1, 1, 1, 1.3072, 1.2056, 1.06, 1.0016, 
1, 1, 1, 1.048, 1.0688, 1, 1, 1, 1, 1, 1, 1, 1, 1.51333333333333, 
1.362, 1, 1, 1.0416, 1.03733333333333, 1.0288, 1.0712, 1, 1, 
1, 1, 1, 1, 1.1664, 1.3464, 1.004, 1.0016, 1, 1, 1, 1, 1, 1, 
1.15801526717557, 1.18396946564885, 1, 1, 1.1128, 1.2352, 1.0712, 
1, 1, 1, 1, 1, 1, 1, 1.0744, 1.0048, 1, 1, 1, 1, 1, 1, 1, 1.0512, 
1.0616, 1.4352, 1, 1.2064, 1.3368, 1.3296, 1.1608, 1.6696, 1.0328, 
1.1976, 1.0912, 1.2024, 1, 1.0128, 1.1528, 1.26, 1, 1, 1.0192, 
1, 1, 1, 1, 1, 1.704, 1.6152, 1, 1, 1.088, 1.4096, 1.0832, 1.1224, 
1.0144, 1, 1.336, 1.552, 1.2248, 1.168, 1.4288, 1.4224, 1.2536, 
1.0048, 1, 1, 1, 1.012, 1, 1, 1.21, 1.008, 1, 1, 1, 1.016, 1, 
1.0048, 1, 1, 1.0176, 1.068, 1, 1, 1.0056, 1.2408, 1.0016, 1, 
1, 1, 1, 1, 1.1632, 1.124, 1, 1, 1, 1, 1.0032, 1.0544, 1.0112, 
1.008, 1.016, 1.0208, 1.084, 1.1688, 1.2384, 1.1736, 1.6168, 
1.5984, 1.2784, 1.0608, 1, 1, 1, 1, 1.02824427480916, 1, 1.3064, 
1.5216, 1, 1, 1.0672, 1.051), .Dim = c(346L, 1L), index = structure(c(1484722282, 
1484725287, 1484729469, 1484731265, 1484737199, 1484740697, 1484744294, 
1484747896, 1484751493, 1484755097, 1484758693, 1484762294, 1484765898, 
1484769495, 1484773093, 1484776694, 1484780298, 1484783899, 1484787499, 
1484791095, 1484794698, 1484798299, 1484800723, 1484805577, 1484809098, 
1484812688, 1484816293, 1484819889, 1484823492, 1484827094, 1484830692, 
1484834292, 1484837891, 1484841494, 1484845158, 1484848699, 1484852298, 
1484855895, 1484859499, 1484863096, 1484866699, 1484870302, 1484873901, 
1484877501, 1484881100, 1484884696, 1484888301, 1484891900, 1484895499, 
1484899097, 1484902699, 1484906297, 1484909902, 1484913499, 1484917102, 
1484920702, 1484924298, 1484927902, 1484931499, 1484935101, 1484938698, 
1484942300, 1484945897, 1484949495, 1484953100, 1484956702, 1484960299, 
1484963902, 1484967501, 1484971104, 1484974700, 1484978300, 1484981900, 
1484985500, 1484989099, 1484992701, 1484996299, 1484999900, 1485003503, 
1485007104, 1485010704, 1485014299, 1485017903, 1485021500, 1485025102, 
1485028701, 1485032300, 1485035899, 1485039502, 1485043100, 1485046701, 
1485050304, 1485053906, 1485057500, 1485061102, 1485064701, 1485068302, 
1485071901, 1485075504, 1485079101, 1485082703, 1485086300, 1485089903, 
1485093500, 1485097100, 1485100702, 1485104305, 1485107903, 1485111501, 
1485115105, 1485118701, 1485122306, 1485125905, 1485129506, 1485133103, 
1485136701, 1485140306, 1485143906, 1485147503, 1485151105, 1485154703, 
1485158303, 1485161904, 1485165481, 1485169077, 1485172682, 1485176276, 
1485179879, 1485183479, 1485187080, 1485190681, 1485194277, 1485197877, 
1485201478, 1485205077, 1485208680, 1485212281, 1485215878, 1485219477, 
1485223082, 1485226680, 1485230278, 1485233881, 1485237478, 1485241076, 
1485244677, 1485248282, 1485251882, 1485255482, 1485259196, 1485262680, 
1485265335, 1485312724, 1485316675, 1485320277, 1485323879, 1485327478, 
1485331075, 1485334678, 1485338280, 1485341881, 1485345479, 1485349077, 
1485352684, 1485356277, 1485359878, 1485363478, 1485367079, 1485370672, 
1485374276, 1485377885, 1485381477, 1485385080, 1485388679, 1485392280, 
1485395878, 1485399478, 1485403082, 1485406677, 1485410284, 1485413877, 
1485417477, 1485421081, 1485424679, 1485428278, 1485431880, 1485435481, 
1485439080, 1485442680, 1485446279, 1485449884, 1485453481, 1485457082, 
1485460680, 1485464280, 1485467876, 1485471483, 1485475081, 1485478680, 
1485482280, 1485485880, 1485489485, 1485493082, 1485496679, 1485500280, 
1485503879, 1485507485, 1485511079, 1485514682, 1485518285, 1485521885, 
1485525482, 1485529085, 1485532684, 1485536281, 1485539882, 1485543484, 
1485547081, 1485550679, 1485554281, 1485557884, 1485561483, 1485565082, 
1485568685, 1485572287, 1485575886, 1485579483, 1485583083, 1485586685, 
1485590282, 1485593886, 1485597487, 1485601085, 1485604681, 1485608285, 
1485611885, 1485615484, 1485619082, 1485622681, 1485626287, 1485629882, 
1485633484, 1485637083, 1485640681, 1485644283, 1485647889, 1485651484, 
1485655086, 1485658686, 1485662288, 1485665889, 1485669486, 1485673085, 
1485676685, 1485680283, 1485683886, 1485687488, 1485691085, 1485694687, 
1485698288, 1485701886, 1485705489, 1485709089, 1485712685, 1485716287, 
1485719884, 1485723484, 1485727084, 1485730688, 1485734287, 1485737884, 
1485741487, 1485745088, 1485748690, 1485752291, 1485755885, 1485759487, 
1485763085, 1485766686, 1485770289, 1485773889, 1485777486, 1485781093, 
1485784691, 1485788287, 1485791887, 1485795492, 1485799088, 1485802689, 
1485806287, 1485809890, 1485813491, 1485817088, 1485820693, 1485824289, 
1485827888, 1485831491, 1485835093, 1485838688, 1485842289, 1485845889, 
1485849489, 1485853090, 1485856691, 1485860290, 1485863888, 1485867490, 
1485871089, 1485874693, 1485878289, 1485881888, 1485885488, 1485889091, 
1485892688, 1485896288, 1485899890, 1485903494, 1485907096, 1485910694, 
1485914292, 1485917890, 1485921490, 1485925090, 1485928695, 1485932291, 
1485935888, 1485939492, 1485943093, 1485946690, 1485950293, 1485953895, 
1485957493, 1485961096, 1485964692, 1485968291, 1485971892, 1485975492, 
1485979084, 1485982689, 1485986289, 1485989895, 1485993493, 1485997092, 
1486000694, 1486004292, 1486006761), tzone = "", tclass = c("POSIXlt", 
"POSIXt")), .indexCLASS = c("POSIXlt", "POSIXt"), .indexTZ = "", tclass = c("POSIXlt", 
"POSIXt"), tzone = "", class = c("xts", "zoo"), frequency = 24)

Now, I want to decompose this timeseries and look at the seasonality, so I run:

dec <- decompose(as.ts(hourplot))
plot(dec)

The plot I get looks like:

Now, I want to have the timestamps mentioned under the individual trend and seasonality plots too, but R seems to strip off the timestamp and put 1-14 in the X axis instead.

How can I preserve the timestamps in the plots?


回答1:


Here's a function that will decompose an xts series and return an object of class "decomposed.xts".

decompose.xts <-
function (x, type = c("additive", "multiplicative"), filter = NULL) 
{
  dts <- decompose(as.ts(x), type, filter)
  dts$x <- .xts(dts$x, .index(x))
  dts$seasonal <- .xts(dts$seasonal, .index(x))
  dts$trend <- .xts(dts$trend, .index(x))
  dts$random <- .xts(dts$random, .index(x))

  with(dts,
  structure(list(x = x, seasonal = seasonal, trend = trend,
    random = if (type == "additive") x - seasonal - trend else x/seasonal/trend, 
    figure = figure, type = type), class = "decomposed.xts"))
}

And here's a plot.decomposed.xts() method:

plot.decomposed.xts <-
function(x, ...)
{
  xx <- x$x
  if (is.null(xx))
    xx <- with(x,
      if (type == "additive") random + trend + seasonal
      else random * trend * seasonal)
  p <- cbind(observed = xx, trend = x$trend, seasonal = x$seasonal, random = x$random)
  plot(p, main = paste("Decomposition of", x$type, "time series"), multi.panel = 4,
       yaxis.same = FALSE, major.ticks = "days", grid.ticks.on = "days", ...)
}

And an example of using it on your data:

dex <- decompose.xts(hourplot)
plot(dex)




回答2:


I would translate the decomposed data into a dataframe:

n = length(index(hourplot))
df1 = data.frame(date = index(hourplot), name = rep("random", n), data = as.numeric(dec$random))
df2 = data.frame(date = index(hourplot), name = rep("seasonal", n), data = as.numeric(dec$seasonal))
df3 = data.frame(date = index(hourplot), name = rep("trend", n), data = as.numeric(dec$trend))
df4 = data.frame(date = index(hourplot), name = rep("observed", n), data = as.numeric(dec$x))
df = rbind(df1, df2, df3, df4)

And then use ggplot2:

library(ggplot2)
ggplot(df, aes(x = date, y = data)) + 
    geom_line() + 
    facet_wrap(~name, ncol = 1, scales = "free") +
    scale_x_datetime(date_labels = "%d", date_breaks = "24 hours")

I'll leave it to you to fine-tune the scales and labels in scale_x_datetime.



来源:https://stackoverflow.com/questions/43175206/preserve-timestamp-after-decomposing-xts-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!