[R] Could someone recommend a package for time series?
ggrothendieck at gmail.com
Mon Sep 29 15:00:17 CEST 2014
On Mon, Sep 29, 2014 at 5:05 AM, jpm miao <miaojpm at gmail.com> wrote:
> I've not used R for about one year and don't know well about the updates
> on the time series-related package.
> My primary job is to do economic/financial time series data analysis -
> annual, monthly, daily, etc. I usually read data by the package
> "XLConnect", which can read xls or xlsx files directly. It's excellent.
> However I can't find a package to manipulate time series data. For example,
> I just want to do an easy manipulation , e.g, to label the dates of the
> data from , say, 1991M10 to 2014M07, and then extract part of the data,
> say, 2005M01 to 2010M12 and do analysis. Is there any package work well for
> my purpose?
> I sometimes need to aggregate monthly data to quarterly data and I find
> "aggregate" function helpful.
> In the past I used packages xts, zoo and don't find it really user
> friendly. Maybe I haven't mastered it; maybe there're some updates (which I
> don't know) now. Could someone recommend a package or provide an example
> (or just the document, I can read it) for my purpose?
The built in "ts" class works well with regularly spaced monthly and
quarterly data but is less suitable for daily data since it cannot
represent exact dates.
What you have described is very easy to do with zoo and/or xts.
If you are familiar with the core functions of R then zoo is pretty
easy to use since nearly all its functions are methods of core
generics allowing you to leverage your knowledge of R. See:
for the design principles used and all 5 vignettes:
vignette(package = "zoo")..
xts (which works with zoo) could also be of interest as well as 139
other packages that work with zoo and/or xts which means that in many
cases whatever functionality you need already exists. See the bottom
of each of these pages for links to the other packages:
Regarding your problem, zoo does have built in yearmon and yearqtr
classes for monthly and quarterly data. Here is an example which
creates some daily test data, aggregates to monthly, extracts a subset
and displays the first few data points. Then it aggregates to
quarterly and displays the first few data points At the end it plots
the data using zoo's classic graphics plot.zoo method. ( zoo also has
direct support for ggplot2 (autoplot.zoo) and lattice graphics
# create test data, z
tt <- seq(as.Date("2000-10-01"), as.Date("2013-12-31"), by = "day")
z <- zoo(seq_along(tt), tt)
# aggregate to monthly series and change time scale to year/month
zm <- aggregate(z, as.yearmon, mean)
# extract part of it
zm0 <- window(zm, start = "2005-01", end = "2010-12")
## Jan 2005 Feb 2005 Mar 2005 Apr 2005 May 2005 Jun 2005
## 1569.0 1598.5 1628.0 1658.5 1689.0 1719.5
# aggregate to quarterly
zq <- aggregate(zm0, as.yearqtr, mean)
## 2005 Q1 2005 Q2 2005 Q3 2005 Q4 2006 Q1 2006 Q2
## 1598.500 1689.000 1780.833 1872.500 1963.500 2054.000
More information about the R-help