是否可以将两个时间序列合并为一个? [英] Is it possible to merge two time series in one?

查看:121
本文介绍了是否可以将两个时间序列合并为一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试合并两个 ts 对象,第二个对象恰好在下一个周期之后的一个周期开始.比如下面两个时间序列

I've been trying to merge two ts objects, the second one starts exactly one period after the next one. For example, take the following two time series

ts1<-ts(c(1:12),star=c(2014,1),freq=12)
ts2<-ts(c(13:24),star=c(2015,1),freq=12)

如您所见,为了从这两个 ts 对象中生成一个 ts,它们两者完美匹配.我认为合乎逻辑的答案是 rbind() 函数.但它由它们组成一个矩阵,如下...

As you can see, both of them match perfectly in order to make a single ts out of this two ts objects. I thought the logical answer would be the rbind() function. But it makes a matrix out of them, as follows...

> rbind(ts1,ts2)
    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
ts1    1    2    3    4    5    6    7    8    9    10    11    12
ts2   13   14   15   16   17   18   19   20   21    22    23    24

我尝试过其他功能,如合并、cbind,但没有成功.使用 c() 我设法只得到一个时间序列,主要问题是我失去了原始时间序列的结构属性,这很糟糕,因为我试图将函数预测与新的 ts 一起使用,但它给了我这个:

I've tried without success other functions like merge,cbind. Using c() I've managed to get one only timeseries, the main problema is that I lose structure attributes of the original timeseries, this is bad because I'm trying to use the function forecast with the new ts but it gives me this:

Error: variables ... were specified with different types from the fit

我很高兴能够为时间序列添加额外的观察结果.类似于在 2015 年 1 月将值 13 添加到 ts1,但我也没有找到如何执行此操作.

I would be happy just with being able to add additional observations to a time series. Something like adding the value 13 to ts1 for January,2015 but I haven´t found how to do this either.

我认为这很有趣,因为我认为这是向 ts 对象提出要求的完全自然的事情,但我还没有找到任何其他问题可以帮助我解决这个问题.好吧,让我们希望这不是一个太愚蠢的问题.

I think is funny because I see this as a perfectly natural thing to ask for to a ts object, but I haven´t found any other question that helps me out here. Well let´s hope this is not a too silly question.

推荐答案

你可以使用 xts 包,它会为你处理细节(例如,即使系列有差距)

You could use the xts package which will take care of details for you (for instance even if the series have a gap)

library(xts)
ts1<-as.xts(ts(c(1:12),star=c(2014,1),freq=12))
ts2<-as.xts(ts(c(13:24),star=c(2015,1),freq=12))
str(ts3 <- c(ts1, ts2))
# An ‘xts’ object on Jan 2014/Dec 2015 containing:
# Data: int [1:24, 1] 1 2 3 4 5 6 7 8 9 10 ...
# Indexed by objects of class: [yearmon] TZ: 
# xts Attributes:
# NULL

干杯,彼得

这篇关于是否可以将两个时间序列合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