<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-1-logistic-regression/"></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/DlUvfDGYRxE/machine-learning-stock-market-data-part.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><br><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></p>
<p><span><span lang="en"><span>We will develop a </span></span></span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span>logistic regression example.</span></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></p>
<p><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span>The example we will develop is about predicting when the market value will rise (UP) or fall (Down).</span></span></span></span></span></span></span></span></span></span></span></p>
<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 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></span>
<p><span><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></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><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)</p></b>
<p><b>### explore the dataset</b><br>names(Smarket)<br>dim(Smarket)<br>summary(Smarket)</p>
<p><b>### correlation matrix</b><br>cor(Smarket[,-9])</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><b>### correlations between th lag variables and today<br>### returns are close to zero<br>### the only substantial correlation is between $Year<br>### and $Volume</b><br>plot(Smarket$Volume, main= “Stock Market Data”,<br>     ylab = “Volume”)</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></span></span></span></span></span></span></span></span></span></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-WbjKSFQQ6XA/WRilhIicHWI/AAAAAAAAC_g/_cLgEuWoDKkV8x9trD77LVgQpF3nS5gowCLcB/s1600/smd_1.png?ssl=1"><img src="https://i2.wp.com/3.bp.blogspot.com/-WbjKSFQQ6XA/WRilhIicHWI/AAAAAAAAC_g/_cLgEuWoDKkV8x9trD77LVgQpF3nS5gowCLcB/s400/smd_1.png?resize=400%2C333&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><br><b> </b></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><b>### scatterplots, distributions and correlations</b><br>pairs.panels(Smarket)</span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<div><a target="_blank" rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-fOqfF-34xZs/WRilo6w7dOI/AAAAAAAAC_k/XcLE0q6vaWkzPR2S2SLM0fUlHsrQdueHQCLcB/s1600/smd_2.png?ssl=1"><img src="https://i0.wp.com/4.bp.blogspot.com/-fOqfF-34xZs/WRilo6w7dOI/AAAAAAAAC_k/XcLE0q6vaWkzPR2S2SLM0fUlHsrQdueHQCLcB/s400/smd_2.png?resize=400%2C333&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><br></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span><span lang="en"><span><span><span lang="en"><span><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><br><b>### fit a logistic regression model to predict $Direction<br>### using $Lag1 through $Lag5 and $Volume<br>### glm(): generalized linear model function<br>### family=binomial => logistic regression</b><br>glm.fit &lt;-glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,<br>               data = Smarket, family = binomial)<br>summary(glm.fit)
<p><b>### the smallest p_value is associated with Lag1<br>### the negative coefficient for this predictor suggests<br>### that if the market had a positive return yesterday,<br>### then it is less likely to go up today<br>### at a value of 0.15, the p-value is still relatively large,<br>### and so there is no clear evidence of a real association<br>### between $Lag1 and $Direction</b></p>
<p><b>### explore fitted model coefficients</b><br>coef(glm.fit)<br>summary(glm.fit)$coef<br>summary(glm.fit)$coef[ ,4]</p>
<p><b>### predict the probability that the market will go up,<br>### given values of the predictors</b><br>glm.probs &lt;-predict(glm.fit, type=”response”)<br>glm.probs[1:10]<br>contrasts(Smarket$Direction)</p>
<p><b>### these values correspond to the probability of the market<br>### going up, rather than down, because the contrasts()<br>### function indicates that R has created a dummy variable with<br>### a 1 for Up
<p>### create a vector of class predictions based on whether the<br>### predicted probability of a market increase is greater than<br>### or less than 0.5<br>glm.pred &lt;-rep (“Down”, 1250)<br>glm.pred[glm.probs > .5] &lt;-“Up”</p></b>
<p><b>### confusion matrix in order to determine how many observations<br>### were correctly or incorrectly classified</b><br>table(glm.pred, Smarket$Direction)<br>mean(glm.pred == Smarket$Direction)</p>
<p><b>### model correctly predicted that the market would go up on 507<br>### days and that it would go down on 145 days, for a total of<br>### 507 + 145 = 652 correct predictions<br>### ogistic regression correctly predicted the movement of the<br>### market 52.2 % of the time
<p>### to better assess the accuracy of the logistic regression model<br>### in this setting, we can fit the model using part of the data,<br>### and then examine how well it predicts the held out data<br>train &lt;-(Smarket$Year &lt; 2005)<br>Smarket.2005 &lt;-Smarket[!train, ]<br>dim(Smarket.2005)</p></b>
<p>Direction.2005 &lt;-Smarket$Direction[!train]</p>
<p>glm.fit &lt;-glm(Direction~Lag1+Lag2+Lag3+Lag4+Lag5+Volume,<br>               data = Smarket, family = binomial, subset = train)<br>glm.probs &lt;-predict(glm.fit, Smarket.2005, type = “response”)</p>
<p><b>### compute the predictions for 2005 and compare them to the</b><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><b>### actual movements of the market over that time period</b><br>glm.pred &lt;-rep(“Down”, 252)<br>glm.pred[glm.probs > 0.5] &lt;-“Up”<br>table(glm.pred, Direction.2005)
<p>mean(glm.pred == Direction.2005)<br>mean(glm.pred != Direction.2005)</p>
<p><b>### not generally expect to be able to use previous days returns ### to predict future market performance
<p>### refit the logistic regression using just $Lag1 and $Lag2,<br><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>### which seemed to have the highest predictive power in the</span></span></span></span></span></span></span></span></span></span></span></span></span></span></b><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><b>### original logistic regression model</b><br>glm.fit &lt;-glm(Direction ~ Lag1 + Lag2 , data = Smarket,<br>               family = binomial, subset = train)<br>glm.probs &lt;-predict(glm.fit, Smarket.2005 , type = “response”)<br>glm.pred &lt;-rep(“Down”, 252)<br>glm.pred[glm.probs > 0.5] &lt;-“Up”<br>table(glm.pred, Direction.2005)<br>mean(glm.pred == Direction.2005)
<p><b>### results appear to be a little better: 56%
<p>### if we want to predict the returns associated with particular<br>### values of $Lag1 and $Lag2<br>predict(glm.fit, newdata = data.frame(Lag1 = c (1.2 ,1.5),<br>           Lag2 = c(1.1, -0.8)) , type = “response”)</p></b>
<p><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span>You can get the example in:</span></span></span></span></span></span></span></span><br><span><span lang="en"><span><span lang="en"><span><span><span lang="en"><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_Classification_StockMarketData">https://github.com/pakinja/Data-R-Value</a> </span></span></span></span></span></span></span></span></p>
<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><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/DlUvfDGYRxE"></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/DlUvfDGYRxE/machine-learning-stock-market-data-part.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/9B-Fo64MSOA"></a></div></span></span></span></span></span></span></span></span></span></span></span></span></span></span></b></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>