K-Means Clustering: An Example of Stock Returns and Dividend Yields

A K-Means Clustering algorithm allows us to group observations in close proximity to the mean, which lends to greater efficiency in categorisation of data into specific segments.

This type of analysis can be employed across many different types of situations where we wish to analyse data broken down into specific groups and networks, including social network analysis, market segmentation, and so on.

In this particular example, we use a hypothetical dataset of stock returns and dividends in order to segment our data into different clusters and analyse the relative groupings of stocks based on their 1-year returns and dividend yields. We use the sample_stock dataset (csv found below).

Number of Clusters

Firstly, the appropriate number of clusters for the data must be determined in R as follows:

Determine number of clusters
wss <- (nrow(sample_stocks)-1)*sum(apply(sample_stocks,2,var))
for (i in 2:20) wss[i] <- sum(kmeans(sample_stocks,
centers=i)$withinss)
plot(1:20, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")

In the above code, we are using a maximum of 20 clusters (which is arbitrary, we could have chosen 5 as the maximum possible number of clusters) through plotting 1:20. Once this is done, we then identify the ideal number of clusters at the point at which there is a cut off in our scree plot:

3-clusters

From the above, we see that our scree plot cuts off at 3 clusters; the point at which the “Within groups sum of squares” is minimised at the third cluster.

K-Means Cluster Analysis

In this context, we now compute our k-means algorithm and plot output setting the number of specified clusters equal to 3:

# K-Means Cluster Analysis
fit <- kmeans(sample_stocks, 3) # 3 cluster solution
# get cluster means
aggregate(sample_stocks,by=list(fit$cluster),FUN=mean)
# append cluster assignment
sample_stocks <- data.frame(sample_stocks, fit$cluster)
sample_stocks
sample_stocks$fit.cluster <- as.factor(sample_stocks$fit.cluster)
library(ggplot2)
ggplot(sample_stocks, aes(x=dividendyield, y=returns, color = sample_stocks$fit.cluster)) + geom_point()

Upon running this code, we see that our data has been “clustered” into three separate segments, and even though we do have a few outliers, our data has largely been separated into distinct groups:

3clustersegment

From this graph, we can identify that stocks which have had higher dividend yields have also seen higher returns. In this regard, k-means clustering allows us to identify patterns in data that would not otherwise be apparent through traditional methods of analysis.

Dataset

sample_stocks.csv

Michael Grogan
Michael Grogan is a data science consultant, with a profound passion for statistics and programming. He regularly utilises statistical programming languages such as Python, R and SQL to analyse a wide range of datasets, with the goal of exploring in depth the tools and methods used across the ever expanding field of data science and business analytics.
Michael Grogan on sabtwitterMichael Grogan on sablinkedinMichael Grogan on sabgoogleMichael Grogan on sabgithubMichael Grogan on sabemail
Subscribe to my mailing list, and receive my free e-book; "How To Use Regression Modelling To Analyse Financial Markets".
Subscribe to my mailing list, and receive my free e-book; "How To Use Regression Modelling To Analyse Financial Markets".
Never display this again