tags:

views:

92

answers:

3

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.

A: 

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.

Dirk Eddelbuettel
A: 

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"
Joris Meys
A: 

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.

GailH
I suggest you study the documentation instead. Regarding `gamma`, `?HoltWinters` says: "gamma: gamma parameter used for the seasonal component. If set to 'FALSE', an non-seasonal model is fitted." This is not a bug, it is documented behavior! Why are you surprised that it expects `gamma` to be logical when the documentation says so?
Joshua Ulrich
@Joshua : solves the bug in *OP's* code...
Joris Meys