Created
July 8, 2018 21:11
-
-
Save mzkaramat/59a26577f40948399ee97558023a7353 to your computer and use it in GitHub Desktop.
Clustering Time Series Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(quantmod) | |
symbols = c('A', 'AAPL', 'ADBE', 'AMD', 'AMZN', 'BA', 'CL', 'CSCO', 'EXPE', 'FB', 'GOOGL', | |
'GRMN', 'IBM', 'INTC', 'LMT', 'MSFT', 'NFLX', 'ORCL', 'RHT', 'YHOO') | |
start = as.Date("2014-01-01") | |
until = as.Date("2014-12-31") | |
# Grab data, selecting only the Adjusted close price. | |
# | |
stocks = lapply(symbols, function(symbol) { | |
adjusted = getSymbols(symbol, from = start, to = until, auto.assign = FALSE)[, 6] | |
names(adjusted) = symbol | |
adjusted | |
}) | |
# Merge by date | |
# | |
stocks = do.call(merge.xts, stocks) | |
# Convert from xts object to a matrix (since xts not supported as input for TSclust) | |
# Also need to transpose because diss() expects data to be along rows. | |
# | |
stocks = t(as.matrix(stocks)) | |
library(TSclust) | |
D1 <- diss(stocks, "COR") | |
summary(D1) | |
sort(rowMeans(as.matrix(D1))) | |
C1 <- hclust(D1) | |
D2 <- diss(stocks, "FRECHET") | |
D3 <- diss(stocks, "DTWARP") | |
D4 <- diss(stocks, "INT.PER") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment