Created
March 19, 2019 16:39
-
-
Save Fahad021/0489cd221809bb0adaa755476af33236 to your computer and use it in GitHub Desktop.
Clustering Time Series Data
This file contains hidden or 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