{"id":20456,"date":"2023-02-12T12:50:29","date_gmt":"2023-02-12T12:50:29","guid":{"rendered":"https:\/\/algotrading101.com\/learn\/?p=20456"},"modified":"2023-05-10T16:03:14","modified_gmt":"2023-05-10T16:03:14","slug":"r-finance-guide","status":"publish","type":"post","link":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/","title":{"rendered":"R Finance &#8211; An Econometrics Guide to Stocks"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_20456\" class=\"pvc_stats total_only  \" data-element-id=\"20456\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56-300x300.png\" alt=\"\" class=\"wp-image-20470\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56-300x300.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56-150x150.png 150w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56-768x768.png 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56.png 1024w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Table of contents:<\/h3>\n\n\n\n<ol>\n<li><a href=\"#what-is-r\">What is R?<\/a><\/li>\n\n\n\n<li><a href=\"#r-finance\">How can R be used for Finance?<\/a><\/li>\n\n\n\n<li><a href=\"#r-pros\">Why should I use R for Finance?<\/a><\/li>\n\n\n\n<li><a href=\"#r-cons\">Why shouldn\u2019t I use R for Finance?<\/a><\/li>\n\n\n\n<li><a href=\"#r-alternatives\">What are some R alternatives?<\/a><\/li>\n\n\n\n<li><a href=\"#econometrics\">What is Econometrics?<\/a><\/li>\n\n\n\n<li><a href=\"#stock-problem\">Stock problem definition<\/a><\/li>\n\n\n\n<li><a href=\"#stock-data\">How to download stock data with R?<\/a><\/li>\n\n\n\n<li><a href=\"#r-csv\">How to save data to CSV with R?<\/a><\/li>\n\n\n\n<li><a href=\"#linear-regression\">What is linear regression?&nbsp;<\/a><\/li>\n\n\n\n<li><a href=\"#null-hypothesis\">What is the null hypothesis?<\/a><\/li>\n\n\n\n<li><a href=\"#p-value\">What is the p-value?<\/a>\n<ol>\n<li><a href=\"#p-value-downfalls\">p-value downfalls<\/a><\/li>\n\n\n\n<li><a href=\"#p-value-alternatives\">p-value alternatives and supplements<\/a><\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><a href=\"#reg\">How to do a linear regression on stock data with R?<\/a><\/li>\n\n\n\n<li><a href=\"#granger-causality\">What is the Granger causality test?<\/a><\/li>\n\n\n\n<li><a href=\"#granger-test\">How to do a Granger causality test in R?<\/a><\/li>\n\n\n\n<li><a href=\"#serial-correlation\">What is Serial Correlation?<\/a><\/li>\n\n\n\n<li><a href=\"#serial-corr-test\">How to do a Serial Correlation test with R?<\/a><\/li>\n\n\n\n<li><a href=\"#remove-serial-corr\">How to remove Serial Correlation from a model in R?<\/a><\/li>\n\n\n\n<li><a href=\"#stationarity\">What is Stationarity?<\/a><\/li>\n\n\n\n<li><a href=\"#stationarity-test\">How to test for Stationarity with R?<\/a><\/li>\n\n\n\n<li><a href=\"#cointegration\">What is Cointegration?<\/a><\/li>\n\n\n\n<li><a href=\"#cointegration-test\">How to test for Cointegration with R?<\/a><\/li>\n\n\n\n<li><a href=\"#common-mistakes\">What are the 3 Common Analysis\/Testing Mistakes?<\/a><\/li>\n\n\n\n<li><a href=\"#final-thoughts\">Final thoughts<\/a><\/li>\n\n\n\n<li><a href=\"#full-code\">Full code<\/a><\/li>\n<\/ol>\n\n\n\n<a name=\"what-is-r\">\n\n\n\n<h2 class=\"wp-block-heading\">What is R?<\/h2>\n\n\n\n<p>R is a programming language and fee software that is primarily used for statistical computing and graphics. It is mostly used for data analytics and is favored by many scientists and statisticians.<\/p>\n\n\n\n<a name=\"r-finance\">\n\n\n\n<h2 class=\"wp-block-heading\">How can R be used for Finance?<\/h2>\n\n\n\n<p>The Financial sector has a plethora of data that requires careful and rigorous analysis in order to make good data-driven decisions and R can shine here with its great statistical and graphical packages.<\/p>\n\n\n\n<a name=\"r-pros\">\n\n\n\n<h2 class=\"wp-block-heading\">Why should I use R for Finance?<\/h2>\n\n\n\n<ul>\n<li>R is free to use<\/li>\n\n\n\n<li>Has great packages<\/li>\n\n\n\n<li>Easy to learn<\/li>\n\n\n\n<li>Has advanced statistics<\/li>\n\n\n\n<li>Has a good community<\/li>\n<\/ul>\n\n\n\n<a name=\"r-cons\">\n\n\n\n<h2 class=\"wp-block-heading\">Why shouldn&#8217;t I use R for Finance?<\/h2>\n\n\n\n<ul>\n<li>Can have issues with handling Big Data<\/li>\n\n\n\n<li>Might be confusing for programming beginners<\/li>\n\n\n\n<li>R is a bit slower language<\/li>\n\n\n\n<li>R is more of a specialty language and not a multi-purpose one like Python<\/li>\n<\/ul>\n\n\n\n<a name=\"r-alternatives\">\n\n\n\n<h2 class=\"wp-block-heading\">What are some R alternatives?<\/h2>\n\n\n\n<p>Depending on the problem you&#8217;re trying to solve, R can be replaced with the following alternatives:<\/p>\n\n\n\n<ul>\n<li>Python<\/li>\n\n\n\n<li>Julia<\/li>\n\n\n\n<li>Matlab<\/li>\n\n\n\n<li>RapidMiner<\/li>\n\n\n\n<li>SPSS<\/li>\n\n\n\n<li>Jasp<\/li>\n\n\n\n<li>Jamovi<\/li>\n\n\n\n<li>Orange<\/li>\n\n\n\n<li>Mathematica<\/li>\n<\/ul>\n\n\n\n<a name=\"econometrics\">\n\n\n\n<h2 class=\"wp-block-heading\">What is Econometrics?<\/h2>\n\n\n\n<p>Econometrics involves applying statistical and mathematical methods to economic data in order to uncover meaningful relationships. In other words, it is the quantitative analysis of economic phenomena.<\/p>\n\n\n\n<a name=\"stock-problem\">\n\n\n\n<h2 class=\"wp-block-heading\">Stock problem definition<\/h2>\n\n\n\n<p>In this article, we will address the problem of finding stocks that move in a similar matter over a certain time frame. In order to do this properly some of the main statistical methods that we need to use are the following:<\/p>\n\n\n\n<ul>\n<li>Linear regression<\/li>\n\n\n\n<li>Serial correlation<\/li>\n\n\n\n<li>Stationarity test<\/li>\n\n\n\n<li>Cointegration test<\/li>\n<\/ul>\n\n\n\n<p>Each of the above-mentioned procedures will help us to uncover the real relationship between two or more stocks. For the showcase of these procedures, we will use the Box and Dropbox stocks.<\/p>\n\n\n\n<p>The analysis that we&#8217;re going to perform can inform your domain knowledge when setting up a pair&#8217;s trading strategy. You can read more about it <a href=\"https:\/\/algotrading101.com\/learn\/pairs-trading-guide\/\">here<\/a>, and you can also check out our <a href=\"https:\/\/algotrading101.com\/learn\/cluster-analysis-guide\/\">cluster analysis article<\/a>.<\/p>\n\n\n\n<p>If you&#8217;re unfamiliar with the R basics or would like a refresher on it, I&#8217;d advise skimming through some bits in <a href=\"https:\/\/www.w3schools.com\/r\/default.asp\">this article<\/a>. We will use R studio without any fancy code for our analysis, and I&#8217;ll explain it bit by bit.<\/p>\n\n\n\n<a name=\"stock-data\">\n\n\n\n<h2 class=\"wp-block-heading\">How to download stock data with R?<\/h2>\n\n\n\n<p>Stock data can be downloaded in many ways with R and we will use the tidyquant library that was built for handling financial data. If the library isn&#8217;t installed you can obtain it with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>install.packages(\"tidyquant\")<\/code><\/pre>\n\n\n\n<p>You&#8217;ll see your console being populated and if it gets cluttered at any moment you can press CTRL + L to clear it out. Now that the library is installed let&#8217;s load it and obtain stock data for Box and Dropbox.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(tidyquant)\n\nboxx &lt;- tq_get('BOX',\n              from = '2020-01-01',\n              to = '2021-06-06',\n              get = 'stock.prices')\n\ndropbox &lt;- tq_get('DBX',\n                  from = '2020-01-01',\n                  to = '2021-06-06',\n                  get = 'stock.prices')<\/code><\/pre>\n\n\n\n<p>Take note: as &#8220;box&#8221; is a function in R, we named it &#8220;boxx&#8221;. Now let&#8217;s print out the first few columns of our data so you can see what we obtained and graph our stocks.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"595\" height=\"183\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/Capture.png\" alt=\"\" class=\"wp-image-9695\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/Capture.png 595w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/Capture-300x92.png 300w\" sizes=\"(max-width: 595px) 100vw, 595px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>library(ggplot2)\n\nboxx %&gt;%\n  ggplot(aes(x = date, y = adjusted)) +\n  geom_line() +\n  theme_classic() +\n  labs(x = 'Date',\n       y = \"Adjusted Price\",\n       title = \"Box price chart\") +\n  scale_y_continuous(breaks = seq(0,300,10))\n\ndropbox %&gt;%\n  ggplot(aes(x = date, y = adjusted)) +\n  geom_line() +\n  theme_classic() +\n  labs(x = 'Date',\n       y = \"Adjusted Price\",\n       title = \"Dropbox price chart\") +\n  scale_y_continuous(breaks = seq(0,300,10))<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-gallery alignwide has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"520\" data-id=\"9697\"  src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5d529c3b-55f5-49f3-b206-fe2142becd55.png\" alt=\"\" class=\"wp-image-9697\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5d529c3b-55f5-49f3-b206-fe2142becd55.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5d529c3b-55f5-49f3-b206-fe2142becd55-300x198.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5d529c3b-55f5-49f3-b206-fe2142becd55-768x507.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"520\" data-id=\"9698\"  src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5fe4fb16-d1c5-4a16-a8d5-2aee74f43a88.png\" alt=\"\" class=\"wp-image-9698\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5fe4fb16-d1c5-4a16-a8d5-2aee74f43a88.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5fe4fb16-d1c5-4a16-a8d5-2aee74f43a88-300x198.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5fe4fb16-d1c5-4a16-a8d5-2aee74f43a88-768x507.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>Just by eye-balling, we can state that the stocks have indeed moved in a similar fashion. But our eyes have fooled us enough times in our lives to trust them, especially when it comes to financial matters.<\/p>\n\n\n\n<p>On the bottom right side of your interface, you can browse through the plots. Now, let&#8217;s merge the data, rename the columns, and then take what we need from it into a new data frame for analysis.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Merge\nmerged &lt;- merge(boxx, dropbox, by=c(\"date\"))\nhead(merged)\n\n# Subset\ndf = subset(merged, select=c(\"date\",\"adjusted.x\",\"adjusted.y\"))\n\n# Rename\nnames(df)&#91;names(df) == \"adjusted.x\"] &lt;- \"box\"\nnames(df)&#91;names(df) == \"adjusted.y\"] &lt;- \"dropbox\"\n\nhead(df)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"968\" height=\"491\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1-1.png\" alt=\"\" class=\"wp-image-9699\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1-1.png 968w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1-1-300x152.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1-1-768x390.png 768w\" sizes=\"(max-width: 968px) 100vw, 968px\" \/><\/figure>\n\n\n\n<a name=\"r-csv\">\n\n\n\n<h2 class=\"wp-block-heading\">How to save data to CSV with R?<\/h2>\n\n\n\n<p>To export a data frame to a CSV file in R, you will need to use the <code>write.csv()<\/code> command where you specify the data frame and the directory path.<\/p>\n\n\n\n<p>To obtain your default directory path you can write the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dir &lt;- getwd()\ndir<\/code><\/pre>\n\n\n\n<p>And now we save our data:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>write.csv(df, \"box_dropbox.csv\")<\/code><\/pre>\n\n\n\n<p>To load it again simply write the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df &lt;- read.csv(\"box_dropbox.csv\")\n\n# Delete index created by loading\ndf &lt;- df&#91;,-1]<\/code><\/pre>\n\n\n\n<a name=\"linear-regression\">\n\n\n\n<h2 class=\"wp-block-heading\">What is linear regression?&nbsp;<\/h2>\n\n\n\n<p>Linear regression is a statistical method that attempts to model the relationship between two variables by fitting a straight line to the observed data. Simply said, it helps us understand if we can predict y given X.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-3 is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" data-id=\"8646\"  src=\"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l1-1024x576.png\" alt=\"\" class=\"wp-image-8646\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l1-1024x576.png 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l1-300x169.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l1-768x432.png 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l1-1536x864.png 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l1.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" data-id=\"8647\"  src=\"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l2-1024x576.png\" alt=\"\" class=\"wp-image-8647\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l2-1024x576.png 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l2-300x169.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l2-768x432.png 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l2-1536x864.png 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/04\/l2.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>Before fitting a linear regression model on our data, we will need to go over some fundamental concepts that will aid our summary statistics interpretation.<\/p>\n\n\n\n<a name=\"null-hypothesis\">\n\n\n\n<h2 class=\"wp-block-heading\">What is the null hypothesis?<\/h2>\n\n\n\n<p>A null hypothesis states that there is no difference, relationship and or significance between two or more sets of data or measured phenomena. <\/p>\n\n\n\n<p>An example of a null hypothesis would be the following sentence: There is no significant correlation between the observed stock movements of Box and Dropbox in a given time frame.<\/p>\n\n\n\n<p>We always want to disprove the null hypothesis and the most used statistic that is used for that is the p-value.<\/p>\n\n\n\n<a name=\"p-value\">\n\n\n\n<h2 class=\"wp-block-heading\">What is the p-value?<\/h2>\n\n\n\n<p>The p-value s the probability of obtaining test results at least as extreme as the&nbsp;results actually observed, under the assumption that the&nbsp;null hypothesis&nbsp;is correct.<\/p>\n\n\n\n<p>The p-value helps us to determine the significance of our results. By convention, if the p-value is less than 0.05 (5%), we have strong evidence to reject the null hypothesis and vice-versa.<\/p>\n\n\n\n<p>As this can be an article for itself let&#8217;s put it this way: the lower our p-value is, the more surprising our evidence is, and the more ridiculous is the null hypothesis.<\/p>\n\n\n\n<a name=\"p-value-downfalls\">\n\n\n\n<h3 class=\"wp-block-heading\">p-value downfalls<\/h3>\n\n\n\n<p>As you might have guessed already, the simplistic nature of the p-value, which is often misunderstood in itself, has led astray many. Some of the most prominent p-value downfalls are the following:<\/p>\n\n\n\n<ul>\n<li>Puts too much significance onto a single research paper and\/or analysis<\/li>\n\n\n\n<li>Focuses on either\/or thinking<\/li>\n\n\n\n<li>Doesn&#8217;t say anything about approving the null hypothesis<\/li>\n\n\n\n<li>Depends on the quality of the sample (sampling should be random)<\/li>\n\n\n\n<li>Can easily be tinkered with to show significance and more.<\/li>\n<\/ul>\n\n\n\n<a name=\"p-value-alternatives\">\n\n\n\n<h3 class=\"wp-block-heading\">p-value alternatives and supplements<\/h3>\n\n\n\n<p>We shouldn&#8217;t throw out the baby with the bathwater, and thus the best way is to supplement the p-value with other statistics and procedures that shed more light on our data. Some of these are the following:<\/p>\n\n\n\n<ul>\n<li>Replication<\/li>\n\n\n\n<li><a href=\"https:\/\/algotrading101.com\/learn\/train-test-split\/\">Cross-validation<\/a><\/li>\n\n\n\n<li>Confidence Intervals<\/li>\n\n\n\n<li>Standardized measures of effect<\/li>\n\n\n\n<li>Statistical Power<\/li>\n\n\n\n<li>Margin of Error<\/li>\n\n\n\n<li>Bayesian statistics<\/li>\n\n\n\n<li>Meta-analysis and more.<\/li>\n<\/ul>\n\n\n\n<a name=\"reg\">\n\n\n\n<h2 class=\"wp-block-heading\">How to do a linear regression on stock data with R?<\/h2>\n\n\n\n<p>Linear regression on stock data can be done with R by using the in-built <code>lm()<\/code> function and stating the appropriate variables. In our case, we will assume that the relationship between our stocks is linear.<\/p>\n\n\n\n<p>Now, let us attach our dataset so R knows that we will pull the data from it and run the linear regression model on our stocks. We will also print out the summary statistics of our regression model.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>attach(df)\n\nreg &lt;- lm(box~dropbox)\nsummary(reg)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"302\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2-1.png\" alt=\"\" class=\"wp-image-9700\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2-1.png 548w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2-1-300x165.png 300w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure>\n\n\n\n<p>If you look at the &#8220;Estimate&#8221; it tells us how much our dependent variable changes (Box) for each unit increase ($). In this case for each 1$ increase in Dropbox the price of Box changes by $0.807.<\/p>\n\n\n\n<p>Below you can see that the p-value is really low and that we can reject the null hypothesis aka it is unlikely that the results above are due to random chance. <\/p>\n\n\n\n<p>The F-statistic shows if there is a relationship between our predictor (Dropbox) and the response variable. The further it is away from 1 the better the case it makes for rejecting the null hypothesis.<\/p>\n\n\n\n<p>The R-squared states that the movement in Dropbox explains 76% variance in Box. Let&#8217;s plot our linear regression to see the fit and check it for homoscedasticity.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>par(mfrow=c(2,2))\nplot(reg)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"520\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6392dff0-85dd-4039-b9bc-353cc418fc3a.png\" alt=\"\" class=\"wp-image-9705\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6392dff0-85dd-4039-b9bc-353cc418fc3a.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6392dff0-85dd-4039-b9bc-353cc418fc3a-300x198.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6392dff0-85dd-4039-b9bc-353cc418fc3a-768x507.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<p>The scale location plot is used to check the homogeneity of variance of the residuals (homoscedasticity). A horizontal line with equally spread points is a reliable indication of homoscedasticity. In our case, it isn&#8217;t too bad.<\/p>\n\n\n\n<p>The Residuals vs Fitted plot help us to check the linear relationship assumption. If the red line is horizontal or close to being one, the linear relationship is true. <\/p>\n\n\n\n<p>In our case, it has a pattern and this isn&#8217;t surprising as we&#8217;re using a simple model on stock data. The Normal Q-Q plot shows if the data is normally distributed. If it follows a straight dashed line it is a good indicator.<\/p>\n\n\n\n<p>And finally, the Residuals vs Leverage plot shows us if we have any extreme values that might skew our linear regression results. <\/p>\n\n\n\n<p>Now that you know some basics about the regression, reverse the variables and see what you get. What does the data tell you? For a look at how to do some of this in python, you can check out his <a href=\"https:\/\/algotrading101.com\/learn\/sklearn-guide\/\">article<\/a>.<\/p>\n\n\n\n<a name=\"granger-causality\">\n\n\n\n<h2 class=\"wp-block-heading\">What is the Granger causality test?<\/h2>\n\n\n\n<p>The Granger causality test is a statistical hypothesis test that shows if one time series can predict another. For example, can we predict Box prices by observing the movement of Dropbox prices with some time lag?<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"514\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1920px-GrangerCausalityIllustration.svg_-1024x514.png\" alt=\"\" class=\"wp-image-9726\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1920px-GrangerCausalityIllustration.svg_-1024x514.png 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1920px-GrangerCausalityIllustration.svg_-300x150.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1920px-GrangerCausalityIllustration.svg_-768x385.png 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1920px-GrangerCausalityIllustration.svg_-1536x770.png 1536w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/1920px-GrangerCausalityIllustration.svg_.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/Granger_causality#\/media\/File:GrangerCausalityIllustration.svg\">Source<\/a><\/figcaption><\/figure>\n\n\n\n<a name=\"granger-test\">\n\n\n\n<h2 class=\"wp-block-heading\">How to do a Granger causality test in R?<\/h2>\n\n\n\n<p>To perform a Granger causality test in R you will need to use the lmtest library and the <code>grangertest()<\/code> function in which you specify the variables and lags.<\/p>\n\n\n\n<p>Let&#8217;s do it for our data:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>library(lmtest)\n\ngrangertest(box ~ dropbox, order = 3, data=df)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"157\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/3-1.png\" alt=\"\" class=\"wp-image-9727\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/3-1.png 578w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/3-1-300x81.png 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/figure>\n\n\n\n<p>As you can see the p-value and F statistic support the rejection of the null hypothesis and thus we can say that the movement in Dropbox can predict Box. But have in mind the downfalls of the p-value and that it isn&#8217;t too small.<\/p>\n\n\n\n<p>Now try to reverse the variables and see what you get.<\/p>\n\n\n\n<a name=\"serial-correlation\">\n\n\n\n<h2 class=\"wp-block-heading\">What is Serial Correlation?<\/h2>\n\n\n\n<p>Serial Correlation, also known as autocorrelation, represents a relationship between a variable and a lagged version of itself over a specified time frame. If the variable is serially correlated it implies that it is not random.<\/p>\n\n\n\n<a name=\"serial-corr-test\">\n\n\n\n<h2 class=\"wp-block-heading\">How to do a Serial Correlation test with R?<\/h2>\n\n\n\n<p>A Serial Correlation test can be done with R by using the Durbin Watson test that comes stored as a <code>dw()<\/code> function from the lmtest library. As a parameter you pass the model that you want to test for serial correlation.<\/p>\n\n\n\n<p>We want to do this test as a supplement to our linear regression so we will check our model for serial correlation. This is done as the linear regression might have picked up the noise from the correlated error terms in our time series.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dwtest(reg)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Result:\ndata:  reg\nDW = 0.16026, p-value &lt; 2.2e-16\nalternative hypothesis: true autocorrelation is greater than 0<\/code><\/pre>\n\n\n\n<p>The value of the DW test shows that the alternative hypothesis is true and the p-value is almost non-existent. This means that we indeed have a case of serial correlation in our data. <\/p>\n\n\n\n<p>If you look at the residuals from our regression you will see that we have an overall positive trend. The residual is the difference between the observed value and the predicted one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>residuals = reg$residuals\nplot(residuals, type='l')<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"518\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/65eef318-c709-4573-a959-06c5c85ad2ae.png\" alt=\"\" class=\"wp-image-9745\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/65eef318-c709-4573-a959-06c5c85ad2ae.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/65eef318-c709-4573-a959-06c5c85ad2ae-300x197.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/65eef318-c709-4573-a959-06c5c85ad2ae-768x505.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<p>And if we do a lagged regression on the residuals we can see how they impacted the main model.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sub_box &lt;- subset(df, select=c(\"date\",\"box\"))\nsub_dropbox &lt;- subset(df, select=c(\"date\",\"dropbox\"))\n\nd_box = diff(as.numeric(unlist(sub_box&#91;\"box\"])))\nd_dbox = diff(as.numeric(unlist(sub_dropbox&#91;\"dropbox\"])))\n\nlagged_reg &lt;- lm(d_box~d_dbox)\nsummary(lagged_reg)\n\nlagged_reg_res = lagged_reg$residuals\nplot(lagged_reg_res, type='l')<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"570\" height=\"309\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5-1.png\" alt=\"\" class=\"wp-image-9749\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5-1.png 570w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/5-1-300x163.png 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"518\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/b89108d1-e001-4ae3-9412-67e6cb661726.png\" alt=\"\" class=\"wp-image-9746\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/b89108d1-e001-4ae3-9412-67e6cb661726.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/b89108d1-e001-4ae3-9412-67e6cb661726-300x197.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/b89108d1-e001-4ae3-9412-67e6cb661726-768x505.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<a name=\"remove-serial-corr\">\n\n\n\n<h2 class=\"wp-block-heading\">How to remove Serial Correlation from a model in R?<\/h2>\n\n\n\n<p>To eliminate Serial Correlation in your model you can do the Cochrane-Orcutt test. This can be done by using the <code>cochrane.orcutt()<\/code> function that is a part of the orcutt package.<\/p>\n\n\n\n<p>Let us run it on our model and see the diffrence:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#install.packages(\"orcutt\")\nlibrary(orcutt)\n\nco &lt;- cochrane.orcutt(reg)\nsummary(co)\ndwtest(co)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"379\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/4-1.png\" alt=\"\" class=\"wp-image-9748\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/4-1.png 577w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/4-1-300x197.png 300w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><\/figure>\n\n\n\n<p>As you can see, we have quite an improvement on the serial correlation front. The results of the test show a lower estimate value (0.43) from the prior 0.80 one. The Durbin-Watson test isn&#8217;t significant anymore so we have eliminated autocorrelation.<\/p>\n\n\n\n<p>Now, let us take the residuals from the first regression and the lagged one so you can see the correlation between each lag. This will get us a sense of stationarity that we will test later on.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>acf_residual_reg = acf(residuals)\nacf_lag_residual_reg = acf(lagged_reg_res)\n\nacf_residual_reg\nacf_lag_residual_reg<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"970\" height=\"242\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6-1.png\" alt=\"\" class=\"wp-image-9752\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6-1.png 970w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6-1-300x75.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6-1-768x192.png 768w\" sizes=\"(max-width: 970px) 100vw, 970px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-5 is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"518\" data-id=\"9754\"  src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/fda0f15a-5bf8-4e37-b944-9686dfe6da4d.png\" alt=\"\" class=\"wp-image-9754\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/fda0f15a-5bf8-4e37-b944-9686dfe6da4d.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/fda0f15a-5bf8-4e37-b944-9686dfe6da4d-300x197.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/fda0f15a-5bf8-4e37-b944-9686dfe6da4d-768x505.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"518\" data-id=\"9753\"  src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2e320365-cfa7-43cd-8ab8-c1b70ca7fa9a-1.png\" alt=\"\" class=\"wp-image-9753\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2e320365-cfa7-43cd-8ab8-c1b70ca7fa9a-1.png 787w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2e320365-cfa7-43cd-8ab8-c1b70ca7fa9a-1-300x197.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/2e320365-cfa7-43cd-8ab8-c1b70ca7fa9a-1-768x505.png 768w\" sizes=\"(max-width: 787px) 100vw, 787px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>On the left chart, you can see some substantial correlation between the residuals, and when we do a regression on them the correlations (right chart) have dropped which we want to be the case.<\/p>\n\n\n\n<p>If you need a practical guide to correlations you can check out <a href=\"https:\/\/algotrading101.com\/learn\/python-correlation-guide\/\">this article<\/a>.<\/p>\n\n\n\n<p> But there is one thing that we need to have in mind and in states that the Cochrane-Orcutt test needs our time series to have constant mean, variance, and to be stationary.<\/p>\n\n\n\n<a name=\"stationarity\">\n\n\n\n<h2 class=\"wp-block-heading\">What is Stationarity?<\/h2>\n\n\n\n<p>Stationarity in a time series means that the said time series has the same mean, variance, and autocorrelation over time. For example, if your stock data doesn&#8217;t show any trends it is stationary.<\/p>\n\n\n\n<p>We need the data to be stationary in order to be able to make accurate predictions. If the data is full of noise and trends, we can&#8217;t use it for reasonable predictions.<\/p>\n\n\n\n<a name=\"stationarity-test\">\n\n\n\n<h2 class=\"wp-block-heading\">How to test for Stationarity with R?<\/h2>\n\n\n\n<p>Testing for Stationarity can be done by using several tests in R. Some of the tests are the following: ADF, KPSS, Philips-Peron, Zivot-Andrews, and more.<\/p>\n\n\n\n<p>Let&#8217;s check out what our data shows us and run some of the mentioned tests in order:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#install.packages(\"egcm\")\n#install.packages(\"tseries\")\n\nlibrary(egcm)\nlibrary(tseries)\n\nadf.test(as.numeric(unlist(sub_box&#91;\"box\"])))\nadf.test(as.numeric(unlist(sub_dropbox&#91;\"dropbox\"])))\n\nadf.test(d_box)\nadf.test(d_dbox)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"529\" height=\"269\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/7-2.png\" alt=\"\" class=\"wp-image-9756\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/7-2.png 529w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/7-2-300x153.png 300w\" sizes=\"(max-width: 529px) 100vw, 529px\" \/><\/figure>\n\n\n\n<p>Here we can observe that our data is non-stationary because we can&#8217;t reject the null hypothesis. But when we do a test on the difference of our stock data we obtain the opposite.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"531\" height=\"339\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/8-1.png\" alt=\"\" class=\"wp-image-9757\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/8-1.png 531w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/8-1-300x192.png 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/figure>\n\n\n\n<p>When we run the Philips-Peron test, it confirms the results of the previous one.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pp.test(as.numeric(unlist(sub_box&#91;\"box\"])))\npp.test(as.numeric(unlist(sub_dropbox&#91;\"dropbox\"])))\n\npp.test(d_box)\npp.test(d_dbox)<\/code><\/pre>\n\n\n\n<p>And finally, the KPSS test confirms it once again. The null hypothesis of the KPSS test states that the data IS stationary.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kpss.test(as.numeric(unlist(sub_box&#91;\"box\"])), null=\"Trend\")\nkpss.test(as.numeric(unlist(sub_dropbox&#91;\"dropbox\"])), null=\"Trend\")\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"338\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/9-1.png\" alt=\"\" class=\"wp-image-9758\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/9-1.png 661w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/9-1-300x153.png 300w\" sizes=\"(max-width: 661px) 100vw, 661px\" \/><\/figure>\n\n\n\n<a name=\"cointegration\">\n\n\n\n<h2 class=\"wp-block-heading\">What is Cointegration?<\/h2>\n\n\n\n<p>Cointegration can be viewed as a long-run relationship between two or more variables. For example, if two stocks tend to follow the same trends over enough time, we can say that they are cointegrated.<\/p>\n\n\n\n<p>You can imagine it like walking an overly curious dog on a leash. The dog will move around and go from side to side, but he won&#8217;t ever move too much from you as he is on a leash. Well, that is cointegration.<\/p>\n\n\n\n<a name=\"cointegration-test\">\n\n\n\n<h2 class=\"wp-block-heading\">How to test for Cointegration with R?<\/h2>\n\n\n\n<p>Testing for Cointegration with R can be done by using the Engle-Granger test that comes as a part of the egcm package.<\/p>\n\n\n\n<p>Let&#8217;s see how our two stocks do on the cointegration front:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>egcm(as.numeric(unlist(sub_box&#91;\"box\"])), as.numeric(unlist(sub_dropbox&#91;\"dropbox\"])))\negcm(d_box, d_dbox)\n\nplot(egcm(as.numeric(unlist(sub_box&#91;\"box\"])), as.numeric(unlist(sub_dropbox&#91;\"dropbox\"]))))\nplot(egcm(d_box, d_dbox))<\/code><\/pre>\n\n\n\n<p>As we can observe, the pairs seem to be cointegrated while their differences, which I added out of curiosity, seem not to be.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"254\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/10-1-1024x254.png\" alt=\"\" class=\"wp-image-9762\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/10-1-1024x254.png 1024w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/10-1-300x74.png 300w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/10-1-768x191.png 768w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/10-1.png 1052w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now let&#8217;s look at the plots that will shed more light on how the cointegration looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"518\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6ba445d8-fa8a-47e0-928c-ca390fdff4d2.png\" alt=\"\" class=\"wp-image-9763\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6ba445d8-fa8a-47e0-928c-ca390fdff4d2.png 735w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/6ba445d8-fa8a-47e0-928c-ca390fdff4d2-300x211.png 300w\" sizes=\"(max-width: 735px) 100vw, 735px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"518\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/7f314134-a504-41ed-ac98-234ac996d46c.png\" alt=\"\" class=\"wp-image-9764\" srcset=\"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/7f314134-a504-41ed-ac98-234ac996d46c.png 735w, https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2021\/06\/7f314134-a504-41ed-ac98-234ac996d46c-300x211.png 300w\" sizes=\"(max-width: 735px) 100vw, 735px\" \/><\/figure>\n\n\n\n<a name=\"common-mistakes\">\n\n\n\n<h2 class=\"wp-block-heading\">What are the 3 Common Analysis\/Testing Mistakes?<\/h2>\n\n\n\n<p>When you run your analysis, there are 3 common mistakes to take note:<\/p>\n\n\n\n<ul>\n<li>Overfitting<\/li>\n\n\n\n<li>Look-ahead Bias<\/li>\n\n\n\n<li>P-hacking<\/li>\n<\/ul>\n\n\n\n<p><a>Do check out this lecture PDF to learn more:&nbsp;<\/a><a href=\"https:\/\/course.algotrading101.com\/courses\/pt101-practical-python-for-finance-trading-masterclass\/lectures\/27360454\">3 Big Mistakes of Backtesting \u2013 1) Overfitting 2) Look-Ahead Bias 3) P-Hacking<\/a><\/p>\n\n\n\n<a name=\"final-thoughts\">\n\n\n\n<h2 class=\"wp-block-heading\">Final thoughts<\/h2>\n\n\n\n<p>When working with a time series you need to test your model for serial correlation and correct it with the Cochrane-Orcutt method. We also want to make sure that the time series is stationary so we can make predictions.<\/p>\n\n\n\n<p>When looking for the correlation between time series you need to use cointegration because it is more informative. Too many people just look for plain correlations that can be meaningless.<\/p>\n\n\n\n<p>And remember these wise words: &#8220;Correlation doesn&#8217;t mean causation!&#8221;.<\/p>\n\n\n\n<a name=\"full-code\">\n\n\n\n<h2 class=\"wp-block-heading\">Full code<\/h2>\n\n\n\n<p><a href=\"https:\/\/github.com\/IgorWounds\/Econometrics-on-Stock-Data\/tree\/main\">GitHub link<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_20456\" class=\"pvc_stats total_only  \" data-element-id=\"20456\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/algotrading101.com\/learn\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Table of contents: What is R? R is a programming language and fee software that is primarily used for statistical computing and graphics. It is mostly used for data analytics and is favored by many scientists and statisticians. How can R be used for Finance? The Financial sector has a plethora of data that requires [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":20470,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_lmt_disableupdate":"no","_lmt_disable":"no","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[3,2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>R Finance - An Econometrics Guide to Stocks - AlgoTrading101 Blog<\/title>\n<meta name=\"description\" content=\"We will use R, a free programming language, to run data analysis to explore market relationships and patterns.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/algotrading101.com\/learn\/r-finance-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Finance - An Econometrics Guide to Stocks - AlgoTrading101 Blog\" \/>\n<meta property=\"og:description\" content=\"We will use R, a free programming language, to run data analysis to explore market relationships and patterns.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/algotrading101.com\/learn\/r-finance-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-12T12:50:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-10T16:03:14+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Igor Radovanovic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Igor Radovanovic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Finance - An Econometrics Guide to Stocks - AlgoTrading101 Blog","description":"We will use R, a free programming language, to run data analysis to explore market relationships and patterns.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/","og_locale":"en_US","og_type":"article","og_title":"R Finance - An Econometrics Guide to Stocks - AlgoTrading101 Blog","og_description":"We will use R, a free programming language, to run data analysis to explore market relationships and patterns.","og_url":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/","og_site_name":"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog","article_published_time":"2023-02-12T12:50:29+00:00","article_modified_time":"2023-05-10T16:03:14+00:00","og_image":[{"width":1024,"height":1024,"url":"http:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2023\/02\/DALL\u00b7E-2023-02-12-21.23.56.png","type":"image\/png"}],"author":"Igor Radovanovic","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Igor Radovanovic","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/#article","isPartOf":{"@id":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/"},"author":{"name":"Igor Radovanovic","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/a7ae60c112a73b7c3fe14ac56726a0ae"},"headline":"R Finance &#8211; An Econometrics Guide to Stocks","datePublished":"2023-02-12T12:50:29+00:00","dateModified":"2023-05-10T16:03:14+00:00","mainEntityOfPage":{"@id":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/"},"wordCount":2572,"publisher":{"@id":"https:\/\/algotrading101.com\/learn\/#organization"},"articleSection":["Programming","Trading"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/","url":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/","name":"R Finance - An Econometrics Guide to Stocks - AlgoTrading101 Blog","isPartOf":{"@id":"https:\/\/algotrading101.com\/learn\/#website"},"datePublished":"2023-02-12T12:50:29+00:00","dateModified":"2023-05-10T16:03:14+00:00","description":"We will use R, a free programming language, to run data analysis to explore market relationships and patterns.","breadcrumb":{"@id":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/algotrading101.com\/learn\/r-finance-guide\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/algotrading101.com\/learn\/r-finance-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/algotrading101.com\/learn\/"},{"@type":"ListItem","position":2,"name":"R Finance &#8211; An Econometrics Guide to Stocks"}]},{"@type":"WebSite","@id":"https:\/\/algotrading101.com\/learn\/#website","url":"https:\/\/algotrading101.com\/learn\/","name":"Quantitative Trading Ideas and Guides - AlgoTrading101 Blog","description":"Authentic Stories about Algorithmic trading, coding and life.","publisher":{"@id":"https:\/\/algotrading101.com\/learn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/algotrading101.com\/learn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/algotrading101.com\/learn\/#organization","name":"AlgoTrading101","url":"https:\/\/algotrading101.com\/learn\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/logo\/image\/","url":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/11\/AlgoTrading101-Lucas-Liew.jpg","contentUrl":"https:\/\/algotrading101.com\/learn\/wp-content\/uploads\/2020\/11\/AlgoTrading101-Lucas-Liew.jpg","width":1200,"height":627,"caption":"AlgoTrading101"},"image":{"@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/a7ae60c112a73b7c3fe14ac56726a0ae","name":"Igor Radovanovic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/algotrading101.com\/learn\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d46175c509b3ee240a1e2bbe735a4d1e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d46175c509b3ee240a1e2bbe735a4d1e?s=96&d=mm&r=g","caption":"Igor Radovanovic"},"sameAs":["https:\/\/igorradovanovic.com","https:\/\/www.linkedin.com\/in\/igor-radovanovic-profile"],"url":"https:\/\/algotrading101.com\/learn\/author\/igor\/"}]}},"modified_by":"Lucas Liew","_links":{"self":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/20456"}],"collection":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/comments?post=20456"}],"version-history":[{"count":7,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/20456\/revisions"}],"predecessor-version":[{"id":21741,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/posts\/20456\/revisions\/21741"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/media\/20470"}],"wp:attachment":[{"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/media?parent=20456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/categories?post=20456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algotrading101.com\/learn\/wp-json\/wp\/v2\/tags?post=20456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}