Rajiv
Rajiv

Reputation: 300

Analyzing Daily/Weekly data using ts in R

I have just started playing with the ts class to analyze some time series data I have.

I am getting a sense that the ts class is not well suited for analyzing daily or weekly data. Almost all the examples I see on the web or in Cowpertwait and Metcalfe's "Introductory Time Series with R" use monthly or annual data.

I would like to use ts to analyze weekly data, is it not advisable ?

Upvotes: 18

Views: 34512

Answers (5)

Tony Ladson
Tony Ladson

Reputation: 3639

zoo works well with daily data. For example, if you had a daily series of streamflows in a vector Q and with corresponding date stamps D (created using as.Date(), for example).

Q.z <- zoo(Q, order.by=D) 

will create an object that will plot nicely and you can use functions such as window() to extract single years e.g.

window(Q.z, start = as.Date('2000-01-01'), end=as.Date('2000-31-12')

Check the zoo package for more information.

Upvotes: 5

G. Grothendieck
G. Grothendieck

Reputation: 269441

The main problem is that most people who want to analyse daily or weekly data want to do something like use a frequency of 365 days per year or 52 weeks per year but the number of days or weeks in a year is not 365 or 52 or even constant. If you drop one day in leap years in the case of daily data, say, or use 7 days for each week of the year except one which has 8 or 9 days in the case of weekly data, say, then you can fudge it. An endless variety of schemes is possible.

Read through this thread for an example.

Upvotes: 12

MJV
MJV

Reputation: 11

I suppose the problem with daily/weekly data is that both have leap units. Every fourth year has one additional 366 days and every fourth year has 53 weeks. Or has had since 1900. So the "frequency" is either not constant or a fraction. Otherwise ts should work just fine.

Easiest way around weeks is to use the date of the start of the week. On unixlike systems you can also use ISO weeks, and there's also a package that implements them. Of course if your data is already in weeks, you only need these for labeling the graphs etc.

Upvotes: 1

Andrie
Andrie

Reputation: 179398

The ts class is suitable for any type of data that has strictly metronomic intervals between observations. Whether that interval is years, months, days, minutes doesn't make any difference. This makes it very suitable for much scientific data, e.g. weather observations.

However, a large class of time-related data has gaps. Take for example stock closing prices which doesn't exist for weekend days. For this type of data you'd be better off using the packages xts (extensible time series) or zoo(Z's ordered observations - where Z is the initial of one of the package authors).

Links to CRAN:

Upvotes: 10

Isaac
Isaac

Reputation: 16362

The unit of time doesn't come into play; only the number of datapoints per unit (the frequency) do. As far as ts and its functions care, a day is a year is a decade is a whatever. At no point do you tell it that the time unti it's dealing with is a week or day. It doesn't matter!

Upvotes: 4

Related Questions