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

发布于 2024-09-26 18:54:47 字数 652 浏览 1 评论 0原文

我在将不规则时间序列转换为规则时间序列时遇到问题。下面是一个简化的示例:

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

我想创建一个常规时间序列 ts.d,如下所示 c(1981,NA,NA,1984,1985)。

令人惊奇的是,我第一次运行这个:它有效!但是当我想再次运行它或重复它(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 

等等

。出了什么问题?

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).

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:

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?

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

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

发布评论

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

评论(2

土豪 2024-10-03 18:54:47

正如已经指出的那样, as.Date(as.character(t), "%Y") 是不正确的,因为它没有给出所需的月份和日期。如果我们想将年份转换为 "Date" 类,我们可以使用动物园的 as.yearmon 来执行此 as.Date(as.yearmon(t)) 操作代码>;然而,这样我们就会遇到进一步的问题,不同的年份有不同的天数,因此无法使用日期来表示年份的常规序列。

事实上,我们一开始就不想要约会。我们只想使用年份,在这种情况下,它可以简化为:

> 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

或者如果我们想要安全,我们可以这样做,这将强制它是每年的,即使输入偶然具有较低的频率:频率(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

在这个例子中,它没有什么区别,但在这种情况下 z <- Zoo( c(1, 3, 6), c(1981, 1983, 1985), 频率 = 1) 需要显式的 频率 来防止其频率为 0.5 。

As has been pointed out the as.Date(as.character(t), "%Y") is incorrect as it does not give the desired month and day. If we wanted to convert years to "Date" class we could do this as.Date(as.yearmon(t)) using zoo's as.yearmon; 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) or just define the original zoo series to have a frequency of 1 right from the beginning:

> 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 .

·深蓝 2024-10-03 18:54:47

这不是一个错误。您的时间序列中有 4 年跨越 1,461 天。当我第一次运行它时,它对我不起作用。 as.Date(t,"%Y") 不知道使用哪个月/日来创建日期,因此它使用今天的月/日。这不利于可重复的分析。试试这个:

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)

产生:

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

It's not a bug. There are 1,461 days spanning the 4 years in your time series. 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. 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文