[R-SIG-Finance] Seasonal plot of daily data

Arun Kumar Saha arun.kumar.saha at gmail.com
Tue Aug 3 20:50:52 CEST 2010


Reply on the posting at
http://r.789695.n4.nabble.com/Seasonal-plot-of-daily-data-td2310429.html#a2310429

In case understanding seasonality is the prime concern then I would
not suggest to work with data on daily frequency. In daily data you
would find so much noise, hardly any inference can be drawn on.
Therefore some sort of working to reduce the noise may be somewhat
better idea to work with. One possible alternative could be work with
Monthly average. If some month is fundamentally strong then one should
expect it's average would be higher as compared to the other months.

Anyway following UDF might be helpful to you. Which creates monthly
average (other kind of statistic can be easily calculated after little
tweaking this code) from daily data and plots
accordingly...........Please note that, you would need to have 2
libraries viz. "zoo" & "ggplot2" pre-installed.

seasonalityPlot =
function(dat = dat, start.mo = 1) {
      library(zoo)
      library(ggplot2)
      mm = as.yearmon(index(dat)); dat = aggregate(dat, by = mm, FUN = "mean")
      mo.start.include = as.numeric(format(index(dat)[1], "%m")) - start.mo +
                         ifelse(as.numeric(format(index(dat)[1],
"%m")) - start.mo > 0 |
                                as.numeric(format(index(dat)[1],
"%m")) - start.mo == 0, 0, 12)
      end.mo = ifelse(start.mo == 1, 12, start.mo-1)
      data.end = as.numeric(format(index(dat)[length(index(dat))], "%m"))
      mo.end.include = ifelse(end.mo > data.end | end.mo > data.end,
end.mo - data.end, 12 - abs(end.mo - data.end))
      data1 = zooreg(c(rep(NA, mo.start.include), coredata(dat),
rep(NA, mo.end.include)),
                       as.yearmon(paste(ifelse(start.mo ==
as.numeric(format(index(dat)[1], "%m")) | start.mo <
as.numeric(format(index(dat)[1], "%m")),
as.numeric(format(index(dat)[1], "%Y")),
as.numeric(format(index(dat)[1], "%Y"))-1),
                                  ifelse(start.mo < 10,
paste("0",start.mo,sep=""), as.character(start.mo)), "01", sep="-")),
                       frequency=12 )
      df1 = data.frame(QUOTE = coredata(data1),
                        MONTH = format(index(data1)[1:12], "%b"),
                        YEAR =  paste("Year",
rep(1:(length(data1)%/%12), each=12), sep="-"))
      df1$MONTH = suppressWarnings(relevel(df1[,2], as.vector(df1[,2][1:12])))
      df1$YEAR = suppressWarnings(relevel(df1[,3], unique(as.vector(df1[,3]))))
      p = ggplot(df1) + geom_line(aes(y=QUOTE, x=MONTH, group=YEAR,
colour=YEAR), size=1.2)
      return(list(dat=df1, p=p)) }

You just need to copy-paste entire code and need to put some daily TS
as "zoo" object. Here is an example

library(quantmod)   # required to download data from net
dat = as.zoo(get(getSymbols("^NSEI")))[,4]
plot(dat)
seasonalityPlot(dat, 1)

Please note that, here you would have additional option (2nd argument)
to select the base month, which is by default kept as January (jan =
1).

I would also like to know from peers how above function can be
improved further...................
_____________________________________________________

Arun Kumar Saha, FRM
QUANTITATIVE RISK AND HEDGE CONSULTING SPECIALIST
Visit me at : http://in.linkedin.com/in/ArunFRM



More information about the R-SIG-Finance mailing list