<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/machine-learning-stock-market-data-part-2-linear-discriminant-analysis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/DataRValue/~3/JBn3WPHlWEc/machine-learning-stock-market-data-part_15.html"> Data R Value</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span><span lang="en">It is important to mention that the present posts series began as a personal way of practicing R programming and machine learning. <span>Subsequently feedback from the community, urged me to continue performing these exercises and sharing them.</span><span> </span></span></span><span><span lang="en"><span>The bibliography and corresponding authors are cited at all times and this posts series is a way of honoring and giving them the credit they deserve for their work.</span></span></span><br> <span><br></span><span><span lang="en"><span>We will develop a linear discriminant analysis</span></span><span lang="en"><span><span lang="en"><span><span lang="en"><span> example.</span></span></span></span> The exercise was originally published in <b><i>“An Introduction to Statistical Learning. With applications in R” </i>by Gareth James, Daniela Witten, Trevor Hastie and Robert Tibshirani. Springer 2015</b>.</span></span></span><span></span><br><span><br></span> <span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>The example we will develop is about c</span></span></span></span></span></span></span></span></span><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>lassifying</span></span> when the market value will rise (UP) or fall (Down).</span></span></span></span></span></span></span></span></span><span><br></span><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>We will carry out the exercise verbatim as published in the aforementioned reference</span></span></span></span></span></span><span lang="en"><span> and o</span></span></span></span></span></span><span lang="en"><span>nly with slight changes in the coding style.</span></span>
<p><span lang="en"><span>For more details on the models, algorithms and parameters interpretation, it is recommended to check the aforementioned reference or any other bibliography of your choice.</span></span></p></span>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><b>### “An Introduction to Statistical Learning.<br>### with applications in R” by Gareth James,<br>### Daniela Witten, Trevor Hastie and Robert Tibshirani.<br>### Springer 2015.
<p>### install and load required packages<br>library(ISLR)<br>library(psych)<br>library(MASS)</p></b>
<p><b>### perform linear discriminant analysis LDA on the stock<br>### market data</b><br>train &lt;-(Smarket$Year &lt; 2005)<br>Smarket.2005 &lt;-Smarket[!train, ]<br>Direction.2005 &lt;-Smarket$Direction[!train]</p>
<p>lda.fit &lt;-lda(Direction ~ Lag1 + Lag2 , data = Smarket , subset = train )<br>lda.fit</p></span>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i1.wp.com/2.bp.blogspot.com/-huw0v4iZRe0/WRnLwxn1DMI/AAAAAAAADAA/TQk8FkvaivEc2DTXrySn3Uynr8fc67DhgCLcB/s1600/LDA_1.png?ssl=1"><img src="https://i1.wp.com/2.bp.blogspot.com/-huw0v4iZRe0/WRnLwxn1DMI/AAAAAAAADAA/TQk8FkvaivEc2DTXrySn3Uynr8fc67DhgCLcB/s400/LDA_1.png?resize=400%2C236&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span>
<p><b>### LDA indicates that 49.2% of training observations<br>### correspond to days during wich the market went down
<p>### group means suggest that there is a tendency for the<br>### previous 2 days returns to be negative on days when the<br>### market increases, and a tendency for the previous days<br>### returns to be positive on days when the market declines</p>
<p>### coefficients of linear discriminants output provides the<br>### linear combination of Lag1 and Lag2 that are used to form<br>### the LDA decision rule</p>
<p>### if (−0.642 * Lag1 − 0.514 * Lag2) is large, then the LDA<br>### classifier will predict a market increase, and if it is<br>### small, then the LDA classifier will predict a market decline</p>
<p>### plot() function produces plots of the linear discriminants,<br>### obtained by computing (−0.642 * Lag1 − 0.514 * Lag2) for<br>### each of the training observations<br>plot (lda.fit)</p></b>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-VBXsaNwX_Wk/WRnL37NALJI/AAAAAAAADAE/wc1XlgKV0AU-uYf6Df3mjABV0k4OTH14ACLcB/s1600/LDA_2.png?ssl=1"><img src="https://i0.wp.com/1.bp.blogspot.com/-VBXsaNwX_Wk/WRnL37NALJI/AAAAAAAADAE/wc1XlgKV0AU-uYf6Df3mjABV0k4OTH14ACLcB/s400/LDA_2.png?resize=400%2C351&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><br><b>### predictions</b></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><b><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><b>### the LDA and logistic regression predictions are almost identical</b></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </b><br>lda.pred &lt;-predict(lda.fit, Smarket.2005)<br>names(lda.pred)
<p>lda.class &lt;-lda.pred$class<br>table(lda.class, Direction.2005)<br>mean(lda.class == Direction.2005)</p>
<p><b>### apply a 50% threshold to the posterior probabilities allows<br>### us to recreate the predictions in lda.pred$class</b><br>sum(lda.pred$posterior [ ,1] >= 0.5)<br>sum(lda.pred$posterior [ ,1] &lt; 0.5)</p>
<p><b>### posterior probability output by the model corresponds to<br>### the probability that the market will decrease</b><br>lda.pred$posterior[1:20 ,1]<br>lda.class[1:20]</p>
<p><b>### use a posterior probability threshold other than 50 % in order<br>### to make predictions
<p>### suppose that we wish to predict a market decrease only if we<br>### are very certain that the market will indeed decrease on that<br>### day-say, if the posterior probability is at least 90%<br>sum(lda.pred$posterior[ ,1] > 0.9)</p></b>
<p><b>### No days in 2005 meet that threshold! In fact, the greatest<br>### posterior probability of decrease in all of 2005 was 52.02%</b></p></span>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span lang="en"><span>You can get the example in:</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span lang="en"><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_Classification_StockMarketData_LDA">https://github.com/pakinja/Data-R-Value</a> </span></span></span></span></span></span><b> </b></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><span><b> </b> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/JBn3WPHlWEc"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://feedproxy.google.com/~r/DataRValue/~3/JBn3WPHlWEc/machine-learning-stock-market-data-part_15.html"> Data R Value</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ofZ9opVFh5I"></a></div></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>