在 R 中,类 ts 和类 timeSeries 有什么区别?

发布于 2024-09-25 16:51:27 字数 523 浏览 0 评论 0 原文

在 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)。

In R, what is the difference between class ts and class timeSeries? I think I am getting a problem in HoltWinters because of that. I'm getting:

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)

"Error in decompose(ts(x[1L:wind], start = start(x), frequency = f), seasonal) :
time series has no or less than 2 periods"

even though gamma=0.

Running R 2.11.1 (win32 x86) on a Windows 7 x64 machine.

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

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

发布评论

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

评论(3

铃予 2024-10-02 16:51:27

ts 来自基本 R 中包含的 stats 包。它对于常规时间序列非常有用,例如每月、每季度、每年……政府统计中常见的系列。 tsarima() 以及基础 R 及其 stats 包提供的其他时间序列方法使用。您在此处使用的 HoltWinters 就是这样一个示例。

timeSeries 是许多附加时间序列类之一;这个来自 Rmetrics。几个 CRAN 任务视图对此进行了更多讨论:TimeSeries, 计量经济学以及金融

尝试使用 ts 和/或 HoltWinters 上的文档来掌握所需的格式。 ts 使用固定增量(例如每月数据为1/12)或频率

ts comes from the stats 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 by arima() and other time series methods provided by base R and its stats 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/or HoltWinters to come to grips with the required format. ts uses either a fixed delta (eg 1/12 for monthly data) or frequency.

追风人 2024-10-02 16:51:27

我通过研究 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.

Smile简单爱 2024-10-02 16:51:27

这是两个不同的班级。 ts 包含在基本 R 安装中,函数 HoltWinters() 需要一个 ts 时间序列。

timeSeries 具有完全不同的结构。它还专门针对财务。与 ts 的最大区别在于它允许不规则的时间序列。类ts只能保存等距序列。

在内部,ts 有一个槽“tsp”,其中包含时间序列的开始、结束和频率。

> test <- ts(1:10, frequency = 4, start = c(1959, 2))
> slotNames(test)
[1] ".Data"    "tsp"      ".S3Class"
> slot(test,"tsp")
[1] 1959.25 1961.50    4.00

HoltWinters() 需要但 timeSeries 中缺少此槽。其中,关于时间的信息包含在两个时隙中:位置时隙和格式时隙。它们一起将时间定义为 timeDate 对象。

> data = as.matrix(MSFT[, 4])
>    charvec = rownames(MSFT)
>    Close = timeSeries(data, charvec, units = "Close")
> slotNames(Close)
[1] ".Data"         "units"         "positions"     "format"        "FinCenter"     "recordIDs"     "title"         "documentation"
> head(slot(Close,"positions"))
[1] 970012800 970099200 970185600 970444800 970531200 970617600
> slot(Close,"format")
[1] "%Y-%m-%d"

It's two separate classes. ts is contained in the basic R installation, and the function HoltWinters() demands a ts 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 class ts can only hold equispaced series.

Internally, ts has a slot "tsp" which contains the start, end and frequency of the timeseries.

> test <- ts(1:10, frequency = 4, start = c(1959, 2))
> slotNames(test)
[1] ".Data"    "tsp"      ".S3Class"
> slot(test,"tsp")
[1] 1959.25 1961.50    4.00

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 a timeDate object.

> data = as.matrix(MSFT[, 4])
>    charvec = rownames(MSFT)
>    Close = timeSeries(data, charvec, units = "Close")
> slotNames(Close)
[1] ".Data"         "units"         "positions"     "format"        "FinCenter"     "recordIDs"     "title"         "documentation"
> head(slot(Close,"positions"))
[1] 970012800 970099200 970185600 970444800 970531200 970617600
> slot(Close,"format")
[1] "%Y-%m-%d"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文