Showing posts with label Quantmod. Show all posts
Showing posts with label Quantmod. Show all posts

Saturday, May 7, 2011

K-means clustering using R

I'm trying to learn R, and I'm a firm believer that there's not better way to learn than by getting your hands dirty. After reading an excellent post on Intelligent Trading blog, it got me thinking how you would do a clustering analysis with R, using K-means.

In the rest of this post I will try to detail the different steps that I followed, in hope that it can be useful to others. In this post I will be using a couple of R packages, namely quantmod, fpc and a few others. The most crucial is quantmod. You can install it with:



They are used for:

  • quantmod: this is your bread and butter to ease time series analysis
  • graphics, scatterplot3d, gplots RColorBrewer are used for plotting
  • fpc: this packages is package dedicated to clustering 


First this article supposes that you already have your data handy in a xts object used by quantmot. If it's not the case have a look at this article.

In the details below, "x" is the name of the object that contains my timeserie. Now let dig into it.

The first thing you need to do is create matrix with the different criteria you want to use for the clustering. In my case I'm going to use three normalized ratios, Close / Open, High / Open,  Low / Open , and then stuff them into a matrix. Then you want to process the actual cluster and display it:



The result looks like:
This works pretty well, except you have to specify a number of cluster you're looking for. Another may to do this is through the pamk function from the fpc package, for for which you don't specify the actual number of cluster, it will be calculated (you provide a range of value though):

Note the difference with kmeans method, the cluster information is packed into a pam object. That why you access the cluster details through zpamk$pamobject.

Enjoy.

Thursday, April 21, 2011

How to load csv data with quantmod in R

In this article I'm going to describe how to load data in an xts object, which is the preferred object to handle time series quantmod from a CSV file. This article is really for beginner (like myself), hopefully those can be helpful to others.

Let's imagine you have some csv data that look like:

First thing you need to make sure is that you column headers have names that contain  Open, High, Low and Close. This will make you life much easier. Secondly, your csv file should only contain dates, and numerical data, otherwise it's kind of a hassle and you have to go and manually convert data afterwards. So  what your data should really look like is:


Personnally I "massaged" the data in R, but you could very well do that in Excel, Perl, Python, or whatever tool you fancy (or simply correct the data at the source ;) ) but for the sake of example this is how I did it:


Now that you have the correct data, it's pretty simple to load :



You're now the proud owner of an xts object that you can start abusing in all the ways you'd like ;)



Hope this helps