<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>The Data Sandbox</title>
<link>https://datasandbox.netlify.app/</link>
<atom:link href="https://datasandbox.netlify.app/index.xml" rel="self" type="application/rss+xml"/>
<description>The Data Sandbox is a collection of Data Science projects and discussions on Data Science topics.</description>
<generator>quarto-1.4.551</generator>
<lastBuildDate>Tue, 12 Mar 2024 04:00:00 GMT</lastBuildDate>
<item>
  <title>Tracking you time dashboard featuring Toggl API</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/images/tab3.png" class="img-fluid"></p>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Do you lead a busy life? It sure feels like it, but how do you truly know how you spend your time? Would you like to focus your time on specific projects or specific tasks?</p>
<p>The solution is to start tracking you time. I personally, started tracking my time almost five years ago when I had multiple jobs. I was a full-time graduate student with a few part-time job. This created a demand on my time and I needed to manage my time well.</p>
<p>I learnt about Toggl, a free easy to use time tracking application. It is simple to use on android with additional feature on their website. You simply start at timer and save it under a project. You can also capture the task which may be distinct from the project.</p>
<p>Its really up to your own whims on the definition of project. You can track one-off task, like a household repair, but I try to keep these tasks grouped together in a unified project.</p>
<p>I hadn’t really done anything with the data I created tracking my time, so I thought it would be a great opportunity to learn how to create a dashboard in <code>python</code>.</p>
</section>
<section id="initialization" class="level1">
<h1>Initialization</h1>
<p>As always, we start with loading our python packages. There isn’t really anything out of the ordinary, we will use the <code>requests</code> library to make our API call. You will need to create an account at the Toggl website and create a API key.</p>
<p>I’ve saved the API key in my environmental variables, it is loaded into the script with the <code>os.environ</code> function. You also need to create a header for the API call, this is the part that caused the most difficulty for me as it is very specific in the format.</p>
<div id="b9e4359c" class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> base64</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> seaborn <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sns</span>
<span id="cb1-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-9"></span>
<span id="cb1-10">key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.environ[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Toggl'</span>]</span>
<span id="cb1-11"></span>
<span id="cb1-12">string<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>key<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':api_token'</span></span>
<span id="cb1-13">headers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{</span>
<span id="cb1-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Authorization'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Basic '</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>base64.b64encode(string.encode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ascii'</span>)).decode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8'</span>),</span>
<span id="cb1-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Content-Type"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"application/json"</span>   </span>
<span id="cb1-16">}</span></code></pre></div>
</details>
</div>
</section>
<section id="toggl-api" class="level1">
<h1>Toggl API</h1>
<p>This section goes more into the details for the API. The call accepts the following parameters: <code>since</code> for your starting date, <code>until</code> for you finishing date, <code>user_agent</code> this is your user account email and <code>workspace_id</code> which is found on the Toggl website when you click on your workspace. I have kept mine secret, you will need to replace them with your own values.</p>
<div id="423d936d" class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">params <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'since'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2020-01-01'</span>,</span>
<span id="cb2-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'until'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2020-12-31'</span>,</span>
<span id="cb2-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user_agent'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'email'</span>,</span>
<span id="cb2-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'workspace_id'</span> : <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workspace"</span></span>
<span id="cb2-6">}</span></code></pre></div>
</details>
</div>
<p>With the header and parameters for the API call setup, we can now use the requests package to make the actual call. The API call returns a json document, for ease I will convert that json into a dataframe. We can than analyze what the API call has returned.</p>
<div id="f5cada26" class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://api.track.toggl.com/reports/api/v2/details'</span>, headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> headers, params<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params)</span>
<span id="cb3-2">my_json <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.loads(response.content)</span>
<span id="cb3-3">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(my_json[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data'</span>])</span>
<span id="cb3-4">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Month'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DatetimeIndex(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'start'</span>]).month</span>
<span id="cb3-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(df.columns, df.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Index(['id', 'pid', 'tid', 'uid', 'description', 'start', 'end', 'updated',
       'dur', 'user', 'use_stop', 'client', 'project', 'project_color',
       'project_hex_color', 'task', 'billable', 'is_billable', 'cur', 'tags',
       'Month'],
      dtype='object')            id          pid   tid      uid    description  \
0  1825387165  162435592.0  None  4504525  Data Products   
1  1825095042  162435592.0  None  4504525  Data Products   
2  1824585854  150042491.0  None  4504525        Commute   
3  1824350140  150042504.0  None  4504525   Walmart work   
4  1824350131  150042491.0  None  4504525        Commute   

                       start                        end  \
0  2020-12-31T20:26:49-05:00  2020-12-31T20:44:21-05:00   
1  2020-12-31T16:56:40-05:00  2020-12-31T17:21:47-05:00   
2  2020-12-30T22:55:00-05:00  2020-12-31T23:30:00-05:00   
3  2020-12-30T14:24:04-05:00  2020-12-30T22:55:48-05:00   
4  2020-12-30T14:24:02-05:00  2020-12-30T14:24:04-05:00   

                     updated       dur     user  ...  client  \
0  2021-01-01T20:08:38-05:00   1052000  M2edney  ...    None   
1  2020-12-31T20:26:49-05:00   1507000  M2edney  ...    None   
2  2020-12-31T01:06:18-05:00  88500000  M2edney  ...    None   
3  2020-12-30T22:55:49-05:00  30704000  M2edney  ...    None   
4  2020-12-30T14:24:05-05:00      2000  M2edney  ...    None   

              project project_color project_hex_color  task billable  \
0  Data Science Study             0           #9e5bd9  None     None   
1  Data Science Study             0           #9e5bd9  None     None   
2             Commute             0           #c7af14  None     None   
3             Walmart             0           #d94182  None     None   
4             Commute             0           #c7af14  None     None   

  is_billable   cur tags Month  
0       False  None   []    12  
1       False  None   []    12  
2       False  None   []    12  
3       False  None   []    12  
4       False  None   []    12  

[5 rows x 21 columns]</code></pre>
</div>
</div>
<p>The issue with this method is that it only returns a limited number of values. We can see it only returned 50 time entries.</p>
<div id="9070ead1" class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(df))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>50</code></pre>
</div>
</div>
<p>To collect all entries within the time frame, we need to try something a little different. The json file does include two important attributes, the total count and the number of entries per page. With these two values, we can loop through all the pages of entries and concatenate them into a single dataframe. The previous attempt already retrieved the first page so we can start at the second. I also included some simple data cleanup to remove null entries and change the duration values into hrs.</p>
<div id="de42a6b4" class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">page_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(my_json[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'total_count'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>my_json[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'per_page'</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (my_json[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'total_count'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> my_json[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'per_page'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb7-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> page <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, page_count):</span>
<span id="cb7-3">    params[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'page'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(page)</span>
<span id="cb7-4">    response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://api.track.toggl.com/reports/api/v2/details'</span>, headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> headers, params<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> params)</span>
<span id="cb7-5">    my_json <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.loads(response.content)</span>
<span id="cb7-6">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pd.concat([df, pd.DataFrame(my_json[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data'</span>])])</span>
<span id="cb7-7">    </span>
<span id="cb7-8">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dur'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dur'</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb7-9">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project'</span>].isnull()]</span>
<span id="cb7-10">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.drop([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tid'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'updated'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'user'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'use_stop'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'client'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project_color'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project_hex_color'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'task'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'billable'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'is_billable'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cur'</span>],</span>
<span id="cb7-11">            axis <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-12">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Month'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'start'</span>], utc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).dt.month_name()</span>
<span id="cb7-13">df<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'start'</span>)</span>
<span id="cb7-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(df))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>863</code></pre>
</div>
</div>
</section>
<section id="simple-graphics" class="level1">
<h1>Simple Graphics</h1>
<p>Before creating any complicated graphics for our dashboard, we should make a simple graphics. In this graph we total the duration’s and split them by their labeled project. I use the <code>sns.barplot</code> from the<code>seaborn</code> library because it is well designed and simple to use. This simple graph, shows the total number of hours attributed to each project.</p>
<div id="bfb8da5d" class="cell" data-execution_count="7">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">sns.barplot(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project'</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dur'</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df, estimator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sum'</span>, hue<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project'</span>)</span>
<span id="cb9-2">plt.xticks(rotation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="17">
<pre><code>([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
 [Text(0, 0, 'Commute'),
  Text(1, 0, 'Thesis work'),
  Text(2, 0, 'Walmart'),
  Text(3, 0, 'Writing course'),
  Text(4, 0, 'CHE215'),
  Text(5, 0, 'CHE615'),
  Text(6, 0, 'Administrative'),
  Text(7, 0, 'CHE616'),
  Text(8, 0, 'CE8201'),
  Text(9, 0, 'Mentoring'),
  Text(10, 0, 'Data Science Study'),
  Text(11, 0, 'Job Seeking')])</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/index_files/figure-html/cell-8-output-2.png" width="612" height="518" class="figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="dash-dashboard" class="level1">
<h1>Dash Dashboard</h1>
<p>The dash dashboard has a pretty simple construction. I start with loading the libraries required. The basic libraries include ‘Dash’ for the basic functions, ‘html’ to design the dashboard layout with html and ‘dcc’ for adding additional core components including radio-buttons and graphs.</p>
<p>Interactive graphs require some additional work. Interactive graphs require the use of the ‘callback’, ‘Input’ and ‘Output’ libraries. The input function is used to define the behaviour of the input componets, sush as the radio button. We assign the button an id and define that the component returned is the ‘value’.</p>
<p>I’ve also taken some monthly average values by using the <code>group_by</code> method. This creates a new dataframe which has multi-indexes which doesn’t work well with plotly express graphs. I drop the indexes to column values with the <code>reset_index</code> method. I finished the summary data by sorting it by the start time with the <code>sort_values</code> method.</p>
<div id="fcb65296" class="cell" data-execution_count="8">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import packages</span></span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dash <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Dash, html, dash_table, dcc, callback, Output, Input, jupyter_dash</span>
<span id="cb11-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb11-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> plotly.express <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> px</span>
<span id="cb11-5"></span>
<span id="cb11-6">jupyter_dash.default_mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"external"</span></span>
<span id="cb11-7"></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize the app</span></span>
<span id="cb11-9">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Dash(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>)</span>
<span id="cb11-10"></span>
<span id="cb11-11">df2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Month'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project'</span>]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>().reset_index().sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'start'</span>)</span></code></pre></div>
</details>
</div>
<p>The next stage of the dashboard is the <code>app.layout</code>. This forms the skeleton of the dashboard and as such utilizes some basic <code>html</code>. Through some experimentation, I have found I’ve liked the styling of the tabs. With the tabs, I can display different content on different tabs. Some tabs will be interactive while others will not.</p>
<p>Tabs are created with the <code>dcc.Tabs</code> function were you provide a list of <code>dcc.Tab</code> functions as children. For any interactive component, you have to provide an “id” so you can reference to it later, this includes the tab setup itself. For this dashboard I’ve decided to have a Datatable on tab one, an interactive annual review chart on tab 2, and a monthly review chart on tab 3. I decided to only make tab 2 interactive as the chart in tab 3 already has some basic interactive features by the nature of being a plotly express graph.</p>
<div id="a745c6a3" class="cell" data-execution_count="9">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># App layout</span></span>
<span id="cb12-2">app.layout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> html.Div([</span>
<span id="cb12-3">    dcc.Tabs(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tabs"</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-1'</span>, children<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb12-4">        dcc.Tab(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Data Table'</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-1'</span>),</span>
<span id="cb12-5">        dcc.Tab(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Annual Review'</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-2'</span>, children<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb12-6">            dcc.RadioItems(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'avg'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'count'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sum'</span>], value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sum'</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'controls-and-radio-item'</span>)]),</span>
<span id="cb12-7">        dcc.Tab(label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Monthly Review'</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-3'</span>),</span>
<span id="cb12-8">    ]),</span>
<span id="cb12-9">    html.Div(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tabs-content'</span>)</span>
<span id="cb12-10">])</span></code></pre></div>
</details>
</div>
<p>The next stage is the callback functions which runs whenever an input component is changed. Mainly, it is where the interactive inputs and output are declared. You declare them with the previously mention <code>Output</code> and <code>Input</code> functions. It is here were you reference the components based on the <code>id</code>s that you have called them. Your outputs need to be declared prior to your inputs and the order of your inputs will affect your update function. The second part of each declaration is the component that is effected.</p>
<div id="a756b1cc" class="cell" data-execution_count="10">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@app.callback</span>(</span>
<span id="cb13-2">  Output(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tabs-content'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'children'</span>),</span>
<span id="cb13-3">  Input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tabs'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>),</span>
<span id="cb13-4">  Input(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'controls-and-radio-item'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'value'</span>)</span>
<span id="cb13-5">  )</span></code></pre></div>
</details>
</div>
<p>The final step is to define the effect on your interactive elements in an update function. There are two components here for our dashboard, the updates due to the tab change and the updates from the radio button in the graph on tab 2. With an if statement, we return an HTML Div function/element with the corresponding content for that tab number. For those not familiar with HTML, a Div is just some user defined section.</p>
<div id="71d550c0" class="cell" data-execution_count="11">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> render_content(tab, hist):</span>
<span id="cb14-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> tab <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-1'</span>:</span>
<span id="cb14-3">        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">return</span> html.Div([</span>
<span id="cb14-4">            dash_table.DataTable(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>df.drop(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tags'</span>, axis <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).to_dict(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'records'</span>), page_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb14-5">        ])</span>
<span id="cb14-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">elif</span> tab <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-2'</span>:</span>
<span id="cb14-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">return</span> html.Div([</span>
<span id="cb14-8">            dcc.Graph(figure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> px.histogram(df, x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project'</span>, y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dur'</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'description'</span>, histfunc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hist),</span>
<span id="cb14-9">                     <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'controls-and-graph'</span>)   </span>
<span id="cb14-10">        ])</span>
<span id="cb14-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">elif</span> tab <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tab-3'</span>:</span>
<span id="cb14-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">return</span> html.Div([</span>
<span id="cb14-13">            dcc.Graph(figure <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> px.line(df2, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dur'</span>, x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Month'</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'project'</span>, markers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>))          </span>
<span id="cb14-14">        ])</span>
<span id="cb14-15"></span>
<span id="cb14-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run the app</span></span>
<span id="cb14-17"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'__main__'</span>:</span>
<span id="cb14-18">    app.run(debug<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Dash app running on http://127.0.0.1:8050/</code></pre>
</div>
</div>
<p>Our program has now run a server to host the dashboard. You view the dashboard by opening the hyperlink or going to the default address ‘http://127.0.0.1:8050/’ in your internet browser. The server is host localy, mean I can not easily display the interactive features.</p>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>We have connected to the Toggl service with an API call. With this API call, we collected a years worth of time tracking data to create a local dataframe. Exploring the data with graphs helped us understand the structure and the quality of data in our dataframe. A simple dash dashboard was created with some interactive features takes to the Input and Output functions. This dashboard has multiple tabs, display different types of data on each tab. The first tab simply shows a table of the data as illustrated in the following image:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/images/tab1.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
<p>On the second tab, we’ve created an interactive chart that uses a radio button. This radio button controls how the data in our chart will be aggregated.</p>
<p><img src="https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/images/tab2.png" class="img-fluid"></p>
<p>And on the final tab, we created a interactive graph with the plotly express library. This chart summaries or data on a month by month basis.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/images/tab3.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>


<!-- -->

</section>

 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <category>Dashboard</category>
  <guid>https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/</guid>
  <pubDate>Tue, 12 Mar 2024 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2024-03-12-Tracking you time dashboard featuring Toggl API/images/tab3.png" medium="image" type="image/png" height="60" width="144"/>
</item>
<item>
  <title>Custom OpenAI Chatbot Pt2: Fun with Lang Chain</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2023-10-30-Custom OpenAI Chatbot Pt2/</link>
  <description><![CDATA[ 




<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2023-10-30-Custom OpenAI Chatbot Pt2/pexels-google-deepmind-18069697.jpg" class="img-fluid figure-img"></p>
<figcaption>Photo by Google DeepMind</figcaption>
</figure>
</div>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>This is a continuation from a previous post about creating a custom ChatGPT bot with Langchain. From the previous post, we have created a CSV file which contains the text captured from a folder full of different PDF files. We will continue from there, creating a custom ChatGPT bot from that CSV files.</p>
</section>
<section id="loading-packages" class="level1">
<h1>Loading packages</h1>
<p>As with any project, the first step is to load some of the necessary python packages. The <code>load_dotenv</code> function from the <code>dotenv</code> plays a special role in this program. In order to use the Open AI API, we will need to create and Open AI account. With this account, you will receive an API key, which will be connected to any functional calls with your account. You can keep these API keys in your code, but it is better practice to store them in your environmental variables. The environmental variables is a location in windows where you can store some sensitive information that will be only stored on your computer. You can find where your environmental variables are with the windows search, or you can find it under your advanced system properties. Make a new entry in either the user variables or system variables (whichever you prefer) and name it “OPENAI_API_KEY”. The value will be the actual key you get from your Open AI account. After you save your API key, you will need to restart windows. You will need to pay for your API requests for your Open AI account. Using the embedding model turns out to be pretty cheap, while questioning the ChatGPT cost a bit more, usually only a one or two cents per question.</p>
<div class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sys</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dotenv <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_dotenv</span>
<span id="cb1-5"></span>
<span id="cb1-6">load_dotenv()</span></code></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>False</code></pre>
</div>
</div>
</section>
<section id="document-loading" class="level1">
<h1>Document Loading</h1>
<p>The next step is to load your CSV files into a document format used by Lang Chain. I found that the best way to complete this is with the <code>DirectoryLoader</code> and the <code>CSVLoader</code> functions. I have also found that it is important to specify the encoding used to create the CSV files from the pandas dataframes. Furthermore, I’ve also included an additional step which will add 1 to the row numbers in the metadata. The row value represents the page number within the document, but the default indexing for a pandas dataframe starts at 0 and not at 1. Getting the length of our documents represents the overall number of pages that we have scanned from our PDF files.</p>
<div class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.document_loaders <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> DirectoryLoader, CSVLoader</span>
<span id="cb3-2"></span>
<span id="cb3-3">loader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> DirectoryLoader(path<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/Users/Mark/Desktop/csv_files'</span>, glob <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'**/*.csv'</span>, loader_cls <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CSVLoader, use_multithreading <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, show_progress <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, silent_errors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, loader_kwargs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'encoding'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8-sig'</span>})</span>
<span id="cb3-4"></span>
<span id="cb3-5">docs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> loader.load()</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> doc <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> docs:</span>
<span id="cb3-8">  doc.metadata[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'row'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(docs))</span></code></pre></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>  0%|          | 0/3 [00:00&lt;?, ?it/s]100%|██████████| 3/3 [00:00&lt;?, ?it/s]</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>46</code></pre>
</div>
</div>
<p>Just to get an idea of the data format, we can print the 10 item stored in our documents.</p>
<div class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">docs[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]</span></code></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="3">
<pre><code>Document(page_content=": 7\n0: Chatbot - Wikipedia https://en.wikipedia.org/wiki/Chatbot\n\nThe creation and implementation of chatbots is still a developing area, heavily related to artificial intelligence and machine learning,\nso the provided solutions, while possessing obvious advantages, have some important limitations in terms of functionalities and use\ncases. However, this is changing over time.\n\nThe most common limitations are listed below:!83]\n\n= As the input/output database is fixed and limited, chatbots can fail while dealing with an unsaved query.!52]\n\n= A chatbot's efficiency highly depends on language processing and is limited because of irregularities, such as accents and\nmistakes.\n\n= Chatbots are unable to deal with multiple questions at the same time and so conversation opportunities are limited.|83I\n\n= Chatbots require a large amount of conversational data to train. Generative models, which are based on deep learning\nalgorithms to generate new responses word by word based on user input, are usually trained on a large dataset of natural-\nlanguage phrases. |S]\n\n« Chatbots have difficulty managing non-linear conversations that must go back and forth on a topic with a user.|&amp;41\n\n= As it happens usually with technology-led changes in existing services, some consumers, more often than not from older\ngenerations, are uncomfortable with chatbots due to their limited understanding, making it obvious that their requests are being\ndealt with by machines. |83]\n\nChatbots and jobs\n\nChatbots are increasingly present in businesses and often are used to automate tasks that do not require skill-based talents. With\ncustomer service taking place via messaging apps as well as phone calls, there are growing numbers of use-cases where chatbot\ndeployment gives organizations a clear return on investment. Call center workers may be particularly at risk from AI-driven\nchatbots.!85]\n\nChatbot jobs\n\nChatbot developers create, debug, and maintain applications that automate customer services or other communication processes.\nTheir duties include reviewing and simplifying code when needed. They may also help companies implement bots in their operations.\n\nA study by Forrester (June 2017) predicted that 25% of all jobs would be impacted by AI technologies by 2019.!86]\n\nSee also\n\n= Applications of artificial intelligence =» Autonomous agent\n\n8 of 18 2023-11-10, 9:32 a.m.", metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 8})</code></pre>
</div>
</div>
</section>
<section id="document-transformation" class="level1">
<h1>Document Transformation</h1>
<p>We could work with the documents we have just as they are now, but it is sometimes difficult to get the proper level of context from a question from an entire page of information. One way to rectify this is by using the <code>ReqcursiveCharacterTextSplitter</code> function which will break down the data into smaller sections. The function has some built-in breakpoints point like section titles and ends of paragraphs. We could then use the smaller split up documents to makes sure our search’s are more accurate. I have found that using both the overall page documents for overall context and the smaller in-depth documents for smaller details together works best. We see that this drastically increases the number of documents that we need to search through.</p>
<div class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.text_splitter <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RecursiveCharacterTextSplitter</span>
<span id="cb8-2"></span>
<span id="cb8-3">text_splitter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RecursiveCharacterTextSplitter(chunk_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">512</span>, chunk_overlap <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb8-4"></span>
<span id="cb8-5">documents <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> text_splitter.split_documents(docs)</span>
<span id="cb8-6">documents <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> documents <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> docs</span>
<span id="cb8-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(documents)</span></code></pre></div>
</details>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre><code>454</code></pre>
</div>
</div>
</section>
<section id="text-embeddings" class="level1">
<h1>Text Embeddings</h1>
<p>Text embeddings are the real power behind all natural language processes, including chatbots. A text embedding is a vector that represents the meaning behind a text and not the text itself. In this way, we can search through the meaning of our documents for an answer without needing to match the text exactly. In order to use the Open AI API to answer our questions, we need to utilize the Open AI Embeddings model. The use of it is pretty simple, when send them a document and they return the embedding.</p>
<div class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> openai</span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.embeddings <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> OpenAIEmbeddings</span>
<span id="cb10-3"></span>
<span id="cb10-4">openai.api_key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.getenv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OPENAI_API_KEY"</span>)</span>
<span id="cb10-5">embeddings_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> OpenAIEmbeddings(request_timeout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="vector-store" class="level1">
<h1>Vector Store</h1>
<p>The next step is not 100% necessary, but it will make our lives much easier. What we need to do is to create a database that connects each of the documents with their embedding. We can do this by creating a vector store. There are a few different vector stores, the one I chose to us is `FAISS’. All we need to do is define the vector store, send it our stored documents and send the connection to our embedding model. The function will then process each document for us.</p>
<div class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.vectorstores <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> FAISS</span>
<span id="cb11-2"></span>
<span id="cb11-3">db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> FAISS.from_documents(documents, embeddings_model)</span></code></pre></div>
</details>
</div>
</section>
<section id="multiquery-retriever" class="level1">
<h1>MultiQuery Retriever</h1>
<p>What we have now is a database that we can search for ourselves if we like. You can query the vector store, convert that query into an embedding, and search the vector store for matches. The return from this query will then be a list of documents in the vector store, which maybe useful, but can be difficult to interpret by itself. What a custom chatbot does is use this basic search results and supply it to a Large Language Model (llm) to make sense of it. We would like our Chatbot to have the entire vector store to look through to answer our questions, but there are some limitations to how much context we can provide to Open Ai.</p>
<p>For reference, the Chat GPT3 API has a limit of about 4000 tokens. What are tokens? Well, tokens are small sections of text. They are usually longer than 1 character but smaller than most words. One thing to keep in mind is that the 4000 token limitation covers everything, including context provided, the query and the answer returned. Do to this limitation, we need to do a search through our vector store and limit the context that we send to the Open AI API.</p>
<p>We could perform a basic query, but I prefer to create a Multi-query. In this query, we are actually asking ChatGPT to create different variations to our query. We can then search our vector store on multiple queries are return the best results over all queries. There is some expense to this as it requires an additional API call to generate the new queries.</p>
<p>We can then send our question to our retriever which will automatically perform our search over the vector store, I use the default prompt as it seems to do a good job. The custom Chat bot will then return the answer to our question, based on the context provided by the vector store search, with the source documents it used to generate an answer.</p>
<div class="cell" data-execution_count="7">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.chains <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> RetrievalQA</span>
<span id="cb12-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.retrievers.multi_query <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MultiQueryRetriever</span>
<span id="cb12-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> langchain.llms <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> OpenAI</span>
<span id="cb12-4"></span>
<span id="cb12-5">retriever_from_llm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MultiQueryRetriever.from_llm(</span>
<span id="cb12-6">  retriever <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db.as_retriever(search <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mmr'</span>, search_kwags <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'k'</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>}),</span>
<span id="cb12-7">  llm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> OpenAI())</span>
<span id="cb12-8">  </span>
<span id="cb12-9">question <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"What do Chat bots do?"</span></span>
<span id="cb12-10">qa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> RetrievalQA.from_chain_type(llm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> OpenAI(),</span>
<span id="cb12-11">chain_type <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stuff"</span>,</span>
<span id="cb12-12">retriever <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> retriever_from_llm,</span>
<span id="cb12-13">return_source_documents <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-14"></span>
<span id="cb12-15">ans <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> qa({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"query"</span>:question}, return_only_outputs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb12-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ans)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>{'result': ' Chatbots are software applications or web interfaces that aim to mimic human conversation through text or voice interactions. Modern chatbots are typically online and use artificial intelligence (AI) systems that are capable of maintaining a conversation with a user in natural language and simulating the way a human would behave as a conversational partner. They are used for B2C customer service, sales and marketing, as well as in toys, devices, and other applications. They can also be used to fill chat rooms with spam and advertisements, by mimicking human behavior and conversations or to entice people into revealing personal information.', 'source_documents': [Document(page_content='A chatbot (originally chatterbot!!) is a software application or web interface that aims to\nmimic human conversation through text or voice interactions.l2I[3Il41 Modern chatbots are\ntypically online and use artificial intelligence (AI) systems that are capable of maintaining a\nconversation with a user in natural language and simulating the way a human would behave as a\nconversational partner. Such technologies often utilize aspects of deep learning and natural', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 1}), Document(page_content=': 3\n0: Chatbot - Wikipedia https://en.wikipedia.org/wiki/Chatbot\n\nIn 2016, Facebook Messenger allowed developers to place chatbots on their platform. There were 30,000 bots created for Messenger\nin the first six months, rising to 100,000 by September 2017.!27]\n\nSince September 2017, this has also been as part of a pilot program on WhatsApp. Airlines KLM and Aeroméxico both announced\ntheir participation in the testing;!281[29l[30I[31] oth airlines had previously launched customer services on the Facebook Messenger\nplatform.\n\nThe bots usually appear as one of the user\'s contacts, but can sometimes act as participants in a group chat.\n\nMany banks, insurers, media companies, e-commerce companies, airlines, hotel chains, retailers, health care providers, government\nentities and restaurant chains have used chatbots to answer simple questions, increase customer engagement,!32! for promotion, and\nto offer additional ways to order from them.!33]\n\nA 2017 study showed 4% of companies used chatbots.!34] According to a 2016 study, 80% of businesses said they intended to have\none by 2020,[35]\n\nAs part of company apps and websites\n\nPrevious generations of chatbots were present on company websites, e.g. Ask Jenn from Alaska Airlines which debuted in 200834 or\nExpedia\'s virtual customer service agent which launched in 2011.[361[37] The newer generation of chatbots includes IBM Watson-\npowered "Rocky", introduced in February 2017 by the New York City-based e-commerce company Rare Carat to provide information\nto prospective diamond buyers.!381[39]\n\nChatbot sequences\n\nUsed by marketers to script sequences of messages, very similar to an autoresponder sequence. Such sequences can be triggered by\nuser opt-in or the use of keywords within user interactions. After a trigger occurs a sequence of messages is delivered until the next\nanticipated user response. Each user response is used in the decision tree to help the chatbot navigate the response sequences to\ndeliver the correct response message.\n\nCompany internal platforms\n\nOther companies explore ways they can use chatbots internally, for example for Customer Support, Human Resources, or even in\nInternet-of-Things (IoT) projects. Overstock.com, for one, has reportedly launched a chatbot named Mila to automate certain simple\nyet time-consuming processes when requesting sick leave.[4°] Other large companies such as Lloyds Banking Group, Royal Bank of\n\n4 of 18 2023-11-10, 9:32 a.m.', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 4}), Document(page_content='of chatbots). Chatbots can also be designed or customized to further target even more specific\nsituations and/or particular subject-matter domains.!Z]', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 1}), Document(page_content=': 2\n0: Chatbot - Wikipedia https://en.wikipedia.org/wiki/Chatbot\n\n(though the program as released would not have been capable of doing so).{15]\n\nFrom 197816] to some time after 1983,!7] the CYRUS project led by Janet Kolodner constructed a chatbot simulating Cyrus Vance\n(57th United States Secretary of State). It used case-based reasoning, and updated its database daily by parsing wire news from\nUnited Press International. The program was unable to process the news items subsequent to the surprise resignation of Cyrus Vance\nin April 1980, and the team constructed another chatbot simulating his successor, Edmund Muskie.8]57]\n\nOne pertinent field of AI research is natural-language processing. Usually, weak AI fields employ specialized software or\nprogramming languages created specifically for the narrow function required. For example, A.L.I.C.E. uses a markup language called\nAIML,3] which is specific to its function as a conversational agent, and has since been adopted by various other developers of, so-\ncalled, Alicebots. Nevertheless, A.L.I.C.E. is still purely based on pattern matching techniques without any reasoning capabilities, the\nsame technique ELIZA was using back in 1966. This is not strong AI, which would require sapience and logical reasoning abilities.\n\nJabberwacky learns new responses and context based on real-time user interactions, rather than being driven from a static database.\nSome more recent chatbots also combine real-time learning with evolutionary algorithms that optimize their ability to communicate\nbased on each conversation held. Still, there is currently no general purpose conversational artificial intelligence, and some software\ndevelopers focus on the practical aspect, information retrieval.\n\nChatbot competitions focus on the Turing test or more specific goals. Two such annual contests are the Loebner Prize and The\nChatterbox Challenge (the latter has been offline since 2015, however, materials can still be found from web archives)./291\n\nChatbots may use neural networks as a language model. For example, generative pre-trained transformers (GPT), which use the\ntransformer architecture, have become common to build sophisticated chatbots. The "pre-training" in its name refers to the initial\ntraining process on a large text corpus, which provides a solid foundation for the model to perform well on downstream tasks with\nlimited amounts of task-specific data. An example of a GPT chatbot is ChatGPT.[2°] Despite criticism of its accuracy, ChatGPT has\ngained attention for its detailed responses and historical knowledge. Another example is BioGPT, developed by Microsoft, which\nfocuses on answering biomedical questions.!21I[22]\n\nDBpedia created a chatbot during the GSoC of 2017./231[241I25] Tt can communicate through Facebook Messenger.\n\nApplication\nMessaging apps\nMany companies’ chatbots run on messaging apps or simply via SMS. They are used for B2C customer service, sales and\n\nmarketing. [26]\n\n3 of 18 2023-11-10, 9:32 a.m.', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 3}), Document(page_content=': 2\n0: Chatbot - Wikipedia https://en.wikipedia.org/wiki/Chatbot\n\n(though the program as released would not have been capable of doing so).{15]', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 3}), Document(page_content=": 6\n0: Chatbot - Wikipedia https://en.wikipedia.org/wiki/Chatbot\n\nIn India, the state government has launched a chatbot for its Aaple Sarkar platform,!Z] which provides conversational access to\ninformation regarding public services managed.!721I73]\n\nToys\nChatbots have also been incorporated into devices not primarily meant for computing, such as toys.[74]\n\nHello Barbie is an Internet-connected version of the doll that uses a chatbot provided by the company ToyTalk,!75] which previously\nused the chatbot for a range of smartphone-based characters for children.!76] These characters' behaviors are constrained by a set of\nrules that in effect emulate a particular character and produce a storyline.!77]\n\nThe My Friend Cayla doll was marketed as a line of 18-inch (46 cm) dolls which uses speech recognition technology in conjunction\nwith an Android or iOS mobile app to recognize the child's speech and have a conversation. It, like the Hello Barbie doll, attracted\ncontroversy due to vulnerabilities with the doll's Bluetooth stack and its use of data collected from the child's speech.\n\nIBM's Watson computer has been used as the basis for chatbot-based educational toys for companies such as CogniToys!74] intended\nto interact with children for educational purposes.!78]\n\nMalicious use\n\nMalicious chatbots are frequently used to fill chat rooms with spam and advertisements, by mimicking human behavior and\nconversations or to entice people into revealing personal information, such as bank account numbers. They were commonly found on\nYahoo! Messenger, Windows Live Messenger, AOL Instant Messenger and other instant messaging protocols. There has also been a\npublished report of a chatbot used in a fake personal ad on a dating service's website.[79]\n\nTay, an AI chatbot that learns from previous interaction, caused major controversy due to it being targeted by internet trolls on\nTwitter. The bot was exploited, and after 16 hours began to send extremely offensive Tweets to users. This suggests that although the\nbot learned effectively from experience, adequate protection was not put in place to prevent misuse.[8°]\n\nIf a text-sending algorithm can pass itself off as a human instead of a chatbot, its message would be more credible. Therefore,\nhuman-seeming chatbots with well-crafted online identities could start scattering fake news that seems plausible, for instance\nmaking false claims during an election. With enough chatbots, it might be even possible to achieve artificial social proof.[81J[82]\n\nLimitations of chatbots\n\n7 of 18 2023-11-10, 9:32 a.m.", metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 7}), Document(page_content='Chatbots and jobs\n\nChatbots are increasingly present in businesses and often are used to automate tasks that do not require skill-based talents. With\ncustomer service taking place via messaging apps as well as phone calls, there are growing numbers of use-cases where chatbot\ndeployment gives organizations a clear return on investment. Call center workers may be particularly at risk from AI-driven\nchatbots.!85]\n\nChatbot jobs', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 8}), Document(page_content='Many high-tech banking organizations are looking to integrate automated AI-based solutions such as chatbots into their customer\nservice in order to provide faster and cheaper assistance to their clients who are becoming increasingly comfortable with technology.\nIn particular, chatbots can efficiently conduct a dialogue, usually replacing other communication tools such as email, phone, or SMS.', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 5}), Document(page_content='Chatbot jobs\n\nChatbot developers create, debug, and maintain applications that automate customer services or other communication processes.\nTheir duties include reviewing and simplifying code when needed. They may also help companies implement bots in their operations.\n\nA study by Forrester (June 2017) predicted that 25% of all jobs would be impacted by AI technologies by 2019.!86]\n\nSee also\n\n= Applications of artificial intelligence =» Autonomous agent\n\n8 of 18 2023-11-10, 9:32 a.m.', metadata={'source': 'C:\\Users\\Mark\\Desktop\\csv_files\\Chatbot - Wikipedia.csv', 'row': 8})]}</code></pre>
</div>
</div>
<p>If you are wondering what the default query looks like, you can use the following code. You can create your own prompt if you like, but the default seems to work well for me. With a custom prompt you can customize the output, like make the results return summarized in a Markdown table if you like.</p>
<div class="cell" data-execution_count="8">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(retriever_from_llm.llm_chain.prompt)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>input_variables=['question'] template='You are an AI language model assistant. Your task is \n    to generate 3 different versions of the given user \n    question to retrieve relevant documents from a vector  database. \n    By generating multiple perspectives on the user question, \n    your goal is to help the user overcome some of the limitations \n    of distance-based similarity search. Provide these alternative \n    questions separated by newlines. Original question: {question}'</code></pre>
</div>
</div>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Well, there are all the steps needed to create your very own custom ChatGPT bot with Lang chain. We loaded our CSV files, split the documents into smaller section, created a vector store of our documents, search through our vector store with a multi-query and forward the context and our question to the Open AI API. The result is an answer that is limited to the data stored in our PDF documents.</p>
<p><a href="https://www.pexels.com/photo/an-artist-s-illustration-of-artificial-intelligence-ai-this-illustration-depicts-language-models-which-generate-text-it-was-created-by-wes-cockx-as-part-of-the-visualising-ai-project-l-18069697/">Photo by Google DeepMind</a></p>


<!-- -->

</section>

 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <category>AI</category>
  <guid>https://datasandbox.netlify.app/posts/2023-10-30-Custom OpenAI Chatbot Pt2/</guid>
  <pubDate>Wed, 29 Nov 2023 05:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2023-10-30-Custom OpenAI Chatbot Pt2/pexels-google-deepmind-18069697.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Custom OpenAI Chatbot Pt1: PDF scanning</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2023-09-12-Custom OpenAI Chatbot Pt1/</link>
  <description><![CDATA[ 




<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://unsplash.com/@siva_photography?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash"><img src="https://datasandbox.netlify.app/posts/2023-09-12-Custom OpenAI Chatbot Pt1/chat.jpg" class="img-fluid figure-img" alt="Photo by Levart_Photographer on Unsplash"></a></p>
<figcaption>Photo by Levart_Photographer on Unsplash</figcaption>
</figure>
</div>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>I have recently completed a project at work, the creation of a custom ChatGPT chatbot. I will break the project into two parts, the first part will scan a folder of PDF files into a dataframe and the second part will pass the data to OpenAI API. This entire project was completed in <code>python</code>.</p>
</section>
<section id="project-outline" class="level1">
<h1>Project outline</h1>
<p>PDFs can be easily scanned in <code>python</code> with the <code>pypdf</code> module. It is easily installed and easily run, but I have found that the quality of the scan to be lacking. <code>Pypdf</code> also seems to have some issues with PDFs that created from scanned documents, not directly created from a text document. For this reason, I have found an alternative method.</p>
<p>The first step is to convert all the PDFs in a directory to PNG images. This can be achieved with the <code>convert from path</code> function from the <code>pdf2image</code> library with the poppler application. The poppler program can be downloaded <a href="https://poppler.freedesktop.org/">here</a> and unzipped into its own directory. There is no Windows version on the poppler site, but I found a repo with a nearly updated Windows version <a href="https://github.com/oschwartz10612/poppler-windows/releases">here</a>. You will need to copy the directory path into your code. We can create a for loop to open each PDF file one at a time. It’s important to remember to change the ‘' to’/’ for Windows users when referring to directory positions.</p>
<p>The second step is to then scan through the PNG images with OCR. For this task, we can use Tesseract. Tesseract is a Google project that is easy to use. Like Poppler, you will need to download the application separately. You will also need to install the helper <code>python</code> package <code>pytessseract</code>. The Tesseract application can be found <a href="https://github.com/UB-Mannheim/tesseract/wiki">here</a>. I have my program to save the data in a CSV file, but you can store it anyway you want. I decided to save each PDF file as a separate CSV file and assigning each row as a different PNG file or page of the PDF. This was to ensure that may data is easily organized</p>
<p>The next stages require getting into the <code>Langchain</code> library. These steps will be included in the follow-up to this post as both post, are quite lengthy and each can stand alone.</p>
</section>
<section id="converting-pdf-to-png-with-poppler" class="level1">
<h1>Converting PDF to PNG with Poppler</h1>
<p>Again, prior to running this code, you will need to install the Poppler Application. You also need to copy the directory to the location of the Poppler bin folder. The rest of this section is pretty simple, I’ve created a loop to go through every filename that ends with ‘.pdf’ in a specific PDF folder. I also save the PNG file with the page number included into the title. If the results from the OCR scans are inaccurate, you can adjust the resolution of the PNG files with the parameter ‘dpi = 300’ passed to the convert from path function. The default value is 100. Fair warning, increasing the resolution will slow down the entire process and can potentially add additional artifacts into the OCR scan.</p>
<div class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> PIL <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Image</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pdf2image <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> convert_from_path</span>
<span id="cb1-5"></span>
<span id="cb1-6">poppler_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/Program Files/poppler-23.08.0/Library/bin'</span></span>
<span id="cb1-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> pdf_file <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> [f <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> os.listdir(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF'</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> f.endswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.pdf'</span>)]:</span>
<span id="cb1-8">  images <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> convert_from_path(pdf_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF/'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pdf_file, poppler_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> poppler_path)</span>
<span id="cb1-9"></span>
<span id="cb1-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> count, img <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(images):</span>
<span id="cb1-11">    img_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pdf_file[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_page_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.png"</span></span>
<span id="cb1-12">    img.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF/'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> img_name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PNG"</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="ocr-from-png-files" class="level1">
<h1>OCR from PNG files</h1>
<p>The Tesseract application is required for the next stage. Since every PNG from every PDF will need to go through the process, I’ve recreated the first section and included the Tesseract functions into the same loop. I’ve also included a step to delete each PNG file after it has been scanned, since it will no longer be needed. The final stage is to save all the returned data as a CSV file. I have found that it is useful to specify the encoding used in saving the CSV.</p>
<div class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> PIL <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Image</span>
<span id="cb2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pdf2image <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> convert_from_path</span>
<span id="cb2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pytesseract</span>
<span id="cb2-6"></span>
<span id="cb2-7">poppler_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/Program Files/poppler-23.08.0/Library/bin'</span></span>
<span id="cb2-8">pytesseract.pytesseract.tesseract_cmd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Tesseract-OCR/tesseract.exe'</span></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> pdf_file <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> [f <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> os.listdir(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF'</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> f.endswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.pdf'</span>)]:</span>
<span id="cb2-11">  images <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> convert_from_path(pdf_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pdf_file, poppler_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> poppler_path)</span>
<span id="cb2-12">  extracted_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-13">  <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> count, img <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(images):</span>
<span id="cb2-14">    img_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pdf_file[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_page_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>count<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.png"</span></span>
<span id="cb2-15">    img.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> img_name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PNG"</span>)</span>
<span id="cb2-16">    </span>
<span id="cb2-17">    extracted_data.append(pytesseract.image_to_string(Image.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/Users/Mark/Desktop/PDF'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> img_name)))</span>
<span id="cb2-18">    os.remove(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> img_name)</span>
<span id="cb2-19">    </span>
<span id="cb2-20">  df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(extracted_text)</span>
<span id="cb2-21">  df.to_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'//Desktop/PDF'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> pdf_name[:<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.csv'</span>, encoding <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8-sig'</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>We are finally able to create a usable CSV file from a OCR scanned PDF file. The first step was to convert the pdf into PNG files with Poppler. Each png is then scanned with Tesseract. And the returned values are stored in a CSV file. By why would you want to go through all the steps in the first place? Well, we will need to proceed with the next post about creating the ChatGPT chatbot.</p>


<!-- -->

</section>

 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <category>AI</category>
  <guid>https://datasandbox.netlify.app/posts/2023-09-12-Custom OpenAI Chatbot Pt1/</guid>
  <pubDate>Mon, 30 Oct 2023 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2023-09-12-Custom OpenAI Chatbot Pt1/chat.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Tree Based Methods: Exploring the Forest</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-10-03-Tree Based Methods/</link>
  <description><![CDATA[ <div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><a href="https://creator.nightcafe.studio/creation/W67swvukEgY208MwFY50"><img src="https://datasandbox.netlify.app/posts/2022-10-03-Tree Based Methods/forest.jpg" class="img-fluid figure-img" alt="Forest: Generated by Nightcafe AI"></a></p>
<figcaption>Forest: Generated by Nightcafe AI</figcaption></figure>
</div>
<section id="introduction" class="level1"><h1>Introduction</h1>
<p>I was recently reading my copy of <a href="https://amzn.to/3y9X7n8">“An Introduction to Statistical Learning”</a> (my Amazon affiliate link) and got the chapter about the different tree based methods. I am pretty familiar with Random Forest, but a few of the other methods are new to me. Let’s explore these different techniques.</p>
<p>For these examples, I will explore the glass dataset from the <a href="https://www.openml.org/search?type=data&amp;status=active&amp;sort=nr_of_downloads&amp;id=41">openml</a> site. This dataset has 9 different features used to predict the type of glass. The dataset has 214 observations.</p>
<p>The dataset is downloaded with the following code. This requires the <code>farff</code> package to open the <em>arff</em> files used on the openml site.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://tidyverse.tidyverse.org">tidyverse</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/download.file.html">download.file</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.openml.org/data/download/41/dataset_41_glass.arff"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.arff"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">farff</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readARFF</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.arff"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</details>
</div>
<p>From there, we can start to explore the dataset and set up a train and testing split for the data.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/utils/str.html">str</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>'data.frame':   214 obs. of  10 variables:
 $ RI  : num  1.52 1.52 1.52 1.51 1.53 ...
 $ Na  : num  12.8 12.2 13.2 14.4 12.3 ...
 $ Mg  : num  3.5 3.52 3.48 1.74 0 2.85 3.65 2.84 0 3.9 ...
 $ Al  : num  1.12 1.35 1.41 1.54 1 1.44 0.65 1.28 2.68 1.3 ...
 $ Si  : num  73 72.9 72.6 74.5 70.2 ...
 $ K   : num  0.64 0.57 0.59 0 0.12 0.57 0.06 0.55 0.08 0.55 ...
 $ Ca  : num  8.77 8.53 8.43 7.59 16.19 ...
 $ Ba  : num  0 0 0 0 0 0.11 0 0 0.61 0 ...
 $ Fe  : num  0 0 0 0 0.24 0.22 0 0 0.05 0.28 ...
 $ Type: Factor w/ 7 levels "build wind float",..: 1 3 1 6 2 2 3 1 7 2 ...</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/droplevels.html">droplevels</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Random.html">set.seed</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/sample.html">sample</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nrow.html">nrow</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/Round.html">floor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nrow.html">nrow</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test</span>,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">traindf</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">test</span>,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span></code></pre></div>
</details>
</div>
</section><section id="decision-tree" class="level1"><h1>Decision Tree</h1>
<p>The most basic model is the decision tree. In this model, all classes are stored in a container at the root of the tree. The root is split by certain feature values into two smaller containers. These smaller containers are called leafs. After these initial two leafs, we perform an additional split resulting in 4 new leafs. At this point, there should be some better sorting on the response value in the new leafs. Splitting and creating new leafs is continued until the desire results are obtained.</p>
<p>The main disadvantage of decision trees is that they are very prone to over fitting. It is pretty easy to imagine a tree that has split so many times that each leaf now represents a single observation. This tree would have 100% accuracy on the training set, but would not perform well on a test set. We prevent over fitting by cutting off or pruning the number of leafs to a certain value.</p>
<p>For our example, the decision tree is created with the <code>tree</code> package. It is pretty simple to use, you just supply it with the function and the data.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdltree</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tree</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tree</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">traindf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</details>
</div>
<p>A tree visualization is easily created with the print command. With an extra line, we can add the text that shows where each leaf is split. It does get difficult to understand the splits when getting to the lower level leafs.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdltree</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdltree</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/graphics/text.html">text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdltree</span>, cex <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</details><div class="cell-output-display">
<div>
<figure class="figure"><p><img src="https://datasandbox.netlify.app/posts/2022-10-03-Tree Based Methods/index_files/figure-html/treeplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can now test the decision tree’s performance on the test data. Since the response is a factor, the resulting prediction is a matrix with a probability assigned to each response for every observation. We can summarize the mostly likely response with the <code>max.col</code> function.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">treevalues</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/predict.html">predict</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdltree</span>, newdata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">treepred</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/colnames.html">colnames</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">treevalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/maxCol.html">max.col</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">treevalues</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Acctree</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">treepred</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Acctree</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 0.5</code></pre>
</div>
</div>
<p>From this prediction, we can see that the decision tree didn’t provide a high level of accuracy (50% Accuracy). For a higher level of accuracy, let’s explore some additional methods.</p>
</section><section id="bagging" class="level1"><h1>Bagging</h1>
<p>Bootstrapping is a useful technique when we have a limited number of observations. Ordinarily, we take each training observation out of a bag and train our model with it. In bootstrapping, after we train our model with an observation, we put the observation back in the bag, allowing repeated selection. The repeated selection creates a smoother distribution of observations.</p>
<p>Bagging takes this process one step further. We take multiple models from bootstrapped training data and average their values. Each bootstrapped model is unique because the training observations are randomly selected.</p>
<p>Bagging can be performed with the <code>randomForest</code> function. By default, the function creates trees from a subset of features, but we can create bagged trees by using all features. The default behaviour for a random forest is to take the majority vote for constructed trees or average their prediction.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://www.stat.berkeley.edu/~breiman/RandomForests/">randomForest</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdlbag</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/randomForest/man/randomForest.html">randomForest</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/droplevels.html">droplevels</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">traindf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, mtry <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/nrow.html">ncol</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">df</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n.trees <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bagpreds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/predict.html">predict</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdlbag</span>, newdata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Accbag</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bagpreds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Accbag</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6666667</code></pre>
</div>
</div>
</section><section id="random-forest" class="level1"><h1>Random Forest</h1>
<p>A random forest is an ensemble model, meaning the prediction is created by combining multiple models together. It is very similar to the previous example with Bagging. The main difference is that the Random Forest selects a random set of features when creating each tree.</p>
<p>The rationale for a random feature selection is to create more unique trees. If there is a strong feature in bagging, then most of the trees will use it to create their first split. This creates trees that appear very similar.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdlforest</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/randomForest/man/randomForest.html">randomForest</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/droplevels.html">droplevels</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">traindf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, n.trees <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rfpreds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/predict.html">predict</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdlforest</span>, newdata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Accrf</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rfpreds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Accrf</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6666667</code></pre>
</div>
</div>
</section><section id="boosting" class="level1"><h1>Boosting</h1>
<p>Boosting is another multiple use technique, much like bagging. Instead of effecting the train data to create different trees, boosting builds trees in sequential order with the default training set. Rather than fitting trees to the response, trees are fit to their residuals. Each tree then learns slowly with the residuals from the previous tree.</p>
<p>For this model, we will to use two libraries, <code>caret</code> and <code>gbm</code>. The <code>gbm</code> package is the package that provides the actual model, but a warning is displayed when using the <code>gbm</code> function. To get around the issue, we can use the <code>caret</code> package with the <code>train</code> function. This function can accept many different models and will automatically go through tuning parameters.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://github.com/topepo/caret/">caret</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://github.com/gbm-developers/gbm">gbm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdlboost</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">caret</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/caret/man/train.html">train</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">traindf</span>, method <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gbm'</span>, verbose <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">boostpreds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/predict.html">predict</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mdlboost</span>, newdata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Accboost</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">boostpreds</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">testdf</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Accboost</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 0.6904762</code></pre>
</div>
</div>
</section><section id="bayesian-additive-regression" class="level1"><h1>Bayesian Additive Regression</h1>
<p>Bayesian Additive Regression Trees (BART) are similar to the previously mentioned models, creating trees with some random elements that model the signal not captured in previous trees.</p>
<p>In the first iteration of the model, all trees are initialized to the same root node. After other iteration updates each of the kth trees one at a time. During the bth iteration, we subtract the predicted values from each response to produce a partial residual. i represents each iteration.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_i%20=%20y_i%20-%20%5CSigma_%7Bk'%3Ck%7Df%5Eb_%7Bk'%7D(x_i)%20-%20%5CSigma_%7Bk'%3Ek%7Df%5E%7Bb-1%7D_%7Bk'%7D(x_i)%0A"> Rather than fitting a new tree to this residual, BART randomly chooses a tree from the previous iteration (<img src="https://latex.codecogs.com/png.latex?f%5E%7Bb-1%7D_k">), favouring trees that improve fit.</p>
<p>Unfortunately, I have not been able to get any BART model to work for this multi-class classification example. The <code>bartMachine</code> package doesn’t have an implementation, and the <code>BART</code> package’s implementation doesn’t seem to work for me. All the values for me were returning NA. There is also very little resources on the <code>BART</code> package to troubleshoot.</p>
</section><section id="conclusion" class="level1"><h1>Conclusion</h1>
<p>The decision tree as an easy-to-understand model, but they are prone to over fitting data. Pruning is required to reduce this, but will also reduce accuracy(50% Accuracy for this example).</p>
<p>Ensemble models are then introduced with Bagging which averages a bunch of trees created by a selection of different observations. This does increase our accuracy(66.67%). This is very similar to the Random Forest model, but instead we increase the random element to include the features selected (66.67%).</p>
<p>The final models create trees that build of previously generated trees with the boosting and the Bayesian Additive Regression Tree. Unfortunately, there are no results for the BART model, but boosting does produce good results (69.05%).</p>
<p>So there is no clear answer which model is the best, they follow pretty similar methods and produce similar results. I would recommend training them all and finding which performs best on your data.</p>


<!-- -->

</section> ]]></description>
  <category>General</category>
  <category>R</category>
  <category>ML</category>
  <guid>https://datasandbox.netlify.app/posts/2022-10-03-Tree Based Methods/</guid>
  <pubDate>Tue, 15 Nov 2022 05:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-10-03-Tree Based Methods/forest.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Creating Posts for Quarto Blog</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-08-15-Creating a Quarto Blog Template/</link>
  <description><![CDATA[ 




<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-08-15-Creating a Quarto Blog Template/template.jpg" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>I have officially switched my blog from <code>blogdown</code> to <code>quarto</code> due to the new features that <code>quarto</code> adds. The move has not been painless, but most of the most important features remain the same. The feature this is currently missing in <code>quarto</code> that I cannot live without, is the 1 click creation of a new blog post. There are a few different ways to automate the creation of blog posts, here are a few that I’ve found.</p>
</section>
<section id="rmd-template" class="level1">
<h1>RMD Template</h1>
<p>It is fairly simple to create a new RMD template for the <code>markdown</code> package. These templates are available when you click File -&gt; New File -&gt; R Markdown… under the tab ‘from template’. There are many templates available under the <code>rmarkdown</code> package, but you might have more templates from other packages. Unfortunately, this feature is not yet available in <code>quarto</code> for QMD files.</p>
<p>So, currently there is no way to create a QMD template, but there isn’t a major difference between QMD and RMD files. An easy solution would be to create a RMD template with built-in <code>quarto</code> commands and when you use it to create a blog post you just save it as a QMD file. This will still require going through the steps of creating a RMD template.</p>
<p>Creating a RMD template is pretty simple, it just requires creating two files and a folder. This is even easier if you use the <code>usethis</code> package with its <code>use_rmarkdown_template</code> function, as it will automatically create the structure.</p>
<p>Within the main template folder, you will have a ‘skeleton’ folder and a ‘template.yaml’ file. The YAML file will contain the name and description of the template. It also includes the parameter <code>create_dri</code> which will create a file in a nested folder if the option is true. The structure of the file is the following:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Quarto Blog Post</span>
<span id="cb1-2">description<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-3">  Template <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> Quatro Blog post. </span>
<span id="cb1-4">create_dir<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> true</span></code></pre></div>
</details>
</div>
<p>Within the ‘skeleton’ folder, you will find a ‘skeleton.RMD’ files. This is the actual template structure. This file should have all the YAML, including QMD features, and the basic structure of your blog post. The following is a sample that I have created:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb2-2">title<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Post for Quarto Blog'</span></span>
<span id="cb2-3">author<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Mark Edney</span>
<span id="cb2-4">date<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb2-5">categories<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-6">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> How<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>to</span>
<span id="cb2-7">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R</span>
<span id="cb2-8">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Rmarkdown</span>
<span id="cb2-9">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Python</span>
<span id="cb2-10">draft<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> true</span>
<span id="cb2-11">description<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span></span>
<span id="cb2-12">image<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb2-13">archives<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-14">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb2-15">toc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> false</span>
<span id="cb2-16"></span>
<span id="cb2-17">format<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> </span>
<span id="cb2-18">  html<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> </span>
<span id="cb2-19">    code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>fold<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> false</span>
<span id="cb2-20">    code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>tools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> false</span>
<span id="cb2-21">    code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>link<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> false</span>
<span id="cb2-22"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div>
</details>
</div>
<p>With the template setup, all you need to do is add it to a package. You can add it to any package, but I would recommend adding it to the <code>rmarkdown</code> package with the rest of its templates. The directory for this package in windows can be found somewhere like this: ‘C:~login-library~R version’ With the folder moved over, you should be able to see your template listed when you create a RMD file after you restart R Studio.</p>
<p>This is a pretty easy way to create a <code>quarto</code> blog post, but there are additional steps required. You will need to manually type the date, saved the file as a QMD, and create whatever folder structure you like for your <code>quarto</code> blog. So, let’s explore more options to make it even easier.</p>
</section>
<section id="whisker-package" class="level1">
<h1>Whisker package</h1>
<p>There are a few different packages available to create template files like the <code>brew</code> and <code>whisker</code> packages. I decided to try the <code>whisker</code> package as it seemed to be the easiest to learn. In the <code>whisker</code>, you create a template and refer to your parameters in the ‘{{}}’ set of brackets.</p>
<p>I decided to create a function that will accept the user input for the blog and apply it to the template. The function will then create the proper directory layout that I use for my blog posts.</p>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The first step is the creation of the data that will be passed to create the blog post. This data can be in the form of a Data Frame or a list. This list will contain all the dynamic parameters, including the user input. The only values that I wanted to get user input from was the title. This value is set to variable names which will be passed into the function later. You can also include calculated parameters, such as <code>sys.date()</code> to get the current date.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> title,</span>
<span id="cb3-2">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">author =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mark Edney'</span>,</span>
<span id="cb3-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.Date</span>(),</span>
<span id="cb3-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">categories =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"How-to"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rmarkdown"</span>),</span>
<span id="cb3-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draft =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'true'</span>,</span>
<span id="cb3-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">description =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"''"</span>,</span>
<span id="cb3-7">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">image =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"''"</span>,</span>
<span id="cb3-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">archives =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y/%m"</span>),</span>
<span id="cb3-9">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">toc =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'false'</span>,</span>
<span id="cb3-10">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fold =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"show"</span>,</span>
<span id="cb3-11">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tools =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'true'</span>,</span>
<span id="cb3-12">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'false'</span>)</span></code></pre></div>
</details>
</div>
</section>
<section id="template" class="level2">
<h2 class="anchored" data-anchor-id="template">Template</h2>
<p>The creation of the template is pretty easy, you create a character with the desired structure. Again, you include the parameters in the template with the ‘{{}}’ brackets. I also included markdown for an Introduction and Conclusion, as they should probably be included in every new post.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">Template <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'---</span></span>
<span id="cb4-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">title: {{title}}</span></span>
<span id="cb4-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">author: {{author}}</span></span>
<span id="cb4-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">date: {{date}}</span></span>
<span id="cb4-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">categories: [{{categories}}]</span></span>
<span id="cb4-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">draft: {{draft}}</span></span>
<span id="cb4-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">description: {{description}}</span></span>
<span id="cb4-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">image: {{image}}</span></span>
<span id="cb4-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">archives:</span></span>
<span id="cb4-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  - {{archives}}</span></span>
<span id="cb4-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">toc: {{toc}}</span></span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">format:</span></span>
<span id="cb4-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  html: </span></span>
<span id="cb4-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    code-fold: {{fold}}</span></span>
<span id="cb4-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    code-tools: {{tools}}</span></span>
<span id="cb4-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb4-18"></span>
<span id="cb4-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Introduction</span></span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Conclusion</span></span>
<span id="cb4-22"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span></code></pre></div>
</details>
</div>
</section>
<section id="combining-function" class="level2">
<h2 class="anchored" data-anchor-id="combining-function">Combining Function</h2>
<p>Finally, we create a single function that will tie everything together. By using the <code>dir.create</code> and <code>paste0</code> functions with the date from the data list, we create a new folder that has the date and the blog post title in its structure. We can then create the blog post with the <code>whisker::render</code> and <code>writeLines</code> functions. The same folder name is required to create the post in the folder.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">Blog_post <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(title){</span>
<span id="cb5-2">  data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(...</span>
<span id="cb5-3">  </span>
<span id="cb5-4">  Template <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'---</span></span>
<span id="cb5-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">title: {{title}}...</span></span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  dir.create(paste0("./posts/",data$date, "-", title))</span></span>
<span id="cb5-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  writeLines(whisker.render(Template, data),</span></span>
<span id="cb5-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  paste0("./posts/",data$date, "-", title, "/index.qmd"))</span></span>
<span id="cb5-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div>
</details>
</div>
<p>The function can be run by its own, saved to an r script file, or you can add it to your <code>.Rprofile</code> file so that it will be loaded every time you start R Studio. Then to create a post, you would use <code>Blog_post</code> and pass the blog title. If you’re in the root file for your quarto blog, you shouldn’t have an issue. If you are not, you might get an error saying you could not find the directory.</p>
<p>So creating a function is a pretty code solution and <code>whisker</code> is simple and easy to use. The one pain point for me is that the function needs to be loaded in the system by running an open r script file, using the <code>source</code> function and passing the r script name, or by including the function in the <code>.Rprofile</code>. None of these seem very intuitive to me, so I decided to proceed with the creation of an Addin.</p>
</section>
</section>
<section id="addins" class="level1">
<h1>Addins</h1>
<p>Addins are a special feature of RStudio with its only icon on the toolbar. This icon allows the user to run a set of R code from the toolbar. In order to create an Addin, you first need to create a package that includes all the code.</p>
<section id="package-creation" class="level2">
<h2 class="anchored" data-anchor-id="package-creation">Package creation</h2>
<p>The easiest way to create a package is from the template when you create a new project. This will create the basic structure needed for a package. You need to edit the description file to include information on the package. Here is the description for the package I have created:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">Package<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Qpostr</span>
<span id="cb6-2">Type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Package</span>
<span id="cb6-3">Title<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Create a Quarto Blog Post</span>
<span id="cb6-4">Version<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>.<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb6-5">Author<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Mark Edney</span>
<span id="cb6-6">Maintainer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Mark Edney <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>m2edney<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">@</span>gmail.com<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb6-7">Description<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> This package is used to create an addin <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> creating a new Quarto blog post. This post is created using a default template using the whisker pakage. A Shiny gadget is utilized to get user input prior to the creataion of the blog post. </span>
<span id="cb6-8">License<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Creative Commons Attribution<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>NonCommercial<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>NoDerivs <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span> United States License</span>
<span id="cb6-9">Encoding<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> UTF<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-8</span></span>
<span id="cb6-10">LazyData<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> true</span></code></pre></div>
</details>
</div>
<p>Under the man folder, you need to create a Rd file for each function that will be included in your package. These Rd files will represent the help information when you use the help search for your function. This file will require the function name, an alias, a title for the help page, an example of it usage and a description. Here is a sample for the function that I have created.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">\name{QGadget}</span>
<span id="cb7-2">\alias{}</span>
<span id="cb7-3">\title{Create a Qaurto Blog Post}</span>
<span id="cb7-4">\usage{</span>
<span id="cb7-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Qgadget</span>()</span>
<span id="cb7-6">}</span>
<span id="cb7-7">\description{</span>
<span id="cb7-8">Creates a Quarto Blog Post <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> the posts directory from a template. Opens a Shiny Gadget to get user input before creating the post. Gadget is required <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> creating the addin.</span>
<span id="cb7-9">}</span></code></pre></div>
</details>
</div>
<p>To create the addin, we need some additional folders. From the main project directory, we need to create an ‘inst’ folder and an ‘rstudio’ folder in that. In the new ‘rstudio’ folder, we need to create a file called ‘addins.dcf’. This file will contain about the addin. Here is a sample of mine:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">Name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Quarto Blog Post</span>
<span id="cb8-2">Description<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Creates a new Quarto Blog post from template</span>
<span id="cb8-3">Binding<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> QGadget</span>
<span id="cb8-4">Interactive<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> true</span></code></pre></div>
</details>
</div>
<p>In this file, the binding refers to the function name that will be connected to the addin. The interactive feature determines whether you would like the code to just run or if you like the user to interact first. We keep all the r scripts in the ‘R’ folder from the project main directory. If you are satisfied with the addin being non-interactive, you can stop there and build the package from the build tab. This is a tab near the environment and history tabs. Since I am not satisfied with a non-interactive addin, we need to create a Shiny Gadget and run that as the function.</p>
</section>
<section id="shiny-gadgets" class="level2">
<h2 class="anchored" data-anchor-id="shiny-gadgets">Shiny Gadgets</h2>
<p>A shiny gadget is very similar to a shiny app with the UI and Server functions both in the same file. In an R script file, within the R folder under the project directory, we define a function, which will use functions from the <code>shiny</code> and <code>miniUI</code> libraries.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">QGadget <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>() {....}</span></code></pre></div>
</details>
</div>
<p>Since we are using the <code>miniUI</code>, there are a different series of UI functions we can use. The following creates a basic UI that will ask the user for a title and for an Author name.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">ui <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> miniUI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">miniPage</span>(</span>
<span id="cb10-2">  miniUI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gadgetTitleBar</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quarto Blog Post"</span>),</span>
<span id="cb10-3">  miniUI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">miniContentPanel</span>(</span>
<span id="cb10-4">    shiny<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">textInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Title"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">placeholder =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post Title"</span>),</span>
<span id="cb10-5">    shiny<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">textInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"author"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Author"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">placeholder =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb10-6">    )</span>
<span id="cb10-7">  )</span></code></pre></div>
</details>
</div>
<p>The server function just uses the <code>observeEvent</code> function to accept the information that the user submitted. The user data is than submitted to our previous <code>Blog_post</code> function to create a new blog post.</p>
<p>Additional the Shiny Gadget uses the <code>runGadget</code> function to start the Gadget. The <code>dialogViewer</code> function is passed to create a new window for the gadget. The default behaviour would be to run in the viewer panel.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">server <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(input, output, session) {</span>
<span id="cb11-2">  </span>
<span id="cb11-3">  shiny<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">observeEvent</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>done, {</span>
<span id="cb11-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Blog_post</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>title, input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>author)</span>
<span id="cb11-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopApp</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Post Created"</span>)</span>
<span id="cb11-6">      })</span>
<span id="cb11-7">  }</span>
<span id="cb11-8"></span>
<span id="cb11-9">shiny<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runGadget</span>(ui, server, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">viewer =</span> shiny<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dialogViewer</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quarto Blog Post"</span>))</span></code></pre></div>
</details>
</div>
<p>Again the previous function that we create to make a new Quarto blog post. This function is saved in the same rscript file as the Shiny Gadget, but not in the gadget function itself.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">Blog_post <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(title, author){</span>
<span id="cb12-2">  data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> title,</span>
<span id="cb12-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">author =</span> author,</span>
<span id="cb12-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.Date</span>(),</span>
<span id="cb12-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">categories =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"How-to"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rmarkdown"</span>),</span>
<span id="cb12-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">draft =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'true'</span>,</span>
<span id="cb12-7">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">description =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"''"</span>,</span>
<span id="cb12-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">image =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"''"</span>,</span>
<span id="cb12-9">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">archives =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y/%m"</span>),</span>
<span id="cb12-10">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">toc =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'false'</span>,</span>
<span id="cb12-11">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fold =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"show"</span>,</span>
<span id="cb12-12">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tools =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'true'</span>,</span>
<span id="cb12-13">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">link =</span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'false'</span>)</span>
<span id="cb12-14"></span>
<span id="cb12-15">  Template <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'---</span></span>
<span id="cb12-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">title: {{title}}</span></span>
<span id="cb12-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">author: {{author}}</span></span>
<span id="cb12-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">date: {{date}}</span></span>
<span id="cb12-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">categories: [{{categories}}]</span></span>
<span id="cb12-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">draft: {{draft}}</span></span>
<span id="cb12-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">description: {{description}}</span></span>
<span id="cb12-22"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">image: {{image}}</span></span>
<span id="cb12-23"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">archives:</span></span>
<span id="cb12-24"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  - {{archives}}</span></span>
<span id="cb12-25"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">toc: {{toc}}</span></span>
<span id="cb12-26"></span>
<span id="cb12-27"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">format:</span></span>
<span id="cb12-28"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  html:</span></span>
<span id="cb12-29"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    code-fold: {{fold}}</span></span>
<span id="cb12-30"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    code-tools: {{tools}}</span></span>
<span id="cb12-31"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb12-32"></span>
<span id="cb12-33"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Introduction</span></span>
<span id="cb12-34"></span>
<span id="cb12-35"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"># Conclusion</span></span>
<span id="cb12-36"></span>
<span id="cb12-37"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb12-38"></span>
<span id="cb12-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./posts/"</span>,data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, title))</span>
<span id="cb12-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeLines</span>(whisker<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">whisker.render</span>(Template, data), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./posts/"</span>,data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, title, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/index.qmd"</span>))</span>
<span id="cb12-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.edit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./posts/"</span>,data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, title, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/index.qmd"</span>))</span>
<span id="cb12-42">}</span></code></pre></div>
</details>
</div>
</section>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>In summary, there is no native way to create a new <code>quarto</code> post at this time. We have explored multiple different solution</p>
<ul>
<li>Create a RMD template make a document and rename it as a QMD</li>
<li>Use the <code>whisker</code> package to create template which accepts calculated fields</li>
<li>Store the <code>whisker</code> template in a function stored in a script file, or to be loaded by default in the <code>.Rprofile</code> file</li>
<li>Create an non-interactive addin by storing the function in a package</li>
<li>Create an interactive addin by making a Shiny Gadget.</li>
</ul>
<p>Personally, my go to answer would be to use the interactive addin. If you are interested in installing the package I have create, feel free to use the following code to install it. After it is installed, you will need to restart R for the addin to be in the addin tab.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mark-edney/Qpostr'</span>)</span></code></pre></div>
</details>
</div>
Photo by <a href="https://unsplash.com/@kaleidico?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Kaleidico</a> on <a href="https://unsplash.com/s/photos/template-structure?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>


<!-- -->

</section>

 ]]></description>
  <category>How-to</category>
  <category>R</category>
  <category>Rmarkdown</category>
  <category>Quarto</category>
  <category>Shiny App</category>
  <guid>https://datasandbox.netlify.app/posts/2022-08-15-Creating a Quarto Blog Template/</guid>
  <pubDate>Mon, 15 Aug 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-08-15-Creating a Quarto Blog Template/template.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Quarto: The successor to R Markdown</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-08-01-quarto-the-successor-to-r-markdown/</link>
  <description><![CDATA[ <section id="introduction" class="level2"><h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>RMarkdown has been a staple for any Data Scientist that programs in <code>R</code>. <code>Quarto</code> builds on that, with multiple language support and additional features. Because of its language independent design, <code>Quarto</code> requires an independent installation. <span class="citation" data-cites="quarto">(<span>“Quarto,”</span> n.d.)</span></p>
<p>I have spent the past week moving my blog from <code>blogdown</code> to <code>quarto</code>. There have been some challenges, but I am pretty happy with the new look. Let’s start with the setup, it’s a little more work than a regular package or module.</p>
</section><section id="setup" class="level2"><h2 class="anchored" data-anchor-id="setup">Setup</h2>
<p>The setup for Quarto is pretty simple. You will need to visit the quarto website to <a href="https://quarto.org/docs/get-started/">download</a> the Quarto Command Line Interface (CLI). There are step-by-step instructions for your selected text editor. I am most familiar with RStudio for <code>R</code> and VSCode for <code>Python</code>.</p>
<p>For Rstudio, it’s pretty much just plug and play now. I did install the <code>Quarto</code> package, but all the commands can be done by the command line interface. Switching from <code>RMarkdown</code> is as simple as saving them as <code>qmd</code> file. The process for Quarto for RStudio can be described by the following process flow:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://datasandbox.netlify.app/posts/2022-08-01-quarto-the-successor-to-r-markdown/images/rstudio-qmd-how-it-works.png" class="img-fluid figure-img"></p>
<figcaption>Render qmd in RStudio</figcaption></figure>
</div>
<p>It is not much more difficult for VSCode, all you need to do is download the Quarto extension. The process flow is similar to RStudio but uses Jupyter instead of knitr.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://datasandbox.netlify.app/posts/2022-08-01-quarto-the-successor-to-r-markdown/images/qmd-how-it-works.png" class="img-fluid figure-img"></p>
<figcaption>Render qmd for VSCode</figcaption></figure>
</div>
<p>With the setup complete, there should be no differences between text editors.</p>
</section><section id="code-chunk-options" class="level2"><h2 class="anchored" data-anchor-id="code-chunk-options">Code Chunk Options</h2>
<p>The first new feature to explore the support for code chuck options within the code chunks. These options would usually live within the code chunk title line. Any supported option can be added with the <code>#|</code> tag. This feature is useful for situations with many options, as it does increase readability.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb1-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: load</span></span>
<span id="cb1-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| include: true</span></span>
<span id="cb1-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(tidyverse)</span></span>
<span id="cb1-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">data("msleep")</span></span>
<span id="cb1-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</section><section id="code-folding" class="level2"><h2 class="anchored" data-anchor-id="code-folding">Code-folding</h2>
<p>One of the neat new features is code-folding. When this feature is enabled in the qmd YAML, the person viewing the document can hide/unhide code chunks. This can make it easier for them to read the document. Only the code will be hidden, and not the results.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">msleep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</details>
</div>
<p>This feature is enabled by making the following addition to the YAML. You would change the format from HTML to your required format, such as PDF.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> </span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">html</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> </span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">code</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fold</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span></span>
<span>    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">code</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tools</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span></span></code></pre></div>
</details>
</div>
<p>With the addition of the <code>code-tools: true</code> parameter, the reader can decide to hide all code chunks from the top of the document.</p>
</section><section id="figures" class="level2"><h2 class="anchored" data-anchor-id="figures">Figures</h2>
<p>Quarto provides a bunch of additional tools for displaying figures. You can assign values for captions, sub-captions, width and height. You can even create a figure with multiple plots with separate sub-captions.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb4-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: fig-sleep</span></span>
<span id="cb4-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: "Sleeping habits of animals"</span></span>
<span id="cb4-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-subcap:</span></span>
<span id="cb4-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|   - "Scatter plot of body weight by total sleep"</span></span>
<span id="cb4-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#|   - "Violin plot of REM sleep by vore"</span></span>
<span id="cb4-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| layout-ncol: 2</span></span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">msleep %&gt;%</span></span>
<span id="cb4-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  drop_na(sleep_total, bodywt) %&gt;%</span></span>
<span id="cb4-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(y= sleep_total, x = bodywt)) +</span></span>
<span id="cb4-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_point(color = "blue") +</span></span>
<span id="cb4-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()</span></span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">msleep %&gt;%</span></span>
<span id="cb4-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(vore) %&gt;%</span></span>
<span id="cb4-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  drop_na(sleep_rem, vore) %&gt;%</span></span>
<span id="cb4-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(y= sleep_rem, x = vore)) +</span></span>
<span id="cb4-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_violin(aes(fill = vore)) +</span></span>
<span id="cb4-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()</span></span>
<span id="cb4-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
<div id="fig-sleep" class="quarto-layout-panel">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-sleep-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="quarto-layout-row">
<div class="cell-output-display quarto-layout-cell-subref quarto-layout-cell" data-ref-parent="fig-sleep" style="flex-basis: 50.0%;justify-content: flex-start;">
<div id="fig-sleep-1" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-subfloat-fig figure"><div aria-describedby="fig-sleep-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datasandbox.netlify.app/posts/2022-08-01-quarto-the-successor-to-r-markdown/index_files/figure-html/fig-sleep-1.png" class="img-fluid figure-img" data-ref-parent="fig-sleep" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-subfloat-caption quarto-subfloat-fig" id="fig-sleep-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
(a) Scatter plot of body weight by total sleep
</figcaption></figure>
</div>
</div>
<div class="cell-output-display quarto-layout-cell-subref quarto-layout-cell" data-ref-parent="fig-sleep" style="flex-basis: 50.0%;justify-content: flex-start;">
<div id="fig-sleep-2" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-subfloat-fig figure"><div aria-describedby="fig-sleep-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://datasandbox.netlify.app/posts/2022-08-01-quarto-the-successor-to-r-markdown/index_files/figure-html/fig-sleep-2.png" class="img-fluid figure-img" data-ref-parent="fig-sleep" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-subfloat-caption quarto-subfloat-fig" id="fig-sleep-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
(b) Violin plot of REM sleep by vore
</figcaption></figure>
</div>
</div>
</div>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-sleep-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Sleeping habits of animals
</figcaption></figure>
</div>
<p>You can now use cross-referencing for the figure by referencing the figure. This means that in your text, you can refer to the figure number and link to the figure. This will automatically update your figure numbers and is achieved by typing the ‘@’ symbol followed by the figure label. As an example, ‘@fig-sleep’ turns into Figure&nbsp;1.</p>
<p>There is an additional option to let the figures take up the width of the entire page, but I would not recommend using it as it extends beyond the width of the body of your page. It requires the following code:</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb5-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb5-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| column: page</span></span>
<span id="cb5-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</section><section id="code-linking" class="level2"><h2 class="anchored" data-anchor-id="code-linking">Code Linking</h2>
<p>A reader may not be familiar with all the functions that you use in your document, so it may be useful to enable code linking. With code linking, a function in a code chunk will have a hyperlink to the documentation for that function. To work in <code>R</code>, this feature requires the <code>xml2</code> and <code>downlit</code> packages.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/lm.html">lm</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/factor.html">as.factor</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">order</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sleep_total</span>, data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">msleep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/stats/complete.cases.html">complete.cases</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">msleep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</details>
</div>
</section><section id="table-of-contents" class="level2"><h2 class="anchored" data-anchor-id="table-of-contents">Table of contents</h2>
<p>I think the best feature for <code>Quarto</code> is the floating table of contents. I can’t describe how much time and effort I’ve spent trying to get a floating table of contents in a <code>Blogdown</code> blog. It didn’t work for me, it would require getting deep into the weeds changing the CSS layout for my HUGO theme. It was not worth the effort.</p>
<p>Adding a floating table of contents in <code>Quarto</code> is simple. Just use the following code in the document YAML:</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">toc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span></code></pre></div>
</details>
</div>
<p>One simple line of code in the YAML and your document has a floating table of contents. There is some additional customization such as the level of headers, location and title.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">toc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">true</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">toc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">depth</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">toc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">location</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">left</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">toc</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Contents</span></span></code></pre></div>
</details>
</div>
</section><section id="quarto-vs-blogdown" class="level2"><h2 class="anchored" data-anchor-id="quarto-vs-blogdown">Quarto vs Blogdown</h2>
<p>With my experimentation with Quarto, I decided to move my <code>blogdown</code> blog to <code>Quarto</code>. In theory, this should be a simple switch, with just copying all post from folder to another. <code>Quarto</code> can use rmd files, but they can easily be changed over to qmd files. I decided to switch all my post to the qmd format and include some additional features. The Quarto site has extensive reference information for creating a blog. <span class="citation" data-cites="quarto">(<span>“Quarto,”</span> n.d.-)</span></p>
<p>I did have an issue with one of my post not rendering correctly. This maybe an issue with compatibility with the <code>stargazer</code> package. In the end, I decided to just remove the post altogether as I could get it to render correctly, and I prefer the <code>gt</code> over the <code>stargazer</code> package for creating good-looking tables.</p>
</section><section id="conclusion" class="level2"><h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>It is easy to create great looking documents using <code>quarto</code>, whether that be with code in <code>python</code> or <code>R</code>. <code>Quarto</code> supports most of the features in <code>RMarkdown</code> with some fancy new ones. My personal favourite is the floating table of contents. I have also found that rendering a <code>Quarto</code> blog is a much smoother experience than rendering a <code>blogdown</code> blog.</p>


<!-- -->


</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-quarto" class="csl-entry">
<span>“Quarto.”</span> n.d. <a href="https://quarto.org/">https://quarto.org/</a>.
</div>
</div></section></div> ]]></description>
  <category>How-to</category>
  <category>R</category>
  <category>Rmarkdown</category>
  <category>Python</category>
  <category>Quarto</category>
  <guid>https://datasandbox.netlify.app/posts/2022-08-01-quarto-the-successor-to-r-markdown/</guid>
  <pubDate>Mon, 01 Aug 2022 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Network Graphs in R</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/index.en.html</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/storm_graph.png" class="img-fluid"></p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Network graphs are an important tool for network analysis. They illustrate points, referred to as nodes, with connecting lines, referred to as edges. Since network graphs are such useful tools, there are many options for graph generation. In this posting, I will demonstrate three different techniques for developing network graphs in <code>r</code>.</p>
<p>This is part 3 of a series which is based on the <strong>Stormlight Archive</strong> by Brandon Sanderson. This project was originally inspired by the work of <a href="https://www.youtube.com/watch?v=RuNolAh_4bU">Thu Vu</a> where she created a network mapping of the characters in the Witcher series.</p>
<p>In the first part of the project, we scrapped the Coopermind website to create a verified character name list. This scrapping was performed with the <code>rvest</code> package. The list was then cleaned up and saved for further use.</p>
<p>For the second part of the project, we read through and analyzed the four books that make up the <strong>Stormlight Archive</strong> series. The books were read into memory with the <code>readtext</code> package, which fed nicely into the <code>quanteda</code> to create the body of text called a Corpus. Unfortunately, the body of text was so big that we were unable to model all the text, so we divided the Corpus up into smaller documents with the <code>rainette</code> package.</p>
<p>With the corpus finally prepped, we feed it into the <code>spacyr</code> package, a frontend for the <code>spaCy</code> <code>python</code> library, to identify the entities. We were able to create a table identifying the entities that were people and filter it by the verified character list. We created a moving window model that would create a connection between two named characters if they were both mentioned within the same window. By aggregating the results of this model, we developed the foundation for a network graph.</p>
</section>
<section id="initialization" class="level2">
<h2 class="anchored" data-anchor-id="initialization">Initialization</h2>
<p>The first step of this process is to load in the necessary packages for the graph generation. The <code>Tidyverse</code> package is always useful for analysis, so I’ve loaded it too. I have read that the different graph packages can interrupt each other, requiring one of them to be loaded at a time. I have not found this to be an issue.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(igraph)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggraph)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(networkD3)</span></code></pre></div>
</div>
<p>The next step is to load in the data that we created in part two of the project. This data represents that relationship between all the verified characters as read through the series of books. Saving and loading data in RDS format is much more convenient than the CSV format, as RDS files are compressed and seem to load faster.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_rds</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"StormGraph.RDS"</span>)</span></code></pre></div>
</div>
</section>
<section id="igraph" class="level2">
<h2 class="anchored" data-anchor-id="igraph">IGraph</h2>
<p>The first package to explore is the <code>igraph</code> package. This package is not only for plotting graphs, but also includes many tools for network analysis. For our data, we can create a simple network graph with the <code>graph_from_data_frame</code> function. The relationships are not directional, so we pass this information to the function. The graph can then be plotted with the <code>plot</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">graph <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graph_from_data_frame</span>(data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">directed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(graph)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/index.en_files/figure-html/graph-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The graph created is a mess. There are way too many character nodes and way too many relationships created. We need to create a smaller dataset to reduce the amount of information. I reduced the size of the data by taking only the top 98% quantile in relationships. Since the data is stored as a data table, the data table notation is used to create a subset.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.98</span>),,]</span>
<span id="cb4-2">data2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graph_from_data_frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">directed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> layout_with_graphopt)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/index.en_files/figure-html/smaller-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The plot created is still difficult to understand, but it much more reasonable. I feel the <code>igraph</code> package is best for graph analysis and exploratory plots. For a more attractive plot, we need to move on to the next package.</p>
</section>
<section id="tidygraph-and-ggraph" class="level2">
<h2 class="anchored" data-anchor-id="tidygraph-and-ggraph">Tidygraph and GGraph</h2>
<p>The <code>tidygraph</code> and <code>ggraph</code> packages seek to create graphs in the tidyverse-like environment.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidygraph)</span></code></pre></div>
</div>
<p>Creating a graph with <code>ggraph</code> requires more structure than the previous <code>igraph</code>. The graph requires two data frames, one for nodes and one for edges.</p>
<p>For the nodes dataframe, we need a list of all the node names and an ID number for each node. This is achieved by finding the unique values within both columns of data. These values are then passed to the tibble function to create a tibble, a data structure similar to data frames, and then a column for IDs is created with the <code>rowid_to_column</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">nodes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Person1, data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Person2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> .) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowid_to_column</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>)</span></code></pre></div>
</div>
<p>For the edges dataframe, we need some additional steps. As a reminder, in our subset of data, we have rows with two names and a number to represent the strength of their bond. The character names need to in the form of the node IDs rather than the names. This task is completed with two merges with the node dataframe. The graph can then be created with the <code>tbl_graph</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(nodes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Person1"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(nodes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Person2"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(from, to, N)</span>
<span id="cb7-7"></span>
<span id="cb7-8">graph_tidy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_graph</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nodes =</span> nodes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edges =</span> edges, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">directed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</div>
<p>For the plotting of the graph, we use the <code>ggraph</code> library. With this package, the graph can act as any other <code>ggplot</code> geom. With an extra step, we can create a centrality feature in our graph. There are a bunch of different centrality measures, but they all represent the level of importance of a node.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">graph_tidy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Centrality =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">centrality_authority</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggraph</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"graphopt"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span>Centrality, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> label), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_edge_link</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> N), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_edge_width</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_text</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/index.en_files/figure-html/ggraph-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="network-d3" class="level2">
<h2 class="anchored" data-anchor-id="network-d3">Network D3</h2>
<p>The <code>ggraph</code> has created a better looking plot with a much higher level of customization. It is however a static plot with no level of interaction. I have tried using the <code>ggplotly</code> function from the <code>plotly</code> package it make it more interactive, but many of the <code>ggraph</code> features are not supported.</p>
<p>To create an interactive plot, we move to the <code>networkD3</code> package. This package is based on the <code>D3</code> JavaScript library to create interactive plots. We can use the same nodes and edges data frames from the <code>ggraph</code> plot. This process does require one adjustment to the node IDs, as the package requires an initial ID of 0 rather than the default <code>r</code> index of 1.</p>
<p>The function from the <code>tidygraph</code>, <code>centrality_authority</code>, is only supported for the tidygraph data structure, so we need an alternative function to use with our data frame. This is achieved with the <code>authority.score</code> function from the <code>igraph</code> package. Besides that, we normalize the edge width values, node sizes and set all the parameters for the <code>forceNetwork</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">edges <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> edges <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> from <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> to <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb9-4"></span>
<span id="cb9-5">nodes <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nodes <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id=</span>id<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nodesize =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">authority.score</span>(graph_tidy)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>vector<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>)</span>
<span id="cb9-8">        </span>
<span id="cb9-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forceNetwork</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Links =</span> edges, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Nodes =</span> nodes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Source =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Target =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">NodeID =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">opacity =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontSize =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zoom =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Nodesize =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nodesize"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">opacityNoHover =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div id="htmlwidget-f8e5b18f4fc804fdf1fa" style="width:100%;height:464px;" class="forceNetwork html-widget"></div>
<script type="application/json" data-for="htmlwidget-f8e5b18f4fc804fdf1fa">{"x":{"links":{"source":[0,0,1,2,2,2,2,3,4,5,0,6,7,0,6,8,8,9,9,0,7,7,2,1,1,7,7,7,2,5,0,10,2,2,2,2,2,2,2,10,10,10,11,11,0,6,12,13,2,2,14,11,11,11,15,15,16,16,17,17,7,14,14,14,14,14,14,14,7,18,6,19,19,6,19,19],"target":[7,4,2,1,0,20,3,2,0,0,21,7,6,1,0,0,2,0,2,6,0,21,22,7,0,2,3,23,5,2,2,2,4,21,6,7,13,23,24,0,6,7,2,0,3,2,2,2,25,19,2,7,6,4,0,2,0,2,0,2,24,7,0,23,6,24,3,19,19,2,24,24,6,19,2,0],"value":[5.3,1.52,4.85,2.17,21.445,1.125,4.815,2.07,1.29,1.32,1.655,1.46,8.625,1.565,3.965,1.875,2.68,1.9,2.7,5.82,7.61,1.265,1.76,1.16,1.785,17.735,1.995,2.095,1.14,3.21,10.73,9.385,3.265,3.075,12.135,6.685,1.295,3.815,3.44,4.14,1.375,2.56,9.755,8.8,1.465,5.44,3.565,3.665,1.245,2.95,11.565,1.85,3.34,1.425,3.275,4.195,3.275,4.195,1.72,1.745,3.18,1.915,4.47,1.39,6.91,2.83,1.26,2.935,3.305,2.265,2.665,2.09,4.82,4.875,3.57,1.375],"colour":["#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666"]},"nodes":{"name":["Dalinar","Stormfather","Kaladin","Teft","Elhokar","Adolin","Navani","Jasnah","Vandonas","Rayse","Derethil","Maps","Vathah","Shallan","Glys","Vazrmeb","Droz","Mem","Lyon","Raboniel","Lirin","Renarin","Lopen","Szeth","Venli","Rlain"],"group":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25],"nodesize":[125.446907297931,59.2854876372364,150,53.554728406178,40.314163546729,38.2802977631194,100.341945628879,108.00459625225,24.7837966346087,24.7837966346087,43.5301255520752,47.1574599107645,13.4965011285107,26.9930022570214,61.7552974152141,24.7837966346087,24.7837966346087,24.7837966346087,13.4965011285107,75.6249030064492,13.4965011285107,34.5016910026276,13.4965011285107,28.7709317715693,44.6038435794839,13.4965011285107]},"options":{"NodeID":"label","Group":"id","colourScale":"d3.scaleOrdinal(d3.schemeCategory20);","fontSize":14,"fontFamily":"serif","clickTextSize":35,"linkDistance":50,"linkWidth":"function(d) { return Math.sqrt(d.value); }","charge":-30,"opacity":1,"zoom":true,"legend":false,"arrows":false,"nodesize":true,"radiusCalculation":" Math.sqrt(d.nodesize)+6","bounded":false,"opacityNoHover":true,"clickAction":null}},"evals":[],"jsHooks":[]}</script>
</div>
</div>


</section>

 ]]></description>
  <category>How-to</category>
  <category>Project</category>
  <category>NLP</category>
  <category>R</category>
  <category>GGPlot</category>
  <guid>https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/index.en.html</guid>
  <pubDate>Tue, 12 Jul 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-07-12-network-graphs-in-r/storm_graph.png" medium="image" type="image/png" height="95" width="144"/>
</item>
<item>
  <title>Relationship Extraction with Spacyr</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-07-04-relationship-analysis-with-spacyr/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-07-04-relationship-analysis-with-spacyr/dogcat.jpg" class="img-fluid"></p>
<p>This is the continuation of the previous project, where we scrapped the Cooper Mind website with the <code>rvest</code> package. Please refer to that posting for the necessary steps to obtain the verified character names.</p>
<p>As a reminder, this project was inspired by the work of <a href="https://www.youtube.com/watch?v=RuNolAh_4bU">Thu Vu</a> where she created a network mapping of the characters in the Witcher series. I thought it would be interesting to do some recreation of this project, but in <code>R</code> and with the <strong>Stormlight Archive</strong> book series.</p>
<p>For those unfamiliar with the series, it is an epic fantasy story sprawling over four main books at the time of the publishing of this post. Sanderson is a fantastic author and I feel that the <strong>Stormlight Archive</strong> is his best work.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>So in a previous post, we created a list of characters which will represent the nodes in our network graph. The next step in the project is to create the edges. The edges represent the relationships between characters. In our graph, we are going to have the edges represent that strength of the relationships between characters. In order to determine these edge values, we will need to perform relationship extraction from the text with the <code>spacyr</code> package.</p>
<p>The <code>spacyr</code> package is simply a wrapper for the python <code>spaCy</code> library, with the following functionality:</p>
<ul>
<li>tokenization</li>
<li>lemmatizing tokens</li>
<li>parsing dependencies (to determine grammatical structure)</li>
<li>extracting form named entities</li>
</ul>
<p>It uses the <code>reticulate</code> to create the python environment. I have previously written a post about using the <code>reticulate</code> package for using python code in RMarkdown.</p>
</section>
<section id="initialization" class="level2">
<h2 class="anchored" data-anchor-id="initialization">Initialization</h2>
<p>We start with the loading of the necessary libraries to complete the project.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(spacyr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#necessary to create a corpus object</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readtext)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(quanteda)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rainette)</span></code></pre></div>
</div>
<p>If you have an environment of python with a version a <code>spaCy</code>, you can pass the destination into the <code>spacy_intialize</code> function. If not, you need to use the <code>spacy_install</code> function to create a Conda environment that will also include the <code>spaCy</code> package. For this project, I let <code>spacyr</code> create the Conda environment for me. This process did take a while for me, so don’t be surprised if it is the same for you.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spacy_install</span>()</span></code></pre></div>
</div>
<p>I have the name list from the web scraping post saved as a RDS files. RDS files are compressed text files which load quicker and take up much less space than a CSV file.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_rds</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/names.RDS"</span>)</span></code></pre></div>
</div>
</section>
<section id="text-reading" class="level2">
<h2 class="anchored" data-anchor-id="text-reading">Text Reading</h2>
<p>The first step is to read all the text files into the system. I found this interesting little snippet of code that allows you to create a list of all the text files in a specific folder. For this project, all the books were stored in a single data folder.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">list_of_files <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list.files</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb4-2">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.txt$"</span>, </span>
<span id="cb4-3">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
<p>With the list of files, we can use the <code>map_df</code> function from the <code>purr</code> package. The <code>purr</code> package is part of the <code>tidyverse</code> package, so we don’t need to load it separately. The <code>map</code> series of functions allows use to pass a vector of values and a function. Each value will then be passed to that function. The <code>_df</code> part of the function is just the requirement that the output be in the format of a dataframe.</p>
<p>The same task can be completed with a for loop, but it is much faster in the <code>map</code> function as it utilize vectorization. Vectorization is the strategy of performing multiple operations rather than a single operation at the same time. I am not very familiar with the <code>purr</code> package, so I plan to write a new article on the topic in the near future.</p>
<p>After all the books are read into memory, we need to create a corpus. A corpus is a large body of text, much like a library, for the sorting and organization of books. This is completed with the <code>corpus</code> function from the <code>quanteda</code> package. This corpus structure is necessary to utilize functions from the <code>spacyr</code> package.</p>
<p>The organizational structure in the Corpus is why I needed to load the books in with the <code>readtext</code> function from the <code>readtext</code> package. I’ve tried many methods to read the text(<code>readlines</code>, <code>read_Lines</code>, <code>readfile</code>) but none of them performed the proper way for the <code>corpus</code> function. There were plenty of issues, hours of difficulty, which resulted in me referring to the <code>quanteda</code> package website. There I learnt about the <code>readtext</code> function and it worked flawlessly on the first time. Well, I did find an issue with the default encoding not interpreting characters correctly, but this was corrected easily.</p>
<p>When the time came to modelling, issues arose with the size of the Corpus. There is a limitation in <code>spaCy</code>, it will only work with text files less than 100,000,000 characters long. I think that each book was a little over twice that size. So I needed to batch the process by breaking the corpus up into smaller sections. This was done with the <code>split_segments</code> function from the <code>rainette</code> package. The function only accepts a split based on number of sentences, so I arrived at a value of 100,000 sentences per document.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">corpus <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> list_of_files <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_df</span>(readtext, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">encoding=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corpus</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-4">        rainette<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split_segments</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">segment_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>  Splitting...</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>  Done.</code></pre>
</div>
</div>
<p>With the books read into file, the corpus created and the corpus split into sections, we now have 18 documents. We can proceed to entity modelling with the <code>spaCy</code> functions.</p>
<p>Unfortunately, we still have size issues, as passing the entire Corpus to be parsed is unaffected by the number of documents. So I needed to create a simple for loop to analyze each document one at a time and bind the results to a data table. Data tables are like data frames, but they have some unique notation and increased performance.</p>
<p>The Corpus is parsed with the <code>spacy_parse</code> function. Setting <code>pos</code> and <code>lemma</code> to false should reduce performance time as the function doesn’t need to return dependency POS tag set and lemmatized tokens. The POS tag set refers to the type of word such as Noun, while the lemmatized token is the base of a word, such as for the word “am” would be lemmatized to “be”. The parsing of the corpus takes a very long time.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> corpus[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spacy_parse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lemma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Found 'spacy_condaenv'. spacyr will use this environment</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>successfully initialized (spaCy Version: 3.1.3, language model: en_core_web_sm)</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>(python options: type = "condaenv", value = "spacy_condaenv")</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(corpus)){</span>
<span id="cb12-2">        temp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> corpus[[i]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spacy_parse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pos =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lemma =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb12-4">        </span>
<span id="cb12-5">        df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(df,temp)}</span>
<span id="cb12-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(temp)</span></code></pre></div>
</div>
<p>The parsing creates an object that acts very similarly as a data table. There is an entry for each word, which is more than what is required for this project. The original data table is preserved, in case we would like to reference a sentence in the corpus, and we create a filtered data table. The data table is filtering the tokens in the names list and by the identified entity, making sure it starts with person.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">dfclean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(token <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> names,</span>
<span id="cb13-3">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_starts</span>(entity, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PERSON"</span>)) </span></code></pre></div>
</div>
</section>
<section id="relationship-modelling" class="level2">
<h2 class="anchored" data-anchor-id="relationship-modelling">Relationship modelling</h2>
<p>The final step is to create a model that will connect people in the data table. I have decided to use a sentence windows that creates a connection when two names are mentioned within that window.</p>
<p>This is another very time-consuming tasks the requires two for loops. The first loop goes through all 30025 rows and its sentence id. A second for loop that excludes all rows already used in the first loop is used to compare a second sentence id. If the difference between the sentence ids is less than the windows, the tokens for these rows are added to an empty data table. If the difference is greater than the window size, we break the second for loop as all the sentence ids are incremental. It is not a very clean or smooth method, but it works.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">window_size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb14-2">related <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.table</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Person1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">character</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Person2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">character</span>())</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dfclean)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)){</span>
<span id="cb14-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span>(j <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> (i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dfclean)){</span>
<span id="cb14-6">                <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span>((dfclean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sentence_id[j] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> dfclean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sentence_id[i]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> window_size){</span>
<span id="cb14-7">                        </span>
<span id="cb14-8">                        related <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbindlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(related, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(dfclean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>token[i], dfclean<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>token[j])))</span>
<span id="cb14-9">                }</span>
<span id="cb14-10">                </span>
<span id="cb14-11">                <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">else</span>{</span>
<span id="cb14-12">                        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">break</span></span>
<span id="cb14-13">                }</span>
<span id="cb14-14">        }</span>
<span id="cb14-15">}</span></code></pre></div>
</div>
<p>The following is a sample of the data table we have created to build the relationships.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">related <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Person1 Person2
1: Shallan  Kabsal
2: Jezrien Jezrien
3: Jezrien Jezrien
4: Jezrien Jezrien
5: Jezrien Jezrien
6: Jezrien   Kalak</code></pre>
</div>
</div>
<p>We can identify two issues with this sample. The first issue is when two of the same names are within the same window. We will have to filter out when ‘Person1’ is equal to ‘Person2’. The second issue is that we would actually like to aggregate the data. We would like a count of when two different names are in the same window. Both of these tasks are easy enough to solve using the built-in data table notation. For more information on data tables, please refer to my previous post on the topic.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">relatedagg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> related[Person1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> Person2,.N,by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Person1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Person2"</span>)]</span>
<span id="cb17-2"></span>
<span id="cb17-3">relatedagg <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Person1 Person2  N
1: Shallan  Kabsal  8
2: Jezrien   Kalak 10
3:   Kalak Jezrien  9
4:   Szeth Dalinar 98
5:   Szeth  Jasnah 11
6:   Szeth Elhokar  3</code></pre>
</div>
</div>
<p>The final issue is for the relationships for ‘Person1’ and ‘Person2’ when their places are switched, but that will be dealt with in the next post.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>With some hard-work, we were able to create an organized Corpus of all the current 4 <strong>Stormlight Archive</strong> books. We were able to split this Corpus into smaller sized documents, making them easier to manage. The <code>spacyr</code> library was then used to model entities within the Corpus, identifying the tokens that represent people. The next step was to clean up the results, keeping only the verified characters names as tokens. We then used a model to developed relationships using a window. A relationship was created whenever two character names were mentioned in the same window. We then filtered out characters relationships to themselves and aggregated the data. The clear next step is to actually build the graphs with the characters as nodes and their relationships as edges. But that is a post for another day.</p>
Photo by <a href="https://unsplash.com/@alecfavale?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alec Favale</a> on <a href="https://unsplash.com/s/photos/relationships?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>
  


</section>

 ]]></description>
  <category>How-to</category>
  <category>Project</category>
  <category>NLP</category>
  <category>R</category>
  <guid>https://datasandbox.netlify.app/posts/2022-07-04-relationship-analysis-with-spacyr/</guid>
  <pubDate>Mon, 04 Jul 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-07-04-relationship-analysis-with-spacyr/dogcat.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Webscraping in R with Rvest</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-06-22-webscraping-in-r-with-rvest/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-06-22-webscraping-in-r-with-rvest/harvest.jpg" class="img-fluid"></p>
<p>Web scraping has become an incredibly important tool in data science, as an easy way to generate new data. The main advantage is the automation of some pretty repetitive tasks. Web scrapping can also be a good way of keeping up with new data on a website, assuming it doesn’t have a big change in its HTML structure.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>This project is inspired from a YouTube video created by <a href="https://www.youtube.com/watch?v=RuNolAh_4bU">Thu Vu</a> about at data scraping project about the Witcher books series. Her project utilizes <code>python</code> and <code>Selenium</code>. I love the book series and I loved the project idea. I’ve also had it on my backlog to learn the <code>Rvest</code> library for a while, so it seems like a great opportunity to combine these two interests.</p>
<p>Rather than completing the project on the Witcher series, I thought it would be interesting to explore another book series that I love in the <strong>Stormlight Archive</strong> by Brandon Sanderson. If you are not familiar with the series, it is an epic fantasy story sprawling over four main books at the time of the publishing of this post. Sanderson is a fantastic author and I feel that the <strong>Stormlight Archive</strong> is his best work.</p>
<p>For this project, I will scrap the Coppermind website for all the character names in the series. The Coppermind is a fan made Wiki site that covers the work of Brandon Sanderson. After retrieving all the character names, I will create a graph outlining the relationships between each character. This work will be done in a future post, so please look forward to it.</p>
</section>
<section id="inializaton" class="level2">
<h2 class="anchored" data-anchor-id="inializaton">Inializaton</h2>
<p>The first step is to download the <code>Rvest</code> library. This is done with the following code.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rvest"</span>)</span></code></pre></div>
</div>
<p>The <code>rvest</code> package is also installed if you have the <code>tidyverse</code> package installed. Loading the <code>tidyverse</code> package however will not load the <code>rvest</code> package, so they both need to be loaded separately.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyverse"</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rvest"</span>)</span></code></pre></div>
</div>
</section>
<section id="datascraping" class="level2">
<h2 class="anchored" data-anchor-id="datascraping">Datascraping</h2>
<p>To start the data scraping exercise, we need to save the URL of the website we would like to scrape. This is the URL for the character page for the Stormlight Archives series.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">site <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_html</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://coppermind.net/wiki/Category:Rosharans"</span>)</span></code></pre></div>
</div>
<p>While on the website in your own browser, you right-click on the specific element you’re interested in scrapping and select inspect. This is at least the method used for Firefox, but it should be similar to other browsers.</p>
<p>From here, you have to do a little digging and a little experimentation to determine which HTML elements are important for the character list. It is pretty useful to have a strong understanding of HTML at this point. From my experimentation, I found that the list was contained within a div with the class “mw-category-group”. A div is a generic divider tag in HTML and can represent many things. I selected the elements with the following code:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> site <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"div.mw-category-group"</span>)</span></code></pre></div>
</div>
<p>You use the <code>html_elements</code> command to select the all the elements for a specific HTML tag you pass. The addition of the “.mw-category-group”, specifies the selection to only divs with the specific <code>class</code>. The <code>class</code> is an attribute of the HTML tags, used to identify and group HTML elements together. I have found that this notation is the best way to filter elements.</p>
<p>Within the div elements, there is a further sub-structure for an element in the character list. The characters are contained within an <code>&lt;li&gt;</code> tag as a list item and as an <code>&lt;a&gt;</code> tag as a hyperlink within that list item. We can explore further into the HTML structure by selecting these elements. After the final structure is selected, we can use the <code>html_attr</code> function to return an attribute of the selected elements. The ‘title’ attribute stores the character name in the HTML. We could also the <code>html_text2</code> function to return the text of the hyperlink, but I’ve found that the ‘title’ attribute is better structured.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> names <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"li"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_elements</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html_attr</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title"</span>)</span></code></pre></div>
</div>
</section>
<section id="data-cleaning" class="level2">
<h2 class="anchored" data-anchor-id="data-cleaning">Data Cleaning</h2>
<p>We can start exploring the results of the scrapping</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(names))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Category:Aimians"    "Category:Alethi"     "Category:Azish"     
[4] "Category:Emuli"      "Category:Herdazians" "Category:Iriali"    </code></pre>
</div>
</div>
<p>Oops, the program has captured an additional list that precedes the character list. Through my testing, I have not found a way to distinguish between the two lists from the HTML structure. Thankfully, we can rely on Regular Expressions to complete the job. The unwanted list items all start with “Category:”, so with a single expression of the <code>str_starts</code> from the <code>stringr</code> package we can remove these elements.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> names[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_starts</span>(names, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Category:"</span>)]</span></code></pre></div>
</div>
<p>The list still requires some additional work, as there are “()” used throughout the list to give additional context. These “()” will not appear in the text, so we need to remove them with a second Regular Expression. Although it is not clear to me, the “(” needs to be double escaped with two “\” rather than just one.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove_all</span>(names,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">(.*</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>)</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(names, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "Abaray"        "Abiajan"       "Abrial"        "Abrobadar"    
 [5] "Abronai"       "Abry"          "Acis"          "Adin"         
 [9] "Adis"          "Adolin Kholin"</code></pre>
</div>
</div>
<p>We can see that the scrapped data is in much better condition. There is still additional work we can do, as the names will sometimes include first and last names. The last names are not particularly important, so we can drop them altogether.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove_all</span>(names,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" .*"</span>) </span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(names, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "Abaray"    "Abiajan"   "Abrial"    "Abrobadar" "Abronai"   "Abry"     
 [7] "Acis"      "Adin"      "Adis"      "Adolin"   </code></pre>
</div>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> names[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>names <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Word"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"She"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"User:Thurin"</span>)]</span></code></pre></div>
</div>
<p>Now we can see that the final list is in condition that we can use to better explore the relationships.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>We have scraped the <strong>Stormlight Archive</strong> character wiki website with the <code>rvest</code> package. We loaded the website with <code>read_html</code> function. Furthermore, we were then able to sort through the different HTML elements with the <code>html_elements</code> to find where the character list is stored. We then obtained the actual names with the <code>html_attr</code> function. The data collected still contained some unwanted data. We were able to remove an additional list, data in parentheses and the last names of all characters. We can now move forward with scrapping the books to identify the strength of relationships between each character.</p>
Photo by <a href="https://unsplash.com/@pazarando?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Paz Arando</a> on <a href="https://unsplash.com/s/photos/harvest?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>


</section>

 ]]></description>
  <category>How-to</category>
  <category>Project</category>
  <category>NLP</category>
  <category>R</category>
  <guid>https://datasandbox.netlify.app/posts/2022-06-22-webscraping-in-r-with-rvest/</guid>
  <pubDate>Wed, 22 Jun 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-06-22-webscraping-in-r-with-rvest/harvest.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Text Prediction Shiny App pt 2</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/books.jpg" class="img-fluid"></p>
<section id="description" class="level2">
<h2 class="anchored" data-anchor-id="description">Description</h2>
<p>This is the second part for the creation of a text prediction Shiny Application. From the previous post, we have developed and Corpus of text to start creating text prediction applications.</p>
<p>We have also explored the corpus, looking at the frequency of words in the vocabulary. It is now time to start to develop ngram models.</p>
</section>
<section id="n-gram-models" class="level2">
<h2 class="anchored" data-anchor-id="n-gram-models">N-gram models</h2>
<p>A ngram is a continuous sequence of tokens, where the order is determined by how many tokens are in the sequence. For our purpose, a token is created for each word in a sentence. Other tokens can be created, such as sentence in a paragraph or letters in a word. It really depends on your application needs.</p>
<p>A line of text can be broken down into ngrams in many ways. For example, the following text:</p>
<p>“The quick brown fox”</p>
<p>can be broken down to the following unigrams:</p>
<blockquote class="blockquote">
<p>(“the”)(“quick”)(“brown”)(“fox”)</p>
</blockquote>
<p>or to the following bigrams:</p>
<blockquote class="blockquote">
<p>(“the quick”)(“quick brown”)(“brown fox”)</p>
</blockquote>
<p>or to the following trigrams:</p>
<blockquote class="blockquote">
<p>(“the quick brown”)(“quick brown fox”)</p>
</blockquote>
<p>or to the single tetragram:</p>
<blockquote class="blockquote">
<p>(“the quick brown fox”)</p>
</blockquote>
<p>The process for creating tokens from text, tokenization, drops the text to lower case and removes all punctuation. For this application, I would recommend the <code>unnest_tokens</code> function from the <code>tidytext</code> package.</p>
<p>Ngrams can be used for predictive text by reserving the last word in the ngram as the predicted word.</p>
</section>
<section id="models" class="level2">
<h2 class="anchored" data-anchor-id="models">Models</h2>
<section id="stupid-back-off" class="level3">
<h3 class="anchored" data-anchor-id="stupid-back-off">Stupid Back-off</h3>
<p>A higher level of n-gram should provide a better predictive quality for our models. However, these higher n-grams have lower levels of occurrences. Each additional word included in the created n-grams, reduce the different possible solutions but should have a higher level of accuracy as there is more context provided to the model.</p>
<p>We need to create some shiny functions to help use determine the highest possible ngram model that we can use. The first function, turns the user input in unigram tokens, which does a lot of pre-processing for us. For words not in the vocabulary, we change the values to the ‘<unk>’ token, which the models already have included in the ngrams.</unk></p>
<p>The final function simply finds the minimum between the length of the user input and the highest level of ngram models. The result will be the highest degree of ngram that we can use. This is often refereed to as the “Stupid Back-off” method, as a higher order ngram is “backed-offed” to a lower level ngram.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">truetext <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb1-2">        truetext <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-3">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-4">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_tokens</span>(word, text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">token=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ngrams"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-5">        </span>
<span id="cb1-6">        truetext[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>truetext<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word,] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unk"</span></span>
<span id="cb1-7">        truetext})</span>
<span id="cb1-8">        </span>
<span id="cb1-9">        maxuse <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb1-10">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">truetext</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,maxn)</span>
<span id="cb1-11">                })</span></code></pre></div>
</div>
</section>
<section id="maximum-likelihood-estimation" class="level3">
<h3 class="anchored" data-anchor-id="maximum-likelihood-estimation">Maximum Likelihood Estimation</h3>
<p>The maximum likelihood estimation (MLE) is the simplest model to examine. We simply count all the occurrence where the all values from the user input match with the ngrams to the final word in the n-gram. The final for in the ngram is reserved for the predicted estimation.</p>
<p><img src="https://latex.codecogs.com/png.latex?p_x%20=%20%5Cfrac%7BC_x%7D%7BC%7D%0A"></p>
<p>Where <img src="https://latex.codecogs.com/png.latex?p_x"> is the probability that the word x will be predicted, <img src="https://latex.codecogs.com/png.latex?C_x"> is the count of the word x occurring, and <img src="https://latex.codecogs.com/png.latex?C"> is the count of all words.</p>
<p>The MLE model produces an unbalanced model, where there a many values from the vocabulary that have zero probability of being predicted. We would like to address this issue by developing more complicated models.</p>
<p>The following plot is a sample distribution created with the MLE model. The predicted values are sorted into bins based on the first letter of the predicted value. Some bins/letters have no value and therefore will have no probability assigned to them.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(word1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"what"</span>, word2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(word3, n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">right_join</span>(voc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(word3,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(bin)</span>
<span id="cb2-7"></span>
<span id="cb2-8">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-9"></span>
<span id="cb2-10">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/index_files/figure-html/mleplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="add-one-smoothing" class="level3">
<h3 class="anchored" data-anchor-id="add-one-smoothing">Add One Smoothing</h3>
<p>The simplest way to deal with the issue of zero probability values is to add one to all unseen counts. This is also referred to as Laplace Smoothing.</p>
<p><img src="https://latex.codecogs.com/png.latex?p_x%20=%20%5Cbegin%7Bcases%7D%0A%5Cfrac%7BC_x%7D%7BC%7D%20&amp;%20C_x%20%3E%200%20%5C%5C%0A%5Cfrac%7B1%7D%7BC%7D%20&amp;%20C_x%20=%200%0A%5Cend%7Bcases%7D%0A"></p>
<p>The plot for the add one model is pretty easy to create from the previous sample. It is clear that there are some values now in each bin, so there is some probability to every word in the vocabulary. The heights of the bins are also increased, as there previously were words in each bin that had 0 occurrences now occurring once.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(word1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"what"</span>, word2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(word3, n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">right_join</span>(voc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(word3,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(bin)</span>
<span id="cb3-7"></span>
<span id="cb3-8">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-9"></span>
<span id="cb3-10">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/index_files/figure-html/addplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="good-turing" class="level3">
<h3 class="anchored" data-anchor-id="good-turing">Good Turing</h3>
<p>In order to understand the Good Turing Smoothing, we need to introduce some new notation, <img src="https://latex.codecogs.com/png.latex?N_C">, to represent the frequency of frequencies. The frequency of frequencies represents how often a number of occurrences will happen in or distribution. For example, <img src="https://latex.codecogs.com/png.latex?N_0"> represents the word count in our vocabulary where there are no occurrences of that word in the distribution. <img src="https://latex.codecogs.com/png.latex?N_1"> then represents the count of the words that have one occurrence. The frequency of frequencies is a one layer of abstraction from our counts. It is helpful to consider our previous plots where we created bins based on the first letter of the predicted word, but instead we are creating bins one how often our predicted words occur.</p>
<p>To create these <img src="https://latex.codecogs.com/png.latex?N_C"> values, we can use the count function. The original values for ‘n’ were created with the count function, we can repeat it over the values of ‘n’ to create a count of counts which I have called ‘nn’. The plot is as expected, there are many words with a low number of counts and a few high count values.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(word1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"what"</span>, word2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(word3, n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">right_join</span>(voc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>))</span>
<span id="cb4-5"></span>
<span id="cb4-6">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb4-7"></span>
<span id="cb4-8">Nr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(df, n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nn"</span>)</span>
<span id="cb4-9"></span>
<span id="cb4-10"></span>
<span id="cb4-11">Nr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 2
      n    nn
  &lt;dbl&gt; &lt;int&gt;
1     0 64330
2     2    51
3     3    28
4     4    13
5     5     2
6     6     7</code></pre>
</div>
</div>
<p>The first intuition of the Good Turing is that the probability of something new, a word with a count of zero, should be assigned the probability for an event that occurred once. For this example, we have the very unlikely event that there are no counts of words that appear once, so we use the next available count(X). This will give the probability of all words with zero count, we will later divide it by the number of words with the count 0.</p>
<p><img src="https://latex.codecogs.com/png.latex?P_0%20=%20%5Cfrac%7BC_1%7D%7BC%7D%20=%20%5Cfrac%7BC_x%5Ccdot%20N_x%7D%7B%5CSigma%20C_N%5Ccdot%20N_N%7D%0A"></p>
<p>Since we have grouped the words by frequencies, we can use the product of all frequency of the frequencies by their count.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">total <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nn<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)</span>
<span id="cb6-2">total</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1449</code></pre>
</div>
</div>
<p>Good Turing requires some additional calculations, so it is beneficial to add some columns to the dataframe at this point.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">Nr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">c=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sc =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GT =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div>
</div>
<p>This snippet of code is used to determine the probability for a word with zero count.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#the probability for unseen matches is set to the next value probability</span></span>
<span id="cb9-2">Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT[Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nn[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>total</span></code></pre></div>
</div>
<p>All other counts are to be adjusted. The Good Turing Smoothing is defined by the following equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?C%5E*=%5Cfrac%7B(C+1)N_%7BC+1%7D%7D%7BN_C%7D"></p>
<p>Where <img src="https://latex.codecogs.com/png.latex?C%5E*"> is the adjusted count number. Since the general trend is that the frequencies decrease as the count increases, the term <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7BN_%7BC+1%7D%7D%7BN_C%7D"> will decrease the value for the count. This is the desired behaviour, as we want that probability to be distributed to zero counts.</p>
<p>One major issue that need to be addressed is that the frequency table is not continuous. There are holes as not all counts exist. To overcome this obstacle, we can create a regression model to fill in the missing values. A logistics regression model fits the values much better than a linear model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">Zn <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_row</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Nr)]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb10-2">Zr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(nn)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>.) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata=</span>Zn)</span>
<span id="cb10-3">Zr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(Zr)</span></code></pre></div>
</div>
<p>The next code chunk can look quite complicated. In this chunk, the corrected count, <img src="https://latex.codecogs.com/png.latex?C%5E*">, are calculated. The variable j is used to control whether the regression model is used to substitute the value for <img src="https://latex.codecogs.com/png.latex?N_%7BC+1%7D">.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#creates the new adjusted counts</span></span>
<span id="cb11-2">j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> </span>
<span id="cb11-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Nr)) {</span>
<span id="cb11-4">        Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>c[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  (Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nn[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nn[i]</span>
<span id="cb11-5">        Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>c[i][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>c[i])] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb11-6">        Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sc[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  (Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Zr[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Zr[i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>]</span>
<span id="cb11-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span>(Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Nr)){</span>
<span id="cb11-8">                j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>}</span>
<span id="cb11-9">        Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>c[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>j) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sc[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>j</span>
<span id="cb11-10">        }</span></code></pre></div>
</div>
<p>The probabilities at this time need two additional modifications, they need to be normalized as the regression model skews the overall probability and the probabilities need to be divided by the frequency counts to get a word specific probability.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#the specific prop from words with the same count</span></span>
<span id="cb12-2">Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT[Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nn[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>total</span>
<span id="cb12-3">Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT)</span>
<span id="cb12-4">Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>Nr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nn</span></code></pre></div>
</div>
<p>We can now plot the completed ngram prediction for the Good Turing Smoothing. The plot looks similar to previous plots, but we plot the probabilities rather than the count values ‘n’.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(word1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"what"</span>, word2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(word3, n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">right_join</span>(voc, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"word"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(word3,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(bin)</span>
<span id="cb13-7"></span>
<span id="cb13-8">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb13-9"></span>
<span id="cb13-10">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Nr,n,GT2), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> GT2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/index_files/figure-html/gtplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="absolute-discounting" class="level3">
<h3 class="anchored" data-anchor-id="absolute-discounting">Absolute Discounting</h3>
<p>Good Turing Smoothing is an effective model, but man can it be complicated. One observation that you can make when looking at the values for <img src="https://latex.codecogs.com/png.latex?C"> and <img src="https://latex.codecogs.com/png.latex?C%5E*"> is that there is nearly constant discounting. The distribution in our example is skewed, but we can see that the most common value is between 0 and 1.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">Nr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(c,sc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff =</span> c<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sc) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>diff)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/index_files/figure-html/adinsight-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>This would suggest that we could significantly simplify the adjusted counts calculations by subtracting a constant value. The algorithm is described by the following equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?p_x%20=%20%5Cfrac%7BC_x%20-%20d%7D%7BC%7D%20+%20%5Clambda%20%5Ccdot%20p_%7Bunigram%7D%0A"></p>
<p>where ‘d’ is the discounting amount, <img src="https://latex.codecogs.com/png.latex?%5Clambda"> is the Interpolation rate and <img src="https://latex.codecogs.com/png.latex?p_%7Bunigram%7D"> is the unigram probability based on the MLE.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">discount <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span></span>
<span id="cb15-2">ADI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(word3, n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ADI =</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> discount)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n))</span>
<span id="cb15-6">                </span>
<span id="cb15-7">ADI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ADI[ADI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ADI <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span></code></pre></div>
</div>
<p>As previously mentioned, the unigram probability is calculated by applying the MLE to the unigram counts.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">unigram.prop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>one <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n))</span></code></pre></div>
</div>
<p>The interpolated weight (<img src="https://latex.codecogs.com/png.latex?%5Clambda">) can be found by finding the probability that was discounted.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">uni.wt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(ADI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ADI)</span>
<span id="cb17-2"></span>
<span id="cb17-3">ADI <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ADI <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb17-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_column</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">uni =</span> unigram.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>uni.wt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb17-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ADI =</span> ADI <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> uni, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.keep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unused"</span>)</span></code></pre></div>
</div>
<p>We can see that the plot of the probabilities for the absolute discounting is very similar to the Good Turing plot, but it was much easier to understand and calculate.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">ADI <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(word3,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb18-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(bin) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ADI)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/index_files/figure-html/adiplot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="kneser-ney" class="level3">
<h3 class="anchored" data-anchor-id="kneser-ney">Kneser-Ney</h3>
<p>The issue with Absolute Discounting is the reliance on the unigram probabilities. The unigram probability doesn’t provide any contextual information. We would rather rely on the continuation probability. Rather than looking at how often the word occurs, the continuation probability looks at how many bigrams the word completes. The Kneser-Ney model follows this equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?p_x%20=%20%5Cfrac%7Bmax(C_x%20-%20d,%200)%7D%7BC%7D%20+%20%5Clambda%20%5Ccdot%20p_%7Bcontinuation%7D%0A"></p>
<p>The next chunk of code is very similar to the code used in the absolute discounting model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">KNS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-2">        ungroup <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(word3, n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">KNS =</span> (n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> discount)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n))</span>
<span id="cb19-5"></span>
<span id="cb19-6">KNS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>KNS[KNS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>KNS <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb19-7">cont.wt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(KNS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>KNS)</span></code></pre></div>
</div>
<section id="continuation-probabilities" class="level4">
<h4 class="anchored" data-anchor-id="continuation-probabilities">Continuation Probabilities</h4>
<p>The following code is used to determine the continuation probabilities. Since the highest order ngram is six, the continuation probability needs to be calculated for six different ngram series.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">cont.prop.func <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(word, ngrams){</span>
<span id="cb20-2">        out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ngrams <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb20-3">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.[,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(ngrams)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> word) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-4">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>() </span>
<span id="cb20-5">        out <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ngrams)</span>
<span id="cb20-6">}</span>
<span id="cb20-7">cont.prop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb20-8">cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>one <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word=</span>voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>one<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>one<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n))</span>
<span id="cb20-9">cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>two <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word=</span>voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(word, cont.prop.func, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ngrams=</span>ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>two))</span>
<span id="cb20-10">cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word=</span>voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(word, cont.prop.func, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ngrams=</span>ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three))</span>
<span id="cb20-11">cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>four <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word=</span>voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(word, cont.prop.func, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ngrams=</span>ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>four))</span>
<span id="cb20-12">cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>five <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word=</span>voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(word, cont.prop.func, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ngrams=</span>ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>five))</span>
<span id="cb20-13">cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>six <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word=</span>voc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(word, cont.prop.func, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ngrams=</span>ngrams<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>six))</span>
<span id="cb20-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(cont.prop, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cont.prop.rds"</span>)</span></code></pre></div>
</div>
<p>The difficulty is with finding the continuation probability. After they are found, it is pretty easy to add them to the model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">KNS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>KNS <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> KNS<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>KNS <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> cont.prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>three<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>prop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>cont.wt</span>
<span id="cb21-2"></span>
<span id="cb21-3">KNS <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(word3,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(bin) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bin, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> KNS)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/index_files/figure-html/kn2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="shiny-app" class="level2">
<h2 class="anchored" data-anchor-id="shiny-app">Shiny App</h2>
<p>With all the models created, we can bundle it together in a single Shiny Application. This Shiny Application retrieves the user’s input and attempts to predict the next word. A table is generated to summarize the most highly predicted word. Since there are five different models, there are five different rows. A plot is generated for each model where the predicted words are in bins with other words with the same first letter.</p>
<div class="cell">
<div class="cell-output-display">
<iframe title="Text Prediction" width="100%" height="500" src="https://m2edney.shinyapps.io/Text_Predictive_Model/?_ga=2.265904783.1867833987.1655568288-1341333380.1645206372"></iframe>
</div>
</div>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@jareddc?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jaredd Craig</a> on <a href="https://unsplash.com/s/photos/book?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>


</section>

 ]]></description>
  <category>Project</category>
  <category>R</category>
  <category>NLP</category>
  <category>Shiny App</category>
  <guid>https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/</guid>
  <pubDate>Wed, 08 Jun 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-06-08-text-prediction-shiny-app-pt-2/books.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>The beauty of List comprehensions in Python</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-05-16-the-beauty-of-list-comprehensions-in-python/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-05-16-the-beauty-of-list-comprehensions-in-python/beauty-list.jpg" class="img-fluid"></p>
<p>I have spent awhile learning Python, and I was a little perplexed when it came to list comprehensions. Why would you use them? Isn’t there just an easier why?</p>
<p>As my proficiency increase, I have found them to be an incredibly useful tool. They save you lines of code, are easy to understand, and are usually better for performance. A good list comprehension, is truly a work of beauty.</p>
<section id="structure" class="level2">
<h2 class="anchored" data-anchor-id="structure">Structure</h2>
<p>The basic structure of a list comprehension is pretty simple, you contain an expression and an iterable within a set of <code>[]</code>. Depending on the type of brackets used, you can create a list, a generator, set or a dictionary.</p>
<div class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">[i <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)]</span>
<span id="cb1-2">(i <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb1-3">{i <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)}</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="1">
<pre><code>{0, 1, 2, 3, 4}</code></pre>
</div>
</div>
<p>It may appear from first impressions that a list comprehension is a simple one line for loop, but it is much more powerful than that.</p>
<section id="conditions" class="level3">
<h3 class="anchored" data-anchor-id="conditions">Conditions</h3>
<p>Much more complicated lists can be created with an included if statement. The if statement fits right at the end of the statement.</p>
<div class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">[a <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> a <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="2">
<pre><code>[0, 2, 4, 6, 8]</code></pre>
</div>
</div>
<p>But what if you need to create an even more complicated list, one that requires an else statement along with the if statement. Then the structure of the list comprehension changes a little, the iterable statement is moved to the end.</p>
<div class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">[a <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">else</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> a <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<pre><code>[0, 0, 2, 0, 4, 0, 6, 0, 8, 0]</code></pre>
</div>
</div>
</section>
<section id="expressions" class="level3">
<h3 class="anchored" data-anchor-id="expressions">Expressions</h3>
<p>Of course, expressions can be more complicated than returning single values. One common issue I find is when I have a list of a value type and I need them to be of a different type. This conversion is easy with list comprehensions.</p>
<div class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'0'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4'</span>]</span>
<span id="cb7-2">[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> a]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre><code>[0, 1, 2, 3, 4]</code></pre>
</div>
</div>
<p>There is nearly an unlimited potential of different expressions you can use.</p>
</section>
<section id="more-iterables" class="level3">
<h3 class="anchored" data-anchor-id="more-iterables">More Iterables</h3>
<p>List comprehensions are not limited to a single iterable. Far warning, however, increasing the number of iterables will reduce readability. At some level of complication, it will be a better idea to separate steps.</p>
<div class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb9-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>]</span>
<span id="cb9-3">[x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>y <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> a <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> y <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> b]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<pre><code>[0, 0, 0, 5, 10, 15, 10, 20, 30, 15, 30, 45, 20, 40, 60]</code></pre>
</div>
</div>
<p>The results are an element-wise evaluation across multiple iterables. These iterables don’t need to be the same size.</p>
</section>
<section id="dictionary-comprehensions" class="level3">
<h3 class="anchored" data-anchor-id="dictionary-comprehensions">Dictionary Comprehensions</h3>
<p>As previously mentioned, by changing the structure, we can generate dictionaries.</p>
<div class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">{char : num <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> num, char <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'b'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'c'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'d'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'e'</span>])}</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="6">
<pre><code>{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4}</code></pre>
</div>
</div>
<p>Likewise, you can create a set rather than a list. Sets can be useful if you don’t need the data to be in order, and you don’t want any duplicate values.</p>
</section>
<section id="other-applications" class="level3">
<h3 class="anchored" data-anchor-id="other-applications">Other Applications</h3>
<p>There is great potential in list comprehensions. Often I find that I need to create a list of zeroes or of boolean logic of the same size as a current list. This is easy to create, just don’t refer to the iterable within the expression.</p>
<div class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb13-2">[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> a]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="7">
<pre><code>[True, True, True, True, True]</code></pre>
</div>
</div>
<p>While it may not be best practice, you can nest a list comprehension within another list comprehension.</p>
<div class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">[x <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> [b <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> b <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<pre><code>[0, 6, 12, 18]</code></pre>
</div>
</div>
</section>
<section id="conclusions" class="level3">
<h3 class="anchored" data-anchor-id="conclusions">Conclusions</h3>
<p>Hopefully I have won you over with the beauty of list comprehensions. They are simple and clean to create yet extremely flexible in their design. So take a minute, to really appreciate the beauty of list comprehensions.</p>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Kelly Sikkema</a> on <a href="https://unsplash.com/s/photos/lists?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>


</section>
</section>

 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <guid>https://datasandbox.netlify.app/posts/2022-05-16-the-beauty-of-list-comprehensions-in-python/</guid>
  <pubDate>Mon, 16 May 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-05-16-the-beauty-of-list-comprehensions-in-python/beauty-list.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Formatting our output with Python’s F strings</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-05-09-formatting-our-outout-with-python-s-f-strings/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-05-09-formatting-our-outout-with-python-s-f-strings/format.jpg" class="img-fluid"></p>
<p>I have recently been on a tear of different challenges on the site <a href="hackerrank.com">HackerRank</a>. I am about halfway through their 30 days of code and 10 days of statistics. These challenges often require to output number to a certain a number of significant digits. I’ve always thought that the <code>round</code> function can be used for this, but I am wrong. The F string seems to be a powerful tool to accomplish this, and worth your time learning if you are unfamiliar.</p>
<section id="structure-of-an-f-string" class="level2">
<h2 class="anchored" data-anchor-id="structure-of-an-f-string">Structure of an F string</h2>
<p>The formatting of an F string starts with a <code>f</code> prior to quotations, whether they be single or double quotes. Any variable can then be included within a series of <code>{}</code>. This formatting can make it easier than turning values into strings and concatenating all strings into a single line of text. This is easily demonstrated with a large mix of values and strings.</p>
<div class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-2">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The value is "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" is greater than "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(y))</span>
<span id="cb1-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The value is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> is greater than </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The value is 0.3333333333333333 is greater than 0.16666666666666666
The value is 0.3333333333333333 is greater than 0.16666666666666666</code></pre>
</div>
</div>
<p>The values can then be formatted with <code>:</code> after the variable name. The number of digits prior and post the decimal can then be specified. The <code>f</code> is added after the decimal formatting to ensure the value is treated as a float.</p>
<div class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"The value is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> is greater than </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The value is 0.333 is greater than 0.17</code></pre>
</div>
</div>
<p>The values passed are not specific to the number of digits, but the minimum number of spaces. This means you can ensure specific space aligned, such as for a table, by including these values.</p>
<div class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>]</span>
<span id="cb5-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> z:</span>
<span id="cb5-3">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"the value is: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:5}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>the value is: 10000
the value is:   500
the value is:    10
the value is: 0.001
the value is:   0.1</code></pre>
</div>
</div>
<p>Additionally, we can add leading zeros by adding zero prior to the number of digits.</p>
<div class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> z:</span>
<span id="cb7-2">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"the value is: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:05}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>the value is: 10000
the value is: 00500
the value is: 00010
the value is: 0.001
the value is: 000.1</code></pre>
</div>
</div>
</section>
<section id="alternative-formatting" class="level2">
<h2 class="anchored" data-anchor-id="alternative-formatting">Alternative formatting</h2>
<p>There are a few alternative methods for f strings. From my understanding, they are not as fast when it comes to performance. I don’t think that is of particular importance. If your script needs a high level of performance, than you probably don’t want many print statements.</p>
<section id="format-method" class="level3">
<h3 class="anchored" data-anchor-id="format-method">Format Method()</h3>
<p>The <code>format</code> method is very similar to <code>f strings</code> with the use of the <code>{}</code>. The string is not preceded by f and the <code>{}</code> can remain empty or contain position indexing. The values are then added in the <code>.format</code> function after the string. The order of the variable in the string will correspond with the number used in the <code>{}</code>, if used at all.</p>
<div class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The value is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> is greater than </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The value is 0.3333333333333333 is greater than 0.16666666666666666</code></pre>
</div>
</div>
</section>
<section id="old-method" class="level3">
<h3 class="anchored" data-anchor-id="old-method">Old % Method</h3>
<p>The Old % operator (modulo) replaces the value in the string. Formatting details, such as those previously discussed, are entered after the <code>%</code>. The variables or values are then entered after the string when preceded by another <code>%</code>. Multiple values can be passed.</p>
<div class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The value is </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%5.3f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> is greater than </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%5.3f</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>(x,y))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The value is 0.333 is greater than 0.167</code></pre>
</div>
</div>
</section>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>Whichever method you decide, it probably won’t make a huge difference. The important part is to understand is the actual formatting. F strings also seem to make it easier to understand the code, as the actual values are inline with the string and the formatting.</p>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@sigmund?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Sigmund</a> on <a href="https://unsplash.com/s/photos/output?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>


</section>

 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <guid>https://datasandbox.netlify.app/posts/2022-05-09-formatting-our-outout-with-python-s-f-strings/</guid>
  <pubDate>Mon, 09 May 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-05-09-formatting-our-outout-with-python-s-f-strings/format.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Level up your programming skills</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-05-01-level-up-your-progamming-skills/index.en.html</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-05-01-level-up-your-progamming-skills/hackerrank.png" class="img-fluid"></p>
<p>How do you become a better programmer? Well, there is strong scientific evidence for the support of the principle of deliberate practice. Deliberate practice is a method of skill development first written by Anders Ericsson in the book “Peak: Secrets from the New Science of Expertise”. I would also recommend reading “Talent Is Overrated: What Really Separates World-Class Performers from Everybody Else” by Geoff Colvin.</p>
<section id="deliberate-practice" class="level2">
<h2 class="anchored" data-anchor-id="deliberate-practice">Deliberate Practice</h2>
<p>Deliberate Practice can be summarized to the following points:</p>
<ul>
<li><p>Talent is not enough, and to become great at a task requires a lot of practice and repetition.</p></li>
<li><p>Deliberate practice is hard-work, in order strengthen your skills through practice you need to be challenged. This means that repetition by itself will not develop skill. This also means you need to constantly increase the challenge of your practice as you become better at it.</p></li>
<li><p>Focus plays a large role in deliberate practice. This connects to the previous point for the required challenge of practice. This can also tie into the principles of flow, as best described by Mihaly Csikszentmihalyi in his book “Flow: The Psychology of Optimal Experience”.</p></li>
<li><p>Setting goals becomes a powerful motivator. With the completion of a goal, there is a release of endorphins, which cause a sense of satisfaction. Goals are can also be utilized to increase the difficulty of practice, making an otherwise easy task a challenge.</p></li>
<li><p>Feedback is important. Feedback provides motivation by comparing current to previous performance.</p></li>
</ul>
</section>
<section id="application-for-programmers" class="level2">
<h2 class="anchored" data-anchor-id="application-for-programmers">Application for programmers</h2>
<p>So, how can programmers incorporate the principles of deliberate practice? I’ve recently been recommended the site <a href="https://www.hackerrank.com">Hacker Rank</a> and I can say it is fantastic. The Hacker Rank site provides a wide array of challenges for programmers of varying skills levels. There is a selection of different topics from Algorithms to Regular Expressions.</p>
<p>So how does Hacker Rank fit in with deliberate practice? Well, there certainly are a good level of challenges to work through. There a three different levels per topic, with challenges in the same difficulty having nothing in common.</p>
<p>The design of the site is quite simple, with very little to distract you from your challenge. There is the option for a dark theme if you are, like me, a person of sophistication. It also pretty easy to have the problem on one side of your screen with your programming on the other.</p>
<p>There are multiple built-in goals to work on. Certifications for each topic to test your current skill level and to advertise to potential employers. There a preparation kits for interviews with time frames between 1 and 12 weeks, which ever is most convenient for you.</p>
<p>There is immediate feedback from assignments, with automated program testing. A leader board is provided for the most competitive, who are interested in their global ranking. You also get feedback from the built-in IDE on your programming errors.</p>
</section>
<section id="site-criticisms" class="level2">
<h2 class="anchored" data-anchor-id="site-criticisms">Site Criticisms</h2>
<p>I do enjoy the site, but I still have some minor issues. To its credit, the site does support multiple programming languages, even different versions of the same programming language. This does, however, make it difficult to following along with the tutorials if they are done in a language that you are not familiar with.</p>
<p>Also, at this time, there doesn’t seem to be support to retake certification exams for some topics. I myself, had failed the R basic certification as I am more used to using the <code>tidyverse</code> package rather than base <code>R</code>.</p>
<p>I can’t really speak on behalf of the incorporation of potential employers in it. But that does seem like a very promising idea. I still think it is a great to tool for the programming community, and I will continue to utilize it for my personal skill development, as it can easily provide a source of deliberate practice.</p>


</section>

 ]]></description>
  <category>General</category>
  <category>Python</category>
  <category>R</category>
  <guid>https://datasandbox.netlify.app/posts/2022-05-01-level-up-your-progamming-skills/index.en.html</guid>
  <pubDate>Sun, 01 May 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-05-01-level-up-your-progamming-skills/hackerrank.png" medium="image" type="image/png" height="64" width="144"/>
</item>
<item>
  <title>Dashboards in R with Shiny Dashboard</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/index.en.html</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/shiny-dash.png" class="img-fluid"></p>
<p>In a previous post, I explore the Flex dashboard library for the creation of a clean and interactive dashboard. That post can be found <a href="https://datasandbox.netlify.app/post/2022-03-10-creating-dashboard-in-r/">here</a>. Unknown to me at the time, but I sort of skipped over the more natural progression of creating a dashboard with R Shiny. This is my attempt to recreate that dashboard and compare the ease of creation and functionality of the <code>Shinydashboard</code> library.</p>
<section id="application-structure" class="level2">
<h2 class="anchored" data-anchor-id="application-structure">Application Structure</h2>
<p>My shiny dashboard will at heart be a shiny application. I will use the single App file structure rather than the separate UI/Server file structure. The R Studio site has a very good outline on the basic structure for a <a href="https://rstudio.github.io/shinydashboard/get_started.html">Shiny Dashboard</a>.</p>
<p>MY application will then be broken down into the following structure:</p>
<ul>
<li>Data Collection</li>
<li>Plot Creation</li>
<li>UI</li>
<li>Server</li>
</ul>
<p>Before starting to write the Shiny App, the raw data needs to be cleaned and setup to increase application performance.</p>
</section>
<section id="data-preparation" class="level2">
<h2 class="anchored" data-anchor-id="data-preparation">Data Preparation</h2>
<p>The data is the same from the previous dashboard, which was the level of donations for Canadians to charities. The raw data can be found <a href="https://open.canada.ca/data/en/dataset/74c77af4-73c4-4e0d-ac5d-f74a247cdf12">here</a>.</p>
<p>The following code is used to clean up the raw data and <em>is not</em> included in the Shiny App. This code is just used to create an RDS file (compressed data file) that the Shiny App will more easily load.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download the data and unzip</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www150.statcan.gc.ca/n1/tbl/csv/45100007-eng.zip"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"donordata.zip"</span>)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"donordata.zip"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read the data into R</span></span>
<span id="cb1-7">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"45100007.csv"</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean up the data</span></span>
<span id="cb1-10">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-11">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Donation statistics (UOM)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Average annual donations'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-12">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>GEO<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Canada"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-13">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>Education <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All education levels"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Province =</span> GEO)</span>
<span id="cb1-14">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Province[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Province <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Newfoundland and Labrador"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Newfoundland"</span></span>
<span id="cb1-15">data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Province[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Province <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prince Edward Island"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P.E.I."</span></span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saved the clean data as an RDS file</span></span>
<span id="cb1-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(data, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.rds'</span>)</span></code></pre></div>
</div>
</section>
<section id="data-collection" class="level2">
<h2 class="anchored" data-anchor-id="data-collection">Data Collection</h2>
<p>With the data preparation completed, we can now start with writing the application. Shiny Application can be broken down to two parts, reactive and nonreactive. It is important to keep our calculations in the nonreactive part if their values do not change because it will be very demanding on system resources otherwise.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading the libraries</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shinydashboard)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(plotly)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(leaflet)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rgdal)</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt)</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data is loaded into the shiny app from the previously generated RDS file</span></span>
<span id="cb2-11">data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.rds'</span>)</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Summary data is created from the loaded data and saved as data2</span></span>
<span id="cb2-14">data2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-15">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Province) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-16">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Donations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(VALUE))</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For regional information for mapping, the rgdal library is used.</span></span>
<span id="cb2-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rgdal)</span>
<span id="cb2-20"></span>
<span id="cb2-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The following code will download a regional outlines for maps if the file doesn't exist on the system </span></span>
<span id="cb2-22"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> (<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./src/ref/ne_50m_admin_1_states_provinces_lakes/ne_50m_admin_1_states_provinces_lakes.dbf"</span>)){</span>
<span id="cb2-23">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://www.naturalearthdata.com/http/'</span>,</span>
<span id="cb2-24">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'www.naturalearthdata.com/download/50m/cultural'</span>,</span>
<span id="cb2-25">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ne_50m_admin_1_states_provinces_lakes.zip'</span>), </span>
<span id="cb2-26">                      f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tempfile</span>())</span>
<span id="cb2-27">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span>(f, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">exdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./src/ref/ne_50m_admin_1_states_provinces_lakes"</span>)</span>
<span id="cb2-28">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(f)</span>
<span id="cb2-29">}</span>
<span id="cb2-30"></span>
<span id="cb2-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The regional data is then loaded into R and some data is edited to make it more inline with the regional data</span></span>
<span id="cb2-32">region <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readOGR</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"./src/ref/ne_50m_admin_1_states_provinces_lakes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ne_50m_admin_1_states_provinces_lakes'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">encoding=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'UTF-8'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-33">                  )</span>
<span id="cb2-34">data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Province <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alberta"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"British Columbia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Manitoba"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"New Brunswick"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Newfoundland and Labrador"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nova Scotia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ontario"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prince Edward Island"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QuÃ©bec"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Saskatchewan"</span>)</span></code></pre></div>
</div>
</section>
<section id="plot-creation" class="level2">
<h2 class="anchored" data-anchor-id="plot-creation">Plot Creation</h2>
<p>Just as the data was collected in the nonreactive section, so should the plot creations. This doesn’t mean that the plots won’t be interactive, just that their designs will remain static.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">bar_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Province) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Donations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(VALUE)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Province, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Donations, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Province)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'none'</span>)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This call was added for illustration</span></span>
<span id="cb3-9">bar_plot</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/index.en_files/figure-html/plots-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">edu_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Education) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Donations =</span> VALUE) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span> Donations, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Education, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Education)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb4-7">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb4-8">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This call was added for illustration</span></span>
<span id="cb4-11">edu_plot</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/index.en_files/figure-html/plots-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">pie_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Province) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Donations =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(VALUE)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Donations, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Province)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-7">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>()</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This call was added for illustration</span></span>
<span id="cb5-10">pie_plot</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/index.en_files/figure-html/plots-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">map_leaf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">leaflet</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addTiles</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setView</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">74.09</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45.7</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zoom =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addPolygons</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(region, name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Province), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#444444"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">opacity =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fillOpacity =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>,</span>
<span id="cb6-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fillColor =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorQuantile</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Greens"</span>, data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Donations)(data2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Donations),</span>
<span id="cb6-6">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This call was added for illustration</span></span>
<span id="cb6-9">map_leaf</span></code></pre></div>
<div class="cell-output-display">
<div id="htmlwidget-3e3044ad17d5a23a0f38" style="width:100%;height:464px;" class="leaflet html-widget"></div>
<script type="application/json" data-for="htmlwidget-3e3044ad17d5a23a0f38">{"x":{"options":{"crs":{"crsClass":"L.CRS.EPSG3857","code":null,"proj4def":null,"projectedBounds":null,"options":{}}},"calls":[{"method":"addTiles","args":["https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",null,null,{"minZoom":0,"maxZoom":18,"tileSize":256,"subdomains":"abc","errorTileUrl":"","tms":false,"noWrap":false,"zoomOffset":0,"zoomReverse":false,"opacity":1,"zIndex":1,"detectRetina":false,"attribution":"&copy; <a href=\"https://openstreetmap.org\">OpenStreetMap<\/a> contributors, <a href=\"https://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA<\/a>"}]},{"method":"addPolygons","args":[[[[{"lng":[-109.999432144043,-110.3212986114,-110.747678022196,-111.174062926157,-111.600442336953,-112.026827240914,-112.45320665171,-112.879591555671,-113.305970966467,-113.732355870428,-114.062517015835,-114.050602342529,-114.057474291033,-114.13253838081,-114.163673635904,-114.189793632018,-114.3548467389,-114.378154234907,-114.386163268415,-114.425906311923,-114.479645937997,-114.565223944191,-114.588015082756,-114.590053046701,-114.576254218049,-114.577473700518,-114.601550239522,-114.680898997432,-114.716714428486,-114.734023389107,-114.737330273999,-114.686040599191,-114.642842355355,-114.637596383475,-114.684930980008,-114.677877757082,-114.651395212127,-114.651961008047,-114.667748362165,-114.716379345465,-114.789136306248,-114.992795371634,-115.047880822955,-115.091188930075,-115.127669034006,-115.188698089069,-115.205957611211,-115.222085541514,-115.266629610596,-115.305438816178,-115.310964939436,-115.307630588723,-115.298358127431,-115.311019871078,-115.334997533126,-115.346522191769,-115.359777197157,-115.375509619632,-115.38620481047,-115.398712745517,-115.433105447025,-115.633891587497,-115.639884629717,-115.588001693168,-115.596192001098,-115.618933701184,-115.793781119963,-115.940437619698,-115.997643432427,-116.013535156666,-116.035002442642,-116.04187988431,-116.054382326193,-116.13492310075,-116.176341559361,-116.250312509449,-116.278761607214,-116.30995179395,-116.377100234012,-116.410795303657,-116.457717912868,-116.501745624509,-116.56735048541,-116.633856225252,-116.674033228737,-116.7177038847,-116.758188505385,-116.790773955847,-116.808236725068,-116.814696686252,-116.82991824445,-116.912782627493,-116.938721349186,-117.037059975984,-117.076984293913,-117.244569749525,-117.32014470358,-117.347588552283,-117.384947562497,-117.587315734279,-117.750638494416,-117.787766791731,-117.821818917053,-117.734642400056,-117.731698064006,-117.757614813041,-117.883056712358,-118.001989212012,-118.02860359291,-118.044286576907,-118.060101396846,-118.191256187005,-118.221023644196,-118.242804040534,-118.246676721346,-118.22502816095,-118.266314783619,-118.288435756142,-118.350261320024,-118.319334805173,-118.330211270431,-118.357935270512,-118.412894379054,-118.428423554451,-118.476922701809,-118.50185617444,-118.605495704761,-118.630709328769,-118.668178202269,-118.678357035665,-118.73148142734,-118.766363020467,-118.773570051992,-118.803672592203,-118.914365345449,-118.992549552533,-119.01611522726,-119.022108269481,-119.045542108266,-119.261060915315,-119.308785526511,-119.399763313184,-119.434903085033,-119.592386611548,-119.647730241589,-119.680106951808,-119.723255757165,-119.819605858497,-119.855080713366,-119.89384048047,-119.896114650478,-119.920811917045,-119.918230129837,-119.886968531965,-119.743124532337,-119.734829854287,-119.811569359167,-119.884639430314,-119.904431301186,-119.917169949133,-119.972694853595,-120.001017608581,-120.000962676939,-120.000885772639,-120.000808868339,-120.000759429861,-120.000704498218,-120.000627593918,-120.000550689618,-120.00050125114,-120.000446319497,-120.000369415197,-120.000292510898,-120.000243072419,-120.000188140776,-120.000138702298,-120.000083770655,-120.000006866355,-119.37500562181,-118.750031843087,-118.125030598542,-117.500029353997,-116.875033602616,-116.250032358071,-115.625031113526,-115.000029868981,-114.375028624436,-113.750027379891,-113.125026135346,-112.500024890801,-111.875023646256,-111.250027894875,-110.62502665033,-110.000025405785,-109.999997939963,-109.999970474142,-109.999970474142,-109.999948501485,-109.999921035664,-109.999921035664,-109.999893569842,-109.999871597185,-109.999844131364,-109.999816665542,-109.999816665542,-109.999789199721,-109.999767227064,-109.999767227064,-109.999739761243,-109.999712295421,-109.999690322764,-109.999662856943,-109.999662856943,-109.999635391121,-109.999613418464,-109.999613418464,-109.999585952643,-109.999558486822,-109.999531021,-109.999509048343,-109.999509048343,-109.999481582522,-109.999454116701,-109.999454116701,-109.999432144043,-109.999432144043],"lat":[48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,49.0146268849277,49.0379618467553,49.0940580403015,49.1467704446561,49.1680619493745,49.2106889041256,49.2322660533862,49.252030458437,49.2744096096814,49.3334281666202,49.3938859325984,49.4203190390755,49.451706979726,49.5186576658737,49.5436186043259,49.5505399913087,49.5555497571248,49.5685026384782,49.5872013696603,49.6038456574046,49.643066850307,49.7302982989471,49.7967546003104,49.9342704746968,49.9547270184459,49.9853239434412,50.0196122748273,50.0609758017958,50.1293437243258,50.3591447584826,50.5450334374487,50.5770475988262,50.5848149331069,50.5849467690494,50.5571733304899,50.5536577053558,50.5626225494478,50.5942302166691,50.6345610287545,50.6421306091214,50.6558195744873,50.6734965771148,50.6968535115995,50.7195952116858,50.7289445772768,50.7286369600776,50.7270109834531,50.7264616670259,50.7313725558851,50.75638842598,50.8552434102197,50.8691740748137,50.9091972696998,50.9362675832324,50.9623161682105,51.0704216410844,51.1020512809629,51.1295390749802,51.1506987437561,51.2176494299038,51.2299980631874,51.2369194501702,51.2662199883973,51.2970036809778,51.3226567581283,51.3478264368228,51.4477690675884,51.4972734640081,51.5437236610926,51.5736504200467,51.6147832341158,51.6506975421264,51.758473425144,51.7906853404353,51.7999358290694,51.7885430063692,51.7709538943701,51.7526067257014,51.7218559921065,51.715242222323,51.7138469585979,51.7357536977148,51.885903849927,51.926388470612,52.0471062486545,52.1559587918694,52.1590898955044,52.1444121605695,52.1417314964048,52.2077153856406,52.2325225154932,52.2721611888803,52.3525701274945,52.3831560661613,52.3946367794898,52.4301555796729,52.4858892243771,52.4658941064268,52.4274199838654,52.4127202762734,52.3841448357302,52.3861004022111,52.4088640749544,52.4407573867179,52.4826922027707,52.5182879072536,52.5622661804156,52.6234600304062,52.6744585675079,52.7088238031939,52.7419256110972,52.784222975992,52.8355401166214,52.8785076475574,52.88837337059,52.8951189763161,52.909434162409,52.9716717136113,53.0259112176335,53.0578484747112,53.0654180550781,53.121569180267,53.1533965740592,53.2116900333142,53.2355852978976,53.2043731385038,53.1394439368082,53.1437835365831,53.2094927676054,53.2418914504819,53.3525018062639,53.3615984862984,53.3724529788999,53.3630486816662,53.3706402346901,53.3997869643176,53.4909954638907,53.5086175348754,53.5155499081868,53.5667022538881,53.6022869720424,53.6122295993748,53.6147125096258,53.615020126825,53.6336529400358,53.6987908819737,53.7079315073224,53.7230047500849,53.7698944003111,53.7970526044721,53.8437884460987,54.2286285486698,54.6134576649124,54.9982977674835,55.383126883726,55.7679669862972,56.1527961025397,56.5376362051108,56.9224433486963,57.3072504922818,57.6920905948529,58.0769197110955,58.4617598136666,58.8465889299091,59.2314290324803,59.6162581487228,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,59.6571382772354,59.3131453441912,58.9691633974756,58.6252034234171,58.2812544356871,57.9372395299858,57.5932795559273,57.2493195818688,56.9053596078103,56.5613776610946,56.217395714379,55.8734357403205,55.529475766262,55.1854718468892,54.8415118728307,54.4975518987722,54.1535919247137,53.809609977998,53.4656280312824,53.1216680572239,52.7777080831654,52.4337261364497,52.0897441897341,51.7457842156756,51.401824241617,51.0578422949014,50.7138603481858,50.3699003741273,50.0259404000687,49.6819584533531,49.337965520309,48.9930826946527]}]],[[{"lng":[-120.000006866355,-120.000083770655,-120.000138702298,-120.000188140776,-120.000243072419,-120.000292510898,-120.000369415197,-120.000446319497,-120.00050125114,-120.000550689618,-120.000627593918,-120.000704498218,-120.000759429861,-120.000808868339,-120.000885772639,-120.000962676939,-120.001017608581,-119.972694853595,-119.917169949133,-119.904431301186,-119.884639430314,-119.811569359167,-119.734829854287,-119.743124532337,-119.886968531965,-119.918230129837,-119.920811917045,-119.896114650478,-119.89384048047,-119.855080713366,-119.819605858497,-119.723255757165,-119.680106951808,-119.647730241589,-119.592386611548,-119.434903085033,-119.399763313184,-119.308785526511,-119.261060915315,-119.045542108266,-119.022108269481,-119.01611522726,-118.992549552533,-118.914365345449,-118.803672592203,-118.773570051992,-118.766363020467,-118.73148142734,-118.678357035665,-118.668178202269,-118.630709328769,-118.605495704761,-118.50185617444,-118.476922701809,-118.428423554451,-118.412894379054,-118.357935270512,-118.330211270431,-118.319334805173,-118.350261320024,-118.288435756142,-118.266314783619,-118.22502816095,-118.246676721346,-118.242804040534,-118.221023644196,-118.191256187005,-118.060101396846,-118.044286576907,-118.02860359291,-118.001989212012,-117.883056712358,-117.757614813041,-117.731698064006,-117.734642400056,-117.821818917053,-117.787766791731,-117.750638494416,-117.587315734279,-117.384947562497,-117.347588552283,-117.32014470358,-117.244569749525,-117.076984293913,-117.037059975984,-116.938721349186,-116.912782627493,-116.82991824445,-116.814696686252,-116.808236725068,-116.790773955847,-116.758188505385,-116.7177038847,-116.674033228737,-116.633856225252,-116.56735048541,-116.501745624509,-116.457717912868,-116.410795303657,-116.377100234012,-116.30995179395,-116.278761607214,-116.250312509449,-116.176341559361,-116.13492310075,-116.054382326193,-116.04187988431,-116.035002442642,-116.013535156666,-115.997643432427,-115.940437619698,-115.793781119963,-115.618933701184,-115.596192001098,-115.588001693168,-115.639884629717,-115.633891587497,-115.433105447025,-115.398712745517,-115.38620481047,-115.375509619632,-115.359777197157,-115.346522191769,-115.334997533126,-115.311019871078,-115.298358127431,-115.307630588723,-115.310964939436,-115.305438816178,-115.266629610596,-115.222085541514,-115.205957611211,-115.188698089069,-115.127669034006,-115.091188930075,-115.047880822955,-114.992795371634,-114.789136306248,-114.716379345465,-114.667748362165,-114.651961008047,-114.651395212127,-114.677877757082,-114.684930980008,-114.637596383475,-114.642842355355,-114.686040599191,-114.737330273999,-114.734023389107,-114.716714428486,-114.680898997432,-114.601550239522,-114.577473700518,-114.576254218049,-114.590053046701,-114.588015082756,-114.565223944191,-114.479645937997,-114.425906311923,-114.386163268415,-114.378154234907,-114.3548467389,-114.189793632018,-114.163673635904,-114.13253838081,-114.057474291033,-114.050602342529,-114.062517015835,-114.158735281224,-114.585120185184,-115.011499595981,-115.437884499941,-115.864263910738,-116.048493654094,-116.290648814698,-116.717028225495,-117.039053994615,-117.143413129455,-117.569792540252,-117.996177444212,-118.422556855009,-118.848941758969,-119.275321169766,-119.701706073726,-120.128090977687,-120.554470388483,-120.980855292444,-121.40723470324,-121.833619607201,-122.259999017997,-122.686383921958,-122.788776503989,-122.826706803287,-122.924172016966,-122.962723043827,-123.002306785572,-123.027240258202,-123.049201928962,-123.086434596398,-123.117646755792,-123.109330105084,-123.077288477885,-123.079562647894,-123.150149808789,-123.181883818789,-123.196352813482,-123.19107937578,-123.229421662399,-123.183949248555,-123.067263453088,-122.947633321572,-122.913009907165,-122.879111590442,-122.964453390573,-123.015534325139,-123.174286772601,-123.276761752096,-123.290505649105,-123.286292392108,-123.264072542627,-123.24771939259,-123.223016632858,-123.19066738846,-123.17960964878,-123.187486846347,-123.324997227569,-123.336675694811,-123.322415440361,-123.335642979928,-123.39897367082,-123.436980874419,-123.508194256041,-123.530567914122,-123.858921808483,-123.891836848801,-123.948399961311,-124.028600159682,-124.053791811034,-124.024024353844,-123.992608947372,-123.95953460529,-123.922768856817,-123.847111505298,-123.817190239508,-123.739055470903,-123.61275663796,-123.582478316492,-123.708310230472,-123.762670584108,-123.818019707314,-123.874396052238,-123.903800960586,-123.904267879549,-123.884964900297,-123.823804009292,-123.784659720689,-123.78770842686,-123.825457451738,-123.88010894308,-123.933568417776,-123.945889585238,-123.863052668016,-123.865738825345,-123.957441709702,-123.971394346953,-123.972119444637,-123.984934996884,-124.058779604193,-124.141616521416,-124.281269236704,-124.412605301284,-124.483247393823,-124.702281826006,-124.782377654257,-124.784261809602,-124.934175755751,-124.93335178111,-124.985597266502,-125.04359958805,-125.056700784839,-124.936839940423,-124.862654756929,-124.854261201921,-124.857540620992,-124.875420870697,-124.859864229479,-124.933582494009,-124.949265478006,-124.931077611101,-124.942547338101,-124.985415992081,-125.058771707769,-125.209872177401,-125.476312617252,-125.507189693625,-125.525976315436,-125.539335690945,-125.555562498205,-125.585818847015,-125.610131592083,-125.641316285656,-125.697544315144,-125.7412314506,-125.772421637337,-125.83959754322,-125.9650449357,-126.024134903775,-126.094337543172,-126.236550073011,-126.404476104807,-126.449948518651,-126.447471101564,-126.416127106228,-126.238906640483,-126.067234270653,-125.897605357932,-125.904120250759,-125.980727919697,-126.370319609364,-126.492943515408,-126.514339390248,-126.517338657941,-126.472223299774,-126.39713723734,-126.374604277496,-126.418198029159,-126.488164462492,-126.521755162015,-126.484599398879,-126.517338657941,-126.562866003427,-126.631799721877,-126.960384329138,-127.014052544076,-127.057585870932,-127.267474184603,-127.356946844266,-127.441233956856,-127.590862258463,-127.708119343014,-127.714315632313,-127.689178912604,-127.632720170216,-127.419656807596,-127.346586736449,-127.280646792527,-126.968135183926,-126.735411786377,-126.691433513215,-127.034102593669,-127.33873151154,-127.442706124881,-127.575745070386,-127.60959394863,-127.644865556421,-127.668689409869,-127.714057453592,-127.728735188527,-127.747521810337,-127.818960411695,-127.850535119931,-127.869113001499,-127.863224329399,-127.829968712896,-127.727653035165,-127.858807825325,-127.843328088406,-127.79534529849,-127.673336626843,-127.549729444394,-127.437927071965,-127.24225506743,-127.175743834424,-127.007977104391,-126.959455984376,-126.899976001639,-126.826334641408,-126.738564862669,-126.71399393888,-126.752643842698,-126.895218921379,-126.901393238021,-126.938164479658,-127.127068905809,-127.160577207869,-127.193986632971,-127.208252380586,-127.187114684467,-126.995189017966,-126.951315114925,-126.951364553404,-126.966404837181,-127.008235283112,-127.019347954434,-127.006378593588,-127.013223076271,-127.034877129832,-127.066193659347,-127.107068294695,-127.519264355341,-127.56032026511,-127.713387287551,-127.791884604998,-127.834308312671,-127.902187343581,-127.99538436862,-128.102253879533,-128.193566749227,-128.357614607048,-128.037478486437,-128.029128876744,-128.06031906348,-128.051557466466,-128.021301117656,-127.940222013001,-127.943375089293,-128.0382530226,-128.183981177573,-128.240978250059,-128.271542216069,-128.275162211324,-128.196769263998,-128.132378392401,-128.108818210838,-128.053265840555,-128.105945285924,-128.365024885651,-128.451970689749,-128.524683705218,-128.652322870243,-128.868539309155,-129.08092701257,-129.129530530049,-129.171569716223,-129.114462780451,-129.021419564011,-128.935610844917,-128.854586671904,-128.850422853386,-128.905612674827,-128.833036988465,-128.542124501782,-128.478612536468,-128.358048567025,-128.291075908221,-128.132691502765,-128.079177096426,-127.927845913896,-127.950065763376,-128.11514633608,-128.207233741936,-128.369161238348,-128.469647692376,-128.51176378285,-128.600335563573,-128.67555346195,-128.750793332984,-128.767899046527,-128.763658323709,-128.745931882603,-128.714719723209,-128.652866693506,-128.560444204629,-128.532121449642,-128.650900140697,-128.704799068534,-128.890165896894,-128.927838017472,-128.944009893089,-128.959357794065,-129.013981819586,-129.056405527259,-129.208126724453,-129.231741837659,-129.240322160252,-129.257839861115,-129.284300433414,-129.462399805442,-129.563715727276,-129.686729647984,-129.821784584776,-129.911883465167,-130.074382250662,-130.263259210992,-130.335241635613,-130.232843560418,-130.085956347784,-130.063500292239,-130.043296434047,-129.790781165625,-129.626035675942,-129.7949669568,-129.898419719536,-130.084247973695,-130.290313045032,-130.396787048117,-130.430251404863,-130.393458190568,-130.388624206009,-130.369996885963,-130.350485166468,-130.30723199099,-130.21894585481,-130.140860524683,-130.108643116227,-129.948522870861,-129.890152507306,-129.780756140829,-129.560667021105,-129.630117096996,-129.666630159912,-129.701330478619,-129.734196080459,-129.765457678331,-129.795170203878,-129.81191336858,-129.815637733956,-129.837725747494,-129.877155680639,-129.985184249213,-130.048465501627,-130.091795581405,-130.058413622123,-129.995830001572,-129.985134810734,-130.044048997552,-130.07998527822,-130.09296013223,-130.094690478976,-130.085126879978,-130.060352709111,-130.020302048404,-130.025081101321,-130.014072800119,-130.022883835612,-130.055958177694,-130.097865527925,-130.214705131992,-130.413145691319,-130.477097109775,-130.64907160364,-130.741680860102,-130.930217244247,-131.082921717846,-131.199404266235,-131.33579953511,-131.47189267995,-131.575114729786,-131.651519151646,-131.824273674837,-131.83308471033,-131.886000361762,-131.866159052412,-131.962509153743,-132.104282230441,-132.062913210308,-132.031519776493,-132.157016607452,-132.337988904395,-132.279415293762,-132.23215760153,-132.301662609064,-132.442479875178,-132.55048097793,-132.691506984287,-132.815531647221,-132.91684207589,-133.001409339859,-133.120424236977,-133.275298510463,-133.422575737761,-133.401102958621,-133.546391660453,-133.673926455357,-133.820742256856,-133.965745314145,-134.069203570045,-134.218518761288,-134.296994106078,-134.329650967675,-134.363527311741,-134.393058562868,-134.410213714889,-134.440755708242,-134.621980690741,-134.677225443825,-134.802409164421,-134.907240711388,-134.943770253798,-135.071310541866,-135.050843011788,-135.036659661638,-135.051029779374,-135.260780763937,-135.367853521928,-135.475937022145,-135.702590473174,-135.934643704682,-136.097164462835,-136.321829388398,-136.247133340626,-136.277982951178,-136.34784501439,-136.466343554067,-136.466728075566,-136.578739188237,-136.813269836832,-136.939310491054,-137.126198925918,-137.27755757427,-137.438578698576,-137.5208772857,-137.484160975706,-137.543695890086,-137.593299163463,-137.69662558342,-137.870572123259,-138.001133651677,-138.187450797456,-138.317625057793,-138.453635805169,-138.632257027803,-138.70545618831,-138.868754229207,-139.043445092805,-139.056518823772,-138.095495227542,-137.143738599438,-136.191965491842,-135.240214356903,-134.288463221964,-133.336690114367,-132.384938979428,-131.433187844489,-130.48143670955,-129.52968557461,-128.577934439671,-127.626183304732,-126.674432169793,-125.722681034853,-124.770907927257,-123.819156792318,-123.34174039227,-122.864356951208,-122.386968016981,-121.909579082754,-121.432195641692,-120.954806707466,-120.477423266403,-120.000006866355],"lat":[60.0010872649654,59.6162581487228,59.2314290324803,58.8465889299091,58.4617598136666,58.0769197110955,57.6920905948529,57.3072504922818,56.9224433486963,56.5376362051108,56.1527961025397,55.7679669862972,55.383126883726,54.9982977674835,54.6134576649124,54.2286285486698,53.8437884460987,53.7970526044721,53.7698944003111,53.7230047500849,53.7079315073224,53.6987908819737,53.6336529400358,53.615020126825,53.6147125096258,53.6122295993748,53.6022869720424,53.5667022538881,53.5155499081868,53.5086175348754,53.4909954638907,53.3997869643176,53.3706402346901,53.3630486816662,53.3724529788999,53.3615984862984,53.3525018062639,53.2418914504819,53.2094927676054,53.1437835365831,53.1394439368082,53.2043731385038,53.2355852978976,53.2116900333142,53.1533965740592,53.121569180267,53.0654180550781,53.0578484747112,53.0259112176335,52.9716717136113,52.909434162409,52.8951189763161,52.88837337059,52.8785076475574,52.8355401166214,52.784222975992,52.7419256110972,52.7088238031939,52.6744585675079,52.6234600304062,52.5622661804156,52.5182879072536,52.4826922027707,52.4407573867179,52.4088640749544,52.3861004022111,52.3841448357302,52.4127202762734,52.4274199838654,52.4658941064268,52.4858892243771,52.4301555796729,52.3946367794898,52.3831560661613,52.3525701274945,52.2721611888803,52.2325225154932,52.2077153856406,52.1417314964048,52.1444121605695,52.1590898955044,52.1559587918694,52.0471062486545,51.926388470612,51.885903849927,51.7357536977148,51.7138469585979,51.715242222323,51.7218559921065,51.7526067257014,51.7709538943701,51.7885430063692,51.7999358290694,51.7906853404353,51.758473425144,51.6506975421264,51.6147832341158,51.5736504200467,51.5437236610926,51.4972734640081,51.4477690675884,51.3478264368228,51.3226567581283,51.2970036809778,51.2662199883973,51.2369194501702,51.2299980631874,51.2176494299038,51.1506987437561,51.1295390749802,51.1020512809629,51.0704216410844,50.9623161682105,50.9362675832324,50.9091972696998,50.8691740748137,50.8552434102197,50.75638842598,50.7313725558851,50.7264616670259,50.7270109834531,50.7286369600776,50.7289445772768,50.7195952116858,50.6968535115995,50.6734965771148,50.6558195744873,50.6421306091214,50.6345610287545,50.5942302166691,50.5626225494478,50.5536577053558,50.5571733304899,50.5849467690494,50.5848149331069,50.5770475988262,50.5450334374487,50.3591447584826,50.1293437243258,50.0609758017958,50.0196122748273,49.9853239434412,49.9547270184459,49.9342704746968,49.7967546003104,49.7302982989471,49.643066850307,49.6038456574046,49.5872013696603,49.5685026384782,49.5555497571248,49.5505399913087,49.5436186043259,49.5186576658737,49.451706979726,49.4203190390755,49.3938859325984,49.3334281666202,49.2744096096814,49.252030458437,49.2322660533862,49.2106889041256,49.1680619493745,49.1467704446561,49.0940580403015,49.0379618467553,49.0146268849277,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930826946527,48.9930607219956,48.99302776301,48.99302776301,48.99302776301,48.99302776301,48.99302776301,48.99302776301,48.99302776301,48.99302776301,48.99302776301,49.0284257135791,49.0746781567497,49.074623225107,49.0608793280983,49.0385111631825,48.99302776301,48.99302776301,49.0563309880811,49.084598811425,49.1183488127125,49.1305985690392,49.1210294768773,49.1295109225133,49.1476932962538,49.2195328986036,49.2605119040731,49.2777164945731,49.2915702548672,49.2932731357916,49.3231889084171,49.3989506300572,49.3293632250589,49.322156193534,49.348204778512,49.343964055694,49.3594657652697,49.3749674748454,49.3904691844212,49.4430277801761,49.5904643092379,49.6443083054325,49.6735539120169,49.6803324767286,49.5776981954697,49.5451457039935,49.5169987302635,49.459199655793,49.4418961883361,49.4513004855698,49.4024662551913,49.3972916944471,49.4828422348199,49.4947074696476,49.5346977055481,49.6028678741642,49.6617216361749,49.71133589588,49.7361649983897,49.7361649983897,49.7175321851789,49.6366618207658,49.5865861352618,49.5935624538873,49.6575907766423,49.6812553283263,49.6569206106011,49.6585246145686,49.6851335023024,49.7368131917738,49.7954582135422,49.9811381522659,50.0170524602765,50.0437162796531,50.0880021700143,50.1067558328391,50.144230199503,50.173629614687,50.1883073496219,50.1839128182043,50.1025700416638,50.0720829799539,49.9927616878656,49.9695036303377,49.8920500141018,49.8755595349571,49.8536527958402,49.792667686092,49.7721012790574,49.7781547460852,49.8082243273104,49.9576713544957,50.0200956732833,50.0728080776378,50.2580705358769,50.2979069631778,50.3556071606914,50.3637480301426,50.4186247412203,50.5373759664535,50.637296624562,50.6686406198983,50.7173430143343,50.8256352547934,50.8724040554056,50.8105949710165,50.7647050766879,50.7184086882031,50.6656962838485,50.5919560466605,50.5138652233691,50.4763029660768,50.4971769903106,50.5072514535856,50.5341459858615,50.6490300234471,50.6348466732967,50.573652823306,50.4860368531669,50.4662175164733,50.4645695671917,50.4785771360854,50.5081852915118,50.5106682017628,50.4873552125922,50.4967155645118,50.4975944707953,50.5232695206028,50.5298832903864,50.5497355860656,50.5877702554852,50.6069633714517,50.6238054131098,50.6643010201234,50.6843950150305,50.7049174767509,50.7113774379348,50.6667289987316,50.6721013133897,50.6793852492144,50.7244511689023,50.7672868638957,50.8070793458824,50.83735766735,50.850200685418,50.8418620620531,50.8660429711786,50.9605034240007,51.0568315526754,50.9654692445027,50.9151408734422,50.8936845737956,50.8668230005052,50.8675371118606,50.9160637250399,50.9455510308522,50.9893974680717,51.0875273546276,51.1511931285406,51.2686589533341,51.3434868370479,51.4273015375108,51.6080596010468,51.6423808914186,51.6541033039751,51.6699236170786,51.6926103855222,51.703409946481,51.7167143903479,51.7073869974139,51.6789653654704,51.5629497360447,51.5140386013664,51.4784648695406,51.4775859632571,51.4901872820972,51.5055351830733,51.543569852493,51.6039287415142,51.6731755703276,51.7752385625023,51.8208208396318,51.8790044356013,51.9932073208171,51.9902849574244,52.0864812501565,52.1910271525821,52.252913141271,52.2976055257884,52.3561296979428,52.3950982052887,52.3148430752741,52.2906621661485,52.2545391178955,52.1883355020888,52.1251421403032,52.0649590325386,52.0606963370635,52.1123760265349,52.2254692925679,52.2653167061973,52.3085918543325,52.3709612414773,52.3948894650463,52.4576763326758,52.4982378576607,52.5376677908054,52.6579241430491,52.7212273681201,52.7510222911317,52.7846624291338,52.8425603805611,52.8424615036042,52.7546148205656,52.7199859129947,52.6817425033327,52.6527056369906,52.6328093959973,52.3592827742349,52.3432097755749,52.3185125090078,52.2893657793803,52.2510015201043,52.1509050807391,51.9505254344233,51.7884111704267,51.998272018276,52.1588811552621,52.3181499601658,52.342484677891,52.4275518198079,52.4533367329009,52.4906682772938,52.5451604668725,52.5507415217729,52.5311638843073,52.4079082643711,52.3682805773125,52.3630071396114,52.4355059216739,52.6232952354781,52.8058220979097,52.8580401174798,52.9106756175345,52.9068963205154,52.8257732705458,52.8766179990478,53.1406853919337,53.2438250443057,53.328112156896,53.3672564454986,53.4422710967977,53.5335784733276,53.6411236434458,53.6921441532046,53.715171497833,53.7045477181308,53.6651946892859,53.5593194411065,53.5493878001026,53.42067197488,53.4102789080773,53.4598162634826,53.4578716833303,53.4177715841443,53.3694317385503,53.2747076138431,53.3298150378203,53.4459405305314,53.4831951706244,53.4903802294922,53.4709234416407,53.4765594281838,53.5060906793103,53.5546172924896,53.660855089511,53.7102056773311,53.7469000146683,53.7801776038284,53.8099944994971,53.8316485530575,53.8450848328669,53.8581036321917,53.9188250700549,53.9186163298125,53.8297589045479,53.822815544908,53.8400201354081,53.8414703307759,53.7974371259712,53.7778045568629,53.6415850692446,53.5764251546496,53.4790643110918,53.4179803243867,53.3931731945341,53.3465582025215,53.2514825153009,53.3335394031967,53.4127618183282,53.551354352912,53.575645125323,53.6541424427705,53.7239056290255,53.8674310251256,53.9757781972275,54.1056366006187,54.1335418751207,54.1657867493976,54.2302545252943,54.2361212247369,54.2263543786612,54.1813983222588,54.2703875834659,54.3516644420351,54.421021134134,54.4796222105882,54.5393548788824,54.6200274893818,54.6553155766654,54.7003045920534,54.7302533236646,54.8227472236774,54.8872479585598,55.0810358077491,55.1646417679696,55.2804486571529,55.4625360664428,55.4522199039399,55.4366632627214,55.4385748838881,55.4579877264255,55.4982636068682,55.5595563338157,55.5326288425541,55.4176019826974,55.3191095472996,55.2506317614843,55.1114679378162,55.0572613927797,55.1077435724398,55.1947662808375,55.2640680412937,55.3588470976436,55.4719074046909,55.5629181503502,55.6318463756358,55.6947870518649,55.7516852473948,55.8137030720262,55.8807746077879,55.8882123522123,55.9505377940429,56.0145111851552,56.0652350640433,56.1092902415051,56.0828351623709,56.1225177810722,56.2305683223034,56.2636701302067,56.3408271155719,56.3785981131065,56.4048224793413,56.449218232988,56.5012275123157,56.5567304441205,56.5988190687729,56.5960834729654,56.5899860606235,56.6848090622875,56.7421137519735,56.7928376308616,56.8186994482544,56.8567890493168,56.9533698635479,57.0265497979802,57.048192865212,57.0794599562485,57.1453449685275,57.1985297850094,57.2763239774302,57.4067316972486,57.499906749631,57.6451130539984,57.7726972873809,57.8769795179215,57.9489729288709,58.0777436857363,58.2228511131467,58.3370649846911,58.4108821261789,58.503485889477,58.5971443403155,58.7050520592756,58.757863340587,58.7955354611647,58.8498958148009,58.89849933228,58.9397090506489,58.9687239443338,59.0091646197047,59.0562410375161,59.0853328355009,59.1553047619982,59.1992830351602,59.2499739550627,59.2712105281384,59.2882722963674,59.4414327026006,59.4960567281219,59.5507027263002,59.5786629324449,59.6950081517269,59.7433040520067,59.7932808605538,59.7287581530144,59.662642427836,59.6383736280821,59.6048323670369,59.5329048740588,59.4803243056467,59.4560335332357,59.459087732571,59.279944659331,59.152250562663,59.1500313242971,59.1061079827778,59.0409480681827,58.9882136911711,58.9031245765971,58.9153743329237,58.9912349315207,59.1194453856303,59.226254471736,59.2811311828137,59.3735811375123,59.4429378296112,59.5419466224506,59.6111165469642,59.6833846161273,59.7782845220911,59.901331401785,59.9457491280888,59.9932430263849,60.0015816497499,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654]}],[{"lng":[-130.214089897593,-130.218506401668,-130.171841971177,-130.036550828321,-130.039264451471,-130.059473802828,-130.120425953591,-130.140421071541,-130.146518483883,-130.137064748171,-130.11197746694,-130.074640429383,-130.025081101321,-130.091564868505,-130.122518849178,-130.109961475652,-130.068543017041,-130.037611009025,-130.021823654907,-130.014902267925,-130.017874069796,-130.063994677024,-130.214089897593],"lat":[55.0258954247863,55.0602606604722,55.1370001653527,55.297916919538,55.3435980736244,55.4123065723392,55.5244110688031,55.5850336297094,55.6544891987652,55.7193854414752,55.7797992621392,55.8360272916278,55.8882123522123,55.7861603463662,55.6910187411743,55.580946715491,55.4828388015923,55.4164594045288,55.3717670200114,55.3316669208255,55.2849420655275,55.2260663308597,55.0258954247863]}],[{"lng":[-130.214089897593,-130.203905571033,-130.349430478928,-130.535489445987,-130.575331366452,-130.49324151957,-130.312532894513,-130.214089897593],"lat":[55.0258954247863,54.9470355584967,54.8145624089121,54.7487543009329,54.7696832568094,54.8341730053633,54.9459479119709,55.0258954247863]}],[{"lng":[-130.927168538076,-130.950272787005,-130.959028890854,-130.953447835954,-130.921768757597,-130.906810871284,-130.777078810671,-130.758006544319,-130.753436231644,-130.76335688632,-130.805137893773,-130.927168538076],"lat":[54.4790509215039,54.4777655210643,54.4986944769408,54.5418377891334,54.6149078602802,54.631804833581,54.6188849112132,54.6137762684401,54.5997137679037,54.5767193822609,54.5438043419428,54.4790509215039]}],[{"lng":[-132.655515771976,-132.564049093682,-132.344421399757,-132.303365489988,-132.261639414178,-132.215903328449,-132.166085821665,-132.155104986286,-132.1751055974,-132.214508064724,-132.564873068323,-132.574096091136,-132.56711977251,-132.534644185334,-132.464414080116,-132.186965339063,-132.171666876566,-132.152237554536,-132.114021610695,-132.110610355682,-132.135906377155,-132.134412236473,-131.940827634362,-131.81964843052,-131.695936877949,-131.667641588784,-131.685395495711,-131.702550647733,-131.821120598545,-131.889153438054,-131.922304684436,-131.928067013758,-131.957416990463,-132.0113159183,-132.347288831507,-132.520460835184,-132.674818751228,-132.747526273533,-132.692567164991,-132.654790674292,-132.546218282455,-132.462398088828,-132.425011612793,-132.431345231198,-132.67016604109,-132.845013459869,-132.89797854978,-132.899582553748,-132.913381382399,-133.052237588868,-133.079467204164,-133.097660564233,-133.097940715611,-133.063861124468,-133.048386880713,-132.991461219362,-132.893073154085,-132.655515771976],"lat":[54.1274664354358,54.0686346460822,54.1060540811033,54.0988690222355,54.0763360623916,54.0284356699393,53.9552117901929,53.8752093457348,53.8465350282347,53.8147515797567,53.6876287721729,53.6753790158463,53.663964220489,53.6517144641623,53.6533184681297,53.6848382447227,53.7068548471251,53.8069952318045,53.8601800482865,53.9002801474724,53.9958502194775,54.0342694103962,54.0419708267056,54.0773248319605,54.1431549125967,54.141342168387,54.0227996833962,53.9863909906011,53.8415252624186,53.7139849743502,53.5878948816496,53.3792205572831,53.30868832803,53.2651714806669,53.189212005113,53.1940679623295,53.2632049278575,53.3104900859112,53.36784970724,53.3705413577333,53.3592803709756,53.3378790029717,53.336956151374,53.3504363764976,53.4585967810142,53.5076946832777,53.5626702713124,53.6053851166918,53.6291815043184,53.7781121740621,53.8370208677156,53.9202752654227,54.0055950928961,54.1440557915374,54.1589202940575,54.1578326475316,54.1407818656312,54.1274664354358]}],[{"lng":[-130.236282281252,-130.267236261925,-130.337570737265,-130.384229674591,-130.407229553398,-130.470247133927,-130.537477971453,-130.589827826966,-130.624632515793,-130.641837106293,-130.646281076189,-130.637909493839,-130.643699288982,-130.663595529975,-130.683436839326,-130.703179271719,-130.707260692773,-130.695686595652,-130.646901803752,-130.494614810638,-130.447999818626,-130.397308898723,-130.315861752062,-130.298503352962,-130.236282281252],"lat":[53.9585626203989,53.922604367074,53.8662774606285,53.8439532410269,53.8555218449838,53.8617730659254,53.9178472868144,53.9402703833729,53.9414129615415,53.9211541717062,53.8940179402023,53.8600152533583,53.8445135437826,53.8475677431179,53.8634539741926,53.8922161823211,53.9214947478911,53.951256711917,53.9912799068032,54.0741717556684,54.0890032992029,54.0856964143111,54.0469366472076,54.0356646741213,53.9585626203989]}],[{"lng":[-129.848607705917,-129.868553385389,-129.934388959189,-130.151401906921,-130.305682918665,-130.410739685368,-130.517559757803,-130.451982362723,-130.394825988472,-130.195017631241,-130.035391770659,-129.944749067006,-129.754839391793,-129.768945837644,-129.848607705917],"lat":[53.1679205003945,53.1645037522173,53.1766546315871,53.345679296238,53.4073895036701,53.4908416552911,53.5442242256869,53.6311480571277,53.6203924414831,53.5496734446447,53.481107768201,53.4363714383695,53.2447588822319,53.2172710882146,53.1679205003945]}],[{"lng":[-129.167740980725,-129.173245131326,-129.276829730004,-129.30571828091,-129.323884175158,-129.331272481104,-129.314375507803,-129.25308827402,-129.251149187032,-129.238201798843,-129.195206802085,-129.177018935181,-129.167740980725],"lat":[53.1178997465333,53.1107586329796,53.1109234279077,53.1211516997823,53.1421355873015,53.1739629810938,53.2123052677127,53.2854852021449,53.3166973615386,53.3300787097054,53.2932305637685,53.2591509726247,53.1178997465333]}],[{"lng":[-128.552429677956,-128.506567249449,-128.509929065983,-128.576824820488,-128.624005608421,-128.678937251141,-128.730924557812,-128.735571774786,-128.749420041916,-128.766448851159,-128.746321897266,-128.76965136593,-128.831202271598,-128.899828372849,-129.022869759378,-129.084700816425,-129.094852183999,-129.175931288655,-129.184302871005,-129.177689101222,-129.111078991259,-129.084107554683,-129.060333139714,-129.033229867196,-128.970234259324,-128.857712282375,-128.740378293524,-128.632684807971,-128.552429677956],"lat":[52.9397674155193,52.6207134482702,52.5186065107814,52.4517876605762,52.3399028906831,52.2896514239225,52.3565252057704,52.4677068506366,52.5560698911167,52.5983892286686,52.7633928970724,52.7511980723884,52.6787981672828,52.6738433331095,52.755955152648,52.8224553993254,52.891867023067,52.9649370942138,52.9906670756641,53.0179021841249,53.0906646380724,53.1397076086933,53.2406280226993,53.2799480925586,53.2743670376582,53.228587006615,53.178873869953,53.1125164455466,52.9397674155193]}],[{"lng":[-131.75373595242,-131.652321153629,-131.62216917494,-131.634671616823,-131.795258781152,-131.879694209178,-131.916333614872,-131.971781615035,-131.904396968909,-131.810062858865,-131.727302845943,-131.610617050476,-131.455226419548,-131.57281858712,-131.590594466704,-131.443883035326,-131.429957863897,-131.382980323042,-131.273611422386,-131.259735689435,-131.259938936513,-131.327070897081,-131.319935276692,-131.25916440035,-131.142637906647,-131.116149868528,-131.221547211415,-131.421871926088,-131.511141338673,-131.562040998818,-131.623690781443,-131.809672844202,-132.092213748535,-132.165108038425,-132.238562631071,-132.259985971732,-132.258123789044,-132.2295483485,-132.144926152889,-132.143761602064,-132.468676775591,-132.504854755487,-132.54678957154,-132.524207173217,-132.345432141983,-132.153890996981,-132.035914307911,-131.989480590319,-131.893130488987,-131.853442377122,-131.75373595242],"lat":[53.1955621030115,53.1029583397133,53.020044518191,52.9221673171917,52.8850445130412,52.9146526684675,52.9091265452098,52.8798260069827,52.8666973443725,52.8186870886348,52.7564165784469,52.7451995370034,52.7016936759688,52.6233281944637,52.5782073431331,52.4533367329009,52.4221245735071,52.4157195439659,52.425815979898,52.4159172978797,52.3900335078299,52.3175347257674,52.3030657310748,52.2916399493889,52.2911235919474,52.2190862356837,52.1536406765466,52.2380046934367,52.3220830657846,52.3999541625052,52.4439873673099,52.5416997733811,52.7528020763559,52.7832891380657,52.8667962213294,52.9069732248152,52.9339007160767,52.9480840662272,52.9574883634609,52.9993023298997,53.071856043605,53.0867315324537,53.1374773839988,53.1449261147517,53.1360821202737,53.1604717696416,53.1791265555095,53.2019671325527,53.2314324657079,53.229718598455,53.1955621030115]}],[{"lng":[-129.313705341762,-129.328690693896,-129.370032248208,-129.409720360073,-129.477775172239,-129.500154323484,-129.514722195133,-129.501082668246,-129.471419581177,-129.450748804021,-129.343494771609,-129.313705341762],"lat":[52.9921941753317,52.9842071144801,52.9975884626469,53.0237359245818,53.0977398336548,53.1288970614059,53.1793902273945,53.1883330988294,53.1830376884712,53.1746880787777,53.0527837772524,52.9921941753317]}],[{"lng":[-128.936879765864,-128.96871265282,-129.102322887409,-129.151025281845,-129.250500993648,-129.267787981612,-129.263525286137,-129.245958146795,-129.2150536046,-129.186165053694,-128.994008674293,-128.940312993534,-128.936879765864],"lat":[52.5100261881884,52.4642351708167,52.5743621281427,52.6053216019799,52.7221612060464,52.77239070015,52.8007793731079,52.8112493442105,52.8038555451003,52.7912432399317,52.6616924537397,52.60071833032,52.5100261881884]}],[{"lng":[-128.368776716849,-128.445384385787,-128.419879624071,-128.412518783947,-128.426290146777,-128.43592515691,-128.439803330886,-128.364871077051,-128.247284402644,-128.248421487648,-128.298156596967,-128.323765728803,-128.343557599676,-128.368776716849],"lat":[52.4008880004314,52.3874956659362,52.4410869765743,52.4728704250523,52.5027422523637,52.560354559249,52.6963653066249,52.7818938743406,52.7843767845916,52.7412224860704,52.5482695978505,52.4589727194441,52.4260466927974,52.4008880004314]}],[{"lng":[-131.029308434551,-131.047243615899,-131.080521205059,-131.103438686402,-131.117314419353,-131.107108120136,-131.098115810222,-131.010653648683,-131.029308434551],"lat":[51.9616326125814,51.9596990187576,51.9804192343918,52.0138835911371,52.1010051764918,52.1365569356605,52.1506194361969,52.0952703129918,51.9616326125814]}],[{"lng":[-127.924643399125,-127.941282193705,-127.981228484291,-128.04455368202,-128.091789401595,-128.148786474082,-128.142403417198,-128.122770848089,-128.031716157116,-127.998691253512,-127.986809539192,-127.93249313087,-127.916370693731,-127.916321255253,-127.924643399125],"lat":[51.4738615978807,51.4571733648222,51.4572173101364,51.4740154064803,51.5111162379737,51.6266924142576,51.6465886552509,51.6667925134435,51.7084197122971,51.7038164406371,51.6735930508123,51.6054778138389,51.585427764246,51.5062053491145,51.4738615978807]}],[{"lng":[-127.197320983685,-126.70094218057,-126.203865745592,-125.839163583242,-125.615223755364,-125.534347897786,-125.482074946573,-125.42045262977,-125.313945667699,-125.233179673407,-125.066418192436,-124.934642674714,-124.904617038803,-124.932417943184,-124.930665623781,-124.830618622894,-124.642851281747,-124.495936603291,-124.185902411777,-123.995811462143,-123.937155454046,-123.854499811244,-123.820035698601,-123.75228301047,-123.626582932433,-123.497032146241,-123.472845743951,-123.457964761938,-123.443056314104,-123.415458656801,-123.389876990786,-123.366316809223,-123.2837875092,-123.310660068819,-123.334533360745,-123.445896280032,-123.484551677015,-123.536456586221,-123.573150923558,-123.594618209534,-123.916924130032,-124.115260319238,-124.376202101653,-124.689383876296,-124.868235811829,-125.017221413216,-125.120729107594,-125.140262799745,-125.135686993907,-124.934747044835,-124.849657930261,-124.817028534485,-124.800230438141,-124.812634003068,-124.820747406697,-124.838732026524,-124.868318209293,-124.904463230203,-124.927358738889,-125.168217512726,-125.362730459599,-125.460294550235,-125.489441279862,-125.543131467457,-125.660487428965,-125.82853980354,-125.811955940603,-125.70229590224,-125.644260621706,-125.654648195344,-125.69369360699,-125.728031376854,-125.796399299384,-125.835439217866,-125.918353039388,-125.951636121712,-125.983831557511,-125.937683484461,-125.935409314453,-126.020311661441,-126.048321306065,-126.074908221141,-126.099841693772,-126.168830343865,-126.2436087891,-126.269701319392,-126.279627467232,-126.304484035563,-126.418582550658,-126.444526765515,-126.499870395555,-126.519145908986,-126.548550817334,-126.56371744389,-126.557488195605,-126.541909581729,-126.442790925605,-126.157800070007,-126.13408058668,-126.347577909277,-126.403157745382,-126.462764070898,-126.525243321328,-126.558262731767,-126.592886146174,-126.683116862507,-126.744607343368,-126.849356492872,-126.903310352352,-126.926095997752,-126.947958791555,-126.977100028018,-127.048752862783,-127.114302792041,-127.165515562549,-127.195876281481,-127.207499817081,-127.179078185137,-127.1796220084,-127.192333190526,-127.21569012501,-127.249797181975,-127.268402529365,-127.271555605657,-127.290029117104,-127.349432195542,-127.397903877078,-127.429764229856,-127.467150705892,-127.674836260689,-127.77043929168,-127.816301720187,-127.86389449544,-127.872991175475,-127.828161461851,-127.839169763052,-127.850848230294,-127.946681974184,-127.962930754101,-127.905884243136,-127.873996424537,-127.831517785221,-127.641426835587,-127.578151076337,-127.486503123622,-127.489343089551,-127.5240214356,-127.529003735595,-127.465936716588,-127.526240673966,-127.751471395449,-127.749691610225,-127.731163167135,-127.864696497424,-127.963655851785,-128.058352510671,-128.135635838814,-128.267438822358,-128.349885724917,-128.346035016762,-128.300842754296,-128.241571511801,-128.101320041607,-127.91807906782,-127.71305220453,-127.197320983685],"lat":[50.6403727965543,50.5155241589792,50.4538469105327,50.380798812043,50.3585295240841,50.3424784980812,50.316770489288,50.2546537876997,50.1067118875249,50.0122184757172,49.8481925905538,49.7316386310295,49.6853642152018,49.6704557673675,49.6431767135925,49.5301054202166,49.4286576624405,49.3802958441893,49.3005790442734,49.2240263069781,49.1708195178391,49.1191727873533,49.0835111648992,48.9512247828997,48.8240470436733,48.5820951301467,48.6023099746678,48.6743912762456,48.6904862475627,48.6981876638721,48.6702054850703,48.6064628068575,48.4551810628053,48.4110270083866,48.4064786683693,48.4272208566605,48.4000956114852,48.3449552285224,48.3227848175204,48.333540433165,48.3865604547188,48.4364273999805,48.5152543072844,48.5972892225231,48.6535941563116,48.711492107739,48.7607987502449,48.8026566619979,48.8223990943916,48.9563444120013,49.0282719049794,49.0833134109854,49.1415519385976,49.212655456935,49.2071293336773,49.1390690283467,49.0785013990831,49.0310075007869,49.0142203907716,48.9910172648864,48.9982462690684,48.9410404563393,48.9338114521573,48.9528287868671,49.029150811263,49.0918388019356,49.1072306482259,49.1392008642892,49.1857828973161,49.1932206417405,49.1903861689761,49.1998344115241,49.2601933005453,49.27668377969,49.2495036028719,49.2480534075041,49.2879008211335,49.3797794867478,49.4014774856224,49.36799115622,49.3789994574211,49.4087724077756,49.4212748496588,49.4151774373169,49.4426652313342,49.4318656703753,49.3921830516741,49.3820536567564,49.4490263155612,49.4511137179846,49.3999284132977,49.3967753370055,49.4189457480075,49.543278028141,49.5785990744103,49.5904643092379,49.6192924353376,49.650153032218,49.6723124568914,49.6608427298914,49.6777397031922,49.7202238356722,49.7195976149452,49.7333964435966,49.7640922455488,49.8764384412407,49.904915004827,49.9228227203538,49.9441361977294,49.9347319004956,49.9026957664611,49.8827995254677,49.8715275523815,49.8797453261325,49.9104411280847,49.9491459635455,49.9924320980092,50.0502751177939,50.0731156948371,50.099889377499,50.1214884994167,50.1380009512185,50.1293437243258,50.0955497777241,50.0708415248284,50.0519560260612,50.0850029023218,50.1308378650077,50.1634452881266,50.1633464111697,50.1211259505748,50.1177201887261,50.1277397203583,50.1501188716026,50.2114006122216,50.2932048145609,50.3137272762813,50.3262297181645,50.3459721505582,50.4451896836399,50.4639433464647,50.4710295283757,50.479093493527,50.4649321160337,50.4046281586552,50.4273588724129,50.4957267949428,50.536760732055,50.5831120521825,50.5966801679345,50.6073478929508,50.5777397375245,50.5357280171718,50.4988798712349,50.4926286502933,50.4984733770788,50.5205339247954,50.6092595141175,50.696600826043,50.7442375466102,50.7941594235146,50.8281621103586,50.8577702657849,50.860538820578,50.8207463385913,50.6403727965543]}],[{"lng":[-125.184136702786,-125.195090072345,-125.259557848241,-125.358467764124,-125.345289663035,-125.301157581274,-125.260953111966,-125.195996444449,-125.139488263583,-125.126491436915,-125.091428569367,-125.074015238624,-125.113005718627,-125.184136702786],"lat":[50.0971208227059,50.0443315140515,50.1300138903669,50.3115080379154,50.3539592114098,50.4140764012031,50.4178007665795,50.3897416834779,50.3397209296166,50.3202641417651,50.2677824503099,50.2206511008557,50.1635002197694,50.0971208227059]}],[{"lng":[-124.977742041592,-125.00156589504,-125.025955544408,-124.995671729776,-124.986992530227,-124.99081577256,-124.937845189485,-124.916399876167,-124.907462497896,-124.908440281136,-124.977742041592],"lat":[50.0296098338025,50.020776825653,50.1341008045854,50.1751786870117,50.1958549573317,50.2171464620502,50.1659281983776,50.1315629626917,50.0839701874386,50.0713139369558,50.0296098338025]}],[{"lng":[-126.641231484932,-126.680425212013,-126.743420819885,-126.814216721023,-126.938549001157,-126.951265676447,-126.940048635003,-126.904859424676,-126.896850391168,-126.925837819031,-126.826076462687,-126.738125409527,-126.698124187298,-126.649910684482,-126.628185219786,-126.625779213835,-126.641231484932],"lat":[49.605812210214,49.6013627471536,49.6134586948807,49.6420890670666,49.7184660231052,49.7357035725908,49.7504801844826,49.7628068451091,49.7829008400163,49.8377336057798,49.8723515270223,49.8436772095222,49.8084879991954,49.7458000085228,49.6751579159843,49.6267851114047,49.605812210214]}],[{"lng":[-124.153685003321,-124.13980927037,-124.362326368702,-124.457204302009,-124.493970050482,-124.517821369751,-124.630942101605,-124.649855066194,-124.623290123774,-124.547171346457,-124.421493241076,-124.309152538549,-124.153685003321],"lat":[49.5311601077569,49.5103410151658,49.5881901392293,49.6342008831719,49.6674564996749,49.6863200257851,49.7357035725908,49.7583573820488,49.7751005467499,49.7649162201896,49.727771443382,49.6673026910753,49.5311601077569]}],[{"lng":[-126.092063373163,-126.064031755883,-126.186814963692,-126.229628686028,-126.231435937073,-126.208545921552,-126.115293964869,-126.092063373163],"lat":[49.3539945736548,49.2636100487225,49.2781010160722,49.2956461827571,49.3390531668348,49.3797794867478,49.3650468201701,49.3539945736548]}],[{"lng":[-123.372359289922,-123.384812293327,-123.54103239206,-123.645627732964,-123.68923796412,-123.482326945484,-123.377918372166,-123.372359289922],"lat":[48.8861088136188,48.8752103757031,48.9459513451986,49.0386100401394,49.0950907551846,48.9547074490482,48.9082242929781,48.8861088136188]}],[{"lng":[-123.435382363616,-123.477262248026,-123.499613933449,-123.517521648975,-123.58234648055,-123.554677412112,-123.467857950792,-123.487545451543,-123.422742592626,-123.406801429908,-123.435382363616],"lat":[48.7544376660179,48.7287626162103,48.732168378059,48.7501529978857,48.9258024186487,48.9220780532722,48.8674100824368,48.8457230698908,48.7933512417211,48.7560636426424,48.7544376660179]}]],[[{"lng":[-88.948495061717,-89.2052235871354,-89.4636879524638,-89.7221413314637,-89.9805727378064,-90.3300368624655,-90.6795009871247,-91.0289870844409,-91.3784512091,-91.6838821289544,-91.9893350214659,-92.2947769276488,-92.6002078475032,-92.8552060261759,-93.1102042048486,-93.3652243561784,-93.6202225348512,-94.0036344147115,-94.3870243219148,-94.7704087359538,-95.1538261089785,-95.1547819195618,-95.1557102643238,-95.1566386090858,-95.157572447012,-95.158500791774,-95.159429136536,-95.1603629744622,-95.1552982770034,-95.1582426130532,-95.1620438827295,-95.3979203565712,-95.8242997673675,-96.2506846713281,-96.6770640821245,-97.1034489860851,-97.2257378091094,-97.5298283968814,-97.956213300842,-98.3825927116384,-98.8089776155989,-99.2353570263953,-99.6617419303559,-100.088121341152,-100.514506245113,-100.940885655909,-101.36727055987,-101.404217582764,-101.44401006475,-101.483802546737,-101.523589535559,-101.563382017546,-101.603174499533,-101.642961488355,-101.682753970342,-101.722546452329,-101.762333441151,-101.802125923138,-101.841918405125,-101.881710887111,-101.921497875934,-101.96129035792,-102.001104812564,-102.001132278386,-102.001181716864,-102.001209182685,-102.001236648507,-102.001286086985,-102.001313552807,-102.001341018628,-102.001390457106,-102.001417922928,-102.001445388749,-102.001494827228,-102.001522293049,-102.001544265706,-102.001599197349,-102.001621170006,-102.001648635827,-101.551731016125,-101.101807903259,-100.651884790392,-100.20196717069,-99.7520440578241,-99.302126438122,-98.8521758594343,-98.4022582397323,-97.9523351268659,-97.5023900413425,-97.0524669284762,-96.6025493087741,-96.1526261959078,-95.7027085762058,-95.2527854633394,-94.771886397143,-94.7858115685727,-94.7766379842383,-94.7882670130023,-94.8195560766958,-94.8702524897626,-94.9573521024602,-94.8466099107357,-94.7761710652752,-94.7437723823987,-94.7133841976457,-94.6733884685809,-94.6237302635616,-94.5791807013153,-94.5397013296921,-94.4192417303704,-94.2870542253278,-94.2808030043862,-94.3326365024573,-94.3322190219726,-94.2721457774935,-94.2089194567222,-94.1231876419284,-94.0557535573247,-93.7800351630178,-93.4861728471204,-93.3750461338969,-93.2781247434808,-93.1787534017995,-93.1545669995097,-93.1265079164081,-93.1002011527092,-92.9251504868519,-92.8417697463665,-92.7398385901344,-92.7016720847722,-92.4896469301997,-92.4493875292498,-92.4328036663125,-92.439779984938,-92.4783529844564,-92.5484787195533,-92.6141110462757,-92.6752444714594,-92.7380038732675,-92.8023892517002,-92.7981540220464,-92.725292691142,-92.6509811648697,-92.5103177073554,-92.4563144093969,-92.3033517570775,-92.2982870596186,-92.3721316669278,-92.3557016125901,-92.2490408419197,-92.0180203252945,-91.1112856715647,-90.8974477727824,-90.5921926341847,-90.3448134743573,-90.0751650265711,-89.7908388438499,-89.3423109947087,-89.2115517123768,-88.948495061717],"lat":[56.8513068713733,56.6948395802483,56.5352521718168,56.3756757497138,56.2160993276108,55.9913410182557,55.7665717225721,55.5417804542313,55.3170111585477,55.107930340025,54.8988495215023,54.6897577166511,54.4806768981285,54.2975128286413,54.1143158001685,53.9311297580243,53.7479327295515,53.5165496640843,53.2851665986171,53.0537615604928,52.8223565223686,52.3909673457561,51.9595122511724,51.5280681429172,51.0966240346621,50.6651799264069,50.2337577908088,49.8023356552108,49.3696720644872,49.2030973511016,48.9917643352274,48.9931815716096,48.9931815716096,48.9931815716096,48.9931815716096,48.9931815716096,48.9931815716096,48.9931815716096,48.9931815716096,48.9931815716096,48.9931595989525,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,49.4205497519749,49.8470390260567,50.2734733684957,50.699951656249,51.1264189576736,51.5528752727698,51.9793315878659,52.4057988892906,52.8322552043868,53.2587115194829,53.6852007935647,54.1116571086608,54.538113423757,54.9646026978388,55.3910370402778,55.8175263143596,56.079000933709,56.34046456673,56.6019172134224,56.8634028191003,57.1248884247784,57.3863630441278,57.6478486498058,57.9092793238411,58.1707649295191,58.4322505351971,58.6937251545466,58.9552107602246,59.2166963659026,59.478149012595,59.7396126456159,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0017684173351,59.9533406811128,59.4781270399379,59.2678596979325,59.1513277110652,59.0879695543515,59.0688533426847,59.0503523654165,59.020579415062,58.9754146184172,58.9033333168394,58.8700996729935,58.8757356595366,58.8684517237119,58.8483687151333,58.7455147073035,58.716005428834,58.6589314520475,58.3390974554717,58.2973713796613,58.3780439901606,58.6263460015857,58.7367036718111,58.7600276473102,58.772541075522,58.7444819924203,58.7410103126004,58.7564131452192,58.7256184663101,58.694560115516,58.564383108597,58.4898628420824,58.2245100487569,58.0758869962123,57.8440425049463,57.7777619848398,57.4685847269518,57.3848469307887,57.3203242232493,57.2750605496476,57.2052973633926,57.1109357875274,57.0390302672062,56.9895478434437,56.952644765864,56.9283100481389,56.9219489639118,56.9335285541973,56.9582807524071,57.0223090751622,57.036723138212,57.0458747498892,57.0227485283039,56.9751227940652,56.970574454048,57.0089716723096,57.0637495064305,57.2412116714032,57.2569221212213,57.224468506702,57.1490693339039,57.0519172305885,56.9813300696927,56.9154120984281,56.8838154175353,56.8513068713733]}]],[[{"lng":[-64.0309416209534,-64.0513542193883,-64.1595915282048,-64.2164677510775,-64.2728386028373,-64.3146525692761,-64.4040483246394,-64.4822380248878,-64.5363347066389,-64.6326848079706,-64.6420451598902,-64.5936723553105,-64.7785173330649,-64.8978947790251,-65.0572844335428,-65.2823393737686,-65.5450115029293,-65.8844780686133,-65.9556035596078,-66.10975273541,-66.0666313958744,-66.0265862283312,-66.064906542293,-66.0897356448027,-66.1827239295999,-66.1073577157874,-66.1437334495969,-66.2515532779287,-66.3519683208217,-66.4398369765174,-66.5109514811833,-66.7071673089808,-66.8724785945838,-66.9081841623521,-66.9186980787688,-66.9765740575391,-67.0840642960145,-67.1248345612417,-67.1709881274554,-67.2132305607075,-67.2495843218599,-67.2707220179787,-67.2906622042863,-67.3152935528821,-67.3669402833679,-67.3997784193862,-67.4526006870262,-67.4725408733338,-67.4619720252743,-67.4385162138327,-67.4279473657733,-67.4537542515233,-67.4772210492935,-67.4936565967955,-67.487789897353,-67.454918802349,-67.4244317406392,-67.4138628925797,-67.4326495143901,-67.4497004500206,-67.4609375,-67.480078125,-67.53271484375,-67.600439453125,-67.707763671875,-67.787060546875,-67.8006975757563,-67.7999114912907,-67.7916937175397,-67.7752581700378,-67.7741046055406,-67.7811248694803,-67.782289420306,-67.7776092443462,-67.7670513826153,-67.7846404946144,-67.7864752114813,-67.7899139323156,-67.7925176921805,-67.7958355634008,-67.7976922529248,-67.8003509444325,-67.8028558273405,-67.8067889329593,-67.9348345921407,-68.0967950475378,-68.235497445407,-68.3108856318766,-68.3580169813307,-68.376902480098,-68.4803827086549,-68.6685455576296,-68.828715241474,-68.8874151948851,-68.9371832631898,-69.0030902481258,-69.0485736482984,-69.0536603184143,-68.6284674311007,-68.4960162541731,-68.4296368571097,-68.3936786037849,-68.3792096090923,-68.3788141012647,-68.3785284567226,-68.378253798509,-68.3779132223241,-68.2524932956647,-68.1135162395819,-68.1135162395819,-68.0234173591918,-67.9134442104654,-67.73932188937,-67.6128912204846,-67.6094634859788,-67.5790423422402,-67.3648528809445,-67.3070538064741,-67.1872369073722,-67.0575762578947,-66.9518218593293,-66.9250262040102,-66.8426836715722,-66.7043767815306,-66.6315593959404,-66.428784730002,-66.3596148054884,-66.2102117236172,-65.8493877352435,-65.7557182980765,-65.6664543786558,-65.6072600404602,-65.483521022068,-65.3439397179153,-65.2281548013891,-65.0016826247811,-65.046385995627,-64.873966555456,-64.7032280235522,-64.7663005357239,-64.852158693296,-64.9121770061324,-65.086123545971,-65.3189018751633,-65.2602238944093,-65.1920647121217,-65.0424089446941,-64.9424333549428,-64.8314165050047,-64.8658806176476,-64.9057719765912,-64.882546878049,-64.8167058110842,-64.7258598603531,-64.6895060992007,-64.6413420348633,-64.64785692769,-64.5568571683593,-64.5415092673831,-64.2118425067605,-64.1449906975697,-63.9159147610967,-63.872628626633,-63.8319132930486,-64.0563969441901,-64.0309416209534],"lat":[46.0126675418964,45.9778628530687,45.9647891221012,45.9007937583319,45.8498611392014,45.835677789051,45.8269216852014,45.8063552781668,45.8666043039026,45.9466506936749,45.9133181728722,45.8136941456343,45.6384292463704,45.626003708787,45.5442434517618,45.4730850017817,45.3372829946481,45.2229043281756,45.2224648750338,45.3166177106567,45.3594534056501,45.4175930563055,45.4008498916043,45.375625281267,45.3352175648818,45.2569289876766,45.2276064767924,45.1890224909456,45.1332119419416,45.0959023702058,45.1433413368592,45.0833999283226,45.0672719980199,45.0976382101158,45.1456155068679,45.1571950971533,45.1439675575863,45.16944485348,45.1820022270059,45.1925381160797,45.2007778624878,45.1867043756228,45.1679177538124,45.1538332806188,45.1737844532549,45.2101601870644,45.2476784990425,45.2758913907437,45.308707554105,45.3403811392976,45.3779543829184,45.4212624900392,45.4459048249636,45.4740737713507,45.5010452079264,45.5139651302943,45.5304006777963,45.565589888123,45.6031082001011,45.6079392406868,45.641650390625,45.683056640625,45.642529296875,45.6373046875,45.69208984375,45.758984375,45.7555027669834,45.7697598177865,45.7955776898651,45.8178799368096,45.842522271734,45.8601443427187,45.8741738842695,45.8917959552543,45.926985165581,45.9528030376596,46.0421548477088,46.2093228228357,46.3373794683457,46.4984060858165,46.615630211382,46.7798538504591,46.9357388661713,47.0828128463911,47.1676163164229,47.2748428830133,47.3459464013507,47.3544717923009,47.3445291649685,47.3161514783391,47.2857962525718,47.2534415150094,47.2033218841913,47.2028274994068,47.2112540134001,47.2364456647517,47.2736563595305,47.2945853154071,47.4206973807647,47.480309199445,47.5281876192402,47.5631461166675,47.5889859614032,47.6918729282186,47.7651517396077,47.8384195646683,47.9293753786849,47.9293753786849,47.9293753786849,47.9985672758556,47.9985672758556,47.9985672758556,47.9985672758556,47.9985672758556,47.9688272844867,47.9395047736025,47.8548001805275,47.8872318223897,47.8940763050727,47.918575817726,47.8994486197307,47.9635099014714,47.9978971098144,48.0224405677819,48.0110697177388,48.0669681573711,48.0606620047868,47.9885916895375,47.9110282100162,47.8597440283724,47.6961575963508,47.6700101344159,47.6870169710021,47.7679422670579,47.8112723468359,47.8468460786616,47.793002082467,47.797242805285,47.7248429001795,47.6734708279073,47.5698587634079,47.3686331697943,47.2338089459011,47.1012039603739,47.0692447306391,47.0495792025452,47.0888003954476,47.0861746629256,47.0607962439887,46.9578323728734,46.8879373506759,46.8228323677236,46.6986978415039,46.671440760386,46.5123037914248,46.4255887002262,46.3559793225708,46.3114407466531,46.2403262419871,46.22023224708,46.1928872753337,46.1658169618011,46.1461734063643,46.1071609537042,46.0213467414462,46.0126675418964]}],[{"lng":[-64.4760856809031,-64.591299308345,-64.5407072653994,-64.5195695692806,-64.5001896857288,-64.4812822143044,-64.4760856809031],"lat":[47.9588846571543,47.9072049676829,47.9849771874465,48.0050821686822,48.0137613682321,48.0069388582062,47.9588846571543]}],[{"lng":[-64.5085612680794,-64.5339067280306,-64.6212700126133,-64.6646220650483,-64.6845732376844,-64.6604912055157,-64.6632817329659,-64.5910905681027,-64.5648662018679,-64.5085612680794],"lat":[47.8867374376052,47.8137772297439,47.7519132137121,47.7476065729228,47.7535941219793,47.7935733715513,47.8630179542785,47.872477183155,47.8662808938561,47.8867374376052]}],[{"lng":[-66.7624834732003,-66.8970440252084,-66.8447051560243,-66.8021660919016,-66.7454326912999,-66.7533648205088,-66.7624834732003],"lat":[44.6817727157362,44.628906502782,44.7639394669175,44.8053798981858,44.7914052882777,44.7097988398521,44.6817727157362]}]],[[{"lng":[-57.1001283682826,-57.1009743155805,-57.1010292472232,-57.5076222803117,-57.9142153134001,-58.3207973601599,-58.7274123659055,-59.1340383579795,-59.5406204047394,-59.9472134378278,-60.3538064709162,-60.7603995040046,-61.1669815507645,-61.5735745838529,-61.9801895895984,-62.3868045953439,-62.7933976284324,-63.1999906615208,-63.6065727082807,-63.7568986417495,-63.7833647072122,-63.7921977153617,-63.7855070412783,-63.7040104561382,-63.6642948784513,-63.6680192438278,-63.6837516663029,-63.7655888276279,-63.883955531362,-63.9354923985623,-63.9427873207156,-63.9511039714235,-64.0616703818912,-64.0661637902658,-64.0594181845397,-64.0176152044294,-63.9554655438555,-63.8985453756685,-63.8106767199728,-63.5225492675754,-63.4227604454094,-63.4096867144419,-63.5502458018351,-63.5634843277307,-63.5667912126225,-63.5486967295104,-63.55458540161,-63.6061881867816,-63.6716117732617,-63.7225443923921,-63.7989652937448,-63.9050602684952,-63.9174638334215,-63.9733732593824,-64.0078923036679,-64.1393656973551,-64.1784770269721,-64.1872111581646,-64.1834648201311,-64.1031877174594,-64.1306755114767,-64.1595695555477,-64.2596110632702,-64.2708281047137,-64.3214201476593,-64.3345707829266,-64.3260673646334,-64.2893730272962,-64.2905156054647,-64.5063420297135,-64.5980119550854,-64.6974162557524,-64.7023271446116,-64.6927360797926,-64.6389689878978,-64.6267192315712,-64.6579643499506,-64.7646416001137,-64.9321501514255,-65.0361796964096,-65.1371330694013,-65.2681560236181,-65.398047385995,-65.4521001224319,-65.4742705334339,-65.55164724537,-65.589143584691,-65.6311003734009,-65.6906352877813,-65.7805583869147,-65.805903846866,-65.8267998437568,-65.8829289962886,-65.9788396444786,-66.0357817853226,-66.0969646489847,-66.1485454614992,-66.2795354567305,-66.2960259358752,-66.3268206147843,-66.345453427995,-66.3788628530976,-66.4074382936408,-66.4392766737616,-66.4622161277617,-66.4634026512444,-66.4016814574837,-66.3942327267308,-66.4260930795087,-66.416589905318,-66.3663604112144,-66.3568023053811,-66.3413445411195,-66.3104729579106,-66.3002117270505,-66.3281389742095,-66.3898931269559,-66.428037659661,-66.4679509912617,-66.4899126620213,-66.5210259444582,-66.615277657038,-66.6313836146837,-66.6712969462844,-66.6883477281848,-66.8147783970702,-66.8558343068395,-66.8974834783502,-66.9510198573456,-67.0294732294789,-67.0680242563402,-67.0696941782789,-67.0536541386045,-67.0280230341111,-66.9703008639405,-66.9652361664816,-66.9379021810639,-66.9444390465476,-66.9803533545583,-67.0259026727021,-67.0760552625059,-67.1284820223183,-67.1649126877706,-67.4570940954008,-67.4735296429027,-67.4769573774085,-67.4557427769898,-67.470991801009,-67.5306255923464,-67.757251577554,-67.7614703277149,-67.7314117328183,-67.6810613891007,-67.5113226130944,-67.5079937555456,-67.515332623013,-67.5819427329759,-67.6097161715353,-67.6201312109951,-67.6014214934845,-67.5785259847986,-67.536514264446,-67.3145684551982,-67.2855206025276,-67.2360711377507,-67.1501800211929,-67.1468731363011,-67.1991790464996,-67.2045953064718,-67.2900249972307,-67.3720269534838,-67.4157745137464,-67.437087991122,-67.4264422387627,-67.4081500017368,-67.3386504873669,-67.3017474097873,-67.1667254319803,-66.867051348283,-66.7244488037807,-66.6507854708925,-66.6567290746349,-66.7103643305872,-66.6927971912451,-66.7474431894235,-66.719043530137,-66.7250640381792,-66.7973101346851,-66.7996172636794,-66.773986159186,-66.7334136478727,-66.6179143758886,-66.2397979063865,-66.1566313993077,-66.082836230477,-65.883643107644,-65.8681194254112,-65.7968291394886,-65.7416448112116,-65.6758037442468,-65.1217851684252,-64.8151896977451,-64.3410527167676,-63.9101798975967,-63.7512406825493,-63.6988249090654,-63.6579997121956,-63.5696586443725,-63.4914909167813,-63.4760441388483,-63.4801200667381,-63.5511576671042,-63.5340189945754,-63.5108927729901,-63.429275338236,-63.1613847030168,-63.1118253749544,-63.1513102397419,-63.1594730818502,-63.1510795268425,-63.1543534527486,-63.3045585366035,-63.3633903259571,-63.4151688923854,-63.5244169434279,-63.7005717353039,-63.7084049875559,-63.6984074285807,-63.6611198295021,-63.5864787133735,-63.5584635755861,-63.5783048849367,-63.836121056881,-63.8858891251857,-63.9683634935662,-64.0041459656344,-63.991720428051,-63.8993254049952,-63.9128166164473,-64.083654025308,-64.1152836651864,-64.1214799544853,-64.1187113996922,-64.1361027577775,-64.0526506061565,-63.9549272137568,-63.925571743887,-63.9549052410997,-64.1118669170092,-64.1155912823857,-64.1006608618942,-64.0303703318691,-64.003552703893,-63.9503788737396,-63.9033024559281,-63.8763529920095,-63.835758508039,-63.7918131938626,-63.7578544523328,-63.7600077727275,-63.7932853618876,-63.7998991316711,-63.7421000572006,-63.7615568450522,-63.7585026457169,-63.7437260338251,-63.6146916050747,-63.5981571806159,-63.6012553252653,-63.6176359411246,-63.7155680737666,-63.7934941021299,-63.8419108520237,-63.8719145152776,-63.9214628570115,-63.9613102706409,-63.985985564551,-64.0454655472887,-64.0787980680915,-64.1335759022124,-64.2265861596667,-64.386997542739,-64.4003788909057,-64.394589095763,-64.3713859698778,-64.0977604711586,-64.0226799018883,-63.8480082643657,-63.8371098264499,-63.8408232054979,-63.8823735000516,-64.0387419142197,-64.0624943565321,-64.0858732636739,-64.0849943573904,-64.0708110072399,-64.0314140330808,-63.9605082686572,-63.5531901378849,-63.5060148431165,-63.4967094228397,-63.5040482903072,-63.5352604497009,-63.5917960963888,-63.6659318414044,-63.7271366777235,-63.9906327815251,-64.1553288327297,-64.191781470839,-64.2355619900872,-64.294075175913,-64.6178642507646,-64.8086198732757,-64.8483134783056,-64.8511259784128,-64.8328337413869,-64.8016765136359,-64.7702556139998,-64.3935014492371,-64.3581804029678,-64.3243644837091,-64.3323735172178,-64.44169847256,-64.4778105344845,-64.5054850960871,-64.5194926649808,-64.51866869034,-64.5031340217786,-64.4257573098426,-64.3786699057026,-64.3852946618147,-64.4448185898666,-64.7146208462525,-64.775551024358,-64.8180241705095,-64.7908220210343,-64.7618510726635,-64.6655778756316,-64.6614689887561,-64.665039545533,-64.6823759719755,-64.8340422375268,-64.8681547876562,-64.8872709993229,-64.8803496123401,-64.7821647941416,-64.7105888636768,-64.6498674258136,-64.6223027274964,-64.6445280701411,-64.7893498530094,-64.8467863786379,-64.846687501681,-64.8158598637863,-64.6142717213307,-64.4994426153878,-64.4363481305591,-64.4195720068722,-64.5277104387318,-64.7133024868272,-64.76844286979,-64.7325834934221,-64.559153311025,-64.4077177583732,-64.2835173141822,-64.1828495857326,-64.1687870851962,-64.2263115014531,-64.1506816157555,-64.0560563680052,-63.9787016287262,-63.9695060717348,-63.9288346834646,-63.8412626586396,-63.7501859950091,-63.8503593386741,-63.9707145678747,-63.9454460122233,-63.7808598243042,-63.7585795500167,-63.7758720311452,-63.7519987392188,-63.6375102094608,-63.539885694018,-63.4151469197283,-63.5061906243732,-63.6455192429695,-63.7564152432936,-63.910487514796,-63.9711540210164,-63.9410295081485,-63.7936698833866,-63.5679008318054,-63.3989640577829,-63.3255314377942,-63.2484074114146,-63.2224906623791,-63.3037235756341,-63.3098759196188,-63.2794437895517,-63.2163932500371,-63.2219193732948,-63.2821244537164,-63.185334899243,-63.0503019351075,-63.0083451463976,-62.9260795182594,-62.8738834713464,-63.1023222007638,-63.2186674200457,-63.3899223093911,-63.4379325651288,-63.5370512212536,-63.47361616024,-63.2964945714521,-63.2099882204959,-63.1454984719421,-63.1195048186067,-63.1321061374468,-63.0756803540443,-62.8373759015944,-62.7373014348862,-62.6078824846368,-62.593852943086,-62.674338786,-62.8120524143002,-63.0628153633192,-63.1516727885839,-63.2615031150392,-63.2200407111138,-62.9809012976944,-62.8175345922437,-62.5880851206003,-62.4862528413251,-62.3056485863887,-62.2015201644478,-62.1174198194427,-61.9586453993235,-61.8990555533003,-61.9140409054344,-61.9677860246722,-61.9948892971904,-61.9312784549201,-61.9679398332718,-62.0839554626974,-62.1669022432053,-62.2536063480753,-62.3385636267068,-62.3771695852108,-62.4955582616019,-62.4549637776315,-62.3964945371198,-62.3031986351234,-62.1942032696374,-62.0880643495729,-61.9211270873454,-61.8511002292053,-61.8498148287657,-61.8858280137332,-61.938848035287,-61.977453993791,-61.9445389534728,-61.8608231299668,-61.7983438795366,-61.7163309369549,-61.6285281992305,-61.3337430317353,-61.3457620751626,-61.3904874186656,-61.372788443381,-61.3716238925554,-61.5316946994428,-62.0624881767223,-62.3661063523669,-62.3817398978851,-62.2958268086703,-62.3720279834521,-62.4602152426756,-62.4972611425263,-62.3955167538794,-62.1164859815165,-61.9916153712843,-61.8549234715387,-61.8133512043278,-61.7377542776159,-61.7600455382319,-61.8993961294852,-62.0096768954108,-61.9404300665974,-61.6924576450286,-61.5145889858997,-61.4252921074933,-61.4986917684964,-61.7071353799635,-61.7130789837058,-61.5585892317188,-61.4210733573323,-61.3647025055726,-61.3243936661443,-61.3011136359594,-61.4489017275345,-61.4495279482615,-61.3512881984202,-61.1878885339839,-61.1338907291897,-61.1229813049454,-61.0893411669434,-60.9957596204048,-60.892663913347,-60.831865571184,-60.7432388588188,-60.7366250890352,-60.6309695674266,-60.592572349165,-60.5621402190979,-60.475842608384,-60.4126162876128,-60.3410183844909,-60.3654080338588,-60.4082986604949,-60.3519717540494,-60.3083340570722,-60.1923623729607,-60.2240139854963,-60.3609585707984,-60.4331057903475,-60.4501016406052,-60.5205349929014,-60.6171267934611,-60.5565591641975,-60.3407547126058,-60.2125442584962,-59.930316464527,-59.8621023505967,-59.7587979032966,-59.6954946782255,-59.6890896486843,-59.6054507294781,-59.5177029233964,-59.4378652738665,-59.4858205979614,-59.741725148739,-59.8163992238533,-59.8377896055286,-59.7499099635044,-59.4285598535896,-59.3941726452466,-59.3241787460922,-59.2595791342529,-59.086313746784,-58.9971157453345,-58.9557961636801,-58.8858022645257,-58.7801577292457,-58.4998855017572,-58.3981301267818,-58.2228542411893,-58.1952565838866,-58.058465807184,-57.9624562820372,-57.9292995424911,-57.8268520288174,-57.7248988999282,-57.6266042184442,-57.4830019180443,-57.4045046005968,-57.4044496689541,-57.4853529923528,-57.5632240890733,-57.6992677954348,-57.8891225390054,-58.1513771876815,-58.1619240630838,-58.2197451102114,-58.3591176741217,-58.4351870129611,-58.5583877012545,-58.6332155849684,-58.7194582640395,-58.8408462081232,-58.9202224318543,-58.9784609594666,-59.0126504138958,-59.0388198484878,-59.2014175109404,-59.4965212819634,-59.6526809558892,-59.7494705103626,-59.8230349662939,-59.8733413646973,-60.0141641239755,-60.0565383931701,-60.0813455230227,-60.1005166663321,-60.1173037763475,-60.1449014336502,-60.2633230690271,-60.3954007107842,-60.3695388933914,-60.1602493346263,-60.1003079260898,-60.1571511899769,-60.2902725329457,-60.3057742425214,-60.2512051486429,-60.2726944072752,-60.3457205331078,-60.3375027593568,-60.3294607668625,-60.1483401544845,-59.9870828241143,-59.8817129470479,-59.8290554743361,-59.6210842749963,-59.498169231245,-59.3222671249255,-59.1294131136624,-58.9195742384702,-58.6520241794359,-58.3267189912452,-58.0880739626104,-57.9359902165745,-57.928266827608,-58.0648488640682,-58.1774257726595,-58.3174685026111,-58.3607766097319,-58.3561733380719,-58.309975826544,-58.1921035075944,-57.6149477238589,-57.4160732045537,-57.1988844755655,-57.1489625986611,-57.1349770024245,-57.1569496595127,-57.2439943405676,-57.4894838518854,-57.5240798004707,-57.5273317537198,-57.4201820914292,-57.3861574319281,-57.3317641193063,-57.2213844764238,-57.0121498493015,-56.8408620009705,-56.6965895345295,-56.5242909439724,-56.4649977288199,-56.4443324448285,-56.354024824196,-56.2701991374046,-56.1101612895028,-55.9660865769755,-55.9112538112119,-55.8593763678267,-55.8634083504024,-55.8547730961667,-55.8168922353467,-55.7979298322796,-55.8082130357968,-55.8923463397875,-55.8298670893573,-55.8579261724589,-55.872527003094,-55.8186830068994,-55.8028187484817,-55.8484449709253,-56.1670045533899,-56.2923695484066,-56.3249000672256,-56.2283961572943,-56.0525929279317,-55.8402052245172,-55.7464588830505,-55.7059742623655,-55.71620253424,-55.7771327123456,-55.8966859395624,-56.0117127994191,-56.0046376038367,-55.8336354000479,-55.7834937965726,-55.6910658145312,-55.6727955501623,-55.6952296330494,-56.0175025945618,-56.2825587570167,-56.5485817163834,-56.9760048283914,-57.0182472616435,-57.0955800282654,-57.1001283682826],"lat":[51.4433415771852,51.7106938823057,52.0011174773689,52.0010845183832,52.0010625457262,52.0010625457262,52.0010405730691,52.0010076140834,52.0010076140834,52.0009856414264,52.0009636687693,52.0009636687693,52.0009307097836,52.0009087371265,52.0009087371265,52.0008538054838,52.0008538054838,52.0008538054838,52.0008318328267,52.0105217746026,52.0204424292779,52.0440630356478,52.0507756823882,52.048369676437,52.0601250479792,52.0941826664659,52.1380291036854,52.2975286214886,52.3449236428278,52.3809368277954,52.4180925909315,52.4287932749334,52.4577532369757,52.4658941064268,52.485120181379,52.5254729661215,52.565606024293,52.5892486033199,52.6112542193937,52.6488054903575,52.6698882548336,52.6887188219582,52.7741485127171,52.7969890897602,52.8258721475027,52.9397124838766,52.9643108734868,53.0348540890684,53.1007391013474,53.1091875879978,53.1042986717956,53.0769207410638,53.0655938363348,52.8950640446734,52.8782220030153,52.8542388478035,52.8153252721003,52.6259649133143,52.5541582699501,52.37848687653,52.1866875528072,52.1480266626605,52.0995110358098,52.0832292969075,51.9296514101895,51.8419036041079,51.8114604877122,51.7690862185176,51.7287773790893,51.6088945620161,51.6003691710659,51.7204607283814,51.7454216668336,51.7626262573337,51.7905315318357,51.8253142480063,51.8485613192056,51.9054155694213,52.0550164052062,52.1134636730608,52.1546953640868,52.1852153847823,52.202958305381,52.2029143600668,52.1962456586405,52.0939519535665,52.0733635738748,52.0730999019898,52.0935894047245,52.1038945808989,52.063695604756,52.0523796863556,52.0542144032225,52.0807463866565,52.1092449228999,52.2095281298504,52.2298308649999,52.3008135337232,52.2945842854388,52.1886541056166,52.1548491726864,52.1645171418052,52.1836882851147,52.2291167536445,52.294529353796,52.3413311133939,52.3978338010962,52.4178289190464,52.6608684790989,52.688433177416,52.7414312263127,52.8062615510514,52.8400774703102,52.8610064261867,52.8968767888831,52.9338787434197,52.9919854350894,53.0108160022139,53.0186272818088,53.0050371933997,52.958499105687,52.9315496417683,52.9102581370499,52.7488250254229,52.7268853273204,52.6864995835923,52.6984417227197,52.6937615467599,52.6983098867772,52.7203704344937,52.7549224377649,52.8325847942431,52.9200249831256,52.9804827491037,53.072987635445,53.1829607841714,53.3288372545799,53.3551385251144,53.3839996101998,53.3931182628914,53.4174090353024,53.5028277397327,53.5430377022041,53.6012212981736,53.6133172459007,53.6352020123605,53.7304644671664,53.7964593427307,53.839371942024,54.0336541759978,54.060735475859,54.0999237097757,54.141342168387,54.2361761563796,54.2528094577954,54.2817254745234,54.3918194728638,54.4516400317864,54.5065387155212,54.5527911586919,54.5654583955032,54.5726654270281,54.5173932081228,54.5185577589485,54.5856622536958,54.6166656728473,54.6530414066567,54.6820892593273,54.6992718771703,54.8257794503556,54.9173504987706,54.9837738411482,55.0362225736177,55.0588653967471,55.071785319115,55.0745978192222,55.0637213539636,54.9374774526634,54.7809772025527,54.7479742716063,54.7985663145518,54.8467853105319,54.9482220819795,54.9980121229413,55.1291449404437,55.1925470424716,55.213190353806,55.2938300053196,55.3111993907479,55.3282501726483,55.3189008070573,55.2559601308282,55.0083722307584,54.9677557741309,54.9417071891529,54.9071881448673,54.898256259761,54.8050262757358,54.7688812548257,54.7449310585996,54.7191681181637,54.7338678257557,54.7263202180459,54.6340350582755,54.6121722644727,54.6151935048223,54.6443622071069,54.7762970265929,54.9177899519124,54.9618671020313,54.9872345346396,55.0862543138075,55.1403509955586,55.1676410356622,55.2098834689142,55.3074750253714,55.3398297629338,55.4044733200872,55.4404425597406,55.4840802567177,55.5135895351872,55.5421100440877,55.5646759629172,55.596008971925,55.7056964761092,55.8609113257802,55.8833563949958,55.9233795898819,55.9478021982355,55.9761688985363,55.9924945827528,56.0255744179991,56.0633234428766,56.0884162172713,56.0947113835271,56.0974579656631,56.1446332604315,56.2123310169202,56.2290192499786,56.2752497204922,56.288653041316,56.2994086569607,56.3343341954024,56.3945722348096,56.4297394724793,56.438880097828,56.4753656949229,56.5269904527516,56.6909064746295,56.7073969537742,56.7378840154841,56.7870807947045,56.8205671241069,56.8634577507431,56.889341540793,57.0230341728461,57.0831293899822,57.1298762179374,57.2276655283084,57.2407941909186,57.2790376005806,57.3187202192818,57.3957783276901,57.5131342891981,57.5529267711848,57.580875991001,57.6692939631239,57.6924531436948,57.7230610550187,57.7261372270111,57.6958589055435,57.7059663278041,57.7133491405857,57.7311579791557,57.783299094426,57.8016902084088,57.8031404037766,57.7852546609068,57.8125227283533,57.8754304455968,58.0175495916432,58.0988813818551,58.1214143416991,58.1443318230421,58.1692927614942,58.3591804640504,58.3987642057947,58.4597383292145,58.4825789062576,58.5051667977443,58.5552095242627,58.5447505394887,58.5548250027636,58.5993745650099,58.6211714408414,58.6416939025618,58.6663142648291,58.684221980356,58.7316060153667,58.7434163185515,58.7534468365123,58.7705745227126,58.7987874144138,58.8297908335652,58.8536201801774,58.8616841453287,58.8109822390977,58.7705745227126,58.7687398058457,58.785768615089,58.865990786118,58.9114961589477,58.9293709154889,58.9492232111681,58.9836873238109,59.0200191123062,59.0402779021416,59.0486934298063,58.9984968946883,59.0019246291941,59.0199641806635,59.04376056829,59.0910676990009,59.1161824460527,59.1660054460002,59.2257930459371,59.3558601895707,59.3986519392499,59.4564180547348,59.4750728406026,59.5147664456325,59.5279939851996,59.4662947640959,59.4883113664983,59.5402437415262,59.8155721211698,59.8640438027063,59.9055391656174,59.9215132873205,59.9327193424355,59.9429036689959,59.9857723229749,60.0087886812748,60.0354085553371,60.0496688097874,60.063775255638,60.0910103640988,60.1100826304514,60.1413167625022,60.1613448394381,60.1961165692802,60.2381832212755,60.2507625674585,60.2674288278599,60.3059798547211,60.2682637888292,60.2281087580006,60.1713643710703,60.0945259892329,60.0371663679042,60.0120955661666,59.9975496671742,60.0434175888458,60.0648079705211,60.0640828728372,59.9729293049069,59.8465315950071,59.7411836905977,59.7936104504101,59.8225484397953,59.7537190914665,59.6976228979204,59.6449434525514,59.5744002369698,59.5125911525807,59.4477937868277,59.4090559923812,59.3801949072958,59.3492464197871,59.3186494947918,59.2771541318808,59.2773628721231,59.3414461265208,59.3328658039279,59.194372146301,59.1152046628123,59.0789278059597,59.0634700416981,59.0655904031071,59.0538130589079,59.0274019250879,59.0269954309317,59.0470454805247,59.0796529036436,59.0815864974673,59.0683369852431,59.0571748754424,59.0344331753561,59.0264790734902,59.0031660843196,58.9279756517638,58.9110347331488,58.867364077186,58.8577510397099,58.8781636381449,58.8554219380586,58.7650483994549,58.6724446361567,58.5457283227291,58.5195259291514,58.4525532703466,58.3988081511089,58.3299238711374,58.330681927807,58.4411824203035,58.4669233880823,58.4604634268984,58.4417537093878,58.4108491671932,58.414782272812,58.4793818846513,58.4921919437337,58.496377734909,58.4739985836647,58.3191792418213,58.2004060439311,58.1270942735564,58.084159701606,58.0146711735646,58.0021467590244,58.0933003269547,58.1292695666081,58.1581086790363,58.1540766964607,57.972274931713,57.9546528607282,57.9641120896047,57.9117622340921,57.8613239997461,57.8250471428935,57.803349144019,57.7694453341319,57.66856886544,57.6119343417952,57.5619135879339,57.5365901006398,57.5287678347164,57.4845039170122,57.4779890241856,57.4892060656291,57.4619709571683,57.4481721285169,57.4406794524498,57.4545771580581,57.4528523044767,57.4207832114565,57.3813093329975,57.3704218814104,57.3478669489093,57.2743574246208,57.2479243181437,57.2281379404358,57.1975410154405,57.1862250970401,57.1962006833581,57.1831818840333,57.0105756762771,56.9215864150699,56.8529658069835,56.7758088216183,56.6808320113546,56.6545746861342,56.6990693167378,56.7669758134688,56.787696029103,56.832805894105,56.8361677106395,56.8184467626979,56.8017035979967,56.730028790575,56.6668244424609,56.5908100352643,56.5842951424376,56.5705292727719,56.5260126695112,56.510763645492,56.5054352761481,56.4538654499621,56.4235871284946,56.3970771177177,56.3903315119916,56.3606464522655,56.3275666170192,56.2887079729587,56.2309308711453,56.2078376085456,56.2218451774394,56.216022423311,56.0762104062588,56.0471735399168,56.0223664100642,55.9957025906877,55.9736859882854,55.9553937512594,55.9302570315505,55.8885529283972,55.8663605447381,55.8623505348195,55.9142060055476,55.9578766615104,55.9414411140084,55.8869818834154,55.8250189904267,55.8148346638663,55.7269879808277,55.8051227494333,55.7885553659888,55.7846552193556,55.7091022379579,55.649578309906,55.6123676151271,55.5569745466078,55.4809052077685,55.4443646790308,55.366284842068,55.2427765365752,55.1999408415818,55.128991131844,55.0602057288295,55.0674896646542,55.1939752651824,55.2364264386767,55.2594208243195,55.2948627202028,55.3095953867804,55.2691327387525,55.1963263394908,55.1733209675195,55.1973590543739,55.1759137410559,55.1301776553268,54.9425860954364,54.8672198816239,54.813936188185,54.8869842866747,55.0555035802126,55.0807172042213,55.1528094921276,55.1999408415818,55.1832416221948,55.1494476755931,55.0550860997279,54.9522540645552,54.8383587965386,54.7831195366188,54.7741217335412,54.8126727604024,54.8659015221986,54.882205233758,54.8757452725741,54.7731439503008,54.7186737333792,54.6737176769767,54.650360742492,54.6402862792171,54.5908807597543,54.5704132296766,54.5175030714082,54.440444963,54.3865460351626,54.3840960838973,54.3504339732382,54.3199908568425,54.2864495957974,54.2533258152369,54.2281121912282,54.102977909111,54.0495733660582,54.0394110121549,54.0445086685993,54.0330828869135,54.0102423098703,53.976294554669,53.9636273178577,53.9291082735722,53.8341534359655,53.8312310725728,53.8422943054167,53.8344390805077,53.8077532884741,53.7615777496032,53.733364857902,53.7010101203396,53.6342462017772,53.6101092379658,53.5961346280577,53.6100652926516,53.6533184681297,53.6074725191152,53.5299859438937,53.486963481315,53.4498077181789,53.3914703136098,53.3601043456164,53.3435699211575,53.3171038556948,53.289000827279,53.2774212369935,53.2661053185931,53.306567966621,53.3928106456922,53.4800640669893,53.5045416069856,53.5368084539196,53.5747662190394,53.6437493759678,53.7439556786185,53.8753192090203,53.9778985586365,54.0517926044241,54.0895196566445,54.0911676059261,54.1035711708524,54.1267742967375,54.1313226367548,54.1144696087681,54.1544708309972,54.1719171207252,54.2016571120941,54.228167122871,54.1911102366917,54.162765509048,53.9243621796411,53.8477215517175,53.7918560710708,53.7568756009864,53.7154791150322,53.6331036236086,53.611405624734,53.5998809660912,53.5832366783469,53.5605499099034,53.4690887247738,53.5285137758688,53.6725884883961,53.7394293112583,53.7576446439845,53.7664337068197,53.7650384430946,53.7183245741251,53.6244793557015,53.6001116789907,53.5875872644504,53.471132181883,53.3908221202257,53.3438775383567,53.3122478984783,53.2858147920012,53.2457476518009,53.2119427188707,53.1346648838915,53.0004339217398,52.8784307432576,52.8233892372517,52.7356963628127,52.6771502180012,52.6431695038144,52.6233281944637,52.5747796086274,52.5737908390584,52.5445232598169,52.5359649098811,52.5374151052489,52.5076201822373,52.4745733059767,52.4282769174918,52.3914837031977,52.3642485947369,52.3695989367378,52.3944719845617,52.370389952393,52.3103936122137,52.2799065505039,52.2416191955277,52.1901482462986,52.137798390786,51.9292888613476,51.7970464246623,51.6810088225796,51.4576897222638,51.4467802980195,51.44255056153,51.4433415771852]}],[{"lng":[-61.7436209770584,-61.6595426047105,-61.6374710706654,-61.7952677075442,-61.9754874409816,-62.0112259677355,-62.007237930474,-61.9832877342479,-61.9375077032046,-61.8930680042438,-61.8483426607407,-61.7436209770584],"lat":[57.5545747204665,57.5249665650401,57.4160810628396,57.4224421470666,57.495402354928,57.5484773081245,57.5576289198017,57.5667695451504,57.5541132946676,57.5731306293774,57.5793269186763,57.5545747204665]}],[{"lng":[-60.9944852062937,-60.9827298347515,-61.1370108464962,-61.1912942958326,-61.1958426358498,-61.1881961511832,-61.1575552808737,-61.0868912156781,-61.0485379427306,-60.966404150535,-60.9553409176911,-60.9944852062937],"lat":[56.0393183150078,56.0151374058822,56.0325507366246,56.0478766649436,56.063916704618,56.08895454737,56.1183869215396,56.1408319907552,56.1292304278126,56.0988532293882,56.0803961974341,56.0393183150078]}],[{"lng":[-55.3612452453089,-55.4089149248617,-55.4196046225351,-55.3998182448272,-55.3464906060742,-55.2740907009686,-55.2935694614772,-55.3612452453089],"lat":[51.8896501879605,51.8888262133197,51.9000322684347,51.938484418339,51.9828691856571,51.9951738736265,51.9299590273888,51.8896501879605]}],[{"lng":[-55.4587269384806,-55.5324452030115,-55.5834217674561,-55.6307838298097,-55.7307044879182,-55.941180570166,-56.0310926829709,-56.0439357010389,-56.0306532298291,-55.9999134825628,-55.9608460982599,-55.8735597179771,-55.8411061034579,-55.8150904774654,-55.7954798810142,-55.7853504860966,-55.7847022927125,-55.8000282210315,-55.871417383911,-55.9619996627571,-56.0780921964825,-56.1065467874117,-56.1212245223467,-56.1356385853965,-56.1957338025327,-56.382413497154,-56.4543519764607,-56.4547914296025,-56.48393815923,-56.5393312277493,-56.6939967609931,-56.7323500339405,-56.7495546244405,-56.747181577475,-56.7541029644578,-56.7895009150269,-56.8388515028469,-56.8486183489227,-56.8291835337281,-56.8092433474206,-56.8068922731122,-56.8221852424456,-56.7567836286226,-56.610643486329,-56.5009340094877,-56.4275892801273,-56.3763929891118,-56.3218238952333,-56.2470509431622,-56.1794081183162,-56.1484046991647,-56.12218033293,-56.1274208116455,-56.164159094297,-56.161291662547,-56.0750160244902,-55.9270191926727,-55.8733290050777,-55.7647291474193,-55.6744544857725,-55.5300391970603,-55.502913951885,-55.5269959840536,-55.5836854393412,-55.7176307569508,-56.0399806227631,-56.1401869254138,-56.1212245223467,-56.0516151446913,-55.9784901419018,-55.9018495139782,-55.8698133799436,-55.8823158218268,-55.8920387225883,-56.0873207124596,-56.0412220778885,-55.8152442860651,-55.6780909605206,-55.4897633166177,-55.3759449529009,-55.3791419745072,-55.3544996395828,-55.3553455868807,-55.3438538872236,-55.2899549593862,-55.2801881133105,-55.2830335724035,-55.2663673120021,-55.2295411387223,-55.2070081788783,-55.2002955321379,-55.2249708260479,-55.2593140890768,-55.3425025688126,-55.3319117480961,-55.3531812801575,-55.3347572071891,-55.2523377704512,-55.2473719499493,-55.2538319111332,-55.2445264908564,-55.1761365956694,-55.0631971382361,-55.0261732110425,-55.0103858569246,-55.0159119801823,-54.9826343910223,-54.9105421031159,-54.8436463486109,-54.7818921958646,-54.7176331602101,-54.6508692416477,-54.5790406256264,-54.502202243789,-54.4691773401854,-54.4806250945284,-54.465452974809,-54.4634644493425,-54.4482483843089,-54.3890760187704,-54.3561609784523,-54.3167310453076,-54.2707862193362,-53.9577307874722,-53.8624463600092,-53.7550110531765,-53.6194617315995,-53.5695618273522,-53.5600806258186,-53.5734180286712,-53.6711414210709,-53.7580542661832,-53.80931647517,-53.8249280480311,-53.8452307831806,-53.9032385978934,-54.1612854827372,-54.0995313299908,-53.9507105235325,-53.8528882541759,-53.8477686250743,-53.8868360093771,-53.9615100844913,-53.9695630633142,-53.9660034928659,-53.8861328843503,-53.7840808785042,-53.6980359533468,-53.7063306313976,-53.7745886906421,-53.7946387402351,-53.8855396226089,-54.0677588678413,-54.1144727368108,-54.1042334786077,-53.937010571838,-53.8527234592477,-53.7993408888519,-53.7388831228738,-53.6444226700517,-53.5520276469958,-53.4113367236601,-53.3610742705709,-53.2754468258982,-53.2202624976212,-53.1273730897809,-53.057269327341,-53.0426465240488,-53.0275842676149,-53.0207397849319,-53.0373291410335,-53.0602136633909,-53.1357226994744,-53.1821179649161,-53.2251184548377,-53.301187793677,-53.3343115742375,-53.4055249558603,-53.5311975680762,-53.6097498171665,-53.5601904891041,-53.5418433204354,-53.5694080187526,-53.7043091469455,-53.710153873731,-53.7582080747829,-53.8695764872344,-53.7935840526948,-53.6530249653017,-53.6382153944243,-53.6576282369617,-53.6950147129972,-53.8616663306826,-53.8636548561491,-53.8377381071136,-53.805339424237,-53.7651404480942,-53.6723499172108,-53.6037512817814,-53.5037537193731,-53.2827417480515,-53.0854382737281,-52.9210058944086,-52.8832788421882,-52.8659973473883,-52.8720178554305,-52.9549536496099,-52.9982617567307,-53.1108057063364,-53.1538611279007,-53.1755371541182,-53.1698242632753,-53.1576843702341,-53.1224402282646,-53.0568628331849,-52.9450329949346,-52.8732043789133,-52.8169214177819,-52.7824023734963,-52.744938993161,-52.7113977321158,-52.7032898216503,-52.6721765392134,-52.6536535892881,-52.6685071054797,-52.6836242935564,-52.9124255718157,-52.8881347994047,-52.8820923187054,-52.8892004732735,-52.961721227993,-53.0319458400469,-53.0697827555528,-53.114815716255,-53.167011763168,-53.2136926731519,-53.2548804188637,-53.2913110843159,-53.3230176284942,-53.3817505408909,-53.5361084569354,-53.5677820421281,-53.5897986445304,-53.61636358695,-53.5951709591885,-53.5813281852229,-53.6121777957747,-53.5796472769557,-53.5784607534729,-53.5973682248973,-53.6363806775574,-53.6953772618392,-53.7743360050856,-53.860018381401,-54.0095972445288,-54.0760205869064,-54.1023987617408,-54.1328089191509,-54.1737439793062,-54.1732715671788,-54.1552430020379,-54.0926868473078,-53.9704969012404,-53.8691040751069,-53.8495264376414,-53.8778711652851,-53.9008435782708,-53.9397461676455,-53.9889978785087,-54.0472913377636,-54.1918274760897,-54.2184143911665,-54.2338941280851,-54.4046875916316,-54.4345044873003,-54.4558948689756,-54.4881397432525,-54.5625501464817,-54.5424012199318,-54.4632337364431,-54.473934420445,-54.5744922856091,-54.6511548861898,-54.7446595284286,-54.8015027923158,-54.8566431752786,-55.0904322466969,-55.0992103232036,-55.1396729712316,-55.2549195576591,-55.3157178998221,-55.401268440195,-55.4792933455152,-55.5307093631015,-55.6523390064132,-55.7885255350458,-55.8446986328918,-55.8806129409024,-55.9499147013586,-55.9581764204237,-55.95450698669,-55.9192188994064,-55.8383705076504,-55.7718153293303,-55.6100855868326,-55.4915211291847,-55.4012135085523,-55.360904669124,-55.1908363032614,-54.9756360997397,-54.8695081660037,-54.7953614346596,-54.7846058190149,-54.8910084109645,-54.945940053685,-55.035006219192,-55.0745679882793,-55.1966041257471,-55.3662769837821,-55.3907764964354,-55.4126832355523,-55.434644906312,-55.4606385596473,-55.4986512564099,-55.5761378316314,-55.7746827610803,-55.811366112089,-55.86206801832,-56.0813771087172,-56.1272670030459,-56.0836732513829,-55.8670777841361,-55.8443910156925,-55.8579261724589,-55.9184388700797,-56.0201393134124,-56.0896498141109,-56.1214222762604,-56.1505690058879,-56.2212660300692,-56.2629701332226,-56.3257570008521,-56.4595485098621,-56.7223195159797,-56.7740980824081,-56.9524830989785,-57.4734328258824,-57.6598049033045,-57.8841127731893,-57.9255532044576,-58.2393117613484,-58.3332338840719,-58.3269277314876,-58.3368483861629,-58.428056885736,-58.5089052774919,-58.6131325763898,-58.9411513877308,-59.1169546170934,-59.2193032538102,-59.2597329428525,-59.320663120958,-59.3624221557542,-59.3620596069122,-59.3408669791506,-59.272059603479,-58.9608388884818,-58.7105922969045,-58.6049697342815,-58.5026540565504,-58.3355519993947,-58.3302346163793,-58.4922390170906,-58.6061342851072,-58.722556408689,-58.9437881065814,-59.1668215623551,-59.1677004686386,-59.0634182380981,-58.8417800460495,-58.8191921545628,-58.8870986512939,-58.90644557586,-58.8772549009184,-58.843406022674,-58.716458996347,-58.6873452257051,-58.6416091399761,-58.5455886285007,-58.4937331577726,-58.4036672363681,-58.35867822098,-58.3187319303937,-58.1861049722093,-58.0496547716917,-58.0055446625871,-57.9905153651388,-58.0405580916572,-58.0818447143259,-58.0989064825549,-58.0490615099503,-57.9906691737384,-57.980100325679,-58.0968630254457,-58.1909389567687,-58.2188991629135,-58.2133950123129,-58.1826992103607,-58.1073989145195,-58.0158278661044,-57.9612148269117,-57.8560536900876,-57.7913222423058,-57.7988478773585,-57.8974721486989,-57.9290688295917,-57.926146466199,-57.712517307659,-57.6079494325763,-57.4655556283163,-57.4325856563555,-57.360449423135,-57.3305556231665,-57.2374355024267,-57.1795814963135,-57.2641432671174,-57.2943996159279,-57.2979921453618,-57.2748988827621,-57.2421376510436,-57.1316481448756,-57.0532826633706,-57.0056569291319,-57.0127431110429,-57.0373085416675,-57.0359132779424,-56.9763563909048,-56.8251515511525,-56.8054970093871,-56.7502247904817,-56.6823951980505,-56.6190480276652,-56.5179408460739,-56.2073683244609,-56.0255665597132,-55.9021131858632,-55.8658363290106,-55.6904505801327,-55.6595680105952,-55.7006568793501,-55.6663905206211,-55.5216236693956,-55.4964539907011,-55.4532008152229,-55.4587269384806],"lat":[51.5365495885533,51.4369695066296,51.3885967020499,51.3728862522319,51.3586699430958,51.3430144249205,51.3283916216283,51.2618803886224,51.226899918538,51.1992473295925,51.1914250636691,51.2079045564852,51.2050700837208,51.1911394191269,51.1661784806747,51.1314506961469,51.0870659288287,51.033320809591,50.90738452549,50.8376762708778,50.7809318839475,50.7592778303871,50.7338005344933,50.6509636172709,50.5847709877927,50.4169767919387,50.3800297690449,50.3504875315898,50.2708366496452,50.2067533952475,50.0596794150277,50.007692108357,49.9665592942879,49.9084745752753,49.8828984024246,49.8337565548469,49.7877677835613,49.7653117280172,49.7246183670898,49.7104020579538,49.6733561581031,49.6134586948807,49.6515922412572,49.7877128519186,49.8696488902004,49.8973893697743,49.9336662266269,50.0138005070275,50.0900675997806,50.1149736065901,50.1003508032979,50.0628324913198,50.0151408391099,49.9572648603396,49.9401591467964,49.9826322929479,50.0177775579605,50.0131193546578,49.9604618819459,49.9665592942879,49.9971562192832,49.9831486503895,49.9367423986192,49.8924125629438,49.8290214472444,49.7046671944537,49.6191166540809,49.6217314002744,49.6584147512831,49.6781352110198,49.6808488341701,49.6701481501682,49.6459672410427,49.5802580100204,49.451970651611,49.4568485814846,49.5152738766821,49.4346012661828,49.4625065406848,49.4897416491456,49.4728996074875,49.4377103971608,49.380834174288,49.3728800724221,49.391919379789,49.4127494587086,49.5138236813143,49.5239311035749,49.508143749457,49.4820182601791,49.4085087358906,49.3346586354172,49.266971865257,49.1681058946887,49.1255778168945,49.0794352370093,49.0778861646846,49.1208756682776,49.1385526709051,49.1796305533314,49.1997904662099,49.2444389054131,49.2973490636814,49.3053580971901,49.2930094639065,49.2603580954735,49.2681034570971,49.3162675214344,49.3454142510618,49.3554887143368,49.3885575632545,49.4445329071867,49.4908073230144,49.5273368654235,49.5297978030174,49.4693290507107,49.4005546340247,49.3417448173281,49.3294401293587,49.3921281200313,49.4150456013743,49.4241203087518,49.4193082968494,49.4418412566934,49.426306588132,49.3853056100054,49.3216398360924,49.2641483788212,49.1916715694158,49.1411893897557,49.0775455884997,49.0354020322046,48.993390311852,48.9513785914993,48.9254398698067,48.8891630129541,48.7876932825208,48.7847709191281,48.8067875215305,48.8113358615477,48.7966581266128,48.7678300005131,48.7388810247994,48.7248844422343,48.7066910821652,48.6846744797629,48.6953971364219,48.6798185225464,48.6555277501354,48.5763163213325,48.5263944444281,48.4845585053322,48.4188492743099,48.3935807186585,48.3883622126001,48.4366251538943,48.4488529375639,48.4492374590629,48.4957975194328,48.5112223247087,48.4818009368676,48.5621549438391,48.572591955956,48.5633414673219,48.5778653936572,48.6325882961353,48.6590433752695,48.6566153966613,48.6347086575443,48.5716141727156,48.5158475690258,48.4803177825142,48.4018534240522,48.3743656300349,48.3640274948749,48.3681583544075,48.3559635297235,48.2943082539341,48.2318839351465,48.207703026021,48.1738321751195,48.1084305612965,48.0880728945043,48.0679239679545,48.0568607351105,48.042391740418,48.0196829993173,48.0097293856564,48.0257474526737,48.0146402745156,47.96865150323,47.9212125365766,47.7992533034086,47.7870365060676,47.7272708787877,47.6820511505002,47.6500919207654,47.6482572038986,47.6623087181065,47.7438602348893,47.9978421781717,48.0684842707102,48.1470914514432,48.1311722613828,48.1129789013138,48.093961566604,48.0293180094505,47.9759134663977,47.8119095538914,47.7345548146124,47.6529813251725,47.5120926479231,47.487801875512,47.4551175480934,47.4831216995523,47.5528079815075,47.6194180914703,47.7278861131862,47.7694364077399,47.7689749819411,47.7452994439286,47.6930045200587,47.6217691657787,47.5494022196588,47.4698172556854,47.4263004083222,47.1031924858404,47.0458658234973,47.0110831073267,46.9741360844329,46.8194156195464,46.7227249420298,46.6812295791188,46.6558072148677,46.6465017945909,46.6605093634846,46.6977200582635,46.7170450101725,46.7183413969407,46.7114090236294,46.6325052120257,46.6282864618648,46.6388882689098,46.6802517958783,46.8884866671031,46.9572940427748,47.0103580096428,47.0994022024927,47.133251080737,47.1460062081767,47.1376895574688,47.0929422413087,47.0118082050106,46.9394632315477,46.839619477739,46.8199869086307,46.8248977974899,46.8385867628558,46.8803897429661,46.9171829572603,46.9674673830066,47.0862186082397,47.2619778922882,47.38700231112,47.4403079772159,47.4635660347438,47.5093240931299,47.6446866471217,47.7562088681728,47.8056363602927,47.8598209326722,47.8467472017047,47.7716556461058,47.5559061261569,47.4623245796183,47.4275967950904,47.4038773117637,47.3751920079351,47.4251138848394,47.5362186254059,47.5470731180075,47.457754266944,47.4082169115386,47.3954507977704,47.3986368330482,47.3850137856535,47.1739444416643,47.103587993668,47.0459647004542,46.9417374015563,46.9057132302603,46.8992532690763,46.9172928205457,46.914007908311,46.8814334441778,46.8672171350418,46.8738309048253,46.887212252992,46.9276749010199,46.9564151364913,46.9732571781494,47.0168729024694,47.0716507365903,47.0921182666679,47.1196060606852,47.1606399977974,47.2214603126176,47.2585831167681,47.4489871767658,47.5161685758129,47.5708914782911,47.6403470473468,47.6647366967147,47.6294925547452,47.6208682868381,47.6338870861629,47.6575845968325,47.6500369891227,47.661067262981,47.6428848892405,47.5503800028992,47.5012821006356,47.4847476761768,47.4750357617438,47.4652139840254,47.4982938192717,47.5163773160552,47.5300662814212,47.4999417685533,47.5028421592889,47.5244852265208,47.5923367916091,47.7878275217228,47.8192044760447,47.7919144359412,47.7637015442399,47.7718643863482,47.7891788401337,47.7745011051988,47.671405398141,47.6584305441304,47.6545084248401,47.6169351812194,47.592292846295,47.5650028061914,47.5744620350679,47.6310965587127,47.6254166268554,47.6600125754408,47.6749210232751,47.6688236109331,47.6768546170989,47.7198770796775,47.730830449236,47.6833914825826,47.6525968036735,47.6262406014962,47.5804605704529,47.5707156970343,47.6025211181695,47.6341947033621,47.7369058889209,47.8656876321147,47.8889676622997,47.9336600468171,47.995568008163,48.1593851530841,48.325058987529,48.4113126529287,48.4420524001951,48.5136832623026,48.5220987899673,48.5130570415756,48.532854405612,48.540709630521,48.5217911727681,48.521769200111,48.5584855101054,48.6276774072761,48.7464286325092,48.7468461129939,48.6915519214315,48.6502103671201,48.6227225731027,48.6052982560318,48.5980692518498,48.622041420733,48.7494279002018,48.8968644292635,49.0032120895704,49.0843461258685,49.0965079915668,49.081346858176,49.0619120429815,48.987556571395,48.9812504188107,48.9879410928941,49.0097709277112,49.0447074524814,49.0774137525572,49.1799931021734,49.2094474490001,49.2296293345356,49.2300687876774,49.258754091506,49.3051054116335,49.3866459420878,49.4353812955094,49.4996952628066,49.5425089851429,49.5315226565988,49.4738004864281,49.4900053210307,49.5085502436131,49.6003849639132,49.6684123102582,49.7008439521204,50.0249076851856,50.1987992933815,50.4636906609082,50.5058012582177,50.5839360268233,50.6051835862276,50.6053923264699,50.6148405690179,50.6493596133034,50.6733977001579,50.6987102011235,50.7252751435431,50.7449186989799,50.7873918451314,50.857308839986,50.9396293997669,50.9677434145113,50.9956486890133,51.0108427813898,51.02797046759,51.1257158326468,51.1444804818001,51.2749101742756,51.3327861530459,51.362449240115,51.3992973860519,51.4886162371154,51.5683769823455,51.5639275192852,51.5082817652093,51.471323755987,51.5110173610168,51.5594341109106,51.5789238577478,51.5963811338044,51.5898222956636,51.5622795700035,51.5365495885533]}],[{"lng":[-55.5361366094023,-55.5696778704474,-55.6007581938987,-55.6293336344419,-55.633860001802,-55.6045045319322,-55.527204724296,-55.46929578654,-55.4727345073743,-55.5038148308256,-55.5361366094023],"lat":[50.7196940886427,50.7086857874415,50.7090483362835,50.7207817351686,50.7401835913774,50.7807011710481,50.8012456054255,50.7963786618805,50.7759221181314,50.7421281715297,50.7196940886427]}],[{"lng":[-54.0936975895339,-54.0199243933603,-53.9806482688151,-54.2383875364596,-54.2692371470114,-54.2861341203123,-54.2887708391628,-54.2776636610048,-54.2589539434942,-54.1993750837995,-54.1377198080101,-54.0936975895339],"lat":[49.7444596764405,49.6794975157592,49.6619853080599,49.5916508327207,49.5870036157465,49.5953751980971,49.6608427298914,49.7114897044796,49.7189823805467,49.688539264151,49.7511503505238,49.7444596764405]}],[{"lng":[-54.5544202633591,-54.7087012751038,-54.7438355537878,-54.7864954675245,-54.818509628902,-54.8635755485899,-54.8554017201531,-54.8130823826012,-54.7887916101902,-54.7826502525341,-54.764072370966,-54.7331128971288,-54.6187781759704,-54.5591663572901,-54.5376990713149,-54.5544202633591],"lat":[49.5888603052705,49.5306767093009,49.507781200615,49.4961576650154,49.5144499020413,49.5760941915022,49.5965617215799,49.5993522490301,49.5911894069218,49.5720622089265,49.5623502944935,49.5621415542512,49.6220829627878,49.6314982463501,49.6199735877073,49.5888603052705]}],[{"lng":[-54.2271265497019,-54.2760596570373,-54.3259595612846,-54.3201368071562,-54.2587012579377,-54.2273792352584,-54.2262476434184,-54.2149536976751,-54.1683716646481,-54.1281617021767,-54.1475415857285,-54.2271265497019],"lat":[47.4413406920991,47.4065360032714,47.4081180345817,47.4385281919918,47.4976675985447,47.539997922425,47.565519163633,47.5851077874271,47.6070914308438,47.6468070085307,47.5731217029855,47.4413406920991]}]],[[{"lng":[-64.3146525692761,-64.2728386028373,-64.2164677510775,-64.1595915282048,-64.0513542193883,-64.0309416209534,-63.8747050427278,-63.7028788642982,-63.5676920915631,-63.5092448237085,-63.358018011299,-63.3159184003181,-63.2928031650613,-63.2169096074787,-63.1079252283213,-62.9108085215831,-62.7006840018487,-62.7183390318191,-62.7501224802972,-62.5856571419921,-62.4830558197188,-62.4472733476506,-62.4218729560567,-62.2177249990503,-61.9555142956884,-61.9235770386107,-61.9116129268262,-61.8772476911403,-61.7765030583909,-61.6569058858599,-61.4922867389552,-61.4276651544588,-61.3505081690936,-61.2770865354334,-61.281964465307,-61.3761173009299,-61.4609976752616,-61.1067325250287,-61.070818217018,-61.0315201198158,-61.067687113383,-61.1010745658285,-61.1653006424973,-61.2837991821739,-61.3872574380736,-61.4978897665127,-61.5687405992935,-61.6474246843263,-61.719220341362,-61.7938944164762,-62.026804581611,-62.2649771981185,-62.5140043072275,-62.7680521684811,-63.0318009578392,-63.0892155108107,-63.1557267438166,-63.3063163491705,-63.3808256293566,-63.4568400365532,-63.5443351570784,-63.6040238800584,-63.5582548353437,-63.5448185555343,-63.5676920915631,-63.6097587435584,-63.7611393645675,-63.8206413199623,-63.8913163714865,-63.923693081706,-63.9997294615596,-64.0449272171901,-64.0446086136623,-64.100858615808,-64.167007299972,-64.2860661424044,-64.3385258612024,-64.312246563325,-64.2757060345873,-64.3345707829266,-64.3782084799037,-64.4688017450783,-64.578478262934,-64.6915715289669,-64.8256486825191,-64.8623430198564,-65.0868266709979,-65.1720915668286,-65.2349223797723,-65.3296025591653,-65.3443022667573,-65.3860832742105,-65.4285344477048,-65.4504411868218,-65.4816863052012,-65.5644463181239,-65.6619060386385,-65.7381511587345,-65.83530326205,-65.8869060472216,-65.9784001913368,-66.0021636199777,-66.0376384748466,-66.1257268571131,-66.1925457073183,-66.193084037417,-66.0995793951782,-65.8680205484543,-65.941936566899,-66.1463921411046,-66.1253093766285,-66.0906365237433,-66.0216973121291,-65.9170525327466,-65.7776799688362,-65.681824252289,-65.6157524724248,-65.5199956328344,-65.5022746848928,-65.5871550592245,-65.7282305040592,-65.6920525241635,-65.6567095052372,-64.9029375038268,-64.751260251947,-64.4488505724423,-64.4068937837324,-64.448158433744,-64.3307475405932,-64.3404374823691,-64.3588285963519,-64.3657060380205,-64.3542363110205,-64.23501267366,-64.1355094960361,-64.1827177497901,-64.0931901584843,-63.7483293054851,-63.4602567847304,-63.3680155702742,-63.6144608921753,-63.9064555322203,-64.0871696504421,-64.3364054997934,-64.6001872481372,-64.681112544193,-64.7466679666156,-64.8319548351034,-64.8731645534723,-64.9128801311592,-64.8273845224291,-64.5600871489512,-64.397093978671,-64.3511052073854,-64.3146525692761],"lat":[45.835677789051,45.8498611392014,45.9007937583319,45.9647891221012,45.9778628530687,46.0126675418964,45.9592080672008,45.8580239813097,45.8779531812887,45.8747232006967,45.811266167026,45.779867240047,45.7519399928879,45.7579605009301,45.7824270545978,45.7763845738986,45.7405691428448,45.6860000489663,45.6482510240888,45.6606765616721,45.6218179176117,45.6405496077793,45.6646536126051,45.7308572284118,45.8681533762274,45.8511575259697,45.7991152876563,45.7142129406675,45.6556118642133,45.6421755844039,45.6870327638494,45.6482729967458,45.5736758259314,45.4760403241601,45.4410488677471,45.41061673768,45.3666933961607,45.3486538446913,45.330152867423,45.2917556491614,45.2528420734582,45.2334621899064,45.2561050130358,45.2354836743586,45.1850454400126,45.1570083295681,45.1538332806188,45.1305312777768,45.0944851338236,45.0844326432058,44.9944655987582,44.9364797567025,44.8436452805049,44.7851211083505,44.7147866330112,44.7085354120696,44.7113259395198,44.6425954681479,44.6519008884248,44.6399587492973,44.6550539647169,44.6832119247754,44.6106142657561,44.5437624565652,44.5144399456811,44.4799758330382,44.4864028352365,44.5106716349904,44.546322271116,44.6038357010443,44.6449245697993,44.5878725656698,44.5453994195183,44.4874685091053,44.586686042187,44.5503322810346,44.4448635270113,44.4147609868005,44.3340883763012,44.2919777789917,44.3035463829486,44.1851577065574,44.142058339679,44.0213405616364,43.9293630190652,43.8678615518754,43.7271816148683,43.7313893787007,43.7267421617265,43.6681081262867,43.5496205729386,43.565276091114,43.561419889795,43.5242421540017,43.51806783736,43.5532570476867,43.5340309727345,43.5607277510967,43.7343886463932,43.7952089612133,43.8148415303216,43.7781032476701,43.7421669670024,43.8138417744241,44.0796889525341,44.1438710838887,44.3674978014038,44.5688002993172,44.5755239323862,44.4359426282335,44.469714602178,44.5049367714904,44.5617031310777,44.6151076741306,44.6462209565675,44.6509231051843,44.6803994246681,44.732672375881,44.7603908827977,44.7285085573627,44.6971206167123,44.7383083624241,44.7603139784979,45.1208083770153,45.1802444144388,45.2560500813931,45.3057082864124,45.3374368032478,45.3093227885034,45.2681899744343,45.2382522291517,45.1872646783785,45.1382217077577,45.1143044705172,45.0230630119585,45.147010770593,45.2170815740472,45.3108828471567,45.3211111190312,45.364781774994,45.3941262585353,45.3781631231607,45.4108694232365,45.3895339732039,45.4100454485957,45.3829641487345,45.3243630722803,45.3502578486587,45.3545974484337,45.3748013066262,45.4755459393756,45.6254873513454,45.7558621121782,45.7831960975959,45.835677789051]}],[{"lng":[-61.105183452704,-61.0713345744596,-60.9365542958807,-60.8652420373009,-60.8683951135931,-60.9842789070762,-61.0375626005151,-60.9706119143674,-60.9715237796365,-61.0519546909078,-61.0921097217365,-61.0590298864902,-60.9303799792389,-60.877601656913,-60.8061026307481,-60.7379104894749,-60.6990518454144,-60.4723709285641,-60.4605935843648,-60.7048965721999,-60.7332852451578,-60.5731924656132,-60.5857498391392,-60.5049344063688,-60.430875565653,-60.3765152120169,-60.2979409902695,-60.2438333221898,-60.2264529504331,-60.0924527011807,-59.9613967879782,-59.8650466866465,-59.8499844302126,-59.8487649477442,-59.8809109450642,-59.9340408299034,-59.8280227594529,-59.8421841369462,-60.0158120732571,-60.1144693035831,-60.2050845414148,-60.3861062768359,-60.6729593151222,-60.7637283615535,-60.871603121528,-60.978620947876,-61.0837051804003,-61.1864383386161,-61.2363052838778,-61.2836673462313,-61.3234048965753,-61.4083402025497,-61.4497806338181,-61.4953079793048,-61.4806082717128,-61.408647819749,-61.3022012824852,-61.2405240340387,-60.9824991218521,-60.9319839832063,-60.8701529261601,-60.7596963789778,-60.6166543813337,-60.5710501315472,-60.4890701479511,-60.4082217561951,-60.4313479777804,-60.4254483193523,-60.3317678958568,-60.3329214603539,-60.3840847923837,-60.4824014465248,-60.5076919748334,-60.4945413395661,-60.5344107258526,-60.576861899347,-60.7448099038006,-60.8305691844158,-60.9121976054984,-61.105183452704],"lat":[45.9447390725083,45.9370925878416,45.9855642693781,45.9834988396118,45.9486392191414,45.9106814540216,45.8822158767638,45.8558047429438,45.8380068907024,45.7950393597664,45.7483694361111,45.703358448066,45.7476992700699,45.7481167505546,45.7380642599367,45.7514236354464,45.7733303745633,45.9465188577324,45.9686892687344,45.9329067966663,45.9565713483503,46.0614248679751,46.1166641278948,46.2038516312207,46.2556301976491,46.2845681870342,46.3112539790678,46.2700991923416,46.19555695317,46.2060159379439,46.1909756541671,46.1595108092168,46.1414163261047,46.1129507488469,46.0616336082174,46.0194131476225,45.9651516709432,45.9415420509019,45.8804580641967,45.8189126516927,45.742997121453,45.65465605363,45.5908144984602,45.5908144984602,45.610677780468,45.6061623994363,45.5823879844669,45.5850247033175,45.5724893024487,45.5738625935167,45.5985049284411,45.6690701166798,45.7162234387911,45.9414321876165,46.0597769186935,46.1703653018184,46.2438418671213,46.3025528068609,46.6504898318524,46.7294266024417,46.7967837827456,46.8633938927084,46.9757840337145,46.9988113783429,47.0097098162587,47.0035354996169,46.9629190429894,46.9231814926454,46.7678457933604,46.7369961828086,46.6133340687163,46.4135257114848,46.3033767815017,46.2702749735983,46.2145413288941,46.1721670596996,46.0926919590116,46.0741360501006,46.044582826317,45.9447390725083]}],[{"lng":[-60.9615701659756,-61.0028897476299,-61.0125247577631,-61.076168559019,-61.0817496139194,-61.0259830102296,-60.9124502910549,-60.9530228023683,-60.9615701659756],"lat":[45.4899380297684,45.4817202560174,45.4960354421103,45.537322064779,45.5577895948567,45.5773232870081,45.5672927690473,45.515514202619,45.4899380297684]}],[{"lng":[-66.2737786205734,-66.3241069916339,-66.3119451259355,-66.2504985903884,-66.2103765185454,-66.2737786205734],"lat":[44.292307368848,44.257293939778,44.2915822711641,44.3790004873894,44.3920192867142,44.292307368848]}],[{"lng":[-59.7875820840821,-59.9222854583612,-60.0377517713597,-60.1142825359979,-60.1174575849471,-59.9360293553699,-59.8663650460718,-59.7271243181039,-59.7875820840821],"lat":[43.9395912909398,43.9038857231715,43.906621318979,43.939129865141,43.9533681469341,43.9395912909398,43.9471608713067,44.0028395843682,43.9395912909398]}]],[[{"lng":[-95.1552982770034,-95.1603629744622,-95.159429136536,-95.158500791774,-95.157572447012,-95.1566386090858,-95.1557102643238,-95.1547819195618,-95.1538261089785,-94.7704087359538,-94.3870243219148,-94.0036344147115,-93.6202225348512,-93.3652243561784,-93.1102042048486,-92.8552060261759,-92.6002078475032,-92.2947769276488,-91.9893350214659,-91.6838821289544,-91.3784512091,-91.0289870844409,-90.6795009871247,-90.3300368624655,-89.9805727378064,-89.7221413314637,-89.4636879524638,-89.2052235871354,-88.948495061717,-88.8264808969063,-88.679901301471,-88.4470570543074,-88.2713527019017,-88.0750819424615,-87.87814101698,-87.5608778212836,-87.4824024764931,-87.2868678010653,-86.9193970839224,-86.3769471120577,-86.1386646322649,-85.984482497477,-85.8305200892601,-85.6766785306571,-85.5593225691491,-85.4784412184075,-85.4072607957703,-85.2826978027373,-85.2179993139411,-85.2120337375417,-85.3620190948257,-85.3652710480747,-85.2135828098664,-85.1288562441344,-85.0609497474033,-84.9199292342113,-84.7057837182298,-84.51796144544,-84.3564734021703,-84.2189355551268,-84.1053479043094,-84.0229833992143,-83.9717651355417,-83.9106042445367,-83.6676525751126,-83.569467756914,-83.2143456730547,-82.9862694924793,-82.9470263269198,-82.8677489801456,-82.8006774443839,-82.6875072740512,-82.5774352483679,-82.3932604366547,-82.3082482263805,-82.2266088189693,-82.2193908011158,-82.3706285998538,-82.4180675665072,-82.4241649788492,-82.3941393429382,-82.2635558418631,-82.2399132628362,-82.1626244415285,-82.1414428000955,-82.1500231226884,-82.1906066203303,-82.1803563757986,-82.1461998803551,-82.1591637480371,-82.2192699515018,-82.2599083807864,-82.2916149249647,-82.291559993322,-82.2604467108851,-82.2027025680573,-82.1079784433501,-82.0200218970261,-81.859291910426,-81.7423424430741,-81.5994103087154,-81.5716808154701,-81.6115282290996,-81.6612413657616,-81.7763451299181,-81.827892983447,-81.8145336079374,-81.6479808672089,-81.5494884318111,-81.4661900887897,-81.3980748518163,-81.2850584900832,-81.1271849489046,-80.9684874330851,-80.7055296593822,-80.6579368841292,-80.5880418619317,-80.4958555791182,-80.447592637824,-80.4432969833632,-80.4954930302762,-80.6726915233639,-80.851230348534,-80.7949803463882,-80.6772398633812,-80.4783433714189,-80.3679527422078,-80.2656590371337,-80.1035557594656,-79.9604148848646,-79.8362364133307,-79.6515122851903,-79.5195554930472,-79.5193797117904,-79.518885327006,-79.51840192855,-79.5179075437655,-79.517413158981,-79.5169187741966,-79.5164353757406,-79.5159409909561,-79.5154466061717,-79.5149632077157,-79.5144688229312,-79.5139744381467,-79.5134580807052,-79.5129746822492,-79.5132493404628,-79.5592930433911,-79.5467136972081,-79.4792246809618,-79.4419370818831,-79.4349168179434,-79.3649448914461,-79.1655540146993,-79.0145798878464,-78.9031675300807,-78.7809995566704,-78.4604514487393,-77.6826743194601,-77.3730026767876,-77.3713437411775,-77.292747546773,-77.2824643432558,-77.2667978387519,-77.2332346050497,-77.1006845511652,-77.0308444606104,-76.9767477788592,-76.9409323478055,-76.9255295151867,-76.9128952373609,-76.8902084689174,-76.8716086146922,-76.82370822224,-76.7538132000424,-76.6665817514023,-76.5531259365275,-76.4134457554178,-76.2699423319749,-76.1226376388556,-76.0006234740449,-75.903965755514,-75.6261324929623,-75.1671346727186,-74.8641866631152,-74.7172115598523,-74.5772237615434,-74.3965645749643,-74.4150106205898,-74.4530672626666,-74.4780611601044,-74.4129451908235,-74.3404464087611,-74.3582991926452,-74.7088729364873,-74.7624642471254,-74.8566390554054,-74.9961434552583,-75.1793844290453,-75.4012533339933,-75.7919601360069,-75.7979604497594,-75.805126953125,-75.831396484375,-75.9416015625,-76.0572265625,-76.34072265625,-76.4078125,-76.490478515625,-76.5888671875,-76.6876953125,-76.78701171875,-76.99150390625,-77.04130859375,-77.062255859375,-77.0552734375,-77.020947265625,-76.896044921875,-76.9130859375,-76.95107421875,-77.010009765625,-77.03310546875,-77.020458984375,-77.05869140625,-77.14775390625,-77.2033203125,-77.225341796875,-77.25078125,-77.279638671875,-77.32568359375,-77.398095703125,-77.547216796875,-77.584375,-77.5857421875,-77.445849609375,-77.380712890625,-77.33271484375,-77.31396484375,-77.33134765625,-77.331640625,-77.29599609375,-77.274853515625,-77.27294921875,-77.29033203125,-77.28984375,-77.271484375,-77.115283203125,-77.083203125,-77.1169921875,-77.102734375,-77.040234375,-76.9693359375,-76.88994140625,-76.86298828125,-76.888427734375,-76.93017578125,-77.0240234375,-77.043798828125,-77.042333984375,-77.0130859375,-76.910009765625,-76.8703125,-76.8791015625,-76.934228515625,-77.0544921875,-77.151171875,-77.207958984375,-77.21923828125,-77.18515625,-77.1900390625,-77.2671875,-77.289404296875,-77.314794921875,-77.310595703125,-77.2755859375,-77.258984375,-77.32451171875,-77.420654296875,-77.502880859375,-77.55400390625,-77.567626953125,-77.54375,-77.5490234375,-77.583447265625,-77.681787109375,-77.70869140625,-77.72646484375,-77.829052734375,-77.88076171875,-77.999853515625,-78.186328125,-78.3349609375,-78.4458984375,-78.52783203125,-78.580908203125,-78.6693359375,-78.79306640625,-78.988671875,-79.06064453125,-79.16533203125,-79.302783203125,-79.396337890625,-79.44599609375,-79.480126953125,-79.498828125,-79.56904296875,-79.5962890625,-79.610107421875,-79.6564453125,-79.774169921875,-79.773046875,-79.72783203125,-79.638427734375,-79.527880859375,-79.40146484375,-79.337548828125,-79.291357421875,-79.21337890625,-79.0613935638494,-79.0592283270496,-79.0660508370754,-79.0660483123406,-79.066064453125,-79.073193359375,-79.04287109375,-79.034521484375,-79.039697265625,-79.010546875,-78.9470703125,-78.92236328125,-78.93642578125,-79.05693359375,-79.121875,-79.175927734375,-79.26396484375,-79.3859375,-79.59384765625,-79.661474609375,-79.698681640625,-79.74658203125,-79.9228515625,-80.099462890625,-80.220458984375,-80.28583984375,-80.325830078125,-80.36474609375,-80.395556640625,-80.451220703125,-80.446044921875,-80.3677734375,-80.304931640625,-80.104833984375,-80.062109375,-80.09384765625,-80.396630859375,-80.473046875,-80.6166015625,-80.827197265625,-81.03916015625,-81.252490234375,-81.40439453125,-81.494921875,-81.729931640625,-81.809033203125,-81.8408203125,-81.866796875,-81.88701171875,-81.88671875,-81.86591796875,-81.874462890625,-81.929345703125,-81.937890625,-82.044384765625,-82.16015625,-82.311376953125,-82.4119140625,-82.461767578125,-82.48984375,-82.49599609375,-82.5115234375,-82.567236328125,-82.60419921875,-82.6451171875,-82.6900390625,-82.833837890625,-82.93662109375,-83.06123046875,-83.107861328125,-83.117236328125,-83.104541015625,-83.07646484375,-83.0466796875,-83.013623046875,-82.97060546875,-82.867333984375,-82.589501953125,-82.51572265625,-82.461865234375,-82.427978515625,-82.413525390625,-82.41845703125,-82.43212890625,-82.45302734375,-82.481103515625,-82.613037109375,-82.633935546875,-82.6451171875,-82.6451155228481,-82.5453222100335,-82.4883471102038,-82.4172326055379,-82.4172314415557,-82.31201171875,-82.1810546875,-82.0880859375,-82.033056640625,-81.97412109375,-81.911376953125,-81.848193359375,-81.784521484375,-81.739404296875,-81.71279296875,-81.70517578125,-81.71650390625,-81.718359375,-81.731982421875,-81.70810546875,-81.646630859375,-81.608935546875,-81.594970703125,-81.56494140625,-81.51884765625,-81.47431640625,-81.43134765625,-81.402197265625,-81.386767578125,-81.351904296875,-81.297509765625,-81.277734375,-81.280712890625,-81.278173828125,-81.300048828125,-81.34111328125,-81.358349609375,-81.355224609375,-81.376953125,-81.415478515625,-81.451171875,-81.49931640625,-81.541455078125,-81.57744140625,-81.59033203125,-81.62880859375,-81.683154296875,-81.7060546875,-81.69736328125,-81.595361328125,-81.39990234375,-81.296484375,-81.285205078125,-81.3158203125,-81.312548828125,-81.2818359375,-81.26416015625,-81.259521484375,-81.207373046875,-81.193310546875,-81.17998046875,-81.137939453125,-81.125,-81.14111328125,-81.13359375,-81.0740234375,-81.03203125,-81.01201171875,-80.97275390625,-80.966259765625,-80.992578125,-81.00595703125,-81.00634765625,-81.03642578125,-81.1275390625,-81.13017578125,-81.11025390625,-81.02197265625,-80.9634765625,-80.91943359375,-80.895361328125,-80.891162109375,-80.901171875,-80.925390625,-80.90966796875,-80.854052734375,-80.78984375,-80.670166015625,-80.64951171875,-80.59287109375,-80.46025390625,-80.2259765625,-80.084814453125,-80.036962890625,-80.0078125,-79.9974609375,-80.021142578125,-80.07890625,-80.10791015625,-80.108203125,-80.06474609375,-79.99677734375,-79.92958984375,-79.92041015625,-79.935302734375,-79.924951171875,-79.89130859375,-79.87626953125,-79.826953125,-79.7173828125,-79.6779296875,-79.70859375,-79.71337890625,-79.6923828125,-79.695947265625,-79.732470703125,-79.732763671875,-79.749609375,-79.77158203125,-79.7845703125,-79.799951171875,-79.828369140625,-79.902392578125,-79.92890625,-79.931787109375,-79.94609375,-79.971875,-80.02158203125,-80.0859375,-80.083349609375,-80.02265625,-80.006494140625,-80.015380859375,-80.049365234375,-80.07490234375,-80.0921875,-80.107568359375,-80.12109375,-80.111474609375,-80.04970703125,-80.04111328125,-80.057080078125,-80.091650390625,-80.14482421875,-80.176123046875,-80.185498046875,-80.16767578125,-80.177294921875,-80.270703125,-80.302490234375,-80.349609375,-80.38486328125,-80.391748046875,-80.388525390625,-80.414013671875,-80.4294921875,-80.42705078125,-80.444287109375,-80.485546875,-80.510693359375,-80.519921875,-80.5455078125,-80.59267578125,-80.628857421875,-80.641015625,-80.643115234375,-80.65615234375,-80.696435546875,-80.71337890625,-80.731591796875,-80.78662109375,-80.80517578125,-80.8177734375,-80.8904296875,-80.96796875,-81.097021484375,-81.16025390625,-81.15751953125,-81.16201171875,-81.173681640625,-81.19130859375,-81.19697265625,-81.23681640625,-81.303515625,-81.4494140625,-81.502392578125,-81.596728515625,-81.62705078125,-81.594970703125,-81.597265625,-81.623828125,-81.6294921875,-81.640234375,-81.541650390625,-81.54453125,-81.61533203125,-81.64619140625,-81.66171875,-81.688671875,-81.72705078125,-81.7501953125,-81.765185546875,-81.765771484375,-81.769287109375,-81.8166015625,-82.011767578125,-82.1158203125,-82.19599609375,-82.317724609375,-82.350048828125,-82.59765625,-82.638134765625,-82.663623046875,-82.719140625,-82.8947265625,-83.02548828125,-83.459130859375,-83.571728515625,-83.5927734375,-83.66064453125,-83.842724609375,-83.863134765625,-83.88173828125,-83.934228515625,-84.033642578125,-84.091845703125,-84.1087890625,-84.10615234375,-84.083837890625,-84.087109375,-84.11826171875,-84.165380859375,-84.23564453125,-84.3439453125,-84.432421875,-84.45888671875,-84.475439453125,-84.500537109375,-84.56845703125,-84.5685546875,-84.547607421875,-84.465966796875,-84.438134765625,-84.464013671875,-84.4880859375,-84.51025390625,-84.5275390625,-84.53994140625,-84.56103515625,-84.553466796875,-84.527734375,-84.4845703125,-84.407763671875,-84.38203125,-84.367431640625,-84.385009765625,-84.434765625,-84.48798828125,-84.544580078125,-84.582080078125,-84.600341796875,-84.62490234375,-84.6556640625,-84.750390625,-84.775146484375,-84.7716796875,-84.7123046875,-84.71953125,-84.68818359375,-84.61826171875,-84.5943359375,-84.616455078125,-84.641162109375,-84.66845703125,-84.693310546875,-84.715625,-84.771337890625,-84.86044921875,-84.93583984375,-84.997607421875,-85.0142578125,-84.98583984375,-84.9748046875,-84.981103515625,-84.960986328125,-84.9224609375,-84.920263671875,-84.915185546875,-84.8673828125,-84.847265625,-84.854736328125,-84.96357421875,-85.173828125,-85.34248046875,-85.46953125,-85.614599609375,-85.777685546875,-85.913623046875,-86.022412109375,-86.104150390625,-86.158935546875,-86.181005859375,-86.191357421875,-86.21552734375,-86.23740234375,-86.256982421875,-86.282568359375,-86.3142578125,-86.324951171875,-86.37099609375,-86.390185546875,-86.396142578125,-86.432421875,-86.45927734375,-86.58095703125,-86.621435546875,-86.64287109375,-86.692529296875,-86.7515625,-86.819873046875,-86.887646484375,-87.008544921875,-87.084716796875,-87.2236328125,-87.31142578125,-87.407080078125,-87.463720703125,-87.51728515625,-87.552001953125,-87.5677734375,-87.5916015625,-87.68525390625,-87.7609375,-87.965625,-88.022509765625,-88.06982421875,-88.12822265625,-88.16064453125,-88.200439453125,-88.24755859375,-88.26357421875,-88.2486328125,-88.24658203125,-88.22431640625,-88.147216796875,-88.1087890625,-88.1091796875,-88.138427734375,-88.23310546875,-88.247998046875,-88.32548828125,-88.36875,-88.399658203125,-88.4181640625,-88.443505859375,-88.475830078125,-88.489453125,-88.48447265625,-88.500927734375,-88.53876953125,-88.559375,-88.5693359375,-88.584521484375,-88.56650390625,-88.393798828125,-88.33359375,-88.3228515625,-88.329150390625,-88.35244140625,-88.3953125,-88.4576171875,-88.50263671875,-88.53515625,-88.542431640625,-88.551953125,-88.549609375,-88.56533203125,-88.602099609375,-88.62529296875,-88.63486328125,-88.6541015625,-88.683154296875,-88.705712890625,-88.721923828125,-88.734375,-88.782861328125,-88.8884765625,-88.93271484375,-88.915673828125,-88.896923828125,-88.876416015625,-88.81826171875,-88.76376953125,-88.774560546875,-89.08642578125,-89.200732421875,-89.225,-89.22939453125,-89.213916015625,-89.221484375,-89.25224609375,-89.2966796875,-89.31513671875,-89.34541015625,-89.4384765625,-89.449658203125,-89.5474609375,-89.5775479607347,-89.7753700932598,-89.9010427054757,-89.9936464687739,-90.0399428572587,-90.0917763553297,-90.3201381804473,-90.6071010820191,-90.7444082161632,-90.7973183744315,-90.8403188643531,-90.9160476270076,-91.0434780517905,-91.2206545722212,-91.3872402719353,-91.5183071714663,-91.647286668574,-91.8583999578773,-92.0051553345694,-92.1717849795976,-92.2986770742819,-92.3484396494224,-92.4145883335864,-92.4608902152355,-92.5005783271011,-92.5832559425596,-92.7326535312665,-92.836732514729,-92.9962540051893,-93.0517074985156,-93.1552151928938,-93.2579428579454,-93.3778860998255,-93.4636179146193,-93.5642856430689,-93.7077396280334,-93.8035459061022,-93.8516275729754,-94.0551822682404,-94.4141550602545,-94.6208848044687,-94.6753550213903,-94.7050950127592,-94.7125602230049,-94.7127689632472,-94.8034611053787,-94.8426053939814,-94.8604032462228,-94.8543332997022,-94.8747953366156,-94.9393400168121,-95.1552982770034],"lat":[49.3696720644872,49.8023356552108,50.2337577908088,50.6651799264069,51.0966240346621,51.5280681429172,51.9595122511724,52.3909673457561,52.8223565223686,53.0537615604928,53.2851665986171,53.5165496640843,53.7479327295515,53.9311297580243,54.1143158001685,54.2975128286413,54.4806768981285,54.6897577166511,54.8988495215023,55.107930340025,55.3170111585477,55.5417804542313,55.7665717225721,55.9913410182557,56.2160993276108,56.3756757497138,56.5352521718168,56.6948395802483,56.8513068713733,56.8142609715226,56.7250629700731,56.6086847918055,56.5356696523014,56.4673017297715,56.3416291175556,56.0563690969082,56.021311722524,55.9746747578543,55.9145465817325,55.7732404239983,55.7178913007932,55.6958746983908,55.6569171773735,55.6010516967267,55.5401764502639,55.474291437985,55.4311371394637,55.3832916786542,55.3489703882825,55.2974774663963,55.0954498707989,55.079277995182,55.2243854225924,55.2661884027027,55.2856671632114,55.2833490478886,55.2592120840772,55.258904466878,55.2825140869192,55.2931378666214,55.29080876497,55.2978180425812,55.3141437267977,55.3146600842393,55.2644855217784,55.2617938712851,55.2146185765167,55.2314056865321,55.2222101295407,55.1607196486794,55.1559076367771,55.1655206742532,55.1487225779092,55.0677972818534,54.9981439588839,54.8559259358805,54.8134747623862,54.4835003845643,54.3558282605534,54.2445916840444,54.1804644843325,54.0729742458571,54.0448162857986,53.8857012894944,53.8176409841637,53.7395611472009,53.6109441989352,53.5128582576935,53.3645977539909,53.2641387657837,53.2114593204147,53.1598016036004,53.0661101937764,53.0307122432073,52.9611138518805,52.9216839187357,52.8773980283745,52.8116118930524,52.6514092502224,52.5636174988265,52.4326165172668,52.3672918077436,52.3240715912511,52.2939141193976,52.2536052799693,52.2242058647853,52.2171965871742,52.239081353634,52.2367632383112,52.2045183640343,52.1422478538463,52.0892278322925,52.0454033677301,51.9722234332979,51.798331825102,51.7583415892015,51.6672319665853,51.5250688752247,51.43221242637,51.3885967020499,51.3446733605307,51.2647258477153,51.1250236939485,51.1318132449888,51.1908757472419,51.3073088571522,51.3298637896532,51.316350605544,51.2828423034845,51.2351726239316,51.1733635395426,51.0077885820545,50.9185246626338,50.7421501441868,50.50027513496,50.2584001257332,50.016536102835,49.7746830662653,49.532819043367,49.2909440341402,49.049101983899,48.8072269746723,48.5653519654454,48.3234879425472,48.0816129333204,47.8397379240936,47.5564224835985,47.5510501689404,47.4830447952525,47.4074478685405,47.3188651014895,47.2276566019164,47.1338113834928,47.0219595725853,46.8271719674985,46.6065115586904,46.486463946689,46.3934427029062,46.3068924066358,46.1866690133778,46.0636990379837,46.0628530906858,45.9767752065428,45.961998594651,45.9293911715322,45.9035842857821,45.8326565487014,45.8074648973498,45.7981045454302,45.8009719771802,45.8196816946908,45.8757998608941,45.8902688555866,45.8874343828222,45.8796780348701,45.7978298872166,45.6425930648886,45.5420791450386,45.4963430593096,45.4839394943833,45.5048684502598,45.483631877184,45.4202297751561,45.4507607821801,45.575301802556,45.6421755844039,45.6513711413953,45.6321999980858,45.567710249532,45.5088455011927,45.393554969451,45.3179031110964,45.2691018397035,45.2144448551966,45.2063918763738,45.003869895992,44.9990578840896,45.0039248276347,44.9701198947045,44.899378925209,44.7722780902824,44.4970485875957,44.4906795798657,44.494677734375,44.489990234375,44.410791015625,44.3666015625,44.2947265625,44.262451171875,44.243701171875,44.2384765625,44.21796875,44.1822265625,44.07412109375,44.07109375,44.091162109375,44.106396484375,44.13310546875,44.187548828125,44.19775390625,44.18623046875,44.152880859375,44.1564453125,44.196923828125,44.205078125,44.180810546875,44.175390625,44.18876953125,44.188818359375,44.1755859375,44.178076171875,44.16064453125,44.12373046875,44.0943359375,44.084326171875,44.13076171875,44.14580078125,44.1529296875,44.14267578125,44.11904296875,44.10419921875,44.09794921875,44.10419921875,44.110693359375,44.117529296875,44.12685546875,44.138671875,44.17294921875,44.148095703125,44.0794921875,44.046337890625,44.0486328125,44.06630859375,44.09931640625,44.09990234375,44.068017578125,44.03837890625,43.98720703125,43.951904296875,43.9427734375,43.95654296875,43.96494140625,43.96494140625,43.942919921875,43.9296875,43.8830078125,43.866455078125,43.8759765625,43.899658203125,43.9375,43.947607421875,43.921630859375,43.92275390625,43.946533203125,43.952099609375,43.95166015625,43.96396484375,43.97158203125,43.95400390625,43.962548828125,43.99033203125,44.007666015625,44.014453125,44.024755859375,44.03837890625,44.04482421875,44.03330078125,44.00927734375,44.010693359375,43.9939453125,43.97890625,43.96552734375,43.94443359375,43.915625,43.904833984375,43.911962890625,43.90244140625,43.87626953125,43.85546875,43.83515625,43.7798828125,43.689697265625,43.64541015625,43.647119140625,43.635986328125,43.611962890625,43.578271484375,43.551806640625,43.51328125,43.45283203125,43.31982421875,43.301123046875,43.25283203125,43.234912109375,43.209521484375,43.20869140625,43.197705078125,43.207470703125,43.24228515625,43.2829103148252,43.2780605039857,43.1061024896135,43.1060998572468,43.106103515625,43.09306640625,43.063623046875,43.043896484375,43.0212890625,42.99052734375,42.9515625,42.923828125,42.9072265625,42.862255859375,42.85751953125,42.872607421875,42.880322265625,42.8806640625,42.853955078125,42.8505859375,42.86162109375,42.858447265625,42.822412109375,42.80234375,42.774365234375,42.7384765625,42.707373046875,42.68310546875,42.67587890625,42.6134765625,42.60419921875,42.60791015625,42.589697265625,42.571533203125,42.554150390625,42.54638671875,42.577392578125,42.57392578125,42.595849609375,42.6431640625,42.662451171875,42.653759765625,42.62998046875,42.591162109375,42.44658203125,42.368408203125,42.295458984375,42.258349609375,42.25703125,42.2736328125,42.308056640625,42.3150390625,42.279248046875,42.268896484375,42.2572265625,42.22275390625,42.158251953125,42.10322265625,42.05771484375,42.004638671875,41.94404296875,41.932177734375,41.999951171875,42.025244140625,42.037255859375,42.0359375,41.997216796875,42.006396484375,42.0501953125,42.07099609375,42.119775390625,42.215185546875,42.27548828125,42.297021484375,42.311865234375,42.3267578125,42.33525390625,42.31455078125,42.32177734375,42.339111328125,42.36669921875,42.411083984375,42.472265625,42.497802734375,42.495458984375,42.499853515625,42.524609375,42.5423828125,42.558056640625,42.5580611608992,42.6246815728112,42.7395106787541,43.0173769002914,43.0173840143012,43.04326171875,43.085107421875,43.132666015625,43.185791015625,43.2234375,43.24560546875,43.280126953125,43.32685546875,43.391552734375,43.474072265625,43.5576171875,43.642138671875,43.981591796875,44.060400390625,44.130712890625,44.19248046875,44.2525390625,44.31083984375,44.35966796875,44.39892578125,44.424560546875,44.436474609375,44.460595703125,44.496923828125,44.54228515625,44.5966796875,44.643701171875,44.716259765625,44.78994140625,44.831982421875,44.869921875,44.91103515625,44.9775390625,44.984130859375,44.987353515625,45.014990234375,45.0814453125,45.12490234375,45.14541015625,45.165869140625,45.189111328125,45.20283203125,45.223486328125,45.251220703125,45.261962890625,45.255615234375,45.24873046875,45.241259765625,45.188671875,45.15625,45.123974609375,45.08134765625,45.0283203125,45.00869140625,45.00263671875,44.974365234375,44.96806640625,44.9525390625,44.927783203125,44.91787109375,44.91376953125,44.95693359375,44.967333984375,44.96962890625,44.958642578125,44.934375,44.90693359375,44.876171875,44.842822265625,44.782421875,44.769677734375,44.76533203125,44.8021484375,44.808984375,44.79677734375,44.78095703125,44.761328125,44.71201171875,44.632861328125,44.617578125,44.66611328125,44.70029296875,44.727001953125,44.7212890625,44.638525390625,44.577783203125,44.509521484375,44.48935546875,44.5173828125,44.5716796875,44.65234375,44.709521484375,44.743115234375,44.77548828125,44.806787109375,44.83359375,44.855712890625,44.86298828125,44.854541015625,44.82353515625,44.812890625,44.809375,44.79853515625,44.768017578125,44.761181640625,44.7689453125,44.791162109375,44.821142578125,44.8587890625,44.875146484375,44.862939453125,44.83203125,44.815478515625,44.8193359375,44.854150390625,44.88486328125,44.936328125,44.95654296875,44.974365234375,45.001220703125,45.0171875,45.02216796875,45.072119140625,45.109375,45.126416015625,45.1361328125,45.153271484375,45.17587890625,45.204052734375,45.21103515625,45.196875,45.2009765625,45.22333984375,45.259228515625,45.301123046875,45.3423828125,45.374560546875,45.393359375,45.398828125,45.39658203125,45.386572265625,45.348486328125,45.34384765625,45.36015625,45.38310546875,45.399267578125,45.4583984375,45.500537109375,45.573583984375,45.619873046875,45.625244140625,45.597802734375,45.584130859375,45.576953125,45.58974609375,45.62255859375,45.650048828125,45.68203125,45.7271484375,45.747265625,45.7671875,45.790234375,45.81591796875,45.85849609375,45.87724609375,45.919140625,45.94248046875,45.951025390625,45.951025390625,45.951806640625,45.9416015625,45.9548828125,45.99169921875,46.012060546875,46.016064453125,46.00615234375,45.990966796875,45.97412109375,45.97412109375,45.99443359375,45.99296875,45.966552734375,45.97802734375,45.99638671875,46.0119140625,46.01669921875,46.025390625,46.0400390625,46.074658203125,46.090380859375,46.119384765625,46.115283203125,46.09287109375,46.07880859375,46.073095703125,46.057470703125,46.05791015625,46.071435546875,46.104443359375,46.11474609375,46.1287109375,46.131640625,46.133935546875,46.162939453125,46.19326171875,46.177099609375,46.172412109375,46.175927734375,46.20712890625,46.188427734375,46.181298828125,46.237646484375,46.260498046875,46.27783203125,46.28984375,46.301953125,46.306201171875,46.33291015625,46.342041015625,46.343798828125,46.36083984375,46.393212890625,46.43046875,46.472509765625,46.506982421875,46.539453125,46.56259765625,46.556787109375,46.53115234375,46.5220703125,46.503857421875,46.489013671875,46.483251953125,46.543310546875,46.57666015625,46.599951171875,46.674072265625,46.723046875,46.746923828125,46.743505859375,46.712744140625,46.69736328125,46.697314453125,46.72978515625,46.80556640625,46.833984375,46.84765625,46.84365234375,46.8533203125,46.87998046875,46.90576171875,46.93076171875,46.94404296875,46.945703125,46.937158203125,46.918408203125,46.92236328125,46.948974609375,46.98125,47.00400390625,47.03671875,47.119873046875,47.14208984375,47.191162109375,47.2671875,47.32021484375,47.350244140625,47.367041015625,47.37060546875,47.390478515625,47.42666015625,47.461669921875,47.4955078125,47.53984375,47.594775390625,47.63876953125,47.671728515625,47.697998046875,47.717626953125,47.74580078125,47.792578125,47.827880859375,47.855126953125,47.896435546875,47.92802734375,47.949951171875,47.960205078125,47.958984375,47.950244140625,47.93408203125,47.94072265625,47.970166015625,48.029541015625,48.118798828125,48.210302734375,48.303955078125,48.3853515625,48.40556640625,48.42294921875,48.47060546875,48.5484375,48.59794921875,48.619091796875,48.63662109375,48.687548828125,48.714111328125,48.731298828125,48.758154296875,48.76494140625,48.75302734375,48.777490234375,48.794580078125,48.80791015625,48.8005859375,48.77255859375,48.772412109375,48.788818359375,48.77890625,48.77578125,48.7994140625,48.834814453125,48.842041015625,48.83857421875,48.84833984375,48.871240234375,48.878173828125,48.89921875,48.919921875,48.95869140625,48.998681640625,49.001416015625,48.99814453125,48.981640625,48.97744140625,48.985546875,48.973681640625,48.94189453125,48.9169921875,48.867138671875,48.820751953125,48.7759765625,48.73291015625,48.682861328125,48.59697265625,48.595947265625,48.58447265625,48.56240234375,48.557275390625,48.569140625,48.56025390625,48.5306640625,48.506591796875,48.4880859375,48.4669921875,48.443408203125,48.446826171875,48.47392578125,48.492431640625,48.52802734375,48.655810546875,48.70908203125,48.7384765625,48.77568359375,48.82080078125,48.844873046875,48.8478515625,48.833056640625,48.785693359375,48.75322265625,48.683740234375,48.658154296875,48.63984375,48.629931640625,48.59873046875,48.54609375,48.516552734375,48.510107421875,48.476123046875,48.414599609375,48.38408203125,48.368310546875,48.3373046875,48.335400390625,48.362744140625,48.378076171875,48.381494140625,48.5064453125,48.57236328125,48.583056640625,48.49765625,48.450830078125,48.417578125,48.382275390625,48.344921875,48.3107421875,48.279736328125,48.204248046875,48.158984375,48.121142578125,48.098779296875,48.092236328125,48.012939453125,48.0017562117522,48.0153104405568,47.9954691312062,48.0153104405568,48.078152239829,48.1180985304154,48.0991800726624,48.1125943798147,48.1046073189632,48.1310623980974,48.2005179671531,48.2091532213888,48.1936954571273,48.1046073189632,48.0585416433778,48.0583109304784,48.1046073189632,48.1975736311033,48.3018338889868,48.3383963903815,48.3288822298624,48.2765873059925,48.2765873059925,48.3658841843989,48.4353397534546,48.4651017174806,48.5318216907289,48.5677799440537,48.6118131488584,48.6198771140098,48.6253483056248,48.6288639307589,48.6165372701324,48.5612760375556,48.5369083608448,48.5254606065018,48.5489493769291,48.6072867814983,48.6590214026124,48.7041092949574,48.7426053901759,48.7744437702967,48.8085233614405,48.8630155510192,48.8634220451753,49.0029484176853,49.1191727873533,49.2586002829064,49.304589054192,49.3190360762275,49.3494132746519,49.3696720644872]}],[{"lng":[-79.5204673583163,-79.6361973431998,-79.7144529614194,-79.7314048663629,-79.7374583333907,-79.7232420242547,-79.688821856926,-79.6429868942401,-79.5857261498682,-79.5474387948921,-79.5280039796976,-79.5213462645998,-79.5203574950309,-79.5204673583163],"lat":[50.9659856019442,51.049031259409,51.1176079221813,51.1505009898423,51.1862834619104,51.2516521167478,51.3465849816973,51.4135027088594,51.4524712162053,51.4938567158309,51.5376811803933,51.5447124306615,51.2258891763119,50.9659856019442]}],[{"lng":[-85.605859375,-85.666015625,-85.85859375,-85.9421875,-85.950732421875,-85.930908203125,-85.88271484375,-85.82744140625,-85.76513671875,-85.64111328125,-85.59609375,-85.605859375],"lat":[47.742138671875,47.733349609375,47.719091796875,47.733740234375,47.747314453125,47.766455078125,47.791015625,47.8044921875,47.80673828125,47.781787109375,47.757080078125,47.742138671875]}],[{"lng":[-88.05673828125,-87.996142578125,-87.9150390625,-87.813427734375,-87.76513671875,-87.77021484375,-87.78212890625,-87.812353515625,-87.81630859375,-87.859228515625,-87.9412109375,-87.9984375,-88.030908203125,-88.056689453125,-88.07568359375,-88.086572265625,-88.05673828125],"lat":[48.836767578125,48.8572265625,48.8646484375,48.859033203125,48.84111328125,48.8109375,48.79541015625,48.7853515625,48.76787109375,48.758349609375,48.7568359375,48.743212890625,48.717529296875,48.71669921875,48.740625,48.8080078125,48.836767578125]}],[{"lng":[-87.690283203125,-87.73046875,-87.743701171875,-87.729931640625,-87.7310546875,-87.752685546875,-87.748193359375,-87.73134765625,-87.702099609375,-87.6669921875,-87.626025390625,-87.6216796875,-87.690283203125],"lat":[48.75,48.7546875,48.76416015625,48.77841796875,48.79384765625,48.824072265625,48.83466796875,48.840478515625,48.84140625,48.821435546875,48.780419921875,48.756884765625,48.75]}],[{"lng":[-80.173046875,-80.21572265625,-80.2328125,-80.236279296875,-80.2158203125,-80.171435546875,-80.157666015625,-80.17431640625,-80.173046875],"lat":[44.796240234375,44.82265625,44.843310546875,44.864501953125,44.8716796875,44.86484375,44.847900390625,44.80888671875,44.796240234375]}],[{"lng":[-76.236962890625,-76.32958984375,-76.352734375,-76.364892578125,-76.422705078125,-76.4693359375,-76.484521484375,-76.47431640625,-76.476708984375,-76.491796875,-76.483349609375,-76.45146484375,-76.43046875,-76.328759765625,-76.279150390625,-76.22705078125,-76.236962890625],"lat":[44.2326171875,44.2078125,44.175537109375,44.15634765625,44.121533203125,44.12451171875,44.127783203125,44.141455078125,44.15283203125,44.161962890625,44.178515625,44.202294921875,44.226806640625,44.239208984375,44.246630859375,44.2541015625,44.2326171875]}],[{"lng":[-76.687548828125,-76.7310546875,-76.78671875,-76.80947265625,-76.799365234375,-76.760693359375,-76.642626953125,-76.63955078125,-76.687548828125],"lat":[44.1375,44.126708984375,44.130419921875,44.139892578125,44.155126953125,44.17216796875,44.199267578125,44.181396484375,44.1375]}],[{"lng":[-82.638232421875,-82.684521484375,-82.68544921875,-82.6744140625,-82.65439453125,-82.64287109375,-82.627197265625,-82.638232421875],"lat":[41.746875,41.73583984375,41.757080078125,41.8060546875,41.82744140625,41.8154296875,41.771142578125,41.746875]}],[{"lng":[-81.71259765625,-81.727294921875,-81.7880859375,-81.81494140625,-81.832861328125,-81.8375,-81.82880859375,-81.73505859375,-81.71259765625],"lat":[45.526318359375,45.50048828125,45.4658203125,45.463232421875,45.472509765625,45.483984375,45.49765625,45.53056640625,45.526318359375]}],[{"lng":[-82.962255859375,-83.16005859375,-83.221826171875,-83.2158203125,-83.2005859375,-83.176171875,-83.150830078125,-83.12451171875,-83.097314453125,-83.0693359375,-83.035400390625,-82.995703125,-82.965478515625,-82.94482421875,-82.904736328125,-82.84521484375,-82.82099609375,-82.832080078125,-82.82294921875,-82.79345703125,-82.7740234375,-82.764599609375,-82.736865234375,-82.69091796875,-82.66025390625,-82.64482421875,-82.62734375,-82.6078125,-82.583447265625,-82.554248046875,-82.531396484375,-82.514892578125,-82.52041015625,-82.560009765625,-82.55625,-82.58046875,-82.568505859375,-82.52724609375,-82.49794921875,-82.48056640625,-82.462646484375,-82.444140625,-82.400634765625,-82.33203125,-82.29453125,-82.28798828125,-82.268359375,-82.235546875,-82.20458984375,-82.175341796875,-82.142578125,-82.106396484375,-82.05458984375,-81.9873046875,-81.9388671875,-81.909326171875,-81.895458984375,-81.897314453125,-81.87900390625,-81.840576171875,-81.82822265625,-81.841845703125,-81.838427734375,-81.803759765625,-81.795849609375,-81.7630859375,-81.73505859375,-81.698193359375,-81.674755859375,-81.66484375,-81.67275390625,-81.698583984375,-81.68505859375,-81.60146484375,-81.593017578125,-81.6857421875,-81.754736328125,-81.828076171875,-81.88125,-81.9142578125,-81.94619140625,-81.977197265625,-81.976708984375,-81.9224609375,-81.90234375,-81.864501953125,-81.7595703125,-81.759765625,-81.821142578125,-82.02900390625,-82.0552734375,-82.0759765625,-82.09111328125,-82.208056640625,-82.2560546875,-82.323193359375,-82.40947265625,-82.510400390625,-82.6259765625,-82.735791015625,-82.936669921875,-82.962255859375],"lat":[45.833447265625,45.8751953125,45.9021484375,45.932666015625,45.9548828125,45.968896484375,45.963232421875,45.937939453125,45.93505859375,45.95458984375,45.957421875,45.94345703125,45.94521484375,45.9626953125,45.977783203125,45.9904296875,45.9765625,45.936181640625,45.909814453125,45.89755859375,45.880322265625,45.858154296875,45.85234375,45.862890625,45.85966796875,45.842626953125,45.83857421875,45.847509765625,45.83701171875,45.80703125,45.80576171875,45.833349609375,45.84892578125,45.859619140625,45.869921875,45.8955078125,45.914404296875,45.938623046875,45.943896484375,45.9302734375,45.93388671875,45.95478515625,45.970849609375,45.981982421875,45.9734375,45.945166015625,45.9359375,45.94580078125,45.92421875,45.871142578125,45.86943359375,45.919091796875,45.95498046875,45.97705078125,45.979248046875,45.961767578125,45.93876953125,45.910302734375,45.89248046875,45.885400390625,45.862158203125,45.82275390625,45.788427734375,45.743212890625,45.740380859375,45.82119140625,45.863671875,45.89375,45.896484375,45.871923828125,45.844677734375,45.81484375,45.8013671875,45.801171875,45.79169921875,45.656689453125,45.59111328125,45.5466796875,45.535498046875,45.557470703125,45.56611328125,45.56142578125,45.569873046875,45.6037109375,45.603076171875,45.610595703125,45.68515625,45.69755859375,45.691552734375,45.565966796875,45.561328125,45.569677734375,45.590869140625,45.627490234375,45.660107421875,45.68359375,45.697998046875,45.727490234375,45.77216796875,45.796728515625,45.810986328125,45.833447265625]}],[{"lng":[-83.828955078125,-83.856494140625,-83.875927734375,-83.887255859375,-83.9068359375,-83.9302734375,-83.97861328125,-84.03876953125,-84.07822265625,-84.097021484375,-84.0611328125,-83.970703125,-83.891064453125,-83.822119140625,-83.794580078125,-83.80830078125,-83.828955078125],"lat":[46.14765625,46.157470703125,46.147900390625,46.118896484375,46.097216796875,46.09169921875,46.109423828125,46.17705078125,46.23642578125,46.28759765625,46.309765625,46.302978515625,46.2841796875,46.253466796875,46.214306640625,46.1666015625,46.14765625]}],[{"lng":[-83.345263671875,-83.29638671875,-83.271044921875,-83.269189453125,-83.331982421875,-83.386572265625,-83.4533203125,-83.47998046875,-83.466650390625,-83.43740234375,-83.39228515625,-83.345263671875],"lat":[45.9927734375,45.97666015625,45.961328125,45.94658203125,45.88037109375,45.8685546875,45.88427734375,45.90673828125,45.93603515625,45.963232421875,45.988232421875,45.9927734375]}]],[[{"lng":[-63.8112699817142,-63.7842436134958,-63.7370133870847,-63.6814335509801,-63.5343595707603,-63.4564774877112,-63.4131474079333,-63.3686637636583,-63.2860795319923,-63.1293705416393,-62.9640043243936,-62.7120328792348,-62.6819083663669,-62.4231144111822,-62.1635623993279,-62.0742655209215,-62.0408890548046,-62.0237284096187,-62.1717582004218,-62.3199857451388,-62.5260453233118,-62.551995031333,-62.5392289175647,-62.5432609001404,-62.5025895118702,-62.4780899992168,-62.531318761013,-62.7431901069858,-62.8048893280895,-62.8783549070639,-62.9035465584155,-62.9946012493889,-63.0220890434062,-62.8945487553378,-62.9526664333361,-63.015057793138,-63.0563224431496,-63.052916681301,-62.9951395794876,-62.9784733190862,-63.0568717595769,-63.1170219083558,-63.1947172238196,-63.270808535316,-63.1527824077668,-63.21350384563,-63.2765983304588,-63.5688786150459,-63.6409928756093,-63.7317838946977,-63.8005473250552,-63.7632377533194,-63.750548543851,-63.7586564543166,-63.8605436652345,-64.0197026068528,-64.110812229469,-64.106571506651,-64.1360588124633,-64.235638894387,-64.3880522302792,-64.4031474456988,-64.3546208325195,-64.280001689048,-64.2232353294607,-64.1568998777115,-63.9935771175749,-63.9972795102943,-63.9814921561764,-64.0878617891404,-63.9030497703716,-63.8793302870449,-63.8637187141837,-63.8756278943255,-63.9055546532797,-63.8336161739729,-63.8112699817142],"lat":[46.4686880671047,46.4546585255539,46.4804983702896,46.5619180511299,46.5406265464114,46.5039322090742,46.5120181468826,46.508249836192,46.4602176077972,46.4222049110346,46.4277310342923,46.4502969531219,46.4594375784706,46.4782461729381,46.487189044373,46.4657437310549,46.4456936814619,46.4215567176505,46.3553860608295,46.2783169660926,46.2028628616518,46.1659378114151,46.0979324377271,46.0286636362566,46.0229287727566,45.9997256468714,45.9773025503129,45.9668875108532,45.9732156360946,46.0013735961531,46.068247378001,46.0584256002826,46.0665994287194,46.1235965012061,46.1951614453424,46.1899429392839,46.2239456261279,46.2698135477995,46.2921377674011,46.3163516355123,46.2953677479931,46.2528396701989,46.2367117398961,46.1999954299018,46.1883389353165,46.1598403990731,46.1532815609323,46.2092239458788,46.2304605189545,46.2890615954088,46.3673281999569,46.3703714129636,46.3843570092002,46.3976065214244,46.4081753694838,46.4048135529493,46.4254348916266,46.5621158050437,46.5996890486645,46.6314395381569,46.6408767943763,46.6916226459215,46.7692410570855,46.8357413037629,46.9012417945428,46.9548880368236,47.0615762733154,46.9817276374569,46.912997166085,46.7754373463844,46.6391189818092,46.6089615099557,46.5723550632468,46.538659993602,46.5087661936336,46.4938797184563,46.4686880671047]}]],[[{"lng":[-109.999432144043,-109.999432144043,-109.999454116701,-109.999454116701,-109.999481582522,-109.999509048343,-109.999509048343,-109.999531021,-109.999558486822,-109.999585952643,-109.999613418464,-109.999613418464,-109.999635391121,-109.999662856943,-109.999662856943,-109.999690322764,-109.999712295421,-109.999739761243,-109.999767227064,-109.999767227064,-109.999789199721,-109.999816665542,-109.999816665542,-109.999844131364,-109.999871597185,-109.999893569842,-109.999921035664,-109.999921035664,-109.999948501485,-109.999970474142,-109.999970474142,-109.999997939963,-110.000025405785,-109.500130977536,-109.000242042451,-108.50032014838,-108.000431213295,-107.500536785046,-107.00062038414,-106.500731449055,-106.000837020806,-105.500942592557,-105.001053657472,-104.501137256566,-104.001242828317,-103.501353893232,-103.001431999161,-102.501543064076,-102.001648635827,-102.001621170006,-102.001599197349,-102.001544265706,-102.001522293049,-102.001494827228,-102.001445388749,-102.001417922928,-102.001390457106,-102.001341018628,-102.001313552807,-102.001286086985,-102.001236648507,-102.001209182685,-102.001181716864,-102.001132278386,-102.001104812564,-101.96129035792,-101.921497875934,-101.881710887111,-101.841918405125,-101.802125923138,-101.762333441151,-101.722546452329,-101.682753970342,-101.642961488355,-101.603174499533,-101.563382017546,-101.523589535559,-101.483802546737,-101.44401006475,-101.404217582764,-101.36727055987,-101.793649970666,-102.220034874627,-102.646414285423,-103.072799189384,-103.49917860018,-103.925563504141,-104.033927155735,-104.351942914937,-104.778327818898,-105.204707229694,-105.631092133654,-106.057477037615,-106.483856448411,-106.910241352372,-107.336620763168,-107.763005667129,-108.189385077925,-108.615769981886,-109.042149392682,-109.468534296643,-109.894913707439,-109.999432144043],"lat":[48.9930826946527,49.337965520309,49.6819584533531,50.0259404000687,50.3699003741273,50.7138603481858,51.0578422949014,51.401824241617,51.7457842156756,52.0897441897341,52.4337261364497,52.7777080831654,53.1216680572239,53.4656280312824,53.809609977998,54.1535919247137,54.4975518987722,54.8415118728307,55.1854718468892,55.529475766262,55.8734357403205,56.217395714379,56.5613776610946,56.9053596078103,57.2493195818688,57.5932795559273,57.9372395299858,58.2812544356871,58.6252034234171,58.9691633974756,59.3131453441912,59.6571382772354,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,60.0010872649654,59.7396126456159,59.478149012595,59.2166963659026,58.9552107602246,58.6937251545466,58.4322505351971,58.1707649295191,57.9092793238411,57.6478486498058,57.3863630441278,57.1248884247784,56.8634028191003,56.6019172134224,56.34046456673,56.079000933709,55.8175263143596,55.3910370402778,54.9646026978388,54.538113423757,54.1116571086608,53.6852007935647,53.2587115194829,52.8322552043868,52.4057988892906,51.9793315878659,51.5528752727698,51.1264189576736,50.699951656249,50.2734733684957,49.8470390260567,49.4205497519749,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931376262954,48.9931046673098,48.9930826946527,48.9930826946527,48.9930826946527]}]]],null,null,{"interactive":true,"className":"","stroke":true,"color":"#444444","weight":1,"opacity":1,"fill":true,"fillColor":["#238B45","#74C476","#238B45","#EDF8E9","#EDF8E9","#BAE4B3","#74C476","#BAE4B3","#EDF8E9","#238B45"],"fillOpacity":0.75,"smoothFactor":1,"noClip":false},null,null,null,{"interactive":false,"permanent":false,"direction":"auto","opacity":1,"offset":[0,0],"textsize":"10px","textOnly":false,"className":"","sticky":true},null]}],"setView":[[45.7,-74.09],2,[]],"limits":{"lat":[41.73583984375,60.3059798547211],"lng":[-139.056518823772,-52.6536535892881]}},"evals":[],"jsHooks":[]}</script>
</div>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">gt_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> Province, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> Donations) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>()</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This call was added for illustration</span></span>
<span id="cb7-6">gt_table</span></code></pre></div>
<div class="cell-output-display">
<div>
<div id="qenxvcpyux" style="overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>html {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
}

#qenxvcpyux .gt_table {
  display: table;
  border-collapse: collapse;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#qenxvcpyux .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qenxvcpyux .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#qenxvcpyux .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 0;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#qenxvcpyux .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qenxvcpyux .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#qenxvcpyux .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#qenxvcpyux .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#qenxvcpyux .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#qenxvcpyux .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#qenxvcpyux .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#qenxvcpyux .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
}

#qenxvcpyux .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#qenxvcpyux .gt_from_md > :first-child {
  margin-top: 0;
}

#qenxvcpyux .gt_from_md > :last-child {
  margin-bottom: 0;
}

#qenxvcpyux .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#qenxvcpyux .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#qenxvcpyux .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#qenxvcpyux .gt_row_group_first td {
  border-top-width: 2px;
}

#qenxvcpyux .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qenxvcpyux .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#qenxvcpyux .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#qenxvcpyux .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qenxvcpyux .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#qenxvcpyux .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#qenxvcpyux .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qenxvcpyux .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qenxvcpyux .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qenxvcpyux .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-left: 4px;
  padding-right: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qenxvcpyux .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#qenxvcpyux .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#qenxvcpyux .gt_left {
  text-align: left;
}

#qenxvcpyux .gt_center {
  text-align: center;
}

#qenxvcpyux .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#qenxvcpyux .gt_font_normal {
  font-weight: normal;
}

#qenxvcpyux .gt_font_bold {
  font-weight: bold;
}

#qenxvcpyux .gt_font_italic {
  font-style: italic;
}

#qenxvcpyux .gt_super {
  font-size: 65%;
}

#qenxvcpyux .gt_two_val_uncert {
  display: inline-block;
  line-height: 1em;
  text-align: right;
  font-size: 60%;
  vertical-align: -0.25em;
  margin-left: 0.1em;
}

#qenxvcpyux .gt_footnote_marks {
  font-style: italic;
  font-weight: normal;
  font-size: 75%;
  vertical-align: 0.4em;
}

#qenxvcpyux .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#qenxvcpyux .gt_slash_mark {
  font-size: 0.7em;
  line-height: 0.7em;
  vertical-align: 0.15em;
}

#qenxvcpyux .gt_fraction_numerator {
  font-size: 0.6em;
  line-height: 0.6em;
  vertical-align: 0.45em;
}

#qenxvcpyux .gt_fraction_denominator {
  font-size: 0.6em;
  line-height: 0.6em;
  vertical-align: -0.05em;
}
</style>

<table class="gt_table table table-sm table-striped small" data-quarto-postprocess="true">
<thead class="gt_col_headings">
<tr class="header">
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Alberta</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">British Columbia</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Manitoba</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">New Brunswick</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Newfoundland and Labrador</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Nova Scotia</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Ontario</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Prince Edward Island</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">QuÃ©bec</th>
<th class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th">Saskatchewan</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_right">3350</td>
<td class="gt_row gt_right">2411</td>
<td class="gt_row gt_right">2928</td>
<td class="gt_row gt_right">1359</td>
<td class="gt_row gt_right">1486</td>
<td class="gt_row gt_right">1580</td>
<td class="gt_row gt_right">2084</td>
<td class="gt_row gt_right">1982</td>
<td class="gt_row gt_right">1039</td>
<td class="gt_row gt_right">2637</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</div>
</section>
<section id="ui" class="level2">
<h2 class="anchored" data-anchor-id="ui">UI</h2>
<p>The following code creates the UI interface that the user interacts with. For those familiar to Shiny Apps, the structure will be similar. The actual functions used will be different, but usually their names will just have the ‘dashboard’ prefix. For those not familiar with Shiny Apps, the UI is created by a nesting of a series of functions. The notation can get a little difficult as you need to remember to use ‘,’ when passing multiple functions. Though not used in this dashboard, UI interfaces can be used to control elements of the display. These controls create variables that need to be saved as <code>input$"variable_name"</code>. This allows the transfer of information from the UI to the server side. Likewise, plots and figures from the server side need to be saved as <code>output$"variable_name"</code>. When an output variable is referenced in the UI, such as the <code>plotOutput</code> function for displaying plots, they are referenced as “variable_name” in quotes.</p>
<p>Again, the syntax can be difficult to learn and use, but the R Studio post on <a href="https://rstudio.github.io/shinydashboard/get_started.html">Shiny Dashboards</a> provide the basic skeleton to learn from. My biggest issue is keeping track of all the ‘)’ and ‘,’ used.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The main page structure is the dashboardPage</span></span>
<span id="cb8-2">ui <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dashboardPage</span>(</span>
<span id="cb8-3">        </span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The header is simple the header for the dashboard</span></span>
<span id="cb8-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dashboardHeader</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Donations in Canada Dashboard"</span>),</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The sidebar functions adds the controls for flipping through dashboard pages        </span></span>
<span id="cb8-8">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dashboardSidebar</span>(</span>
<span id="cb8-9">                </span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each menu item is a link that will apear on the sidebar</span></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># There are a bunch of quality icons that the icon function can return </span></span>
<span id="cb8-12">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">menuItem</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dashboard"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tabName =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gauge"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">icon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">icon</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashboard"</span>)),</span>
<span id="cb8-13">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">menuItem</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Education"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tabName =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"education"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">icon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">icon</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"school"</span>)),</span>
<span id="cb8-14">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">menuItem</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Map"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tabName =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"map"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">icon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">icon</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"map"</span>))),</span>
<span id="cb8-15">        </span>
<span id="cb8-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The next section includes the body or the main section of the dashboard</span></span>
<span id="cb8-17">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dashboardBody</span>(</span>
<span id="cb8-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In the main body, each of the tabs in the side bar need to be referenced. The tabItems function is the container for all the tabs</span></span>
<span id="cb8-19">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabItems</span>(</span>
<span id="cb8-20">                        </span>
<span id="cb8-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each previous mentioned tab, will have a separate tabItem.</span></span>
<span id="cb8-22">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabItem</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tabName =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashboard"</span>,</span>
<span id="cb8-23"></span>
<span id="cb8-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The fluidRow function is used to align the elements on the page. Through trial and error, I have found this to be the element for changing the size of plots.</span></span>
<span id="cb8-25">                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidRow</span>(</span>
<span id="cb8-26">                                        </span>
<span id="cb8-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A box is drawn around each plot. Each plot/figure/element is referenced here with the plotOutput function or a likewise function. In the server side, I will transform the ggplots into plotly plots to create interactive plots so the plotlyOutput function is used. </span></span>
<span id="cb8-28">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotlyOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot1"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>),</span>
<span id="cb8-29">                </span>
<span id="cb8-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The pie plot is a regular ggplot object, so it can be passed to the plotOutput function</span></span>
<span id="cb8-31">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pie"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))),</span>
<span id="cb8-32">                </span>
<span id="cb8-33">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabItem</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tabName =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"education"</span>,</span>
<span id="cb8-34">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidRow</span>(</span>
<span id="cb8-35">                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plotlyOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot3"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))),</span>
<span id="cb8-36">                </span>
<span id="cb8-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For the map tab, the leafletOutput function is used to display the leaflet map created in the server side. </span></span>
<span id="cb8-38">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabItem</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tabName =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"map"</span>, </span>
<span id="cb8-39">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidRow</span>(</span>
<span id="cb8-40">                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">leafletOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb8-41">                                ),</span>
<span id="cb8-42">                        </span>
<span id="cb8-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># On a second row, under the map, a GT table requires the gt_output function</span></span>
<span id="cb8-44">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidRow</span>(</span>
<span id="cb8-45">                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">box</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt_output</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb8-46">                        ),</span>
<span id="cb8-47">                        )</span>
<span id="cb8-48">                )</span>
<span id="cb8-49">        )</span>
<span id="cb8-50">)</span></code></pre></div>
</div>
</section>
<section id="server" class="level2">
<h2 class="anchored" data-anchor-id="server">Server</h2>
<p>The server is the second major function in the Shiny App. Rather than using the output series of functions, it requires the paired version of the render function. Any controls from the UI would need to be passed as part of the <code>input</code> variable.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The same structure for any other Shiny App</span></span>
<span id="cb9-2">server <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(input, output) {</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Any data to pass back to the UI, such as figures or plots, will need to be saved as part of the output. Anything within the {} brackets becomes an interactive element. If they are not present, the plot will not change.</span></span>
<span id="cb9-5">        output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPlotly</span>({</span>
<span id="cb9-6">                </span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The ggplot for the barplot is converted to an interactive plotly plot.</span></span>
<span id="cb9-8">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplotly</span>(bar_plot)</span>
<span id="cb9-9">                })</span>
<span id="cb9-10"></span>
<span id="cb9-11">        output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderLeaflet</span>({map_leaf})</span>
<span id="cb9-12">        </span>
<span id="cb9-13">        output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>plot3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPlotly</span>({<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplotly</span>(edu_plot)})</span>
<span id="cb9-14">        </span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The GT table requires the render_gt function which pairs with the gt_output function in the UI.</span></span>
<span id="cb9-16">        output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">render_gt</span>({gt_table})</span>
<span id="cb9-17">        </span>
<span id="cb9-18">        output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>pie <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderPlot</span>({pie_plot})</span>
<span id="cb9-19">}</span></code></pre></div>
</div>
</section>
<section id="finalized-dashboard" class="level2">
<h2 class="anchored" data-anchor-id="finalized-dashboard">Finalized Dashboard</h2>
<p>The finalized dashboard looks pretty good. The sidebar adds a good level of control to the user and makes the data better organized. I also enjoy the default styling, but I’m sure it can be changed.</p>
<p>When compared to the dashboard created with <code>flexdashboard</code>, it is much nicer looking. The flex dashboard, however, has the advantage of being much easier to create with r markdown. It is also much more flexible as it produces an HTML file which is easy to manage. The shiny dashboard needs to be hosted, which makes it slightly clunky.</p>
<p>Speaking of the clunky nature of Shiny Apps, you also need to be conscious of what needs to be reactive and what doesn’t. It is very easy to slow down a Shiny Dashboard with a bunch of unnecessary calculations. The writing of the application itself is also quite clunky. It doesn’t compare to the ease of writing in R markdown.</p>
<p>So which dashboard do I prefer? Well I think for a quick solution, flex dashboard is great but the amount of control and styling of a shiny dashboard is just better. I do think that there is a place for both, but if I had to choose, I would go for a Shiny Dashboard.</p>
<div class="cell">
<div class="cell-output-display">
<iframe title="Shiny Dashboard" width="100%" height="500" src="https://m2edney.shinyapps.io/ShinyDashboard/"></iframe>
</div>
</div>


</section>

 ]]></description>
  <category>How-to</category>
  <category>GGPlot</category>
  <category>Leaflet</category>
  <category>R</category>
  <category>Shiny App</category>
  <guid>https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/index.en.html</guid>
  <pubDate>Wed, 20 Apr 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-04-20-dashboards-in-r-with-shiny-dashboard/shiny-dash.png" medium="image" type="image/png" height="64" width="144"/>
</item>
<item>
  <title>Merging PDFs with Python</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-04-14-merging-pdfs-with-python/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-04-14-merging-pdfs-with-python/union.jpg" class="img-fluid"></p>
<p>I am currently looking for a new job, which means I need to create many resumes and cover letters. When creating a resume, it is good practice to create a PDF file. PDFs cannot be edited, which can make them difficult to work with, but they retain their formatting. It is impossible to tell which version of Microsoft Word a hiring manager is using. So you have to risk a possible formatting error or create a compatible resumes without the latest features.</p>
<p>One issue with using PDFs is that employers will sometimes ask for a cover letter and resume to be submitted as a single PDF. This wouldn’t be an issue if they were both stored in the same document, but if you are like me, you have separate documents creating separate PDFs. You can always use free online PDF mergers, but they can have limitations, and it may not be desirable to give away your personal data. So I decided to create a small Python script that will merge my PDFs together.</p>
<p>The script will require the <code>PyPDF2</code> package for dealing with PDFs and the <code>os</code> package. The <code>os</code> package is just used to automatically merge all PDFs in the root folder.</p>
<div class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> PyPDF2, os</span></code></pre></div>
</div>
<p>The first step is to create a list of the PDFs in the current folder. It also ensures that the merged PDF is not in the list.</p>
<div class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">pdfiles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> filename <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> os.listdir(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.'</span>):</span>
<span id="cb2-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> filename.endswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.pdf'</span>):</span>
<span id="cb2-5">                <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> filename <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'merged.pdf'</span>:</span>
<span id="cb2-6">                        pdfiles.append(filename)</span>
<span id="cb2-7">                        </span>
<span id="cb2-8">pdfiles.sort(key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.lower)</span></code></pre></div>
</div>
<p>The file list is also sorted alphabetically to ensure the results are predictable and easy to control. The merged PDF will contain the PDFs in the same order.</p>
<p>The next step is to create a <code>PdfFileMerger</code> object, which will be the destination for all the data in the PDFs. The first step is to open the first PDF file in the PDF file list. The <code>PdfFileMerger</code> object will only accept a file object, so we need to create a <code>PdfFileReader</code> object from the opened PDF. This <code>PdfFileReader</code> object will then be appended to the <code>PdfFileMerger</code> object. We proceed then to the next PDF. After all files are added, the write method is used on the merged object to create a merged PDF.</p>
<div class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">pdfMerge <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileMerger()</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> filename <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> pdfiles:</span>
<span id="cb3-4">        pdfFile <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(filename, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rb'</span>)</span>
<span id="cb3-5">        pdfReader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileReader(pdfFile)</span>
<span id="cb3-6">        pdfMerge.append(pdfReader)</span>
<span id="cb3-7"></span>
<span id="cb3-8">pdfFile.close()</span>
<span id="cb3-9">pdfMerge.write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'merged.pdf'</span>)</span></code></pre></div>
</div>
<p>And that’s everything, a simple Python script that creates a merged PDF from all PDFs in the root folder. It is important to remember that a PDF file needs to be opened, and then a file object can be created. Using the regular PDF file will not work.</p>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@krakenimages?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">krakenimages</a> on <a href="https://unsplash.com/s/photos/union?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>



 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <guid>https://datasandbox.netlify.app/posts/2022-04-14-merging-pdfs-with-python/</guid>
  <pubDate>Thu, 14 Apr 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-04-14-merging-pdfs-with-python/union.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Benchmarking Data Tables</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-04-08-benchmarking-data-tables/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-04-08-benchmarking-data-tables/racecar.jpg" class="img-fluid"></p>
<p>When I started learning R, I heard vague tales of the use of Data Tables. Really just whisperers, of something to consider in the future after I’ve become more proficient. Well now is the time to learn what if anything I’ve been missing out on.</p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Data Tables are a potential replacement for the common dataframe. It seeks to perform that same role but with improved performance. I would like to see the speed comparison between Data Frames, Data Tables and Tibbles. I will use the <code>microbenchmark</code> package to perform the actual benchmarking.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(microbenchmark)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(farff)</span></code></pre></div>
</div>
<p>For the benchmark, I will use the ‘credit-g’ dataset, which can be found on the <a href="https://www.openml.org/search?type=data&amp;status=active&amp;id=31">open ml</a> website. I’m pretty sure the last open ml dataset I used was a csv file, but they seem to have moved to a ARFF format. I will need to use the <code>farff</code> package to load the data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> farff<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readARFF</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dataset_31_credit-g.arff'</span>)</span>
<span id="cb2-2">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setDT</span>(df)</span>
<span id="cb2-3">ti <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(df)</span></code></pre></div>
</div>
</section>
<section id="syntax" class="level2">
<h2 class="anchored" data-anchor-id="syntax">Syntax</h2>
<p>The syntax for Data Tables is a little different:</p>
<blockquote class="blockquote">
<p>DT[i,j,by]</p>
</blockquote>
<p>In this manner, a data table can be subset by i, to calculate j when grouped with a by. Along with the special syntax, there are some common functions that add some additional simplification.</p>
<blockquote class="blockquote">
<p>.()</p>
</blockquote>
<p>The ‘.()’ function can be used as a placeholder for ‘list()’. The list function is useful for subsetting.</p>
</section>
<section id="grouped-aggregate" class="level2">
<h2 class="anchored" data-anchor-id="grouped-aggregate">Grouped Aggregate</h2>
<p>Aggregating data in Data Tables is simple by using the j and by parameters in the syntax. Again, multiple functions or even multiple groupings can be passed with the ‘.()’ function. For this comparison, we will look at the performance of finding the average age of the credit holders grouped by the class or credit rating.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">group <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">microbenchmark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Data_Frame =</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-2">                                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(class) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(age)),</span>
<span id="cb3-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Data_Table =</span> dt[,.(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(age)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> class],</span>
<span id="cb3-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Tibble =</span> ti <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb3-6">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(class) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-7">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(age)))</span>
<span id="cb3-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(group)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Unit: microseconds
       expr    min     lq     mean  median      uq     max neval
 Data_Frame 5406.5 5564.6 6138.870 5904.40 6306.95 17135.6   100
 Data_Table  623.6  766.8  941.930  885.95  958.05  7471.7   100
     Tibble 5602.8 5783.1 6586.445 6103.15 6902.65 14356.9   100</code></pre>
</div>
</div>
</section>
<section id="taking-counts" class="level2">
<h2 class="anchored" data-anchor-id="taking-counts">Taking counts</h2>
<p>Another function of interest is the ‘.N’ function. This function will return the count of rows. The test looks are the number of people with over 5000 in credit and younger than 35.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">microbenchmark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Data_Frame =</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">                                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(credit_amount <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(),</span>
<span id="cb5-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Data_Table =</span> dt[credit_amount <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, .N ,],</span>
<span id="cb5-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Tibble =</span> ti <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-6">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(credit_amount <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-7">                       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>())</span>
<span id="cb5-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(counts)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Unit: microseconds
       expr    min      lq      mean  median      uq     max neval
 Data_Frame 8404.1 8584.90  9576.638 8927.45 9867.95 17254.5   100
 Data_Table  276.7  309.50   429.832  451.80  466.35  1045.6   100
     Tibble 8945.1 9204.75 10206.312 9405.45 9942.45 27081.9   100</code></pre>
</div>
</div>
</section>
<section id="creating-new-columns" class="level2">
<h2 class="anchored" data-anchor-id="creating-new-columns">Creating new columns</h2>
<p>Data Tables also contain a very simple syntax for creating a new column with ‘:=’. I compare this to the <code>tidyverse</code> mutate function. Using the base R to create a column is still the fastest method, taking about half the time of the Data Table method.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">microbenchmark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Data_Frame =</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">property =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(property_magnitude, housing)),</span>
<span id="cb7-2">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Data_Table =</span> dt[,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">property :=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(property_magnitude, housing)],</span>
<span id="cb7-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Tibble =</span> ti <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">property =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(property_magnitude, housing)))</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(new)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Unit: microseconds
       expr    min      lq     mean median      uq    max neval
 Data_Frame 2071.3 2148.55 2396.203 2259.6 2586.40 3386.1   100
 Data_Table  505.7  580.00  657.802  656.6  694.35  996.0   100
     Tibble 2585.3 2754.85 3187.554 2922.9 3331.70 8879.8   100</code></pre>
</div>
</div>
</section>
<section id="chaining-data-tables" class="level2">
<h2 class="anchored" data-anchor-id="chaining-data-tables">Chaining Data Tables</h2>
<p>Another point of exploration is that Data Tables can be chained together to create more complicated structures</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">dt[credit_amount <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, .(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(age)),by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .(purpose, class)][class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"good"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(age)]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>               purpose class      age
1:            radio/tv  good 35.44865
2: furniture/equipment  good 33.21930
3:            used car  good 36.91860
4:            business  good 34.50000
5:  domestic appliance  good 35.50000
6:          retraining  good 34.00000</code></pre>
</div>
</div>
<p>I don’t think this is the most useful feature, as you can already create some very complicated transformation with a single call. Chaining also makes it more difficult to understand.</p>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>It is clear that there are significant performance improvements when using Data Tables versus Data Frames (an average decrease of time by -85%). There are also insignificant differences between Data Frames and Tibbles. Also, the syntax for Data Tables is fairly simple and straight forward and yet extremely powerful.</p>
<p>So, to answer the most important question, should you change to Data Tables from Data Frames? Probably, they present a significant performance gain and their structure is very flexible.</p>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@tyler_clemmensen?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Tyler Clemmensen</a> on <a href="https://unsplash.com/s/photos/race-car?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>


</section>

 ]]></description>
  <category>How-to</category>
  <category>R</category>
  <guid>https://datasandbox.netlify.app/posts/2022-04-08-benchmarking-data-tables/</guid>
  <pubDate>Wed, 13 Apr 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-04-08-benchmarking-data-tables/racecar.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>R-Bloggers site</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-04-11-r-bloggers-site/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-04-11-r-bloggers-site/rblogger.jpg" class="img-fluid"></p>
<p>I would like to take the time to mention the <a href="https://www.google.com/url?q=http://r-bloggers.com&amp;sa=D&amp;source=editors&amp;ust=1649728986132366&amp;usg=AOvVaw2vAkyuJT7uhjS9BQpH-Upw">r-bloggers</a> site. It is a vast collection of Blogs on everything that has to do will the R language. I would very much like to contribute to their work with this blog.</p>
<p>Just to keep it interesting, I would like to recommend this specific post by <a href="https://www.r-bloggers.com/2022/04/upcoming-r-conferences-2022/">The Jumping Rivers Blog</a> about upcoming R conferences. I’ve attended a few digital R Conferences, hosted by R-Studio, and they are very rewarding experiences. They have the ability to both inspire and educate. Unfortunately, there are no conferences in my native Canada.</p>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@andrewtneel?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Andrew Neel</a> on <a href="https://unsplash.com/s/photos/bloggers?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>



 ]]></description>
  <category>General</category>
  <category>R</category>
  <guid>https://datasandbox.netlify.app/posts/2022-04-11-r-bloggers-site/</guid>
  <pubDate>Mon, 11 Apr 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-04-11-r-bloggers-site/rblogger.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Underrated CRAN Packages</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-03-31-underrated-cran-packages/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-03-31-underrated-cran-packages/Rlogo.svg" class="img-fluid"></p>
<p>I sit here looking for inspiration, nothing interesting to write about. Perhaps there are some popular R packages on CRAN that I don’t know about? You can explore the data on downloads from CRAN with the <code>cranlogs</code> package.</p>
<section id="top-cran-downloads" class="level2">
<h2 class="anchored" data-anchor-id="top-cran-downloads">Top CRAN downloads</h2>
<p>With the following code, we can get the most popular packages from CRAN. The CRAN directory doesn’t represent all R packages, but a good amount of them.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cranlogs)</span>
<span id="cb1-3">top100 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cran_top_downloads</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">when =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'last-month'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb1-4">top100 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  rank  package   count       from         to
1    1  ggplot2 2502873 2022-07-17 2022-08-15
2    2    rlang 2039913 2022-07-17 2022-08-15
3    3 devtools 1844834 2022-07-17 2022-08-15
4    4       sf 1756905 2022-07-17 2022-08-15
5    5      cli 1672799 2022-07-17 2022-08-15
6    6     glue 1624788 2022-07-17 2022-08-15</code></pre>
</div>
</div>
<p>From this list, we can see that the <code>tidyverse</code> represents a large amount of the top downloads with <code>ggplot2</code>, <code>rlang</code> and <code>dplyr</code>. The list includes the <code>sf</code> package for geospacial data, the <code>glue</code> package for string manipulation and the <code>cli</code> package which is used to create a command line interface for packages. Most of these packages I already have a good understanding of, so I need to narrow down the search.</p>
</section>
<section id="packages-installed" class="level2">
<h2 class="anchored" data-anchor-id="packages-installed">Packages installed</h2>
<p>You can get a list of your installed packages with the installed_packages function. You can then filter the top 100 list and remove anything you already have installed to find new packages.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">mine <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">installed.packages</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Package)</span>
<span id="cb3-4">new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> top100 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>package <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> mine<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Package)</span>
<span id="cb3-6">new</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   rank     package   count       from         to
1     3    devtools 1844834 2022-07-17 2022-08-15
2     7        ragg 1589393 2022-07-17 2022-08-15
3     8 textshaping 1583922 2022-07-17 2022-08-15
4    10       rgeos 1524077 2022-07-17 2022-08-15
5    11         rgl 1499311 2022-07-17 2022-08-15
6    18     pkgdown 1162782 2022-07-17 2022-08-15
7    19  enrichwith 1158675 2022-07-17 2022-08-15
8    20      brglm2 1154128 2022-07-17 2022-08-15
9    31         zoo  873095 2022-07-17 2022-08-15
10   50       Hmisc  706994 2022-07-17 2022-08-15
11   60     rstatix  648757 2022-07-17 2022-08-15
12   62      nloptr  637641 2022-07-17 2022-08-15
13   63        lme4  623554 2022-07-17 2022-08-15
14   68    corrplot  601404 2022-07-17 2022-08-15
15   72       rJava  577002 2022-07-17 2022-08-15
16   75      ggpubr  544839 2022-07-17 2022-08-15
17   88     cowplot  493972 2022-07-17 2022-08-15
18   94         car  461659 2022-07-17 2022-08-15</code></pre>
</div>
</div>
<p>From some quick research, I have found the following about the new packages:</p>
<ul>
<li><code>ragg</code> - a 2D library as an alternative to the RStudio default</li>
<li><code>rgl</code> - functions for 3D interactive graphics</li>
<li><code>rgeos</code> - a geometry package, but is currently planned to be retired at the end of 2023 for the <code>sf</code> package</li>
<li><code>zoo</code> - a library to deal with time series</li>
<li><code>pkgdown</code> - a library fOR building a blog website, I use blogdown</li>
<li><code>nloptr</code> - a library for solving non-linear optimization problems</li>
<li><code>Hmisc</code> - an assortment of different data analysis tools</li>
<li><code>lme4</code> - for fitting linear and generalized linear mixed-effects models</li>
<li><code>RcppEigen</code> - integration of the <code>eigen</code> library in R for linear algebra</li>
</ul>
</section>
<section id="take-away" class="level2">
<h2 class="anchored" data-anchor-id="take-away">Take-away</h2>
<p>Hopefully your take-way is a simple method to explore R library that you have never heard about. I know that a few of the libraries seem interesting and worth further exploring.</p>
<p>While we are at it, might as well find the daily values for the new packages and plot them for the last month.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">new<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>package <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-2">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cran_downloads</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">when =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last-month"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> count, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> package)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://datasandbox.netlify.app/posts/2022-03-31-underrated-cran-packages/index_files/figure-html/plot-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>


</section>

 ]]></description>
  <category>Project</category>
  <category>R</category>
  <guid>https://datasandbox.netlify.app/posts/2022-03-31-underrated-cran-packages/</guid>
  <pubDate>Thu, 31 Mar 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-03-31-underrated-cran-packages/Rlogo.svg" medium="image" type="image/svg+xml"/>
</item>
<item>
  <title>Simple Neural Networks in Python</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-03-20-simple-neural-networks-in-python/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-03-20-simple-neural-networks-in-python/network.jpg" class="img-fluid"> Neural Networks (NN) have become incredibly popular due to their high level of accuracy. The creation of a NN can be complicated and have a high level of customization. I wanted to explore just the simplest NN that you could create. A framework as a workhorse for developing new NN.</p>
<p>The <code>SciKitlearn</code> provides the easiest solution with the Multi-Layer Perceptron series of functions. It doesn’t provide a bunch of the more advanced features of <code>TensorFlow</code>, like GPU support, but that is not what I’m looking for.</p>
<section id="initialization" class="level2">
<h2 class="anchored" data-anchor-id="initialization">Initialization</h2>
<p>For the demonstration, I decided to use a data set on faults found in <a href="https://www.openml.org/d/1504">steel plates</a> from the OpenML website. The data set includes 27 features with 7 binary predictors.</p>
<div class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> train_test_split</span>
<span id="cb1-4"></span>
<span id="cb1-5">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'https://www.openml.org/data/get_csv/1592296/php9xWOpn'</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7">predictors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V28'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V29'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V30'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V31'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V32'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'V33'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Class'</span>]</span>
<span id="cb1-8">df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Class'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> </span></code></pre></div>
</div>
<p>Since there are multiple binary predictors, I needed to create a single class variable to represent each class. The <code>Class</code> variable doesn’t currently represent this, it represents all faults that don’t fit in the categories of <code>V28</code> to <code>V33</code>. The single variable class was created with the <code>np.argmax</code> function which returns the index of the highest value between all the predictors.</p>
<div class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.argmax(df[predictors].values, axis <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-2">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.drop(predictors, axis <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-3">X_train, X_test, y_train, y_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_test_split(X, y, random_state<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</div>
</section>
<section id="modelling" class="level2">
<h2 class="anchored" data-anchor-id="modelling">Modelling</h2>
<p>This is the most basic model that I would like to evaluate. I’ve used the <code>GridSearch</code> function, so all combinations of parameters are tested. The only parameter I wanted to examine was the size of the hidden layers. Each hidden layer provided is a tuple, where each number represents the number of nodes in a singled layer. Multiple numbers represent additional layers.</p>
<div class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.neural_network <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> MLPClassifier</span>
<span id="cb3-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.model_selection <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> GridSearchCV</span>
<span id="cb3-3"></span>
<span id="cb3-4">parameters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hidden_layer_sizes'</span>:[(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb3-5">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb3-6">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb3-7">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb3-8">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb3-9">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb3-10">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb3-11">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)]}</span>
<span id="cb3-12">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MLPClassifier(random_state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,max_iter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, </span>
<span id="cb3-13">solver <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'adam'</span>, learning_rate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'adaptive'</span>)</span>
<span id="cb3-14"></span>
<span id="cb3-15">grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GridSearchCV(estimator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model, param_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> parameters)</span>
<span id="cb3-16">grid.fit(X_train, y_train)</span>
<span id="cb3-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(grid.best_score_)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>0.4054982817869416</code></pre>
</div>
</div>
<p>The performance of the best model in the grid is not impressive. It took me awhile to realize that I had forgotten to scale the features. I included this error to show the importance of scaling on model performance.</p>
</section>
<section id="feature-scaling" class="level2">
<h2 class="anchored" data-anchor-id="feature-scaling">Feature Scaling</h2>
<p>The features are simply scaled with the <code>StandardScaler</code> function. The same model is used on the scaled features.</p>
<div class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.preprocessing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> StandardScaler</span>
<span id="cb5-2"></span>
<span id="cb5-3">sc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> StandardScaler()</span>
<span id="cb5-4">scaler <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sc.fit(X_train)</span>
<span id="cb5-5">X_train_sc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scaler.transform(X_train)</span>
<span id="cb5-6">X_test_sc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> scaler.transform(X_test)</span>
<span id="cb5-7"></span>
<span id="cb5-8">parameters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hidden_layer_sizes'</span>:[(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb5-9">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb5-10">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb5-11">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), </span>
<span id="cb5-12">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb5-13">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb5-14">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb5-15">(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)]}</span>
<span id="cb5-16">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MLPClassifier(random_state <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,max_iter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, </span>
<span id="cb5-17">solver <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'adam'</span>, learning_rate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'adaptive'</span>)</span>
<span id="cb5-18"></span>
<span id="cb5-19">grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> GridSearchCV(estimator <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model, param_grid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> parameters, cv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb5-20">grid.fit(X_train_sc, y_train)</span>
<span id="cb5-21">grid.best_score_</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre><code>0.7553264604810996</code></pre>
</div>
</div>
<p>The performance of the scaled model is much more impressive. After the <code>GridSearch</code> function finds the parameters for the best model, it retrains the model on the entire dataset. This is because the function utilize cross validation, so some data was withheld for comparing the different models on test data.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>With our model constructed, we can now test its performance on the original test set. It is important to remember to use the scaled test features, as that is what the model is expecting.</p>
<div class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">grid.score(X_test_sc, y_test)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<pre><code>0.7304526748971193</code></pre>
</div>
</div>
<p>The results are pretty satisfactory. A decent level of accuracy without a lot of complicated code. Default values were used, whenever they were appropriate. Additional steps could be taken, but this remains a good foundation for future exploratory analysis.</p>
<blockquote class="blockquote">
<p>Photo by <a href="https://unsplash.com/@alinnnaaaa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Alina Grubnyak</a> on <a href="https://unsplash.com/s/photos/neural-networks?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</blockquote>


</section>

 ]]></description>
  <category>How-to</category>
  <category>Python</category>
  <category>NN</category>
  <guid>https://datasandbox.netlify.app/posts/2022-03-20-simple-neural-networks-in-python/</guid>
  <pubDate>Sun, 20 Mar 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-03-20-simple-neural-networks-in-python/network.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Making the Connection with Crosstalk</title>
  <dc:creator>Mark Edney</dc:creator>
  <link>https://datasandbox.netlify.app/posts/2022-03-18-making-the-connection-with-crosstalk/</link>
  <description><![CDATA[ 




<p><img src="https://datasandbox.netlify.app/posts/2022-03-18-making-the-connection-with-crosstalk/talk.jpg" class="img-fluid"> I recently wrote a post about <a href="https://datasandbox.netlify.app/post/2022-03-10-creating-dashboard-in-r/">creating dashboards in R</a> which was based on the <code>Flexdashboard</code> library. My largest criticism was the lack of communication between visualizations on the same dashboard. This was before I learned about the <code>Crosstalk</code> package which adds this feature to html widgets, such as the Flexdashboard, to at least a limited degree.</p>
<section id="initialization" class="level2">
<h2 class="anchored" data-anchor-id="initialization">Initialization</h2>
<p>The <code>Crosstalk</code> package is available on CRAN and is loaded along with other important packages for this demonstration.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"crosstalk"</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(crosstalk)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(flexdashboard)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(plotly)</span></code></pre></div>
</div>
<p>I have decided to use a Toronto Open dataset about city audits for apartment buildings. I limited the features to only the ones that I feel will be interesting to look at. More information about the data set can be found <a href="https://open.toronto.ca/dataset/apartment-building-evaluation/">here</a>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://ckan0.cf.opendata.inter.prod-toronto.ca/dataset/4ef82789-e038-44ef-a478-a8f3590c3eb1/resource/979fb513-5186-41e9-bb23-7b5cc6b89915/download/Apartment%20Building%20Evaluation.csv"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.csv"</span>)</span>
<span id="cb2-2">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lng =</span> LONGITUDE, </span>
<span id="cb2-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lat =</span> LATITUDE, </span>
<span id="cb2-5">               SCORE, </span>
<span id="cb2-6">               YEAR_BUILT, </span>
<span id="cb2-7">               SITE_ADDRESS, </span>
<span id="cb2-8">               PROPERTY_TYPE) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-9">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span></code></pre></div>
</div>
<p>The key to the <code>crosstalk</code> library is the <code>SharedData</code> functions. An object is created when a Data Frame is passed to the <code>SharedData$new</code> function. This is what enables communication between plots.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">shared_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SharedData<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new</span>(df)</span></code></pre></div>
</div>
</section>
<section id="dashboard-creation" class="level2">
<h2 class="anchored" data-anchor-id="dashboard-creation">Dashboard Creation</h2>
<p>The dashboard is created pretty much as previous mentioned in <a href="https://datasandbox.netlify.app/post/2022-03-10-creating-dashboard-in-r/">my dashboard post</a>, with the exception that the shared Data Frame object is passed rather than the Data Frame.</p>
<p>The dashboard can include filters that are very similar to the Shiny Apt filters, with the <code>filter_*</code> family of functions.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter_slider</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Score"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SCORE"</span>, shared_df, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>SCORE, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">round =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter_checkbox</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Property Type"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PROPERTY_TYPE"</span>, shared_df, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>PROPERTY_TYPE, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inline =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The <code>Crosstalk</code> package does add some significant connectivity to Flex Dashboards. It is relatively simple to use with some basic functions. It does have the issue of not working with aggregating data. The utility of finding the mean value of a selection is something <code>Tableu</code> and <code>PowerBI</code> are still superior at. I think that it is still a welcome improvement.</p>
</section>
<section id="final-dashboard" class="level2">
<h2 class="anchored" data-anchor-id="final-dashboard">Final Dashboard</h2>
<div class="cell">
<div class="cell-output-display">
<iframe title="Cross Talk Demo" width="100%" height="500" src="demo1.html"></iframe>
</div>
</div>
<p>Photo by <a href="https://unsplash.com/@jasongoodman_youxventures?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Jason Goodman</a>on <a href="https://unsplash.com/s/photos/discussion?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>


</section>

 ]]></description>
  <category>How-to</category>
  <category>R</category>
  <category>Shiny App</category>
  <category>GGPlot</category>
  <category>Rmarkdown</category>
  <category>Leaflet</category>
  <guid>https://datasandbox.netlify.app/posts/2022-03-18-making-the-connection-with-crosstalk/</guid>
  <pubDate>Fri, 18 Mar 2022 04:00:00 GMT</pubDate>
  <media:content url="https://datasandbox.netlify.app/posts/2022-03-18-making-the-connection-with-crosstalk/talk.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
