简体   繁体   English

将不规则时间序列转换为常规时间序列

[英]Convert a irregular time series to a regular time series

I am having a problem when converting irregular time series to regular time series. 将不规则时间序列转换为常规时间序列时遇到问题。 Below a simplified example can be found: 下面是一个简化的例子:

require(zoo)
t <- as.character(c(1981,1984,1985))
d <- c(1,3,6)
dt <- data.frame(d,t)
t <- as.Date(t,"%Y")
z <- zoo(d,t)
plot(z)
ts.d <- as.ts(as.zooreg(z,freq=1)) # create a regular ts object
ts.d # regular time series

I would like to create a regular time series ts.d that looks like this c(1981,NA,NA,1984,1985). 我想创建一个看起来像这个c(1981,NA,NA,1984,1985)的常规时间序列ts.d。

The amazing thing is that the first time that I run this: it works! 令人惊奇的是,我第一次运行它:它的工作原理! but when I want to run it again or repeat it (the as.ts()line) it stops workings and I obtain a very long time series: 但是当我想再次运行它或重复它(as.ts()行)它会停止工作并且我获得了很长的时间序列:

ts.d # regular time series
Time Series:
Start = 4299 
End = 5760 
Frequency = 1 
  [1]  1 NA NA NA NA NA NA NA NA NA NA NA NA NA
 [15] NA NA NA NA NA NA NA NA 

etc. 等等

What is going wrong? 出了什么问题?

As has been pointed out the as.Date(as.character(t), "%Y") is incorrect as it does not give the desired month and day. 正如已经指出的那样, as.Date(as.character(t), "%Y")是不正确的,因为它没有给出所需的月份和日期。 If we wanted to convert years to "Date" class we could do this as.Date(as.yearmon(t)) using zoo's as.yearmon ; 如果我们想将年份转换为"Date"类,我们可以使用zoo的as.yearmon来执行as.Date(as.yearmon(t)) ; however, then we would have the further problem that different years have different numbers of days so there is no way to have a regular series using dates to represent years. 然而,那么我们会有另一个问题,即不同的年份有不同的天数,所以没有办法定期使用日期表示年份。

Really we don't want dates in the first place. 真的,我们首先不想要约会。 We just want to work with years in which case it simplifies to just: 我们只想与多年合作,在这种情况下,它简化为:

> z <- zoo(c(1, 3, 6), c(1981, 1984, 1985))
> 
> as.ts(z)
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

or if we want to be safe we could do this which will force it to be annual even if the input has, by chance, a lower frequency: frequency(z) <- 1; as.ts(z) 或者如果我们想要安全,我们可以这样做,这将迫使它成为年度,即使输入偶然有一个较低的频率: frequency(z) <- 1; as.ts(z) frequency(z) <- 1; as.ts(z) or just define the original zoo series to have a frequency of 1 right from the beginning: frequency(z) <- 1; as.ts(z)或者只是将原始动物园系列定义为从一开始就具有1的频率:

> z <- zoo(c(1, 3, 6), c(1981, 1984, 1985), frequency = 1)
> as.ts(z)
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

With this example it does not make a difference but in this case z <- zoo(c(1, 3, 6), c(1981, 1983, 1985), frequency = 1) the explicit frequency would be needed to prevent it from having a frequency of 0.5 . 在这个例子中它并没有什么区别但是在这种情况下z <- zoo(c(1, 3, 6), c(1981, 1983, 1985), frequency = 1)需要明确的frequency来防止它频率为0.5

It's not a bug. 这不是一个错误。 There are 1,461 days spanning the 4 years in your time series. 您的时间序列中有4年的1,461天。 And it doesn't work for me the first time I run it. 它在我第一次运行时对我不起作用。 as.Date(t,"%Y") doesn't know what month/day to use to make a date, so it uses today's month/day. as.Date(t,"%Y")不知道用于创建日期的月/日,因此它使用今天的月/日。 That does not make for reproducible analysis. 这不能用于可重复的分析。 Try this instead: 试试这个:

t <- c(1981,1984,1985)
d <- c(1,3,6)
z <- zoo(d,t)
z <- merge(z,zoo(,c(1981,1982,1983,1984,1985)))
ts.d <- as.ts(z)

Which yields: 产量:

> ts.d
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM