# An Example of Seasonality Analysis

February 3, 2013
By

(This article was first published on Systematic Investor » R, and kindly contributed to R-bloggers)

Today, I want to demonstrate how easy it is to create a seasonality analysis study and produce a sample summary report. As an example study, I will use S&P Annual Performance After a Big January post by Avondale Asset Management.

The first step is to load historical prices and find Big Januaries.

```###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
setInternet2(TRUE)
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
source(con)
close(con)

#*****************************************************************
#******************************************************************

price = getSymbols('^GSPC', src = 'yahoo', from = '1900-01-01', auto.assign = F)

# convert to monthly
price = Cl(to.monthly(price, indexAt='endof'))
ret = price / mlag(price) - 1

#*****************************************************************
# Find Januaries with return > 4%
#******************************************************************
index =  which( date.month(index(ret)) == 1 & ret > 4/100 )

# create summary table with return in January and return for the whole year
temp = c(coredata(ret),rep(0,12))
out = cbind(ret[index], sapply(index, function(i) prod(1 + temp[i:(i+11)])-1))
colnames(out) = spl('January,Year')
```

All the hard work is done now, let’s create a chart and table to summarize the S&P Annual Performance After a Big January numbers.

```	#*****************************************************************
# Create Plot
#******************************************************************
pos = barplot(100*out, border=NA, beside=T, axisnames = F, axes = FALSE,
col=col, main='Annual Return When S&P500 Rises More than 4% in January')
axis(1, at = colMeans(pos), labels = date.year(index(out)), las=2)
axis(2, las=1)
grid(NA, NULL)
abline(h= 100*mean(out\$Year), col='red', lwd=2)
plota.legend(spl('January,Annual,Average'),  c(col,'red'))

# plot table
plot.table(round(100*as.matrix(out),1))
```

That is it, we are done.

Takeaways: It is very easy to create a seasonality analysis study. Next you might want to schedule to run the study script at specific times through out the year and send you a remainder email in case the study conditions are met.

To view the complete source code for this example, please have a look at the bt.seasonality.january.test() function in bt.test.r at github.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...