<?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>Muhammad Nakhaee&#39;s Blog</title>
<link>https://mcnakhaee.com/blog/</link>
<atom:link href="https://mcnakhaee.com/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Articles on data science, analytics, and technology</description>
<generator>quarto-1.8.25</generator>
<lastBuildDate>Fri, 14 Nov 2025 23:00:00 GMT</lastBuildDate>
<item>
  <title>MyNextChart: How I’m using AI for data visualization and it’s not what you think</title>
  <dc:creator>Muhammad Chenariyan Nakhaee</dc:creator>
  <link>https://mcnakhaee.com/blog/posts/mynextchart/</link>
  <description><![CDATA[ 





<section id="intro" class="level2">
<h2 class="anchored" data-anchor-id="intro">Intro</h2>
<p>This is a short blog post to introduce my new project, MyNextChart which came from my frustration with keeping track of 1000s of data visualizations inspirations I saw on the internet and my eternal struggle to categorize them and make sense of them.</p>
<p>I presented this project at PyData Amsterdam 2025 this year and you can find the recording here: <a href="https://www.youtube.com/watch?v=1234567890">MyNextChart: How I Use AI for Data Visualization and It’s not to make Chart Slop</a>. So this blog post will be just a short summary of the talk without going into the details of the project. I’ll write more about the technical details of how I built this project in a future post.</p>
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/QgeUo-6k4oA?start=5" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
<hr>
</section>
<section id="why-i-started-this-project" class="level2">
<h2 class="anchored" data-anchor-id="why-i-started-this-project">Why I started this project</h2>
<p>If you’re reading posts there is a big chance you have more than a 100 open tabs in your browser that you want to read later but never get to it? Or you have 1000s of saved bookmarks on your Twitter, LinkedIn, and other social media accounts that you never get to looked at them.</p>
<p>I suffer from the same problem but probably even worse. I have around a few thousands or more bookmarked tweets, a few hundred bookmarked LinkedIn posts, and I have a few hundred open tabs in my browsers on different computers that I wanted to read look at but never get to it. One time I even realized that I had open tabs from 3 years ago!</p>
<p>A large part of my bookmarked posts and tweets consist of data visualizations articles and charts that I find interesting and want to take inspiration from them for my own data visualizations. To alleviate my problem I have been using a few different applications like Notion to save and organize my bookmarked charts (and articles).</p>
<p>I have been using these saved charts for inspiration when I’m working on a new data visualization project or for challenges such as #TidyTuesday or #30daychartchallenge. And sometimes I also scroll through github repositories for contribution made by others in the #TidyTuesday and #30daychartchallenge to find certain ways of doing things that I can use in my own projects (for example, how create spiral line charts).</p>
<table class="caption-top table">
<colgroup>
<col style="width: 5%">
</colgroup>
<tbody>
<tr class="odd">
<td>However, I realized that it was not practical to save all the charts I find interesting in Notion. The incoming stream of new charts and articles is way more than what I can save in Notion.</td>
</tr>
<tr class="even">
<td>Besides that, the same problem still persisted! I almost never looked at the saved charts in my Notion database. And even when I did, it was hard to find what I was looking for among hundreds of saved charts. In the end, I realized that I was spending way more cataloging charts that actually looking at them or creaiing my own charts.</td>
</tr>
</tbody>
</table>
<p>At the time, I was working on our RAG (retrieval-augmented generation) solutions at my job (and I still do) and RAG became a hammer for me and every problem became naturally a nail.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://mcnakhaee.com/blog/posts/mynextchart/rag.png" class="img-fluid figure-img"></p>
<figcaption>My RAGnar hammer</figcaption>
</figure>
</div>
<p>So I thought what if instead of scrolling through my bookmarked charts and articles and hoping to find something interesting or useful, I could search for the type of charts that I’m looking for in natural language.</p>
<p>For example, what if I search for “circular bar charts” or “lection maps for germany” and get a list of relevant charts that match my query instead of looking through my large Notion gallery and hoping to spot something.</p>
<hr>
<p>So, this became the start of mynextchart.com project. In essence it uses AI and embeddings to ‘understand’ and making them semantically searchable. In my talk at PyData Amsterdam 2025 I explained how I built this project and the technical challenges of building a semantic search engine for data visualizations. You can watch the recorded talk on YouTube.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://mcnakhaee.com/blog/posts/mynextchart/arch.png" class="img-fluid figure-img"></p>
<figcaption>How the architecture works in a simple image</figcaption>
</figure>
</div>


<!-- -->

</section>

 ]]></description>
  <category>data-science</category>
  <category>python</category>
  <category>visualization</category>
  <guid>https://mcnakhaee.com/blog/posts/mynextchart/</guid>
  <pubDate>Fri, 14 Nov 2025 23:00:00 GMT</pubDate>
  <media:content url="https://mcnakhaee.com/blog/posts/mynextchart/featured.JPG" medium="image"/>
