在 R 中存储复杂的时间序列

发布于 2024-11-05 03:18:28 字数 249 浏览 1 评论 0原文

我有一个包含几列的数据框:

  • 州县
  • 年份
  • 然后是

x、y 和 z,其中 x、y 和 z 是上面列出的三元组特有的观测值。我正在寻找一种合理的方法来将其存储在时间序列中,但 xts 不会让我这样做,因为每个时间索引都有多个观察结果。我已经查看了 hts 包,但无法弄清楚如何从数据框中将数据放入其中。

(是的,我确实在 Quora 上发布了同样的问题,并被建议将其带到这里!)

I have a dataframe with several columns:

  • state
  • county
  • year

Then x, y, and z, where x, y, and z are observations unique to the triplet listed above. I am looking for a sane way to store this in a time series and xts will not let me since there are multiple observations for each time index. I have looked at the hts package, but am having trouble figuring out how to get my data into it from the dataframe.

(Yes, I did post the same question on Quora, and was advised to bring it here!)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2024-11-12 03:18:28

一种选择是重塑数据,以便为每个州-县组合提供一列。这允许您构建 xts 矩阵:

require(reshape)
Opt1 <- as.data.frame(cast(Data, Date ~ county + State, value="Val"))
rownames(Opt1) <- Opt1$Date
Opt1$Date <- NULL
as.xts(Opt1)

或者,您可以使用 xts 对象列表,每次都确保您具有 xts 要求的正确格式。对于任何其他时间序列包也是如此。一个可能的解决方案是:

Opt2 <- 
  with(Data,
    by(Data,list(county,State,year),
      function(x){
        rownames(x) <- x$Date
        x <- x["Val"]
        as.xts(x)
      }
    )
  )

这将允许类似:

Opt2[["d","b","2012"]]

选择特定的时间序列。您可以使用所有 xts 选项。您可以循环遍历县、州和年份来构建如下图所示的图:

在此处输入图像描述

图代码:

counties <- dimnames(Opt2)[[1]]
states <- dimnames(Opt2)[[2]]
years <- dimnames(Opt2)[[3]]

op <- par(mfrow=c(3,6))
apply(
  expand.grid(counties,states,years),1,
  function(i){
    plot(Opt2[[i[1],i[2],i[3]]],main=paste(i,collapse="-"))
    invisible()
  }
)
par(op)

测试-数据 :

Data <- data.frame( State = rep(letters[1:3],each=90),
            county = rep(letters[4:6],90),
            Date = rep(seq(as.Date("2011-01-01"),by="month",length.out=30),each=3),
            Val = runif(270)
)
Data$year <- as.POSIXlt(Data$Date)$year + 1900

One option is to reshape your data so you have a column for every State-County combination. This allows you to construct an xts matrix :

require(reshape)
Opt1 <- as.data.frame(cast(Data, Date ~ county + State, value="Val"))
rownames(Opt1) <- Opt1$Date
Opt1$Date <- NULL
as.xts(Opt1)

Alternatively, you could work with a list of xts objects, each time making sure that you have the correct format as asked by xts. Same goes for any of the other timeseries packages. A possible solution would be :

Opt2 <- 
  with(Data,
    by(Data,list(county,State,year),
      function(x){
        rownames(x) <- x$Date
        x <- x["Val"]
        as.xts(x)
      }
    )
  )

Which would allow something like :

Opt2[["d","b","2012"]]

to select a specific time series. You can use all xts options on that. You can loop through the counties, states and years to construct plots like this one :

enter image description here

Code for plot :

counties <- dimnames(Opt2)[[1]]
states <- dimnames(Opt2)[[2]]
years <- dimnames(Opt2)[[3]]

op <- par(mfrow=c(3,6))
apply(
  expand.grid(counties,states,years),1,
  function(i){
    plot(Opt2[[i[1],i[2],i[3]]],main=paste(i,collapse="-"))
    invisible()
  }
)
par(op)

Test-data :

Data <- data.frame( State = rep(letters[1:3],each=90),
            county = rep(letters[4:6],90),
            Date = rep(seq(as.Date("2011-01-01"),by="month",length.out=30),each=3),
            Val = runif(270)
)
Data$year <- as.POSIXlt(Data$Date)$year + 1900
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文