[R-SIG-Finance] Clustering

me me at censix.com
Sat Jun 22 18:48:18 CEST 2013


On Sat, 22 Jun 2013 07:19:38 -0700
BBands <bbands at gmail.com> wrote:

> Good morning,
> 
> I am interested in doing some work on clustering stocks and would like
> to chat with anyone who has had some experience in the area. I am
> primarily interested in creating industry groups from larger lists of
> stocks. We already do this is a semi numerical manner, so I have a lot
> of experience with the issues. R offers several approaches and I am
> hoping not to have to explore them all. Off list is fine if you
> prefer.
> 
> Thanks,
> 
>       John -- who is really sorry that he missed the conference, but
> the daughter only graduates from college once
> 
> --
> John Bollinger
> www.BollingerBands.com
> 
> _______________________________________________
> R-SIG-Finance at r-project.org 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.



John

Not an expert, but here is one way to do it (This example was sitting
around on my drive). By using a sufficiently large stock universe and a
reasonably small number of clusters (kk) you should see sectors emerge
'naturally'. One could try and use weekly or monthly returns instead of
daily returns.

regards

Soren



require(quantmod)

symList <- c('MSN','GOOG','YHOO', 'BA', 'SI', 'BP', 'AMD','AMGN.MX')
getSymbols(symList)

# Matrix of daily returns. Or use weekly, monthly returns...
returns.mat <- NULL
for (sym in symList) returns.mat<- cbind( returns.mat,
dailyReturn( Ad(get(sym)) ) )

colnames(returns.mat) <- symList


# Use kmeans to group into kk clusters
kk <- 3
KM <- kmeans( x=t(returns.mat), centers=kk, nstart=20)   
# Important: nstart should be set to around 20, to 
# make sure the clustering result is stable

# Print the result 
for (ii in 1:kk) {
  print(paste('######### Cluster:',ii))
  print( colnames(returns.mat)[KM$cluster==ii])
  print(' ')
}





------------------------------------------------------------------
Soren Wilkening

http://censix.com



More information about the R-SIG-Finance mailing list