</item>
<item>
  <title>Covid-19 Trends in the Netherlands</title>
  <dc:creator>Muhammad Chenariyan Nakhaee</dc:creator>
  <link>https://mcnakhaee.com/blog/posts/covid-19-netherlands/</link>
  <description><![CDATA[ 





<p>Two weeks ago, I made <a href="https://www.linkedin.com/posts/muhammadcnakhaee_covid19-datavisualization-ggplot-activity-6726775611223764992-c_dk">a visualization</a> that shows how Covid-19 cases spread in the Netherlands from the beginning of March and how grim the situation looked. However, someone pointed out the fact that the number of tests has increased significantly. It means that my plot may exaggerate the Covid-19 situation in the Netherlands. Unfortunately, I could not find testing data for each Dutch municipality. Instead, I decided to use hospitalization admissions and deceased cases to see if we can indeed see a massive spread in the second wave of Covid-19 cases in the Netherlands.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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>(CoronaWatchNL)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gganimate)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(santoku)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(foreign)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>())</span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_update</span>(</span>
<span id="cb1-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#plot.background = element_rect(fill = '#FDF6E3',color = '#FDF6E3'),</span></span>
<span id="cb1-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Poppins Light'</span>),</span>
<span id="cb1-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb1-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Poppins Light'</span>,</span>
<span id="cb1-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb1-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-16">  ),</span>
<span id="cb1-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(</span>
<span id="cb1-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Poppins Light'</span>,</span>
<span id="cb1-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb1-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-21">  )</span>
<span id="cb1-22">)</span></code></pre></div></div>
</details>
</div>
<p>I created an R package called <a href="https://github.com/mcnakhaee/CoronaWatchNL">CoronaWatchNL</a> that allows you to access a wide range of Covid-19 datasets. I’ll use this package in this post to get Covid-19 cases, hospital admissions, and deaths for Dutch municipalities.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post uses R packages that need to be installed: <code>tidyverse</code>, <code>CoronaWatchNL</code>, <code>sf</code>, <code>gganimate</code>, <code>santoku</code>, <code>lubridate</code>, <code>foreign</code>. The code is shown for educational purposes but not executed in this rendered version.</p>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">municipalBoundaries <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;">st_read</span>(</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://geodata.nationaalgeoregister.nl/cbsgebiedsindelingen/wfs?request=GetFeature&amp;service=WFS&amp;version=2.0.0&amp;typeName=cbs_gemeente_2020_gegeneraliseerd&amp;outputFormat=json"</span></span>
<span id="cb2-3">  )</span>
<span id="cb2-4"></span>
<span id="cb2-5">daily_cases_per_municpality <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;">get_daily_cases_per_municipality</span>()</span>
<span id="cb2-6">populatuon_per_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;">get_population_per_region</span>()</span>
<span id="cb2-7"></span>
<span id="cb2-8">daily_cases_per_municpality <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> daily_cases_per_municpality <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;">inner_join</span>(populatuon_per_region, <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;">'Municipality_name'</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;">'Regions'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Date_of_publication =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(Date_of_publication),</span>
<span id="cb2-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_daily_total_cases =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</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;">as.numeric</span>(Total_reported) <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;">as.numeric</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;">Bevolking op 1 januari (aantal)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>),</span>
<span id="cb2-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_daily_hospital_admissions =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</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;">as.numeric</span>(Hospital_admission) <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;">as.numeric</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;">Bevolking op 1 januari (aantal)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>),</span>
<span id="cb2-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_daily_deceased =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</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;">as.numeric</span>(Deceased) <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;">as.numeric</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;">Bevolking op 1 januari (aantal)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb2-15">  )</span></code></pre></div></div>
</details>
</div>
<p>I compute the weekly average number of Covid-19 cases, hospitalizations, and deaths per 100,000 inhabitants in each municipality in the Netherlands. The following piece of code shows how I did this using R.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">weekly_cases <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> daily_cases_per_municpality <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">week =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round_date</span>(Date_of_publication , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'week'</span>))</span>
<span id="cb3-3"></span>
<span id="cb3-4">weekly_cases_per_municpality <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> weekly_cases <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;">group_by</span>(Municipality_name, week) <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;">summarise</span>(</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_weekly_total_cases =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(avg_daily_total_cases),</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_weekly_hospital_admissions =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(avg_daily_hospital_admissions),</span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_weekly_deceased =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(avg_daily_deceased)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-10">  <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="cb3-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d_avg_weekly_total_cases =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chop</span>(avg_weekly_total_cases, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">0.5</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">55</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)),</span>
<span id="cb3-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d_avg_hospital_admissions =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chop</span>(</span>
<span id="cb3-14">      avg_weekly_hospital_admissions,</span>
<span id="cb3-15">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">0.5</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;">2</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</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="cb3-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d_avg_weekly_deceased =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chop</span>(avg_weekly_deceased, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</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;">5</span>)))</span>
<span id="cb3-17"></span>
<span id="cb3-18">data_weekly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> municipalBoundaries <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">right_join</span>(weekly_cases_per_municpality,</span>
<span id="cb3-20">             <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">statnaam =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Municipality_name"</span>))</span></code></pre></div></div>
</details>
</div>
<p>I will create an animation that shows how Covid-19 cases spread in the Netherlands and which municipalities were and are hit hardest by the pandemic.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">make_animation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, var_name, pal, title) {</span>
<span id="cb4-2">  var_name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rlang<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;">enquo</span>(var_name)</span>
<span id="cb4-3">  data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#filter(week &gt; '2020-10-01') %&gt;%</span></span>
<span id="cb4-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">geom_sf</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;">fill =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!</span>var_name), <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;">'gray95'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values  =</span> pal) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">datum =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb4-10">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> title,</span>
<span id="cb4-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Date: {current_frame}'</span>,</span>
<span id="cb4-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Counts per 100,000'</span>,</span>
<span id="cb4-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Source: RIVM'</span></span>
<span id="cb4-14">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transition_manual</span>(week, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cumulative =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ease_aes</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sine"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enter_fade</span>(<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.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exit_fade</span>(<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.5</span>)</span>
<span id="cb4-19">}</span></code></pre></div></div>
</details>
</div>
<section id="covid-19-cases" class="level2">
<h2 class="anchored" data-anchor-id="covid-19-cases">Covid-19 Cases</h2>
<p>The first animation shows the number of infections in each municipality, from the start of the pandemic in February until recently. As you can see, the second wave, which began in late September, looks really terrifying. Note that there are some municipalities for which no data is available.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">pal_cases <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>
<span id="cb5-2">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gray95'</span>,</span>
<span id="cb5-3">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#fee440'</span>,</span>
<span id="cb5-4">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#FFBA08'</span>,</span>
<span id="cb5-5">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#FAA307'</span>,</span>
<span id="cb5-6">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#F48C06'</span>,</span>
<span id="cb5-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#E85D04'</span>,</span>
<span id="cb5-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#DC2F02'</span>,</span>
<span id="cb5-9">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#D00000'</span>,</span>
<span id="cb5-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#9D0208'</span>,</span>
<span id="cb5-11">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#6A040F'</span>,</span>
<span id="cb5-12">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#370617'</span>,</span>
<span id="cb5-13">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#03071e'</span></span>
<span id="cb5-14">    )</span>
<span id="cb5-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_animation</span>(data_weekly,d_avg_weekly_total_cases,pal_cases,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Average Weekly Number of Covid-19 Cases</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">per 100,000 Inhabitants in the Netherlands'</span>)</span></code></pre></div></div>
</details>
</div>
<p><img src="https://mcnakhaee.com/blog/posts/covid-19-netherlands/cases.gif" class="img-fluid"></p>
</section>
<section id="hospital-admissions" class="level2">
<h2 class="anchored" data-anchor-id="hospital-admissions">Hospital Admissions</h2>
<p>If we look at the number of hospital admissions, we see a different story. It seems that the number of hospitalizations was higher during the first wave of Covid-19 compared to the second wave, and mostly the southern parts of the Netherlands were hit harder than the rest of the Netherlands.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">pal_patients <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>
<span id="cb6-2">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gray95'</span>,</span>
<span id="cb6-3">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#caf0f8'</span>,</span>
<span id="cb6-4">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#ade8f4'</span>,</span>
<span id="cb6-5">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#90e0ef'</span>,</span>
<span id="cb6-6">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#48cae4'</span>,</span>
<span id="cb6-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#00b4d8'</span>,</span>
<span id="cb6-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#0096c7'</span>,</span>
<span id="cb6-9">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#0077b6'</span>,</span>
<span id="cb6-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#023e8a'</span>,</span>
<span id="cb6-11">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#03045e'</span>,</span>
<span id="cb6-12">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#03071e'</span></span>
<span id="cb6-13">)</span>
<span id="cb6-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_animation</span>(data_weekly,d_avg_hospital_admissions,pal_patients,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Average Weekly Number of Hospital Admissions</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">per 100,000 Inhabitants in the Netherlands'</span>)</span></code></pre></div></div>
</details>
</div>
<p><img src="https://mcnakhaee.com/blog/posts/covid-19-netherlands/admissions.gif" class="img-fluid"></p>
</section>
<section id="deaths" class="level2">
<h2 class="anchored" data-anchor-id="deaths">Deaths</h2>
<p>The trend for deceased patients looks similar to that of hospital admissions. The average number of deaths during the first wave of coronavirus was higher than the average number of deaths during the second wave.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">pal_deceased <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>
<span id="cb7-2">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gray95'</span>,</span>
<span id="cb7-3">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#fdc5f5'</span>,</span>
<span id="cb7-4">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#e0aaff'</span>,</span>
<span id="cb7-5">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#c77dff'</span>,</span>
<span id="cb7-6">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#9d4edd'</span>,</span>
<span id="cb7-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#7b2cbf'</span>,</span>
<span id="cb7-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#5a189a'</span>,</span>
<span id="cb7-9">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#3c096c'</span>,</span>
<span id="cb7-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#240046'</span>,</span>
<span id="cb7-11">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#10002b'</span></span>
<span id="cb7-12">)</span>
<span id="cb7-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_animation</span>(data_weekly,d_avg_weekly_deceased,pal_deceased,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Average Weekly Number of Deceased Patients</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">per 100,000 Inhabitants in the Netherlands'</span>)</span></code></pre></div></div>
</details>
</div>
<p><img src="https://mcnakhaee.com/blog/posts/covid-19-netherlands/deceased.gif" class="img-fluid"></p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>These different animations show us two distinct trends. On the one hand, we can see the number of confirmed cases rose rapidly during the second wave to affect almost all regions. On the other hand, the number of hospitalizations and deaths during the second wave slightly decreased. This might suggest that the rise in the number of cases is mainly driven by an increase in the number of tests. Alternatively, the virus might have become less deadly and severe.</p>


