library(wikipediatrend)
library(classyfire)
## Loading required package: snowfall
## Loading required package: snow
## Loading required package: e1071
## Loading required package: boot
## Loading required package: neldermead
## Loading required package: optimbase
## Loading required package: Matrix
##
## Attaching package: 'Matrix'
##
## The following objects are masked from 'package:base':
##
## crossprod, tcrossprod
##
## Loading required package: optimsimplex
##
## Attaching package: 'optimsimplex'
##
## The following object is masked from 'package:boot':
##
## simplex
library(caret)
## Loading required package: lattice
##
## Attaching package: 'lattice'
##
## The following object is masked from 'package:boot':
##
## melanoma
##
## Loading required package: ggplot2
views<-wp_trend(page = "Citigroup",from = "2010-01-01",to = "2014-12-31",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/Citigroup
## http://stats.grok.se/json/en/201002/Citigroup
## http://stats.grok.se/json/en/201003/Citigroup
## http://stats.grok.se/json/en/201004/Citigroup
## http://stats.grok.se/json/en/201005/Citigroup
## http://stats.grok.se/json/en/201006/Citigroup
## http://stats.grok.se/json/en/201007/Citigroup
## http://stats.grok.se/json/en/201008/Citigroup
## http://stats.grok.se/json/en/201009/Citigroup
## http://stats.grok.se/json/en/201010/Citigroup
## http://stats.grok.se/json/en/201011/Citigroup
## http://stats.grok.se/json/en/201012/Citigroup
## http://stats.grok.se/json/en/201101/Citigroup
## http://stats.grok.se/json/en/201102/Citigroup
## http://stats.grok.se/json/en/201103/Citigroup
## http://stats.grok.se/json/en/201104/Citigroup
## http://stats.grok.se/json/en/201105/Citigroup
## http://stats.grok.se/json/en/201106/Citigroup
## http://stats.grok.se/json/en/201107/Citigroup
## http://stats.grok.se/json/en/201108/Citigroup
## http://stats.grok.se/json/en/201109/Citigroup
## http://stats.grok.se/json/en/201110/Citigroup
## http://stats.grok.se/json/en/201111/Citigroup
## http://stats.grok.se/json/en/201112/Citigroup
## http://stats.grok.se/json/en/201201/Citigroup
## http://stats.grok.se/json/en/201202/Citigroup
## http://stats.grok.se/json/en/201203/Citigroup
## http://stats.grok.se/json/en/201204/Citigroup
## http://stats.grok.se/json/en/201205/Citigroup
## http://stats.grok.se/json/en/201206/Citigroup
## http://stats.grok.se/json/en/201207/Citigroup
## http://stats.grok.se/json/en/201208/Citigroup
## http://stats.grok.se/json/en/201209/Citigroup
## http://stats.grok.se/json/en/201210/Citigroup
## http://stats.grok.se/json/en/201211/Citigroup
## http://stats.grok.se/json/en/201212/Citigroup
## http://stats.grok.se/json/en/201301/Citigroup
## http://stats.grok.se/json/en/201302/Citigroup
## http://stats.grok.se/json/en/201303/Citigroup
## http://stats.grok.se/json/en/201304/Citigroup
## http://stats.grok.se/json/en/201305/Citigroup
## http://stats.grok.se/json/en/201306/Citigroup
## http://stats.grok.se/json/en/201307/Citigroup
## http://stats.grok.se/json/en/201308/Citigroup
## http://stats.grok.se/json/en/201309/Citigroup
## http://stats.grok.se/json/en/201310/Citigroup
## http://stats.grok.se/json/en/201311/Citigroup
## http://stats.grok.se/json/en/201312/Citigroup
## http://stats.grok.se/json/en/201401/Citigroup
## http://stats.grok.se/json/en/201402/Citigroup
## http://stats.grok.se/json/en/201403/Citigroup
## http://stats.grok.se/json/en/201404/Citigroup
## http://stats.grok.se/json/en/201405/Citigroup
## http://stats.grok.se/json/en/201406/Citigroup
## http://stats.grok.se/json/en/201407/Citigroup
## http://stats.grok.se/json/en/201408/Citigroup
## http://stats.grok.se/json/en/201409/Citigroup
## http://stats.grok.se/json/en/201410/Citigroup
## http://stats.grok.se/json/en/201411/Citigroup
## http://stats.grok.se/json/en/201412/Citigroup
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.
startDate = as.Date("2010-01-01")
endDate = as.Date("2014-12-31")
getSymbols("c", src = "yahoo", 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.
## [1] "C"
RSI3<-RSI(Op(C), n= 3)
#Calculate a 3-period relative strength index (RSI) off the open price
EMA5<-EMA(Op(C),n=5)
#Calculate a 5-period exponential moving average (EMA)
EMAcross<- Op(C)-EMA5
#Let’s explore the difference between the open price and our 5-period EMA
DEMA10<-DEMA(Cl(C),n = 10, v = 1, wilder = FALSE)
DEMA10c<-Cl(C) - DEMA10
MACD<-MACD(Op(C),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(C),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(C),n=20,sd=2)
BBp<-BB[,4]
CCI20<-CCI(C[,3:5],n=20)
#A 20-period Commodity Channel Index calculated of the High/Low/Close of our data
PriceChange<- Cl(C) - Op(C)
#Calculate the difference between the close price and open price
Class<-ifelse(PriceChange>0,"UP","DOWN")
#Create a binary classification variable, the variable we are trying to predict.
DJIADF<-data.frame(date = index(C), C, row.names=NULL)
CombDF<-merge(views,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[34:1258,2])
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[34:1258,2])
colnames(AlldataNormalized)<-c("RSI3","EMAcross","MACDsignal","SMI","BBp","CCI20","DEMA10c","Views","Class")
TrainingSet<-AlldataNormalized[1:1000,]
TestSet<-AlldataNormalized[1001:1225,]
TrainClass<-TrainingSet[,9]
TrainPred<-TrainingSet[,-9]
TestClass<-TestSet[,9]
TestPred<-TestSet[,-9]
ens <- cfBuild(inputData = TrainPred, inputClass = TrainClass, bootNum = 6, ensNum = 6, parallel = TRUE, cpus = 4, type = "SOCK")
## Warning in searchCommandline(parallel, cpus = cpus, type = type,
## socketHosts = socketHosts, : Unknown option on commandline:
## rmarkdown::render('/home/mitra2/git/classyfire~+~wikipedia.Rmd',~+~~
## +~encoding~+~
## R Version: R version 3.2.3 (2015-12-10)
## snowfall 1.84-6.1 initialized (using snow 0.4-1): parallel execution on 4 CPUs.
## Library neldermead loaded.
## Library neldermead loaded in cluster.
## Library e1071 loaded.
## Library e1071 loaded in cluster.
## Library boot loaded.
## Library boot loaded in cluster.
## Library snowfall loaded.
## Library snowfall loaded in cluster.
##
##
## Stopping cluster
attributes(ens)
## $names
## [1] "testAcc" "trainAcc" "optGamma" "optCost" "totalTime"
## [6] "runTime" "confMatr" "predClasses" "testClasses" "missNames"
## [11] "accNames" "testIndx" "svmModel"
##
## $class
## [1] "list" "cfBuild"
getAvgAcc(ens)$Test
## [1] 76.25
getAvgAcc(ens)$Train
## [1] 79.61
ens$testAcc
## [1] 76.35 73.35 74.55 76.05 75.75 81.44
ens$trainAcc
## [1] 78.38 82.58 77.18 82.28 81.98 75.23
# Alternatively
getAcc(ens)$Test
## [1] 76.35 73.35 74.55 76.05 75.75 81.44
getAcc(ens)$Train
## [1] 78.38 82.58 77.18 82.28 81.98 75.23
Prediction<-cfPredict(ens,TestPred)
PredictionClass <-Prediction[,1]
outof ssample metrics
confusionMatrix( PredictionClass,TestClass)
## Confusion Matrix and Statistics
##
## Reference
## Prediction DOWN UP
## DOWN 104 36
## UP 4 81
##
## Accuracy : 0.8222
## 95% CI : (0.7659, 0.8699)
## No Information Rate : 0.52
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.6479
## Mcnemar's Test P-Value : 9.509e-07
##
## Sensitivity : 0.9630
## Specificity : 0.6923
## Pos Pred Value : 0.7429
## Neg Pred Value : 0.9529
## Prevalence : 0.4800
## Detection Rate : 0.4622
## Detection Prevalence : 0.6222
## Balanced Accuracy : 0.8276
##
## 'Positive' Class : DOWN
##
ggClassPred(ens, position = "stack", displayAll = TRUE, showText = TRUE)
ggEnsTrend(ens, showText = TRUE)
ggEnsHist(ens, density = TRUE, percentiles=TRUE, mean=TRUE)