{"id":7107,"date":"2022-11-30T13:55:39","date_gmt":"2022-11-30T13:55:39","guid":{"rendered":"https:\/\/craighordlow.com\/index.php\/2022\/11\/30\/how-to-use-rstudio-to-create-traffic-forecasting-models\/"},"modified":"2022-11-30T13:55:39","modified_gmt":"2022-11-30T13:55:39","slug":"how-to-use-rstudio-to-create-traffic-forecasting-models","status":"publish","type":"post","link":"https:\/\/craighordlow.com\/index.php\/2022\/11\/30\/how-to-use-rstudio-to-create-traffic-forecasting-models\/","title":{"rendered":"How to use RStudio to create traffic forecasting models"},"content":{"rendered":"<p>There is a lot of fervor in the SEO industry for Python right now.<\/p>\n<p>It is a comparably easier programming language to learn and has become accessible to the SEO community through guides and blogs.<\/p>\n<p>But if you want to learn a new language for analyzing and visualizing your search data, consider looking into R.<\/p>\n<p>This article covers the basics of how you can produce time series forecasts in RStudio from your Google Search Console click data.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/craighordlow.com\/index.php\/2022\/11\/30\/how-to-use-rstudio-to-create-traffic-forecasting-models\/#But_first_what_is_R\" >But first, what is R?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/craighordlow.com\/index.php\/2022\/11\/30\/how-to-use-rstudio-to-create-traffic-forecasting-models\/#Step_1_Prepare_the_data\" >Step 1: Prepare the data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/craighordlow.com\/index.php\/2022\/11\/30\/how-to-use-rstudio-to-create-traffic-forecasting-models\/#Step_2_Plot_the_time_series_data_in_RStudio\" >Step 2: Plot the time series data in RStudio<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/craighordlow.com\/index.php\/2022\/11\/30\/how-to-use-rstudio-to-create-traffic-forecasting-models\/#Step_3_Model_and_forecast_your_data_in_RStudio\" >Step 3: Model and forecast your data in RStudio<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"But_first_what_is_R\"><\/span>But first, what is R?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>R is \u201ca language and environment for statistical computing and graphics,\u201d according to <a href=\"https:\/\/www.r-project.org\/about.html\" target=\"_blank\" rel=\"noopener noreferrer\">The R Project for Statistical Computing<\/a>.\u00a0<\/p>\n<p>R isn\u2019t new and has been around since 1993. Still, learning some of the basics of R \u2013 including how to interact with Google\u2019s various APIs \u2013 can be advantageous for SEOs.<\/p>\n<p>If you want to pick up R as a new language, good courses to learn from are:<\/p>\n<p><a href=\"https:\/\/www.coursera.org\/learn\/data-analysis-r\" target=\"_blank\" rel=\"noopener noreferrer\">Data Analysis with R Programming<\/a> (Offered by Google)<a href=\"https:\/\/www.coursera.org\/learn\/r-programming\" target=\"_blank\" rel=\"noopener noreferrer\">R Programming<\/a> (Offered by Johns Hopkins University)<\/p>\n<p>But if you grasp the basics and want to learn data visualization fundamentals in R, I recommend Coursera\u2019s guided project, <a href=\"https:\/\/www.coursera.org\/projects\/application-of-data-analysis-in-business-with-r-programming\" target=\"_blank\" rel=\"noopener noreferrer\">Application of Data Analysis in Business with R Programming<\/a>.<\/p>\n<p>And then you also need to install:<\/p>\n<p>R (through the <a href=\"https:\/\/cran.r-project.org\/mirrors.html\" target=\"_blank\" rel=\"noopener noreferrer\">Comprehensive R Archive Network<\/a>).Posit\u2019s <a href=\"https:\/\/posit.co\/downloads\/\" target=\"_blank\" rel=\"noopener noreferrer\">RStudio integrated development environment (IDE)<\/a> \u2013 which is to R as PyCharm is to Python.<\/p>\n<p>What follows are the steps for creating traffic forecasting models in RStudio using click data.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_1_Prepare_the_data\"><\/span>Step 1: Prepare the data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The first step is to export your Google Search Console data. You can either do this through the user interface and exporting data as a CSV:<\/p>\n<div class=\"wp-block-image\">\n<\/div>\n<p>Or, if you want to pull your data via RStudio directly from the Google Search Console API, I recommend you follow <a href=\"https:\/\/www.jcchouinard.com\/how-to-use-google-search-console-api-with-r\/\" target=\"_blank\" rel=\"noopener noreferrer\">this guide<\/a> from JC Chouinard.<\/p>\n<p>If you do this via the interface, you\u2019ll download a zip file with various CSVs, from which you want the workbook named \u201cDates\u201d:<\/p>\n<div class=\"wp-block-image\">\n<\/div>\n<p>Your date range can be from a quarter, six months, or 12 months \u2013 all that matters is that you have the values in chronological order, which this export easily produces. (You just need to sort Column A, so the oldest values are at the top.)<\/p>\n<p><!-- START INLINE FORM --><\/p>\n<div class=\"nl-inline-form border py-2 px-1 my-2\">\n<div class=\"row align-items-center\">\n<div class=\"col-12 col-lg-3 col-xl-4 pe-md-0 pb-2 pb-lg-0\">\n<p class=\"inline-form-text text-center mb-0\">Get the daily newsletter search marketers rely on.<\/p>\n<\/div>\n<div class=\"col-12 col-lg-7 col-xl-6  pe-lg-0\">\n<div class=\"form-nl-inline\">\n<div class=\"row g-0\">\n<div class=\"form-group col-12 col-lg-8 mb-0\"><\/div>\n<div class=\"col-12 col-lg-4 col-xl-4 mb-0\">\n<div class=\"d-none\">\n<p>Processing\u2026Please wait.<\/p>\n<\/div>\n<p>\t\t\t\t\t\t\t<button class=\"btn btn-dark inlineButton w-100 rounded-0 font-weight-bold\">SUBSCRIBE<\/button>\n\t\t\t\t\t\t<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"col-12 col-lg-2 col-xl-2 ps-1\">\n<p class=\"text-center mb-0\"><a class=\"nl-terms\" href=\"https:\/\/searchengineland.com\/termsofuse\" target=\"_blank\" rel=\"noopener noreferrer\">See terms.<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!-- END INLINE FORM --><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_2_Plot_the_time_series_data_in_RStudio\"><\/span>Step 2: Plot the time series data in RStudio<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Now we need to import and plot our data. To do this, we must first install four packages and then load them.<\/p>\n<p>The first command to run is:<\/p>\n<p>## Install packages<br \/>install.packages(&#8220;tidyverse&#8221;)<br \/>install.packages(&#8220;tsibble&#8221;)<br \/>install.packages(&#8220;fabletools&#8221;)<br \/>install.packages(&#8220;bsts&#8221;)<\/p>\n<p>Followed by:<\/p>\n<p>## Load packages<br \/>library(&#8220;tidyverse&#8221;)<br \/>library(&#8220;tsibble&#8221;)<br \/>library(&#8220;fabletools&#8221;)<br \/>library(&#8220;bsts&#8221;)<\/p>\n<p>You then want to import your data. The only change you need to make to the below command is the file type name (maintaining the CSV extension) in red:<\/p>\n<p>## Read data<br \/>mdat &lt;- read_csv(&#8220;<em><strong>example data csv<\/strong><\/em>.csv&#8221;,<br \/>col_types = cols(Date = col_date(format = &#8220;%d\/%m\/%Y&#8221;)))<\/p>\n<p>Then the last two commands in plotting your data are to make the time series the object, then to plot the graph itself:<\/p>\n<p>## Make time series object<br \/>ts_data &lt;- mdat %&gt;%<br \/>as_tsibble(index = &#8220;Date&#8221;)<\/p>\n<p>Followed by:<\/p>\n<p>## Make plot<br \/>autoplot(ts_data) +<br \/>labs(x = &#8220;Date&#8221;, subtitle = &#8220;Time series plot&#8221;)<\/p>\n<p>And in your RStudio interface, you will have a time series plot appear:<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Step_3_Model_and_forecast_your_data_in_RStudio\"><\/span>Step 3: Model and forecast your data in RStudio<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>At this stage, it\u2019s important to acknowledge that <a href=\"https:\/\/searchengineland.com\/seo-forecasting-template-389686\" target=\"_blank\" rel=\"noopener\">forecasting<\/a> is not an exact science and relies on several truths and assumptions. These being:<\/p>\n<p>Assumptions that historical trends and patterns shall continue to replicate with varying degrees over time.Forecasting will contain errors and anomalies because your data set (your real-world clicks data) will contain anomalies that could be construed as errors.Forecasts typically revolve around the average, making group forecasts more reliable than running a series of micro-forecasts.Shorter-range forecasting is typically more accurate than longer-range forecasting.<\/p>\n<p>With this out of the way, we can begin to model and forecast our traffic data.<\/p>\n<p>For this article, I will visualize our data as a Bayesian Structural Time Series (BSTS) forecast, one of the packages we installed earlier. This graph is used by most forecasting methods.\u00a0<\/p>\n<p>Most marketers will have seen or at least be familiar with the model as it is commonly used across many industries for forecasting purposes.<\/p>\n<p>The first command we need to run is to make our data fit the BSTS model:<\/p>\n<p>ss &lt;- AddLocalLinearTrend(list(), ts_data$Clicks)<br \/>ss &lt;- AddSeasonal(ss, ts_data$Clicks, nseasons = 52)<br \/>model1 &lt;- bsts(ts_data$Clicks,<br \/>state.specification = ss,<br \/>niter = 500)<\/p>\n<p>And then plot the model components:<\/p>\n<p>plot(model1, &#8220;comp&#8221;)<\/p>\n<p>And now we can visualize one- and two-year forecasts.<\/p>\n<p>Going back to the previously mentioned general forecasting rules, the further into the future you forecast, the less accurate it becomes. Thus, I stick to two years when doing this.<\/p>\n<p>And as BSTS considers an upper and lower bound, it also becomes pretty pointless past a certain point.<\/p>\n<p>The below command will produce a one-year future BSTS forecast for your data:<\/p>\n<p># 1-year<br \/>pred1 &lt;- predict(model1, horizon = 365)<br \/>plot(pred1, plot.original = 200)<\/p>\n<p>And you\u2019ll return a graph like this:<\/p>\n<div class=\"wp-block-image\">\n<\/div>\n<p>To produce a two-year forecasting graph from your data, you want to run the below command:<\/p>\n<p>pred2 &lt;- predict(model1, horizon = 365*2)<br \/>plot(pred2, plot.original = 365)<\/p>\n<p>And this will produce a graph like this:<\/p>\n<p>As you can see, the upper and lower bounds in the one-year forecast had a range of -50 to +150, whereas the 2-year forecast has -200 to +600.<\/p>\n<p>The further into the future you forecast, the greater this range becomes and, in my opinion, the less useful the forecast becomes.<\/p>\n<p>The post <a href=\"https:\/\/searchengineland.com\/create-traffic-forecasting-models-rstudio-389823\" target=\"_blank\" rel=\"noopener\">How to use RStudio to create traffic forecasting models<\/a> appeared first on <a href=\"https:\/\/searchengineland.com\/\" target=\"_blank\" rel=\"noopener\">Search Engine Land<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>There is a lot of fervor in the SEO industry for Python right now. It is a comparably easier programming language to learn and has become accessible to the SEO community through guides and blogs. But if you want to learn a new language for analyzing and visualizing your search data, consider looking into R. This article covers the basics of how you can produce time series forecasts in RStudio from your Google Search Console click data. But first, what is R? R is \u201ca language [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"h5ap_radio_sources":[],"cybocfi_hide_featured_image":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-7107","post","type-post","status-publish","format-standard","hentry","category-best-seo-blogs"],"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":false,"author_link":"https:\/\/craighordlow.com\/author\/"},"_links":{"self":[{"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/posts\/7107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/comments?post=7107"}],"version-history":[{"count":0,"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/posts\/7107\/revisions"}],"wp:attachment":[{"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/media?parent=7107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/categories?post=7107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craighordlow.com\/index.php\/wp-json\/wp\/v2\/tags?post=7107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}