<!-- -->

</section>

 ]]></description>
  <category>R</category>
  <category>data visualization</category>
  <category>Covid-19</category>
  <guid>https://mcnakhaee.com/blog/posts/covid-19-netherlands/</guid>
  <pubDate>Fri, 21 May 2021 22:00:00 GMT</pubDate>
  <media:content url="https://mcnakhaee.com/blog/posts/covid-19-netherlands/featured.JPG" medium="image"/>
</item>
<item>
  <title>How Easy Is It to Understand What Donald Trump Says?</title>
  <dc:creator>Muhammad Chenariyan Nakhaee</dc:creator>
  <link>https://mcnakhaee.com/blog/posts/trump-speech-readability/</link>
  <description><![CDATA[ 





<p>Aside from their political differences, Donald Trump and Barack Obama have very contrasting personalities, traits, and characters. Obama is known to be a great communicator and an articulate speaker whose speeches are used in English classes to show how one should speak proper English. On the other hand, Trump is not the most eloquent English speaker or US president in history. Every now and then, you can find a clip on the web where Donald Trump is being mocked for the way he speaks or mispronounces words. This is so obvious that even non-native English speakers can notice how Trump’s speeches are very simple and inarticulate. Of course, this was not a bad thing for Trump at all. Actually, almost every political analyst that you see on the news talks about the fact that a vast majority of Trump’s fervent supporters are not college-educated Americans. We can attribute this to the fact that he knows how to speak to his audience and his base supporters using their language (although it is more likely that he cannot speak English better than this level).</p>
<p>This post will investigate how difficult it is to understand what each US politician talked about in the 2020 US Election cycle. I will use several readability metrics that can help us compute text comprehensibility. A wide range of these measures are implemented in the <a href="https://github.com/shivam5992/textstat">{<code>textstat</code>}</a> Python package, and it is super easy to calculate them using this package.</p>
<p>Aside from their political differences, Donald Trump and Barack Obama have very contrasting personalities, traits, and characters. Obama is known to be a great communicator and an articulate speaker whose speeches are used in English classes to show how one should speak proper English. On the other hand, Trump is not the most eloquent English speaker or US president in history. Every now and then, you can find a clip on the web where Donald Trump is mocked for the way he speaks or mispronounces words. This is so obvious that even non-native English speakers can notice how Trump’s speeches are very simple and inarticulate. Of course, this was not a bad thing for Trump at all. Actually, almost every political analyst you see on the news talks about the fact that a vast majority of Trump’s fervent supporters are not college-educated Americans. We can attribute this to the fact that he knows how to speak to his audience and his base supporters using their language (although it is more likely that he cannot speak English better than this level).</p>
<p>This post will investigate how difficult it is to understand what each US politician talked about in the 2020 US Election cycle. I will use several readability metrics that can help us compute text comprehensibility. A wide range of these measures is implemented in the <a href="https://github.com/shivam5992/textstat">{<code>textstat</code>}</a> Python package, and it is super easy to calculate them using this package.</p>
<p>I compiled a list of US Election-related speeches from rev.com and turned them into an R package called <a href="https://github.com/mcnakhaee/us2020election"><code>{us2020election}</code></a>. I use this package as my data source for my analysis. Like some of my other posts, I use Python to perform the analysis and R to visualize my results. Now let’s get started by importing the necessary packages.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This post uses R packages that need to be installed: <code>tidyverse</code>, <code>reticulate</code>, <code>ggthemes</code>, <code>us2020election</code>, <code>ggridges</code>. The code is shown for educational purposes but not executed in this rendered version.</p>
</div>
</div>
<p>I compiled a list of US Election-related speeches from rev.com and turned them into an R package called <a href="https://github.com/mcnakhaee/us2020election"><code>{us2020election}</code></a>. I use this package as my data source for my analysis. Like some of my other posts, I use Python to perform the analysis and R to visualize my results. Now, let’s get started by importing the necessary packages.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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>(reticulate)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggthemes)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(us2020election)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggridges)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_tufte</span>())</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_update</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="cb1-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</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;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Lobster'</span>),</span>
<span id="cb1-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</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;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Lobster'</span>),</span>
<span id="cb1-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</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;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Lobster'</span>),</span>
<span id="cb1-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#FDF6E3'</span>),</span>
<span id="cb1-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#FDF6E3'</span>))</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</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;">import</span> textstat</span></code></pre></div></div>
</details>
</div>
<p>There are several readability measures for English text included in {<code>textstat</code>}. Calculating these measures is very straightforward and easy. I will explain what each metric represents in more detail.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">us_election_speeches <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r.us_election_speeches</span>
<span id="cb3-2">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Flesch_Reading_Ease_formula'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.flesch_reading_ease(x))</span>
<span id="cb3-3">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gunning_fog'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.gunning_fog(x))</span>
<span id="cb3-4">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'smog_index'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.smog_index(x))</span>
<span id="cb3-5">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'automated_readability_index'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.automated_readability_index(x))</span>
<span id="cb3-6">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'coleman_liau_index'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.coleman_liau_index(x))</span>
<span id="cb3-7">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'linsear_write_formula'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.linsear_write_formula(x))</span>
<span id="cb3-8">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dale_chall_readability_score'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.dale_chall_readability_score(x))</span>
<span id="cb3-9">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text_standard'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.text_standard(x))</span>
<span id="cb3-10">us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text_standard_float'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> us_election_speeches[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: textstat.text_standard(x,float_output  <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></div>
</details>
</div>
<p>Let’s look at the resulting dataframe.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">us_election_speeches <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> py<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>us_election_speeches </span>
<span id="cb4-2">us_election_speeches <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;">glimpse</span>()</span></code></pre></div></div>
</details>
</div>
<p>Now I am going to visualize the changes in the distribution of speech complexity for each politician. To make things easier, I will select a list of politicians that I’d like to analyze in this post.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">speakers <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;">'Barack Obama'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pete Buttigieg'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mike Pence'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Elizabeth Warren'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bernie Sanders'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Donald Trump'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Kamala Harris'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Joe Biden'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mike Bloomberg'</span>)</span>
<span id="cb5-2">custom_palette <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>
<span id="cb5-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mike Bloomberg'</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;">'#4E79A7'</span>,</span>
<span id="cb5-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amy Klobuchar'</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;">'#4E79A7'</span>,</span>
<span id="cb5-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Joe Biden'</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;">'#4E79A7'</span>,</span>
<span id="cb5-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pete Buttigieg'</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;">'#4E79A7'</span>,</span>
<span id="cb5-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Elizabeth Warren'</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;">'#4E79A7'</span>,</span>
<span id="cb5-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Barack Obama'</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;">'#4E79A7'</span>,</span>
<span id="cb5-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bernie Sanders'</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;">'#4E79A7'</span>,</span>
<span id="cb5-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Kamala Harris'</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;">'#4E79A7'</span>,</span>
<span id="cb5-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Donald Trump'</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;">'#E15759'</span> ,</span>
<span id="cb5-12">     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mike Pence'</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;">'#E15759'</span> </span>
<span id="cb5-13">  )</span></code></pre></div></div>
</details>
</div>
<p>Also, I created a function to make ridge plots for each metric easier.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">create_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metric =</span> Flesch_Reading_Ease_formula,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> subtitle) {</span>
<span id="cb6-2">  metrics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rlang<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;">enquo</span>(metric)</span>
<span id="cb6-3">  us_election_speeches <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;">separate_rows</span>(speaker, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</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;">%&gt;%</span></span>
<span id="cb6-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(speaker <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> speakers, type <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;">'Debate'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_count</span>(speaker) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density_ridges</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb6-9">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!</span>metrics ,</span>
<span id="cb6-10">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> speaker,</span>
<span id="cb6-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> speaker</span>
<span id="cb6-12">    )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</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> <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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"How Easy Is It to Comprehend Different US Politicians?"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_wrap</span>(subtitle,<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;">100</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> custom_palette) </span>
<span id="cb6-15">}</span></code></pre></div></div>
</details>
</div>
<p>Now, let’s look at several readability measures in more depth.</p>
<section id="flesch-reading-ease-scores" class="level2">
<h2 class="anchored" data-anchor-id="flesch-reading-ease-scores">Flesch Reading Ease Scores</h2>
<p>The first readability score that I will look at is based on the Flesch Reading Ease formula. It computes the number of syllables to determine how easy a piece of text is. The maximum value of Flesch Reading Ease is 122, and there is no minimum value for it. Higher Flesch Reading Ease scores indicate that the text (speech) is easier to understand by the audience. In our case, it would show how sophisticated each politician is in terms of language use. You can find more about this metric on <a href="https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests">Wikipedia</a>!</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_plot</span>(Flesch_Reading_Ease_formula ,</span>
<span id="cb7-2">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Flesch Reading Ease scores measure the complexity of a text document. Higher scores indicate a text is easier to comprehend.'</span>)</span></code></pre></div></div>
</details>
</div>
<p>We can interpret the scores using the following table:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 13%">
<col style="width: 20%">
<col style="width: 66%">
</colgroup>
<thead>
<tr class="header">
<th>Score</th>
<th>School level</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>100.00–90.00</td>
<td>5th grade</td>
<td>Very easy to read. Easily understood by an average 11-year-old student.</td>
</tr>
<tr class="even">
<td>90.0–80.0</td>
<td>6th grade</td>
<td>Easy to read. Conversational English for consumers.</td>
</tr>
<tr class="odd">
<td>80.0–70.0</td>
<td>7th grade</td>
<td>Fairly easy to read.</td>
</tr>
<tr class="even">
<td>70.0–60.0</td>
<td>8th &amp; 9th grade</td>
<td>Plain English. Easily understood by 13- to 15-year-old students.</td>
</tr>
<tr class="odd">
<td>60.0–50.0</td>
<td>10th to 12th grade</td>
<td>Fairly difficult to read.</td>
</tr>
<tr class="even">
<td>50.0–30.0</td>
<td>College</td>
<td>Difficult to read.</td>
</tr>
<tr class="odd">
<td>30.0–10.0</td>
<td>College graduate</td>
<td>Very difficult to read. Best understood by university graduates.</td>
</tr>
<tr class="even">
<td>10.0–0.0</td>
<td>Professional</td>
<td>Extremely difficult to read. Best understood by university graduates.</td>
</tr>
</tbody>
</table>
</section>
<section id="gunning-fog-index" class="level2">
<h2 class="anchored" data-anchor-id="gunning-fog-index">Gunning Fog Index</h2>
<p><a href="https://en.wikipedia.org/wiki/Gunning_fog_index">The Gunning fog index</a> is another metric to measure the complexity of a text document. It shows how many years of education one might need to understand a piece of text. Larger values of the Gunning fog index correspond to more difficult writings.</p>
<p><a href="https://en.wikipedia.org/wiki/Gunning_fog_index">The Gunning fog index</a> is another metric to measure the complexity of a text document. It shows how many years of education one might need to understand a piece of text. Larger values of the Gunning fog index correspond to more difficult writing.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_plot</span>(gunning_fog,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Gunning fog index measures the complexity of a text document. Larger values of the Gunning fog index correspond to more difficult writings.'</span> )</span></code></pre></div></div>
</details>
</div>
</section>
<section id="the-smog-index" class="level2">
<h2 class="anchored" data-anchor-id="the-smog-index">The SMOG Index</h2>
<p><a href="https://en.wikipedia.org/wiki/SMOG">The SMOG index</a> computes the ratio of polysyllables (words with three or more syllables) in sentences to determine text complexity.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_plot</span>(smog_index,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The SMOG index measures the complexity of a text document. Larger values of the SMOG index indicate more difficult writings.'</span> )</span></code></pre></div></div>
</details>
</div>
</section>
<section id="linsear-write-formula" class="level2">
<h2 class="anchored" data-anchor-id="linsear-write-formula">Linsear Write Formula</h2>
<p>Like the previous metric, <a href="https://en.wikipedia.org/wiki/Linsear_Write">the Linsear Write Formula</a> uses words with three or more syllables to compute text readability. It also relies on the sentence length to measure how difficult reading a text could be.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_plot</span>(linsear_write_formula, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Linsear Write Formula measures the complexity of a text document. Larger values indicate more difficult writings.'</span>)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="dale-chall-readability-score" class="level2">
<h2 class="anchored" data-anchor-id="dale-chall-readability-score">Dale-Chall Readability Score</h2>
<p>This metric is different from the other metrics that we have talked about. It uses a dictionary of 3000 words that are easy to read and understand for a fourth-grade student. So, words that are not in this dictionary are considered to be complex. The higher the Dale-Chall Score is, the more difficult it is to read a text.</p>
<p>This metric is different from the other metrics that we have talked about. It uses a dictionary of 3,000 words that are easy to read and understand for a fourth-grade student. So, words that are not in this dictionary are considered to be complex. The higher the Dale-Chall Score is, the more difficult it is to read a text.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_plot</span>(dale_chall_readability_score,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The Dale-Chall Readability Score measures the complexity of a text document. The higher the Dale-Chall Score is, the more difficult it is to read a text.'</span>)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="a-unified-readability-score" class="level2">
<h2 class="anchored" data-anchor-id="a-unified-readability-score">A Unified Readability Score</h2>
<p>We introduced several readability metrics, but each one of them might give us a slightly different result. There is a way in <code>textstat</code> to combine all these metrics and have a single readability metric.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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">us_election_speeches <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(speaker <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> speakers) <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text_standard =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_replace</span>(text_standard,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' and '</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span>),</span>
<span id="cb12-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text_standard =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb12-5">    text_standard,</span>
<span id="cb12-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb12-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'4th-5th grade'</span>,</span>
<span id="cb12-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'5th-6th grade'</span>,</span>
<span id="cb12-9">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'6th-7th grade'</span>,</span>
<span id="cb12-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'7th-8th grade'</span>,</span>
<span id="cb12-11">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'8th-9th grade'</span>,</span>
<span id="cb12-12">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'9th-10th grade'</span>,</span>
<span id="cb12-13">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'10th-11th grade'</span>,</span>
<span id="cb12-14">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'11th-12th grade'</span>,</span>
<span id="cb12-15">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'12th-13th grade'</span>,</span>
<span id="cb12-16">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'14th-15th grade'</span></span>
<span id="cb12-17">    )</span>
<span id="cb12-18">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(speaker, text_standard) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-20">  <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>()  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</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> text_standard , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span>  n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> speaker)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</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> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Number of Speeches'</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;">"How Easy Is It to Understand Trump's Speeches?"</span>,</span>
<span id="cb12-24">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Based on several readability tests, the education level that one needs to comprehend the 2020 Election speeches by different US politicians is illustrated in this plot.'</span> ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> custom_palette) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> speaker, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</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="cb12-28">  <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  =</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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>),</span>
<span id="cb12-29">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">l =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb12-30">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb12-31">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</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;">size =</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;">margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb12-32">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</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;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>))</span></code></pre></div></div>
</details>
</div>
<p>Interestingly, we can observe that Trump never gave a speech to an audience with difficulty more than the 7th or 8th grade. We can also convert this readability metric to numbers to visualize and compare it to other metrics.</p>
<p>Interestingly, we can observe that Trump never gave a speech to an audience with a difficulty higher than the 7th or 8th grade. We can also convert this readability metric to numbers to visualize and compare it to other metrics.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_plot</span>(text_standard_float,</span>
<span id="cb13-2">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'The complexity of a text document was measured based on several readability metrics where larger values indicate more difficult writings.'</span>)</span></code></pre></div></div>
</details>
</div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>We can consistently see that Trump’s speeches are less sophisticated and less complex than the speeches given by the rest of the politicians. We can attribute this to his lack of sophistication in terms of language, the fact that he knows how to speak to his audience, or both. Also, we can notice that Mike Pence and Barack Obama seem to use more advanced language in their speeches.</p>


<!-- -->

</section>

 ]]></description>
  <category>politics</category>
  <category>data analysis</category>
  <category>NLP</category>
  <category>python</category>
  <category>R</category>
  <guid>https://mcnakhaee.com/blog/posts/trump-speech-readability/</guid>
  <pubDate>Mon, 02 Nov 2020 23:00:00 GMT</pubDate>
  <media:content url="https://mcnakhaee.com/blog/posts/trump-speech-readability/featured.JPG" medium="image"/>
</item>
</channel>
</rss>
