library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
startDate = as.Date("2010-01-01")
endDate = as.Date("2015-12-31")
Symbols<-c("MMM","AXP","AAPL","BA","CAT","CVX","CSCO","KO","DD","XOM","GE","GS","HD","INTC","IBM","JNJ","JPM","MCD","MRK","MSFT","NKE","PFE","PG","TRV","UNH","UTX","VZ","V","WMT","DIS")
dataEnv<-new.env()
getSymbols(Symbols,env=dataEnv,from=startDate,to=endDate)
## As of 0.4-0, 'getSymbols' uses env=parent.frame() and
## auto.assign=TRUE by default.
##
## This behavior will be phased out in 0.5-0 when the call will
## default to use auto.assign=FALSE. getOption("getSymbols.env") and
## getOptions("getSymbols.auto.assign") are now checked for alternate defaults
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## [1] "MMM" "AXP" "AAPL" "BA" "CAT" "CVX" "CSCO" "KO" "DD" "XOM"
## [11] "GE" "GS" "HD" "INTC" "IBM" "JNJ" "JPM" "MCD" "MRK" "MSFT"
## [21] "NKE" "PFE" "PG" "TRV" "UNH" "UTX" "VZ" "V" "WMT" "DIS"
ls(dataEnv)
## [1] "AAPL" "AXP" "BA" "CAT" "CSCO" "CVX" "DD" "DIS" "GE" "GS"
## [11] "HD" "IBM" "INTC" "JNJ" "JPM" "KO" "MCD" "MMM" "MRK" "MSFT"
## [21] "NKE" "PFE" "PG" "TRV" "UNH" "UTX" "V" "VZ" "WMT" "XOM"
Returns <- eapply(dataEnv,dailyReturn)
ReturnsDF <- as.data.frame(do.call(merge, Returns))
colnames(ReturnsDF)<-c("AAPL", "AXP" , "BA" , "CAT", "CSCO", "CVX" , "DD", "DIS", "GE", "GS", "HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM","MRK", "MSFT", "NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V" , "VZ", "WMT", "XOM" )
hc = hclust(dist(1-cor(ReturnsDF)), method = 'ward.D2')
plot(hc, axes=F,xlab='', ylab='',sub ='', main='Correlation')
rect.hclust(hc, k=3, border='red')
Downloading data for first cluster
library(wikipediatrend)
startDate = as.Date("2010-01-01")
endDate = as.Date("2015-12-31")
PageViewClust1<-c("Visa Inc.","Nike, Inc.","Johnson & Johnson","Microsoft")
views1<-wp_trend(page ="Visa Inc." ,from =startDate ,to =endDate ,lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)
## Option 'requestFrom' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## Option 'friendly' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## The package now is friendly by default.
##
## Option 'userAgent' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## http://stats.grok.se/json/en/201001/Visa_Inc.
## http://stats.grok.se/json/en/201002/Visa_Inc.
## http://stats.grok.se/json/en/201003/Visa_Inc.
## http://stats.grok.se/json/en/201004/Visa_Inc.
## http://stats.grok.se/json/en/201005/Visa_Inc.
## http://stats.grok.se/json/en/201006/Visa_Inc.
## http://stats.grok.se/json/en/201007/Visa_Inc.
## http://stats.grok.se/json/en/201008/Visa_Inc.
## http://stats.grok.se/json/en/201009/Visa_Inc.
## http://stats.grok.se/json/en/201010/Visa_Inc.
## http://stats.grok.se/json/en/201011/Visa_Inc.
## http://stats.grok.se/json/en/201012/Visa_Inc.
## http://stats.grok.se/json/en/201101/Visa_Inc.
## http://stats.grok.se/json/en/201102/Visa_Inc.
## http://stats.grok.se/json/en/201103/Visa_Inc.
## http://stats.grok.se/json/en/201104/Visa_Inc.
## http://stats.grok.se/json/en/201105/Visa_Inc.
## http://stats.grok.se/json/en/201106/Visa_Inc.
## http://stats.grok.se/json/en/201107/Visa_Inc.
## http://stats.grok.se/json/en/201108/Visa_Inc.
## http://stats.grok.se/json/en/201109/Visa_Inc.
## http://stats.grok.se/json/en/201110/Visa_Inc.
## http://stats.grok.se/json/en/201111/Visa_Inc.
## http://stats.grok.se/json/en/201112/Visa_Inc.
## http://stats.grok.se/json/en/201201/Visa_Inc.
## http://stats.grok.se/json/en/201202/Visa_Inc.
## http://stats.grok.se/json/en/201203/Visa_Inc.
## http://stats.grok.se/json/en/201204/Visa_Inc.
## http://stats.grok.se/json/en/201205/Visa_Inc.
## http://stats.grok.se/json/en/201206/Visa_Inc.
## http://stats.grok.se/json/en/201207/Visa_Inc.
## http://stats.grok.se/json/en/201208/Visa_Inc.
## http://stats.grok.se/json/en/201209/Visa_Inc.
## http://stats.grok.se/json/en/201210/Visa_Inc.
## http://stats.grok.se/json/en/201211/Visa_Inc.
## http://stats.grok.se/json/en/201212/Visa_Inc.
## http://stats.grok.se/json/en/201301/Visa_Inc.
## http://stats.grok.se/json/en/201302/Visa_Inc.
## http://stats.grok.se/json/en/201303/Visa_Inc.
## http://stats.grok.se/json/en/201304/Visa_Inc.
## http://stats.grok.se/json/en/201305/Visa_Inc.
## http://stats.grok.se/json/en/201306/Visa_Inc.
## http://stats.grok.se/json/en/201307/Visa_Inc.
## http://stats.grok.se/json/en/201308/Visa_Inc.
## http://stats.grok.se/json/en/201309/Visa_Inc.
## http://stats.grok.se/json/en/201310/Visa_Inc.
## http://stats.grok.se/json/en/201311/Visa_Inc.
## http://stats.grok.se/json/en/201312/Visa_Inc.
## http://stats.grok.se/json/en/201401/Visa_Inc.
## http://stats.grok.se/json/en/201402/Visa_Inc.
## http://stats.grok.se/json/en/201403/Visa_Inc.
## http://stats.grok.se/json/en/201404/Visa_Inc.
## http://stats.grok.se/json/en/201405/Visa_Inc.
## http://stats.grok.se/json/en/201406/Visa_Inc.
## http://stats.grok.se/json/en/201407/Visa_Inc.
## http://stats.grok.se/json/en/201408/Visa_Inc.
## http://stats.grok.se/json/en/201409/Visa_Inc.
## http://stats.grok.se/json/en/201410/Visa_Inc.
## http://stats.grok.se/json/en/201411/Visa_Inc.
## http://stats.grok.se/json/en/201412/Visa_Inc.
## http://stats.grok.se/json/en/201501/Visa_Inc.
## http://stats.grok.se/json/en/201502/Visa_Inc.
## http://stats.grok.se/json/en/201503/Visa_Inc.
## http://stats.grok.se/json/en/201504/Visa_Inc.
## http://stats.grok.se/json/en/201505/Visa_Inc.
## http://stats.grok.se/json/en/201506/Visa_Inc.
## http://stats.grok.se/json/en/201507/Visa_Inc.
## http://stats.grok.se/json/en/201508/Visa_Inc.
## http://stats.grok.se/json/en/201509/Visa_Inc.
## http://stats.grok.se/json/en/201510/Visa_Inc.
## http://stats.grok.se/json/en/201511/Visa_Inc.
## http://stats.grok.se/json/en/201512/Visa_Inc.
views2<-wp_trend(page ="Nike, Inc." ,from =startDate ,to =endDate ,lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)
## Option 'requestFrom' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## Option 'friendly' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## The package now is friendly by default.
##
## Option 'userAgent' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## http://stats.grok.se/json/en/201001/Nike,_Inc.
## http://stats.grok.se/json/en/201002/Nike,_Inc.
## http://stats.grok.se/json/en/201003/Nike,_Inc.
## http://stats.grok.se/json/en/201004/Nike,_Inc.
## http://stats.grok.se/json/en/201005/Nike,_Inc.
## http://stats.grok.se/json/en/201006/Nike,_Inc.
## http://stats.grok.se/json/en/201007/Nike,_Inc.
## http://stats.grok.se/json/en/201008/Nike,_Inc.
## http://stats.grok.se/json/en/201009/Nike,_Inc.
## http://stats.grok.se/json/en/201010/Nike,_Inc.
## http://stats.grok.se/json/en/201011/Nike,_Inc.
## http://stats.grok.se/json/en/201012/Nike,_Inc.
## http://stats.grok.se/json/en/201101/Nike,_Inc.
## http://stats.grok.se/json/en/201102/Nike,_Inc.
## http://stats.grok.se/json/en/201103/Nike,_Inc.
## http://stats.grok.se/json/en/201104/Nike,_Inc.
## http://stats.grok.se/json/en/201105/Nike,_Inc.
## http://stats.grok.se/json/en/201106/Nike,_Inc.
## http://stats.grok.se/json/en/201107/Nike,_Inc.
## http://stats.grok.se/json/en/201108/Nike,_Inc.
## http://stats.grok.se/json/en/201109/Nike,_Inc.
## http://stats.grok.se/json/en/201110/Nike,_Inc.
## http://stats.grok.se/json/en/201111/Nike,_Inc.
## http://stats.grok.se/json/en/201112/Nike,_Inc.
## http://stats.grok.se/json/en/201201/Nike,_Inc.
## http://stats.grok.se/json/en/201202/Nike,_Inc.
## http://stats.grok.se/json/en/201203/Nike,_Inc.
## http://stats.grok.se/json/en/201204/Nike,_Inc.
## http://stats.grok.se/json/en/201205/Nike,_Inc.
## http://stats.grok.se/json/en/201206/Nike,_Inc.
## http://stats.grok.se/json/en/201207/Nike,_Inc.
## http://stats.grok.se/json/en/201208/Nike,_Inc.
## http://stats.grok.se/json/en/201209/Nike,_Inc.
## http://stats.grok.se/json/en/201210/Nike,_Inc.
## http://stats.grok.se/json/en/201211/Nike,_Inc.
## http://stats.grok.se/json/en/201212/Nike,_Inc.
## http://stats.grok.se/json/en/201301/Nike,_Inc.
## http://stats.grok.se/json/en/201302/Nike,_Inc.
## http://stats.grok.se/json/en/201303/Nike,_Inc.
## http://stats.grok.se/json/en/201304/Nike,_Inc.
## http://stats.grok.se/json/en/201305/Nike,_Inc.
## http://stats.grok.se/json/en/201306/Nike,_Inc.
## http://stats.grok.se/json/en/201307/Nike,_Inc.
## http://stats.grok.se/json/en/201308/Nike,_Inc.
## http://stats.grok.se/json/en/201309/Nike,_Inc.
## http://stats.grok.se/json/en/201310/Nike,_Inc.
## http://stats.grok.se/json/en/201311/Nike,_Inc.
## http://stats.grok.se/json/en/201312/Nike,_Inc.
## http://stats.grok.se/json/en/201401/Nike,_Inc.
## http://stats.grok.se/json/en/201402/Nike,_Inc.
## http://stats.grok.se/json/en/201403/Nike,_Inc.
## http://stats.grok.se/json/en/201404/Nike,_Inc.
## http://stats.grok.se/json/en/201405/Nike,_Inc.
## http://stats.grok.se/json/en/201406/Nike,_Inc.
## http://stats.grok.se/json/en/201407/Nike,_Inc.
## http://stats.grok.se/json/en/201408/Nike,_Inc.
## http://stats.grok.se/json/en/201409/Nike,_Inc.
## http://stats.grok.se/json/en/201410/Nike,_Inc.
## http://stats.grok.se/json/en/201411/Nike,_Inc.
## http://stats.grok.se/json/en/201412/Nike,_Inc.
## http://stats.grok.se/json/en/201501/Nike,_Inc.
## http://stats.grok.se/json/en/201502/Nike,_Inc.
## http://stats.grok.se/json/en/201503/Nike,_Inc.
## http://stats.grok.se/json/en/201504/Nike,_Inc.
## http://stats.grok.se/json/en/201505/Nike,_Inc.
## http://stats.grok.se/json/en/201506/Nike,_Inc.
## http://stats.grok.se/json/en/201507/Nike,_Inc.
## http://stats.grok.se/json/en/201508/Nike,_Inc.
## http://stats.grok.se/json/en/201509/Nike,_Inc.
## http://stats.grok.se/json/en/201510/Nike,_Inc.
## http://stats.grok.se/json/en/201511/Nike,_Inc.
## http://stats.grok.se/json/en/201512/Nike,_Inc.
views3<-wp_trend(page = "Johnson & Johnson",from =startDate ,to =endDate ,lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)
## Option 'requestFrom' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## Option 'friendly' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## The package now is friendly by default.
##
## Option 'userAgent' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## http://stats.grok.se/json/en/201001/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201002/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201003/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201004/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201005/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201006/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201007/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201008/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201009/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201010/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201011/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201012/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201101/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201102/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201103/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201104/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201105/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201106/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201107/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201108/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201109/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201110/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201111/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201112/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201201/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201202/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201203/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201204/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201205/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201206/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201207/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201208/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201209/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201210/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201211/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201212/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201301/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201302/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201303/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201304/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201305/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201306/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201307/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201308/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201309/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201310/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201311/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201312/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201401/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201402/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201403/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201404/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201405/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201406/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201407/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201408/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201409/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201410/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201411/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201412/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201501/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201502/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201503/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201504/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201505/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201506/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201507/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201508/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201509/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201510/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201511/Johnson_&%20Johnson
## http://stats.grok.se/json/en/201512/Johnson_&%20Johnson
views4<-wp_trend(page ="Microsoft",from =startDate ,to =endDate ,lang = "en",friendly = TRUE,requestFrom = "wp.trend.tester at wptt.wptt",userAgent = TRUE)
## Option 'requestFrom' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## Option 'friendly' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## The package now is friendly by default.
##
## Option 'userAgent' is deprecated and will cause errors
## in futuere versions of the wp_trend() function. Please read
## the package vignette and/or README to learn about the new
## set of options.
##
## Check wp_http_header() to know which information are send to
## stats.grok.se (R and package versions)
##
## http://stats.grok.se/json/en/201001/Microsoft
## http://stats.grok.se/json/en/201002/Microsoft
## http://stats.grok.se/json/en/201003/Microsoft
## http://stats.grok.se/json/en/201004/Microsoft
## http://stats.grok.se/json/en/201005/Microsoft
## http://stats.grok.se/json/en/201006/Microsoft
## http://stats.grok.se/json/en/201007/Microsoft
## http://stats.grok.se/json/en/201008/Microsoft
## http://stats.grok.se/json/en/201009/Microsoft
## http://stats.grok.se/json/en/201010/Microsoft
## http://stats.grok.se/json/en/201011/Microsoft
## http://stats.grok.se/json/en/201012/Microsoft
## http://stats.grok.se/json/en/201101/Microsoft
## http://stats.grok.se/json/en/201102/Microsoft
## http://stats.grok.se/json/en/201103/Microsoft
## http://stats.grok.se/json/en/201104/Microsoft
## http://stats.grok.se/json/en/201105/Microsoft
## http://stats.grok.se/json/en/201106/Microsoft
## http://stats.grok.se/json/en/201107/Microsoft
## http://stats.grok.se/json/en/201108/Microsoft
## http://stats.grok.se/json/en/201109/Microsoft
## http://stats.grok.se/json/en/201110/Microsoft
## http://stats.grok.se/json/en/201111/Microsoft
## http://stats.grok.se/json/en/201112/Microsoft
## http://stats.grok.se/json/en/201201/Microsoft
## http://stats.grok.se/json/en/201202/Microsoft
## http://stats.grok.se/json/en/201203/Microsoft
## http://stats.grok.se/json/en/201204/Microsoft
## http://stats.grok.se/json/en/201205/Microsoft
## http://stats.grok.se/json/en/201206/Microsoft
## http://stats.grok.se/json/en/201207/Microsoft
## http://stats.grok.se/json/en/201208/Microsoft
## http://stats.grok.se/json/en/201209/Microsoft
## http://stats.grok.se/json/en/201210/Microsoft
## http://stats.grok.se/json/en/201211/Microsoft
## http://stats.grok.se/json/en/201212/Microsoft
## http://stats.grok.se/json/en/201301/Microsoft
## http://stats.grok.se/json/en/201302/Microsoft
## http://stats.grok.se/json/en/201303/Microsoft
## http://stats.grok.se/json/en/201304/Microsoft
## http://stats.grok.se/json/en/201305/Microsoft
## http://stats.grok.se/json/en/201306/Microsoft
## http://stats.grok.se/json/en/201307/Microsoft
## http://stats.grok.se/json/en/201308/Microsoft
## http://stats.grok.se/json/en/201309/Microsoft
## http://stats.grok.se/json/en/201310/Microsoft
## http://stats.grok.se/json/en/201311/Microsoft
## http://stats.grok.se/json/en/201312/Microsoft
## http://stats.grok.se/json/en/201401/Microsoft
## http://stats.grok.se/json/en/201402/Microsoft
## http://stats.grok.se/json/en/201403/Microsoft
## http://stats.grok.se/json/en/201404/Microsoft
## http://stats.grok.se/json/en/201405/Microsoft
## http://stats.grok.se/json/en/201406/Microsoft
## http://stats.grok.se/json/en/201407/Microsoft
## http://stats.grok.se/json/en/201408/Microsoft
## http://stats.grok.se/json/en/201409/Microsoft
## http://stats.grok.se/json/en/201410/Microsoft
## http://stats.grok.se/json/en/201411/Microsoft
## http://stats.grok.se/json/en/201412/Microsoft
## http://stats.grok.se/json/en/201501/Microsoft
## http://stats.grok.se/json/en/201502/Microsoft
## http://stats.grok.se/json/en/201503/Microsoft
## http://stats.grok.se/json/en/201504/Microsoft
## http://stats.grok.se/json/en/201505/Microsoft
## http://stats.grok.se/json/en/201506/Microsoft
## http://stats.grok.se/json/en/201507/Microsoft
## http://stats.grok.se/json/en/201508/Microsoft
## http://stats.grok.se/json/en/201509/Microsoft
## http://stats.grok.se/json/en/201510/Microsoft
## http://stats.grok.se/json/en/201511/Microsoft
## http://stats.grok.se/json/en/201512/Microsoft
djia data and indicators
startDate = as.Date("2010-01-01")
endDate = as.Date("2015-12-31")
getSymbols("DJIA", src = "yahoo", from = startDate, to = endDate)
## [1] "DJIA"
RSI3<-RSI(Op(DJIA), n= 3)
#Calculate a 3-period relative strength index (RSI) off the open price
EMA5<-EMA(Op(DJIA),n=5)
#Calculate a 5-period exponential moving average (EMA)
EMAcross<- Op(DJIA)-EMA5
#Let’s explore the difference between the open price and our 5-period EMA
DEMA10<-DEMA(Cl(DJIA),n = 10, v = 1, wilder = FALSE)
DEMA10c<-Cl(DJIA) - DEMA10
MACD<-MACD(Op(DJIA),fast = 12, slow = 26, signal = 9)
#Calculate a MACD with standard parameters
MACDsignal<-MACD[,2]
#Grab just the signal line to use as our indicator.
SMI<-SMI(Op(DJIA),n=13,slow=25,fast=2,signal=9)
#Stochastic Oscillator with standard parameters
SMI<-SMI[,1]
#Grab just the oscillator to use as our indicator
BB<-BBands(Op(DJIA),n=20,sd=2)
BBp<-BB[,4]
CCI20<-CCI(DJIA[,3:5],n=20)
#A 20-period Commodity Channel Index calculated of the High/Low/Close of our data
# Return sign creation
ClosingPrice<-Cl(DJIA)
Trend<-diff(ClosingPrice, lag = 1, differences = 1, arithmetic = TRUE, log = FALSE, na.pad = TRUE)
#Calculate the difference between the close price at T and close price T-1
Class<-ifelse(Trend>0,"UP","DOWN")
#Create a binary classification variable, the variable we are trying to predict.
DJIADF<-data.frame(date = index(DJIA),DJIA, row.names=NULL)
viewdf<-cbind(views1[,1:2],views2[,2],views3[,2],views4[,2])
CombDF<-merge(viewdf,DJIADF, by.x='date', by.y='date')
DataSet<-data.frame(RSI3,EMAcross,MACDsignal,SMI,BBp,CCI20,DEMA10c)
DataSet<-DataSet[-c(1:33),]
Alldata<-cbind(DataSet,CombDF[33:1509,2:5])
Normalized <-function(x) {(x-min(x))/(max(x)-min(x))}
NormalizedData<-as.data.frame(lapply(Alldata,Normalized))
ClassDF<-data.frame(date = index(Class), Class, row.names=NULL)
AlldataNormalized<-data.frame(NormalizedData,ClassDF[33:1509,2])
colnames(AlldataNormalized)<-c("RSI3","EMAcross","MACDsignal","SMI","BBp","CCI20","DEMA10c","Views1","Views2","Views3","Views4","Class")
TrainingSet<-AlldataNormalized[1:1000,]
TestSet<-AlldataNormalized[1001:1477,]
TrainClass<-TrainingSet[,12]
TrainPred<-TrainingSet[,-12]
TestClass<-TestSet[,12]
TestPred<-TestSet[,-12]
library(h2o)
## Loading required package: statmod
##
## ----------------------------------------------------------------------
##
## Your next step is to start H2O:
## > h2o.init()
##
## For H2O package documentation, ask for help:
## > ??h2o
##
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit http://docs.h2o.ai
##
## ----------------------------------------------------------------------
##
##
## Attaching package: 'h2o'
##
## The following objects are masked from 'package:stats':
##
## sd, var
##
## The following objects are masked from 'package:base':
##
## %*%, apply, as.factor, as.numeric, colnames, colnames<-,
## ifelse, %in%, is.factor, is.numeric, log, trunc
localH2O <- h2o.init(ip = "localhost", port = 54321, startH2O = TRUE)
##
## H2O is not running yet, starting it now...
##
## Note: In case of errors look at the following log files:
## /tmp/RtmpnH0G8i/h2o_mitra2_started_from_r.out
## /tmp/RtmpnH0G8i/h2o_mitra2_started_from_r.err
##
##
## ..Successfully connected to http://localhost:54321/
##
## R is connected to the H2O cluster:
## H2O cluster uptime: 1 seconds 975 milliseconds
## H2O cluster version: 3.6.0.8
## H2O cluster name: H2O_started_from_R_mitra2_dzq909
## H2O cluster total nodes: 1
## H2O cluster total memory: 0.66 GB
## H2O cluster total cores: 4
## H2O cluster allowed cores: 2
## H2O cluster healthy: TRUE
##
## Note: As started, H2O is limited to the CRAN default of 2 CPUs.
## Shut down and restart H2O as shown below to use all your CPUs.
## > h2o.shutdown()
## > h2o.init(nthreads = -1)
localH2O = h2o.init(ip = "localhost", port = 54321, startH2O = TRUE,
Xmx = '2g')
## Warning in h2o.init(ip = "localhost", port = 54321, startH2O = TRUE, Xmx =
## "2g"): Xmx is a deprecated parameter. Use `max_mem_size` and `min_mem_size`
## to set the memory boundaries. Using `Xmx` to set these.
## Successfully connected to http://localhost:54321/
##
## R is connected to the H2O cluster:
## H2O cluster uptime: 2 seconds 119 milliseconds
## H2O cluster version: 3.6.0.8
## H2O cluster name: H2O_started_from_R_mitra2_dzq909
## H2O cluster total nodes: 1
## H2O cluster total memory: 0.66 GB
## H2O cluster total cores: 4
## H2O cluster allowed cores: 2
## H2O cluster healthy: TRUE
TrainH2o<-as.h2o(TrainingSet, destination_frame = "TrainH2o")
##
|
| | 0%
|
|=================================================================| 100%
TestH2o<-as.h2o(TestPred, destination_frame = "TestH2o")
##
|
| | 0%
|
|=================================================================| 100%
model <- h2o.deeplearning(x = 1:11,y = 12,training_frame = TrainH2o, activation = "TanhWithDropout",hidden = c(500,500,500),epochs = 200,rate_decay =5e-4, l1=1e-5)
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|==== | 7%
|
|===== | 7%
|
|=================================================================| 100%
h2o_yhat_test <- h2o.predict(model,TestH2o)
df_yhat_test <- as.data.frame(h2o_yhat_test)
prediction <-df_yhat_test[,1]
confusionMatrix(prediction,TestClass)
## Confusion Matrix and Statistics
##
## Reference
## Prediction DOWN UP
## DOWN 151 36
## UP 75 215
##
## Accuracy : 0.7673
## 95% CI : (0.7267, 0.8045)
## No Information Rate : 0.5262
## P-Value [Acc > NIR] : < 2e-16
##
## Kappa : 0.5293
## Mcnemar's Test P-Value : 0.00031
##
## Sensitivity : 0.6681
## Specificity : 0.8566
## Pos Pred Value : 0.8075
## Neg Pred Value : 0.7414
## Prevalence : 0.4738
## Detection Rate : 0.3166
## Detection Prevalence : 0.3920
## Balanced Accuracy : 0.7624
##
## 'Positive' Class : DOWN
##
hidden_opt <- list(c(200,200), c(100,300,100), c(500,500,500))
l1_opt <- c(1e-5,1e-7)
hyper_params <- list(hidden = hidden_opt, l1 = l1_opt)
model_grid <- h2o.grid("deeplearning",hyper_params = hyper_params,x = 1:11,y = 12,training_frame = TrainH2o,distribution = "multinomial", activation = "TanhWithDropout")
##
|
| | 0%
|
|=========== | 17%
|
|====================== | 33%
|
|================================ | 50%
|
|=========================================== | 67%
|
|====================================================== | 83%
|
|=================================================================| 100%
summary(model_grid)
## H2O Grid Details
## ================
##
## Grid ID: Grid_DeepLearning_TrainH2o_model_R_1457009083242_7
## Used hyper parameters:
## - l1
## - hidden
## Number of models: 6
## Number of failed models: 0
##
## Generated models
## ----------------
## l1 hidden status_ok
## 1e-07 [100,300,100] OK
## 1e-05 [100,300,100] OK
## 1e-05 [500,500,500] OK
## 1e-07 [500,500,500] OK
## 1e-05 [200,200] OK
## 1e-07 [200,200] OK
## model_ids
## Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_3
## Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_2
## Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_4
## Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_5
## Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_0
## Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_1
## H2O Grid Summary
## ================
##
## Grid ID: Grid_DeepLearning_TrainH2o_model_R_1457009083242_7
## Used hyper parameters:
## - l1
## - hidden
## Number of models: 6
## - Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_3
## - Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_2
## - Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_4
## - Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_5
## - Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_0
## - Grid_DeepLearning_TrainH2o_model_R_1457009083242_7_model_1
##
## Number of failed models: 0
model_ids <- model_grid@model_ids
models <- lapply(model_ids, function(id) { h2o.getModel(id)})
svm with rbf kernel
library(e1071)
svm.model <- svm( Class~ ., data = TrainingSet, cost = 10, gamma = 1)
svm.pred <- predict(svm.model, TestSet)
confusionMatrix( svm.pred ,TestClass)
## Confusion Matrix and Statistics
##
## Reference
## Prediction DOWN UP
## DOWN 133 54
## UP 93 197
##
## Accuracy : 0.6918
## 95% CI : (0.6482, 0.733)
## No Information Rate : 0.5262
## P-Value [Acc > NIR] : 1.347e-13
##
## Kappa : 0.3766
## Mcnemar's Test P-Value : 0.001723
##
## Sensitivity : 0.5885
## Specificity : 0.7849
## Pos Pred Value : 0.7112
## Neg Pred Value : 0.6793
## Prevalence : 0.4738
## Detection Rate : 0.2788
## Detection Prevalence : 0.3920
## Balanced Accuracy : 0.6867
##
## 'Positive' Class : DOWN
##
library(nnet)
nn <- nnet(Class ~ ., data =TrainingSet, size = 10, rang = 0.1,decay = 5e-4, maxit = 100)
## # weights: 131
## initial value 694.156744
## iter 10 value 493.935718
## iter 20 value 441.357797
## iter 30 value 431.776656
## iter 40 value 427.992373
## iter 50 value 419.712412
## iter 60 value 410.866726
## iter 70 value 400.405954
## iter 80 value 395.247881
## iter 90 value 388.865083
## iter 100 value 383.686256
## final value 383.686256
## stopped after 100 iterations
nnPred<-predict(nn,TestSet,type = "class")
confusionMatrix(nnPred,TestClass)
## Confusion Matrix and Statistics
##
## Reference
## Prediction DOWN UP
## DOWN 160 58
## UP 66 193
##
## Accuracy : 0.74
## 95% CI : (0.6982, 0.7789)
## No Information Rate : 0.5262
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.4777
## Mcnemar's Test P-Value : 0.5296
##
## Sensitivity : 0.7080
## Specificity : 0.7689
## Pos Pred Value : 0.7339
## Neg Pred Value : 0.7452
## Prevalence : 0.4738
## Detection Rate : 0.3354
## Detection Prevalence : 0.4570
## Balanced Accuracy : 0.7384
##
## 'Positive' Class : DOWN
##