在 R 中,类 ts 和类 timeSeries 有什么区别?
在 R 中,类 ts
和类 timeSeries
之间有什么区别?我认为我因此在 HoltWinters
中遇到了问题。我得到:
data(LakeHuron)
x <- LakeHuron
before <- window(x, end=1935)
after <- window(x, start=1935)
a <- .2
b <- 0
g <- 0
model <- HoltWinters(before, alpha=a, beta=b, gamma=g)
“分解错误(ts(x[1L:wind],start = start(x),频率= f),季节性): 时间序列没有或少于 2 个周期"
即使 gamma=0
,
。在 Windows 7 x64 计算机上运行 R 2.11.1 (win32 x86)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ts
来自基本 R 中包含的stats
包。它对于常规时间序列非常有用,例如每月、每季度、每年……政府统计中常见的系列。ts
由arima()
以及基础 R 及其stats
包提供的其他时间序列方法使用。您在此处使用的HoltWinters
就是这样一个示例。timeSeries
是许多附加时间序列类之一;这个来自 Rmetrics。几个 CRAN 任务视图对此进行了更多讨论:TimeSeries, 计量经济学以及金融。尝试使用
ts
和/或HoltWinters
上的文档来掌握所需的格式。ts
使用固定增量(例如每月数据为1/12)或频率。ts
comes from thestats
package included with base R. It is useful for regular time series such as monthly, quarterly, annual, ... series common in goverment statistics.ts
is used byarima()
and other time series methods provided by base R and itsstats
packages.HoltWinters
which you used here is one such example.timeSeries
is one of many add-on time series classes; this one comes from Rmetrics. Several CRAN Task Views discuss these more: TimeSeries, Econometrics as well as Finance.Try the documentation on
ts
and/orHoltWinters
to come to grips with the required format.ts
uses either a fixed delta (eg 1/12 for monthly data) or frequency.我通过研究 HoltWinters 源代码发现了问题。
事实证明,HoltWinters 函数(对于 gamma=0,并且没有季节性分量)期望 gamma 是合乎逻辑的! (零 = FALSE)
因此,输入 gamma as.logic(0) 可以解决该错误。
Joris:谢谢您的回答,很有启发性。
I've found the problem, studying the HoltWinters source code.
It turns out that the HoltWinters function, (for gamma=0, and if there is no seasonal component), expects gamma to be logical!! (zero = FALSE)
So, entering gamma as.logical(0) solves the bug.
Joris: thank you for the answer, that was illuminating.
这是两个不同的班级。
ts
包含在基本 R 安装中,函数HoltWinters()
需要一个ts
时间序列。timeSeries
具有完全不同的结构。它还专门针对财务。与 ts 的最大区别在于它允许不规则的时间序列。类ts
只能保存等距序列。在内部,ts 有一个槽“tsp”,其中包含时间序列的开始、结束和频率。
HoltWinters()
需要但 timeSeries 中缺少此槽。其中,关于时间的信息包含在两个时隙中:位置时隙和格式时隙。它们一起将时间定义为timeDate
对象。It's two separate classes.
ts
is contained in the basic R installation, and the functionHoltWinters()
demands ats
time series.timeSeries
has a completely different structure. It's also specifically directed towards finances. The big difference with ts is that it allows for irregular timeseries. The classts
can only hold equispaced series.Internally, ts has a slot "tsp" which contains the start, end and frequency of the timeseries.
It's this slot that
HoltWinters()
needs but lacks in timeSeries. There the information on the times is contained in two slots, a position slot and a format slot. Together they define the times as atimeDate
object.