在 R 中存储复杂的时间序列
我有一个包含几列的数据框:
- 州县
- 年份
- 然后是
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是重塑数据,以便为每个州-县组合提供一列。这允许您构建 xts 矩阵:
或者,您可以使用 xts 对象列表,每次都确保您具有 xts 要求的正确格式。对于任何其他时间序列包也是如此。一个可能的解决方案是:
这将允许类似:
选择特定的时间序列。您可以使用所有 xts 选项。您可以循环遍历县、州和年份来构建如下图所示的图:
图代码:
测试-数据 :
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 :
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 :
Which would allow something like :
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 :
Code for plot :
Test-data :