【问题标题】:How to turn (interpolate) this irregularly spaced time series into a regularly spaced one in R or Matlab?如何在 R 或 Matlab 中将这个不规则间隔的时间序列转换(插值)成规则间隔的时间序列?
【发布时间】:2016-08-01 19:38:29
【问题描述】:

我有以下数据:

Lines = "20/03/2014,9996792524
21/04/2014,8479115468
21/09/2014,11394750532
16/10/2014,9594869828
18/11/2014,10850291677
08/12/2014,10475635302
22/01/2015,10116010939
26/02/2015,11206949341
20/03/2015,11975140317
09/04/2015,11526960332
29/04/2015,9986194500
16/09/2015,11501088256
13/10/2015,11833183163
10/11/2015,13246940910
16/12/2015,13255698568
27/01/2016,13775653990
23/02/2016,13567323648
22/03/2016,14607415705
11/04/2016,13835444224
04/04/2016,14118970743"

我将这个读入 R:

z <- read.zoo(text = Lines, sep = ",", header = TRUE, index = 1:1, tz = "", format = "%d/%m/%Y")

我希望插入数据,以便我可以将这个不规则间隔的时间序列转换为常规时间序列。时间间隔无关紧要,只要它是定期的,但每月、每周或每两周的时间间隔都可以。

如何在RMatlab 中执行此操作?

注意:我意识到插值可能不是很准确,并且可能会歪曲信息,但是我需要学习如何做到这一点,并且我可以失去一些准确性。

【问题讨论】:

  • 好吧,您可以为数据点之间的值建立一个模型,但简单的事实是,您无法知道未测量的数据的值。

标签: r time-series forecasting matlab interpolation


【解决方案1】:

好的,首先,警告:如果您要进行插值然后执行测试或通用统计估计,您的结果将(严重)有偏差,除非您有充分的理由(领域知识?)假设您的插值方法将生成来自原始点的相同分布的点。不,“情节看起来不错”不是评估这一点的好标准:) 话虽如此,让我们来看看数据:

# Lines contains your data
library(zoo)
fmt <- "%d/%m/%Y" 
z <- read.zoo(text = Lines, sep = ",", header = TRUE, index = 1:1, tz = "", format = fmt)
t <- time(z)
plot(z,type="p",xaxt="n",pch=19,col="cyan",cex=1.5)
labs <- format(t,fmt)
axis(side = 1, at = t, labels = labs,cex.axis = 0.7)    

看起来您丢失的大部分数据都与 2014 年夏季和 2015 年夏季有关。我很想知道这些数据是什么...无论如何,您的大部分数据看起来至少相隔 2 周:

diff(t)
# Time differences in days
# [1] 153  25  33  20  45  35  22  20  20 140  27  28  36  42  27  28  13   7

因此,让我们通过首先创建一个虚拟 zoo 对象来插入双周系列:

t.biweekly <- seq(from = min(t), to=max(t),by="2 weeks")
dummy <- zoo(,t.biweekly)

将虚拟系列与你的合并:

z.interpolated <- merge(z,dummy,all=TRUE)

如果您查看新系列,您会发现dummy 的所有时间都有 NA 值,而z 中没有对应的时间。让我们用线性插值填充这些点并查看结果:

z.interpolated <- na.approx(z.interpolated)
plot(z.interpolated, type = "b")
points(z,pch=19,col="cyan",cex=1.5)

瞧!请记住,从这件事中构建推理模型是个坏主意……

【讨论】:

  • 谢谢!虽然我确实意识到在预测模型中使用这些插值是一个坏主意,但我还是很想看到结果。所以我尝试了:fcst &lt;- forecast(z.interpolated, h=5) 它说:Error in ets(object, lambda = lambda, allow.multiplicative.trend = allow.multiplicative.trend, : You've got to be joking. I need more data! In addition: Warning message: In ets(object, lambda = lambda, allow.multiplicative.trend = allow.multiplicative.trend, : Missing values encountered. Using longest contiguous portion of time series 什么缺失值?
  • 你真的想在一条腿上开枪,我明白了 :D 继续,但你应该知道forecast(以及包中的大多数其他功能forecast)适用于@987654338 @ 对象,而不是 zoo 对象。如何将zoo 对象转换为ts 对象是另一个无法在 cmets 中回答的问题,但是对于可能会弄乱您的日期(但仍然让您获得预测)的快速方法,请参阅this question。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 2014-09-02
  • 2014-11-08
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 2020-07-19
相关资源
最近更新 更多