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

Jeff Ryan jeff.a.ryan at gmail.com
Tue Aug 3 22:03:06 CEST 2010


Not as pretty as ggplot of course, but for those looking for a simple
solution using base graphics:

par(mar=c(3,3,3,3))

dat_by_yr <- lapply(split.xts(apply.monthly(dat,mean),'years'),coredata)

# set up the plot window
plot.new()
plot.window(c(1,12), c(2800,6000))

# add axis and grid lines
axis(2)
axis(1,at=1:12,label=month.abb)
grid()

for(i in 1:4) lines(dat_by_yr[[i]], col=i, lwd=3)

The real value to this post comes from apply.monthly and split.xts.

HTH
Jeff

On Tue, Aug 3, 2010 at 1:50 PM, Arun Kumar Saha
<arun.kumar.saha at gmail.com> wrote:
> 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
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list