<?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>Chris Lee</title>
<link>https://github.com/clfee/home/gallery.html</link>
<atom:link href="https://github.com/clfee/home/gallery.xml" rel="self" type="application/rss+xml"/>
<description>Data science applications brought to you by Chris Lee</description>
<image>
<url>https://github.com/clfee/home/assets/img/cmm1.PNG</url>
<title>Chris Lee</title>
<link>https://github.com/clfee/home/gallery.html</link>
</image>
<generator>quarto-1.4.554</generator>
<lastBuildDate>Tue, 25 Feb 2025 06:00:00 GMT</lastBuildDate>
<item>
  <title>Demand Forecasting: Building a Base Model &amp; Sharing through a Shiny App</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2025-02-25-demand-forecasting/</link>
  <description><![CDATA[ 




<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<p>Forecasting has always been at the forefront of decision making and planning.</p>
<p>The uncertainty that surrounds the future is both exciting and challenging, with individuals and organizations seeking to minimize risks and maximize utilities. In the last article we completed an EDA of the sales data and discovered that promotions boosted sales in some but not all stores.Here we will forecast at the order_id crossed with center_id level.</p>
<p>We want to try a couple different models, including exponential smoothing(ets) and tbats from the forecast package and compare them against baseline models including naive, seasonal naive, and mean based forecasts. Ideally we will find a model type that performs best across all levels and can serve as our base model in production. Once we compare these models and find a base model, we can also look deeper into the results to try to build ideas for further improvement upon our base model.</p>
<p>The paper provides a detailed discussion of various forecasting methods, comparing them across different dimensions such as accuracy, complexity, and adaptability.</p>
<p>Classical statistical methods are based on time series modeling and statistical inference. Exponential Smoothing (ETS) and Autoregressive Integrated Moving Average (ARIMA) are commonly used as baselines in forecastingcompetitions.</p>
<p>Kaggle - <a href="https://www.kaggle.com/code/anoopashware/food-demand-forecasting-predict-orders">Food Demand Forecasting</a>.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Shiny</category>
  <category>Forecasting</category>
  <guid>https://github.com/clfee/home/posts/2025-02-25-demand-forecasting/</guid>
  <pubDate>Tue, 25 Feb 2025 06:00:00 GMT</pubDate>
</item>
<item>
  <title>Forecasting: Exploratory Data Analaysis</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/</link>
  <description><![CDATA[ 




<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<p><strong>Why Forecasting Matters for Our Business</strong></p>
<p>Predicting customer demand is crucial for growing business. Without reliable forecasts, we risk stockouts or excess inventory, leading to wasted resources. This is especially important for our wedding and event catering service, where we work with perishable ingredients. By precisely estimating daily and weekly demand, we can optimize inventory, reduce waste, control costs, and consistently meet customer expectations.</p>
<p>Before diving into forecasting, we’ll start with data exploration. This includes analyzing variable distributions, handling missing values, and uncovering key insights. For this analysis, we’ll use a dataset from Kaggle. You can explore it here: Kaggle - <a href="https://www.kaggle.com/code/anoopashware/food-demand-forecasting-predict-orders">Food Demand Forecasting</a>.</p>
<p>The data contains information from 77 stores over a period of 145 weeks.<br>
Two types of promotions (web, email) were shown in the data as below:<br>
- 10.61% stores ran one type of promotion.<br>
- 4.21% stores ran both types at the same time.<br>
- 85.18% stores didn’t run any promotions.</p>
<p>Our initial analysis is aim to discover:<br>
(1) Did revenue or profit increase?<br>
(2) Which promotion worked best?<br>
(3) To predict future demand for our menu items.</p>
<section id="data-summary" class="level3">
<h3 class="anchored" data-anchor-id="data-summary">Data Summary</h3>
<p>The data contains no missing values.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb1-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Overview data</span></span>
<span id="cb1-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: "Indentify missing values"</span></span>
<span id="cb1-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb1-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb1-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check for missing values</span></span>
<span id="cb1-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">kable(colSums(is.na(train)))</span></span>
<span id="cb1-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">x</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">id</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">week</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">center_id</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">meal_id</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">checkout_price</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">base_price</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">emailer_for_promotion</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">homepage_featured</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">num_orders</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">city_code</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">region_code</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">center_type</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">op_area</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">category</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">cuisine</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">promotions</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">profit</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">promotion_flag</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
<p>Indentify missing values</p>
</div>
</div>
<section id="categorical-variables" class="level4">
<h4 class="anchored" data-anchor-id="categorical-variables">Categorical Variables</h4>
<p>Center Type: Three types, with ‘TYPE_A’ being the most frequent.<br>
Meal Category: Fourteen categories, ‘Beverages’ being the most frequent.<br>
Cuisine: Four types, with ‘Italian’ being the most common.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb2-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Overview categorical data</span></span>
<span id="cb2-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: " Distribution of the categorical variables"</span></span>
<span id="cb2-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb2-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb2-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb2-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">freq_data &lt;- function(df){</span></span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  df &lt;- as.data.frame(df)</span></span>
<span id="cb2-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  results_list &lt;- vector("list", ncol(df) - 1)</span></span>
<span id="cb2-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  </span></span>
<span id="cb2-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # Loop through the columns starting from the 2nd column</span></span>
<span id="cb2-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  for (i in 2:ncol(df)) {</span></span>
<span id="cb2-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       # Store the result of cal_freq(i) in the list</span></span>
<span id="cb2-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       results_list[[i - 1]] &lt;- cal_freqx(i, df)</span></span>
<span id="cb2-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      }</span></span>
<span id="cb2-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  df &lt;- do.call(rbind, results_list)</span></span>
<span id="cb2-20"></span>
<span id="cb2-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # label bar graph</span></span>
<span id="cb2-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  df$vars &lt;- ifelse(df$Freqx &gt; 0.08, as.character(df$Var1), "")</span></span>
<span id="cb2-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  return(df)</span></span>
<span id="cb2-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="cb2-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> p &lt;-  freq_data(train |&gt; select(-promotion_flag) |&gt; mutate(store_id = as.factor(center_id)))</span></span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> p |&gt;</span></span>
<span id="cb2-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       ggplot(aes(x = round(Freqx,2), y = fct_rev(namex), fill = Var1)) + </span></span>
<span id="cb2-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       geom_col(position = "fill", color = "white") +</span></span>
<span id="cb2-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       scale_x_continuous(labels = label_percent()) +</span></span>
<span id="cb2-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       labs(title = "Distribution of Categorical Variables:",</span></span>
<span id="cb2-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       subtitle = paste("Top to down :" , unique(p$namex)[2],"," ,unique(p$namex)[1],"," , unique(p$namex)[3],"," , unique(p$namex)[4],"," , unique(p$namex)[5]),y = NULL, x = NULL, fill = "var1")+</span></span>
<span id="cb2-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       guides(fill="none") +</span></span>
<span id="cb2-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       geom_text(aes(label = vars), position = position_stack(vjust = 0.5),</span></span>
<span id="cb2-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                 color = "white")+</span></span>
<span id="cb2-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      theme_void()</span></span>
<span id="cb2-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/Overview categorical data-1.png" class="img-fluid figure-img" width="768"></p>
<figcaption>Distribution of the categorical variables</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="numerical-variables" class="level4">
<h4 class="anchored" data-anchor-id="numerical-variables">Numerical Variables</h4>
<p>ID Variables: id, week, center_id, meal_id<br>
Pricing Variables: checkout_price, base_price<br>
Demand Variable (Target): num_orders<br>
Location and Size Variables: city_code, region_code, op_area</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb3-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb3-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: data distribution</span></span>
<span id="cb3-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: ""</span></span>
<span id="cb3-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb3-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb3-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 4</span></span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the original distribution of num_orders</span></span>
<span id="cb3-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">s1 &lt;- </span></span>
<span id="cb3-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  train|&gt;</span></span>
<span id="cb3-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = (num_orders))) +</span></span>
<span id="cb3-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_histogram(bins = 50, fill = "steelblue", alpha = 0.7) +</span></span>
<span id="cb3-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_density(alpha = 0.5, color = "#d88bb4") +</span></span>
<span id="cb3-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggtitle("Distribution of num_orders") +</span></span>
<span id="cb3-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  xlab("num_orders") +</span></span>
<span id="cb3-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ylab("Frequency")+</span></span>
<span id="cb3-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()</span></span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">s2 &lt;- </span></span>
<span id="cb3-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  train|&gt;</span></span>
<span id="cb3-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = (checkout_price))) +</span></span>
<span id="cb3-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_histogram(bins = 50, fill = "steelblue", alpha = 0.7) +</span></span>
<span id="cb3-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_density(alpha = 0.3, color = "#d88bb4", outline.type = "upper") +</span></span>
<span id="cb3-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggtitle("Distribution of checkout_price") +</span></span>
<span id="cb3-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  xlab("checkout_price") +</span></span>
<span id="cb3-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ylab("Frequency")+</span></span>
<span id="cb3-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()</span></span>
<span id="cb3-28"></span>
<span id="cb3-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">s3 &lt;- </span></span>
<span id="cb3-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  train|&gt;</span></span>
<span id="cb3-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = (base_price))) +</span></span>
<span id="cb3-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_histogram(bins = 50, fill = "steelblue", alpha = 0.7) +</span></span>
<span id="cb3-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_density(alpha = 0.5, color = "#d88bb4") +</span></span>
<span id="cb3-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggtitle("Distribution of base_price") +</span></span>
<span id="cb3-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  xlab("base_price") +</span></span>
<span id="cb3-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ylab("Frequency")+</span></span>
<span id="cb3-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()</span></span>
<span id="cb3-38"></span>
<span id="cb3-39"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">s4 &lt;- </span></span>
<span id="cb3-40"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  train|&gt;</span></span>
<span id="cb3-41"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = (op_area ))) +</span></span>
<span id="cb3-42"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_histogram(bins = 50, fill = "steelblue", alpha = 0.7) +</span></span>
<span id="cb3-43"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_density(alpha = 0.5, color = "#d88bb4") +</span></span>
<span id="cb3-44"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggtitle("Distribution of base_price") +</span></span>
<span id="cb3-45"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  xlab("operation area") +</span></span>
<span id="cb3-46"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ylab("Frequency")+</span></span>
<span id="cb3-47"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()</span></span>
<span id="cb3-48"></span>
<span id="cb3-49"></span>
<span id="cb3-50"></span>
<span id="cb3-51"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#kable(summary(train))</span></span>
<span id="cb3-52"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#grid.arrange(s1, s2, s3, ncol = 3)</span></span>
<span id="cb3-53"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">plot_grid(s1, s2, s3, s4, labels="AUTO")</span></span>
<span id="cb3-54"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/data distribution-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="time-series-engineering" class="level4">
<h4 class="anchored" data-anchor-id="time-series-engineering">Time series engineering&nbsp;</h4>
<p>Computes correlations to identify feature importance.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb4-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: time series engineering</span></span>
<span id="cb4-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: ""</span></span>
<span id="cb4-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb4-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb4-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">df_filtered &lt;- train |&gt;</span></span>
<span id="cb4-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  #filter(center_id %in% c(24:26))|&gt;</span></span>
<span id="cb4-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(week,center_id,meal_id, promotion_flag) |&gt;</span></span>
<span id="cb4-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  summarise(avg_order = mean(num_orders), .groups = 'drop')|&gt;</span></span>
<span id="cb4-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  arrange(week) |&gt;</span></span>
<span id="cb4-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(</span></span>
<span id="cb4-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    num_orders_lag_1 = lag(avg_order, 1, default = 0),</span></span>
<span id="cb4-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    num_orders_lag_2 = lag(avg_order, 2, default = 0),</span></span>
<span id="cb4-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    num_orders_lag_4 = lag(avg_order, 4, default = 0),</span></span>
<span id="cb4-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    num_orders_ma_2 = rollapply(avg_order, width = 2, FUN = mean, fill = NA, align = "right"),</span></span>
<span id="cb4-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    num_orders_ma_4 = rollapply(avg_order, width = 4, FUN = mean, fill = NA, align = "right"),</span></span>
<span id="cb4-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    week_sin = sin(2 * pi * week / 52),</span></span>
<span id="cb4-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    week_cos = cos(2 * pi * week / 52)</span></span>
<span id="cb4-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  )</span></span>
<span id="cb4-22"></span>
<span id="cb4-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------- Compute Correlation and Feature Importance -----</span></span>
<span id="cb4-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> cor_matrix_menu &lt;- cor(df_filtered |&gt; select( -promotion_flag), use = "complete.obs")</span></span>
<span id="cb4-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">corrplot(cor_matrix_menu,  method = "color",col = COL2('PiYG', 100), addCoef.col = "black", tl.col = "black", type = 'lower', tl.srt = 45)</span></span>
<span id="cb4-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/time series engineering-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>Since we are trying to forecast future demand, we can plot sales and profit of different promotions over time. To do this, we average the data across stores, as this can allow smoothing of the noise to see more of the signal.<br>
</p>
<section id="visulization-effect-of-promotion-types-on-weekly-profit-in-each-store" class="level3">
<h3 class="anchored" data-anchor-id="visulization-effect-of-promotion-types-on-weekly-profit-in-each-store">Visulization: Effect of Promotion types on Weekly Profit in each store</h3>
<p>As one of our goals is to identify patterns and correlations between promotion types, individual store performance, and weekly profit trends. We first used a heatmap to help us visualize how well our stores are performing over time and how different promotions impact our profits. Each cell in the heatmap represents the average profit for a specific store (rows) during a specific week (columns). The color intensity (ranging from light to dark green) indicates the magnitude of average profit, with darker greens representing higher profits. The Color bar represents a profit from 10.0 to 20.0. The border of the cell represents the promotion type being carried out in the store for the corresponding week. Namely Promotion, Double promotion, Email promotion, No promotion, and Homepage promotion.<br>
</p>
<ol type="1">
<li>While some stores (e.g., Store IDs around 40-50) consistently show higher profits (darker green), other stores (e.g., Store IDs around 140-150) show consistently lower profits (lighter green). This indicates that the strategies for these stores need a revisit.<br>
</li>
<li>Weeks with promotions (identified by the border type) often correlate with higher profits (darker green cells). However, the effectiveness varies.<br>
</li>
<li>Patterns in the heatmap suggest potential seasonal cycles, with profit fluctuations happening in waves.</li>
</ol>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb5-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb5-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Heatmaps</span></span>
<span id="cb5-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: " How Promotions Impact overall Profit?"</span></span>
<span id="cb5-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb5-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb5-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb5-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#library(pheatmap) ## for heatmap generation</span></span>
<span id="cb5-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#library(ggplotify) ## to convert pheatmap to ggplot2</span></span>
<span id="cb5-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#library(heatmaply) ## for constructing interactive heatmap</span></span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">tstx &lt;- train |&gt;</span></span>
<span id="cb5-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    group_by(promotion_flag, center_id) |&gt;</span></span>
<span id="cb5-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      dplyr::summarise(`Average Profit (log2)` = log(mean(profit),2))</span></span>
<span id="cb5-16"></span>
<span id="cb5-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">testx &lt;- tstx |&gt;</span></span>
<span id="cb5-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  tidyr::pivot_wider(names_from = center_id, values_from = `Average Profit (log2)`)|&gt;</span></span>
<span id="cb5-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(across(everything(), ~replace_na(.x, 0)))</span></span>
<span id="cb5-20"></span>
<span id="cb5-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">testx &lt;- data.frame(testx)</span></span>
<span id="cb5-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">rownames(testx) &lt;- testx$promotion_flag</span></span>
<span id="cb5-23"></span>
<span id="cb5-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">flag &lt;- data.frame(testx$promotion_flag)</span></span>
<span id="cb5-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">rownames(flag) &lt;- testx$promotion_flag</span></span>
<span id="cb5-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">colnames(flag) &lt;- "promotions"</span></span>
<span id="cb5-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">cols &lt;- colorRampPalette(c( "white", "lightgreen", "darkgreen"))(100)  </span></span>
<span id="cb5-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">pheatmap(testx[,-c(1)],cutree_cols=2, cutree_rows=2, color = cols)</span></span>
<span id="cb5-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/Heatmaps-1.png" class="img-fluid figure-img" width="768"></p>
<figcaption>How Promotions Impact overall Profit?</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb6-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb6-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Overview profit with heatmap </span></span>
<span id="cb6-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: "Overview profit with heatmap "</span></span>
<span id="cb6-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb6-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb6-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 10</span></span>
<span id="cb6-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 10</span></span>
<span id="cb6-8"></span>
<span id="cb6-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#--------------- Heatmap overall------</span></span>
<span id="cb6-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">train |&gt;</span></span>
<span id="cb6-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    group_by(week, promotion_flag, center_id) |&gt;</span></span>
<span id="cb6-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    dplyr::summarise(`Average Profit (log2)` = log(mean(profit),2))|&gt;</span></span>
<span id="cb6-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = as.factor(week), y = as.factor(center_id), fill = `Average Profit (log2)`)) +</span></span>
<span id="cb6-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_tile(aes(color = promotion_flag), linewidth = 0.5) + # Color outline for promotion</span></span>
<span id="cb6-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  scale_fill_gradient(low = "ivory", high = "darkgreen", name = "Average profit") +</span></span>
<span id="cb6-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">scale_color_manual(values = c("Double promotion" = "red", "Email promotion" =</span></span>
<span id="cb6-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            "yellow", "Homepage promotion" = "orange","No promotion" = "white"),</span></span>
<span id="cb6-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                    name = "Promotion") +</span></span>
<span id="cb6-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  </span></span>
<span id="cb6-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = " ", #Heatmap of Weekly Profit and Promotions per Store</span></span>
<span id="cb6-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       x = "Week", y = "Store ID") +</span></span>
<span id="cb6-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal() +</span></span>
<span id="cb6-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(legend.position="bottom",</span></span>
<span id="cb6-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))</span></span>
<span id="cb6-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/Overview profit with heatmap-1.png" class="img-fluid figure-img" width="960"></p>
<figcaption>Overview profit with heatmap</figcaption>
</figure>
</div>
</div>
</div>
<p>Since we are trying to forecast future demand, we can plot sales and profit of different promotions over time. To do this, we average the data across stores, as this can allow smoothing of the noise to see more of the signal.</p>
</section>
<section id="visulization-average-sales-and-revenue-over-time" class="level3">
<h3 class="anchored" data-anchor-id="visulization-average-sales-and-revenue-over-time">Visulization: Average Sales and Revenue Over Time</h3>
<p>The blue line (No Promotions) is consistently lower and less volatile compared to other lines (Homepage promotion, Email promotion, Double promotion) in figure 1A. This suggests that promotions drive short-term spikes in sales, but the effect is not stable over time. There are frequent peaks and drops, indicating that sales surge when promotions are active but return to normal levels afterward. The profit trend (Figure 1B) mirrors the sales trend, confirming that higher sales during promotions contribute to revenue increases.However, the fluctuations in revenue suggest that while sales increase, revenue might not be growing at the same rate, possibly due to discounting.</p>
<p><em>Note:</em> The non-promoted sales and revenue (blue line) remain relatively stable over time. The promoted periods exhibit more volatility, meaning promotions may be shifting demand rather than creating long-term sales growth.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb7-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb7-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Time series plots of sales volume and revenue</span></span>
<span id="cb7-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: " "</span></span>
<span id="cb7-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb7-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb7-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb7-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb7-8"></span>
<span id="cb7-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------</span></span>
<span id="cb7-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Time Series Plot of Sales Volume &amp; Revenue</span></span>
<span id="cb7-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------</span></span>
<span id="cb7-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">sales_trend &lt;- train |&gt;</span></span>
<span id="cb7-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(week, promotion_flag) |&gt;</span></span>
<span id="cb7-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  dplyr::summarise(average_sales = log(mean(num_orders),2), </span></span>
<span id="cb7-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            average_profit = log(mean(profit),2),</span></span>
<span id="cb7-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            .groups = 'drop')</span></span>
<span id="cb7-17"></span>
<span id="cb7-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot Sales Volume Over Time</span></span>
<span id="cb7-19"></span>
<span id="cb7-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> a1 &lt;-</span></span>
<span id="cb7-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   ggplot(sales_trend, aes(x = week, y = average_sales, color = as.factor(promotion_flag))) +</span></span>
<span id="cb7-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_line(size = 1) +</span></span>
<span id="cb7-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = "1A) Average Sales Volume Over Time", x = "", y = "Average Sales (log2)", color = "Promotion") +</span></span>
<span id="cb7-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb7-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   theme(legend.title=element_blank(),</span></span>
<span id="cb7-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         legend.position="bottom",</span></span>
<span id="cb7-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    panel.background = element_blank())+</span></span>
<span id="cb7-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(axis.line = element_line(color = 'black'))+</span></span>
<span id="cb7-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    guides(color="none")</span></span>
<span id="cb7-30"></span>
<span id="cb7-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot Revenue Over Time</span></span>
<span id="cb7-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> a2 &lt;-</span></span>
<span id="cb7-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(sales_trend, aes(x = week, y = average_profit, color = as.factor(promotion_flag))) +</span></span>
<span id="cb7-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_line(size = 0.8) +</span></span>
<span id="cb7-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = "1B) Profit Over Time", x = "Week", y = "Average Profit (log2)", color = "Promotion") +</span></span>
<span id="cb7-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb7-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   theme(legend.title=element_blank(),</span></span>
<span id="cb7-38"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         legend.position="bottom",</span></span>
<span id="cb7-39"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    panel.background = element_blank())+</span></span>
<span id="cb7-40"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(axis.line = element_line(color = 'black'))</span></span>
<span id="cb7-41"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> </span></span>
<span id="cb7-42"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> grid.arrange(a1, a2, nrow = 2)</span></span>
<span id="cb7-43"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/Time series plots of sales volume and revenue-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<p><strong>Promotion Effectiveness: Average Weekly Sales &amp; Revenue by Promotion Type</strong></p>
<p><em>Sales Volume</em><br>
Figure 2A shows how many orders were placed under different types of promotions.The y-axis is in a log scale (log2), meaning the values grow exponentially. The median order volume is highest for double promotions (both email and homepage), followed by homepage promotions, no promotions, and email promotions. The spread (height of the box) indicates variability, with double promotions showing more fluctuation in sales. The presence of numerous outliers at the upper end indicates that some stores experience exceptionally high sales under promotional influence.</p>
<p><em>Checkout Price</em><br>
Figure 2B shows the range of prices customers paid at checkout under different promotions. Unlike the sales volume, there is no major difference in checkout price across promotions. There is some right-skewness with outliers, suggesting a small number of high-value purchases, but the overall trend remains stable. This suggests that promotions influence the number of items bought, but not the price per visit.</p>
<p><em>Profit</em><br>
Revenue is also log-transformed for better visualization as shown in figure 2C. Similar to the sales volume trend, double promotions generate the highest median revenue, followed by homepage promotions, no promotions, and email promotions. The spread of revenue is wider, meaning revenue varies significantly under all conditions. The presence of high-value outliers suggests that under certain conditions, promotions can lead to exceptionally high revenue generation.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb8-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb8-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Boxplot of sales volume, checkout price and revenue</span></span>
<span id="cb8-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: " "</span></span>
<span id="cb8-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb8-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb8-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb8-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 4</span></span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------</span></span>
<span id="cb8-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Boxplots of Sales with vs. without Promotion</span></span>
<span id="cb8-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ---------------------------</span></span>
<span id="cb8-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">a &lt;- ggplot(train, aes(x = promotion_flag, y = log(num_orders,2), fill = promotion_flag)) +</span></span>
<span id="cb8-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_boxplot() +</span></span>
<span id="cb8-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = "2A) Sales Distribution", x = "", y = "Order Volume (log2)") +</span></span>
<span id="cb8-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb8-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(</span></span>
<span id="cb8-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      legend.position="bottom",</span></span>
<span id="cb8-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      plot.title = element_text(size=11),</span></span>
<span id="cb8-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=0.2))+</span></span>
<span id="cb8-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  guides(fill="none")</span></span>
<span id="cb8-21"></span>
<span id="cb8-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">b&lt;- ggplot(train, aes(x = promotion_flag, y = checkout_price,  fill = promotion_flag)) +</span></span>
<span id="cb8-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_boxplot() +</span></span>
<span id="cb8-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = "2B) Checkout Price Distribution", x = "Promotion", y = "Checkout Price") +</span></span>
<span id="cb8-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb8-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(</span></span>
<span id="cb8-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      legend.position="bottom",</span></span>
<span id="cb8-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      plot.title = element_text(size=11),</span></span>
<span id="cb8-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=0.2))+</span></span>
<span id="cb8-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  guides(fill="none")</span></span>
<span id="cb8-31"></span>
<span id="cb8-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">c&lt;- ggplot(train, aes(x = promotion_flag, y = log((profit),2), fill = promotion_flag)) +</span></span>
<span id="cb8-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_boxplot() +</span></span>
<span id="cb8-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = "2C) Profit Distribution", x = "", y = "Revenue (log2)") +</span></span>
<span id="cb8-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb8-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(</span></span>
<span id="cb8-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      legend.position="bottom",</span></span>
<span id="cb8-38"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      plot.title = element_text(size=11),</span></span>
<span id="cb8-39"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      axis.text.x = element_text(angle = 45, vjust = 0.5, hjust=0.2))+</span></span>
<span id="cb8-40"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  guides(fill="none")</span></span>
<span id="cb8-41"></span>
<span id="cb8-42"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">grid.arrange(a,b,c, ncol = 3)</span></span>
<span id="cb8-43"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/Boxplot of sales volume, checkout price and revenue-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="analysis-of-meal-popularity-during-promotions-and-no-promotions" class="level3">
<h3 class="anchored" data-anchor-id="analysis-of-meal-popularity-during-promotions-and-no-promotions">Analysis of Meal Popularity During Promotions and No-Promotions</h3>
<p>In figure 3A, the Meal IDs are arranged from left to right in descending order based on their overall popularity (total percentage of orders). This visualization helps identify which meals are popular and how promotions influence demand.</p>
<p><em>Impact of Promotions:</em><br>
Several meals (especially on the left side of the plot) show a significant increase in order percentage when a promotion is applied. This indicates that promotions can be effective in boosting the popularity of certain meals.For some meals, the Double promotion (email+homepage) or Email promotion seems to have a noticeable impact compared to “No promotion”.</p>
<p><em>Homepage Promotion Baseline:</em><br>
Homepage promotion seems to be always present at some level for all meals, which suggests that homepage promotion might be a baseline strategy applied to all meals.</p>
<p><em>Meal-Specific Promotion Effectiveness:</em><br>
Some meals respond well to specific types of promotions. For example, Meal ID 1962 sees a significant portion of its orders come from “promotion_flag”. Other meals, like Meal ID 1558, benefits more from “Email promotion”.</p>
<p><em>Meals with Low Promotion Impact:</em><br>
On the right side of the plot, the meals have low percentages overall, and the impact of promotions appears to be less significant. This could mean these meals are less popular regardless of promotions, or that the current promotions are not effective for these meals.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb9-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb9-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: del Promotions Impact popular items</span></span>
<span id="cb9-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: " How Promotions Impact checkout price?"</span></span>
<span id="cb9-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb9-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb9-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb9-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 8</span></span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">c1 &lt;- </span></span>
<span id="cb9-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">train |&gt;</span></span>
<span id="cb9-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(promotion_flag)|&gt;</span></span>
<span id="cb9-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  dplyr::count(meal_id) |&gt;</span></span>
<span id="cb9-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(p = round(100*(n / sum(n)),2))|&gt;</span></span>
<span id="cb9-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  select(-n)|&gt;</span></span>
<span id="cb9-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = reorder(factor(meal_id),p,, decreasing = TRUE),  fill = promotion_flag)) +</span></span>
<span id="cb9-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_bar(aes(y = p), stat='identity', alpha=0.7) +</span></span>
<span id="cb9-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  scale_color_manual(values=c('Order Percent'="#d88bb4")) +</span></span>
<span id="cb9-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(x = '', y = 'Percentage', </span></span>
<span id="cb9-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       title = '3A) Popular Meals During Promotions and No Promotions') +</span></span>
<span id="cb9-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb9-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme(legend.position="bottom",</span></span>
<span id="cb9-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=0.5))+</span></span>
<span id="cb9-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  guides(fill="none")</span></span>
<span id="cb9-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">c2 &lt;- </span></span>
<span id="cb9-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">train|&gt;</span></span>
<span id="cb9-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  #filter(meal_id %in% radom_meal)|&gt;</span></span>
<span id="cb9-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(meal_id, promotion_flag) |&gt;</span></span>
<span id="cb9-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  summarise(avg_profit = mean(profit), .groups = 'drop')|&gt;</span></span>
<span id="cb9-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x = as.factor(meal_id), y = log(avg_profit,2), fill = as.factor(promotion_flag))) +</span></span>
<span id="cb9-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_bar(stat = "identity", position = "dodge") +</span></span>
<span id="cb9-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(title = "3B) Average Profit per Menu Item (With &amp; Without Promotion)", x = "Menu ID", y = "Avg Profit (log2)", fill = "Promotion") +</span></span>
<span id="cb9-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb9-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  #guides(fill="none") +</span></span>
<span id="cb9-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(legend.position="bottom",</span></span>
<span id="cb9-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        axis.text.x = element_text(angle = 90, vjust = 0.2, hjust=0.1))</span></span>
<span id="cb9-36"></span>
<span id="cb9-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">grid.arrange(c1, c2, nrow = 2)</span></span>
<span id="cb9-38"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/del Promotions Impact popular items-1.png" class="img-fluid figure-img" width="768"></p>
<figcaption>How Promotions Impact checkout price?</figcaption>
</figure>
</div>
</div>
</div>
<section id="analysis-of-meal-popularity-during-promotions-and-no-promotions-1" class="level4">
<h4 class="anchored" data-anchor-id="analysis-of-meal-popularity-during-promotions-and-no-promotions-1">Analysis of Meal Popularity During Promotions and No-Promotions</h4>
<p>The average profit per menu item (on the log2 scale) generally falls between approximately 12 and 16 in figure 3B. There’s noticeable variation in average profit across different menu items (identified by their Menu ID). Indeed, we compare the mean profits for No promotion vs.&nbsp;promotins (homepage, email, and homepage+email), the p-value is less than 0.05 from ANOVA test results show promotions substantially impact profits across menu items.</p>
<p>We also see some items are inherently more profitable than others, regardless of the promotion strategy. Certain menu items (e.g., 1248, 1445, 1558) show higher profits under double (homepage and email) promotion, suggesting targeted discounts may work well for these meals. This may indicate that these meals experience higher order volume during promotions, making up for the reduced margin. Since, this isn’t universally true for all menu items. Promotions should be customized per meal item—apply them only where they result in a net positive profit effect.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb10-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb10-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Promotions Impact checkout price</span></span>
<span id="cb10-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: " How Promotions Impact checkout price?"</span></span>
<span id="cb10-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb10-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb10-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb10-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute average checkout_price and number of orders before and during promotions for each meal</span></span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">checkout_price_comparison &lt;- train|&gt;</span></span>
<span id="cb10-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(meal_id, promotions) |&gt;</span></span>
<span id="cb10-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  summarise(avg_checkout_price = mean(checkout_price, na.rm = TRUE)) |&gt;</span></span>
<span id="cb10-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  spread(promotions, avg_checkout_price)|&gt;</span></span>
<span id="cb10-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(Price_Change_Percent = 100*(Yes - No)/No)</span></span>
<span id="cb10-16"></span>
<span id="cb10-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">comparison_df_order &lt;- train |&gt;</span></span>
<span id="cb10-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(meal_id, promotions) |&gt;</span></span>
<span id="cb10-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  summarise(avg_num_orders = mean(num_orders, na.rm = TRUE))|&gt;</span></span>
<span id="cb10-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  spread(key = promotions, value = avg_num_orders)|&gt;</span></span>
<span id="cb10-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(Order_Change_Percent = 100*(Yes - No)/No)</span></span>
<span id="cb10-22"></span>
<span id="cb10-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">comparison_df &lt;- left_join(comparison_df_order|&gt; select(meal_id, Order_Change_Percent),</span></span>
<span id="cb10-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                           checkout_price_comparison|&gt; select(meal_id, Price_Change_Percent),</span></span>
<span id="cb10-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                           by = "meal_id") </span></span>
<span id="cb10-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check correlation between Price_Change_Percent vs Order_Change_Percent</span></span>
<span id="cb10-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">correlation_po &lt;- round(cor(comparison_df$Order_Change_Percent , </span></span>
<span id="cb10-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                            comparison_df$Price_Change_Percent , use = "complete.obs"),2)</span></span>
<span id="cb10-29"></span>
<span id="cb10-30"></span>
<span id="cb10-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the results</span></span>
<span id="cb10-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">ggplot(comparison_df, aes(x = reorder(factor(meal_id),round(Price_Change_Percent,2)))) +</span></span>
<span id="cb10-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_bar(aes(y = Price_Change_Percent), stat='identity', fill='steelblue', alpha=0.7) +</span></span>
<span id="cb10-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_line(aes(y = 0.1*(Order_Change_Percent+4), group=1, color='Order Change Percent'), size=0.8) +</span></span>
<span id="cb10-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  geom_point(aes(y = 0.1*(Order_Change_Percent+4), color='darkgray'),shape=21, size=2) +</span></span>
<span id="cb10-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  scale_color_manual(values=c('Order Change Percent'="#d88bb4")) +</span></span>
<span id="cb10-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  labs(x = 'Meal ID', y = 'Percentage Change', </span></span>
<span id="cb10-38"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       title = '4) Checkout Price Change &amp; Order Volume Change During Promotions',</span></span>
<span id="cb10-39"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       subtitle = paste("Correlation : Price Change vs. Order Volumne :" , correlation_po )) +</span></span>
<span id="cb10-40"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme_minimal()+</span></span>
<span id="cb10-41"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme(legend.position="bottom",</span></span>
<span id="cb10-42"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))</span></span>
<span id="cb10-43"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/index_files/figure-html/Promotions Impact checkout price-1.png" class="img-fluid figure-img" width="768"></p>
<figcaption>How Promotions Impact checkout price?</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="keytakeaways" class="level3">
<h3 class="anchored" data-anchor-id="keytakeaways">Keytakeaways</h3>
<p>As shown in the heatmap, we know not all stores respond promotions, and some can maintain high profitability without them. We should consider store-specific promotion strategies instead of universal discounting. While double promotions drive the highest sales and revenue, homepage and email promotions individually are not as effective as using both together.</p>
<p>The data shows checkout price per transaction remains stable regardless of the promotion type, meaning the increase in revenue comes from selling more, not from charging more. We can identify stores where promotions significantly boost profits and focus campaigns on those locations. For low-response stores, explore alternative marketing (e.g., bundling, loyalty programs).</p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Data Visualization</category>
  <guid>https://github.com/clfee/home/posts/2025-01-15-forecasting-for-data-analysis/</guid>
  <pubDate>Sat, 01 Feb 2025 06:00:00 GMT</pubDate>
</item>
<item>
  <title>Graphs for Communication - Creating Animated Maps</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2025-02-01-Graphs-for-Communication/</link>
  <description><![CDATA[ 




<p>Making the data easier to understand!</p>
<p><strong>Goal :</strong> To demonstrate how to create animated maps with <code>ggplot2</code> and <code>gganimate</code> in R to track MRSA BSI incidence in the State of California.</p>
<section id="graphs-for-communication" class="level2">
<h2 class="anchored" data-anchor-id="graphs-for-communication">Graphs for Communication</h2>
<p>Earlier this year, I prepared a presentation for our stakeholders to showcase our business performance, particularly after the lifting of restrictions on large events and gatherings. While we saw overall growth, the performance varied across different locations. One of the key focuses of the presentation was to illustrate how sales at wedding venues and restaurants evolved over time.</p>
<p>To make the data not only accurate but also engaging, I used the <code>ggplot2</code> and <code>gganimate</code> packages in R. These tools allowed me to transform raw numbers into visually compelling animations, making the data more understandable and impactful for the audience.</p>
<p>This same storytelling approach can be applied to datasets tracking Methicillin-resistant Staphylococcus aureus (MRSA) bloodstream infections (BSI) (BSI) in <a href="https://data.chhs.ca.gov/dataset/methicillin-resistant-staphylococcus-aureus-mrsa-bloodstream-infections-bsi-in-california-hospitals">California Hospitals</a>.</p>
<p>These datasets include 95% confidence intervals for the Standardized Infection Ratio (SIR) and statistical interpretations to compare MRSA BSI incidence against the national baseline. Additionally, the data tracks hospital progress towards national Healthcare-Associated Infection (HAI) reduction goals. Hospitals need to meet or exceed incremental SIR targets each year to stay on track.</p>
<p>In this exercise, we will create an animated map to show whether MRSA BSI incidence in California was the same, better, or worse than the national baseline from 2019 to 2023. The map will use a color scale ranging from purple (-1, indicating worse than the national average) to yellow (1, indicating better than the national average) to depict these comparisons.</p>
</section>
<section id="steps" class="level2">
<h2 class="anchored" data-anchor-id="steps">Steps</h2>
<p>The first section simply loads the libraries and data that will be used.</p>
<div class="cell" data-context="setup">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb1-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| context: setup</span></span>
<span id="cb1-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb1-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load required packages</span></span>
<span id="cb1-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(gganimate)</span></span>
<span id="cb1-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(gifski)</span></span>
<span id="cb1-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(dplyr)</span></span>
<span id="cb1-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(tidyverse)</span></span>
<span id="cb1-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(dplyr)</span></span>
<span id="cb1-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">library(ggplot2)</span></span>
<span id="cb1-13"></span>
<span id="cb1-14"></span>
<span id="cb1-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load data</span></span>
<span id="cb1-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">url_2022 &lt;- "https://data.chhs.ca.gov/dataset/a345bba7-25ed-4019-b6e3-0b597bea7368/resource/3ff809c6-c1e2-4107-be0c-f976521aadb2/download/mrsa_bsi_odp_2022.csv"</span></span>
<span id="cb1-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">url_2021 &lt;- "https://data.chhs.ca.gov/dataset/a345bba7-25ed-4019-b6e3-0b597bea7368/resource/23bef156-ae4d-4800-8512-d29660b2269f/download/cdph_mrsa_bsi_odp_2021.csv"</span></span>
<span id="cb1-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">url_2019 &lt;- "https://data.chhs.ca.gov/dataset/a345bba7-25ed-4019-b6e3-0b597bea7368/resource/62aead61-e320-4a74-b64c-1f65efa72b35/download/cdph_mrsa_bsi_odp_2019.csv"</span></span>
<span id="cb1-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">url_2023 &lt;-"https://data.chhs.ca.gov/dataset/a345bba7-25ed-4019-b6e3-0b597bea7368/resource/15c4962f-e357-4b03-9d24-655a2bc8030c/download/mrsa_bsi_odp_2023.csv"</span></span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">data_2023 &lt;- read_csv(url_2023)</span></span>
<span id="cb1-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">data_2022 &lt;- read_csv(url_2022)</span></span>
<span id="cb1-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">data_2021 &lt;- read_csv(url_2021)</span></span>
<span id="cb1-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">data_2019 &lt;- read_csv(url_2019)</span></span>
<span id="cb1-25"></span>
<span id="cb1-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine data</span></span>
<span id="cb1-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">mrsa_combine &lt;- bind_rows(data_2023,data_2022, data_2021, data_2019)</span></span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove redundant data</span></span>
<span id="cb1-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">mrsa_combine &lt;- unique(mrsa_combine)</span></span>
<span id="cb1-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lon and lat info</span></span>
<span id="cb1-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">us_counties &lt;- map_data("county")|&gt; filter(region =="california")|&gt; mutate( subregion = toupper( subregion))|&gt; dplyr::select(-c( order,  region)) </span></span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data transformation</span></span>
<span id="cb1-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">mrsa_combine &lt;- mrsa_combine |&gt;</span></span>
<span id="cb1-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  dplyr::select(-State, -HAI, -Met_2020_Goal, -SIR_CI_95_Lower_Limit,-SIR_CI_95_Upper_Limit, -On_Track, -Notes) |&gt;</span></span>
<span id="cb1-37"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(Months = as.numeric(ifelse(is.na(Months), 13, Months)),</span></span>
<span id="cb1-38"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         Year = as.integer(ifelse(is.na(Year), 2021, Year)),</span></span>
<span id="cb1-39"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         Comparison = case_when(</span></span>
<span id="cb1-40"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      Comparison == "Worse" ~ -1,</span></span>
<span id="cb1-41"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      Comparison == "Better" ~ 1,</span></span>
<span id="cb1-42"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      Comparison == "Same" ~ 0,</span></span>
<span id="cb1-43"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      .default = 0),</span></span>
<span id="cb1-44"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         Patient_Stay = cut(      </span></span>
<span id="cb1-45"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">           Patient_Days,</span></span>
<span id="cb1-46"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">           breaks = quantile(Patient_Days, probs = seq(0, 1, by = 0.1), na.rm = TRUE),</span></span>
<span id="cb1-47"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">           labels = 1:10),</span></span>
<span id="cb1-48"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         Months = as.character(Months),</span></span>
<span id="cb1-49"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         Year = as.character(Year),</span></span>
<span id="cb1-50"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         Infections_Reported = as.numeric(Infections_Reported))|&gt;</span></span>
<span id="cb1-51"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  filter(!grepl("Pooled", Facility_Name, ignore.case = TRUE),</span></span>
<span id="cb1-52"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">         !grepl("Pooled", Facility_Type, ignore.case = TRUE))|&gt; </span></span>
<span id="cb1-53"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate( subregion = toupper(County))</span></span>
<span id="cb1-54"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
</div>
<section id="step-1" class="level3">
<h3 class="anchored" data-anchor-id="step-1">Step 1</h3>
<p>As gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. Therefore, we will need to generate a base map using ggplot2. Then, add a function of facet_wrap(Hospital_Category_RiskAdjustment) to track the average performance of each hospital category in each county across California.</p>
</section>
<section id="step-2" class="level3">
<h3 class="anchored" data-anchor-id="step-2">Step 2</h3>
<p>Then, the gganimate library is used to animate the display of color by year (2019 – 2023). Here we define the transition time (in years), add a title and subtitle.</p>
<p>Note: 1. transition_*() defines how the data should be spread out and how it relates to itself across time. <br> 2. The comparison is depicted values and colors range from -1 (purple, worse than the national average) to 1 (yellow, better than the national average).</p>
<div class="cell" title="Bloodstream infections (BSI) compare to national overtime">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb2-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| title: Bloodstream infections (BSI) compare to national overtime</span></span>
<span id="cb2-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb2-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb2-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb2-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 8</span></span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">st &lt;- mrsa_combine|&gt;</span></span>
<span id="cb2-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    dplyr::select(subregion, Year, Comparison,Hospital_Category_RiskAdjustment)|&gt;</span></span>
<span id="cb2-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  mutate(Year = as.integer(Year))|&gt;</span></span>
<span id="cb2-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  group_by(subregion, Year,Hospital_Category_RiskAdjustment)|&gt; </span></span>
<span id="cb2-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  summarise(`national baseline` = round(mean(na.omit(as.numeric(Comparison))),2)) </span></span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">st &lt;- left_join(st, unique(us_counties), by= "subregion", relationship = "many-to-many") </span></span>
<span id="cb2-15"></span>
<span id="cb2-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">a &lt;-</span></span>
<span id="cb2-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">st|&gt; </span></span>
<span id="cb2-18"></span>
<span id="cb2-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(mapping = aes(x = long, y = lat, group = group, fill = `national baseline` ))+</span></span>
<span id="cb2-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">              geom_polygon(color = "gray90", linewidth = 0.3) +</span></span>
<span id="cb2-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  coord_map(projection = "albers", lat0 = 39, lat1 = 45) +</span></span>
<span id="cb2-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  scale_fill_continuous(type = "viridis")+</span></span>
<span id="cb2-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  theme(axis.line=element_blank(),</span></span>
<span id="cb2-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        axis.text=element_blank(),</span></span>
<span id="cb2-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        axis.ticks=element_blank(),</span></span>
<span id="cb2-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        axis.title=element_blank(),</span></span>
<span id="cb2-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        panel.background=element_blank(),</span></span>
<span id="cb2-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        panel.border=element_blank(),</span></span>
<span id="cb2-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        panel.grid=element_blank())+</span></span>
<span id="cb2-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        scale_color_gradient(low = "green", high = "red")+ </span></span>
<span id="cb2-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   facet_wrap(~Hospital_Category_RiskAdjustment)</span></span>
<span id="cb2-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            </span></span>
<span id="cb2-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            </span></span>
<span id="cb2-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">a + transition_time(Year) +labs(title = "MRSA BSI incidence compares to the national baseline, Year: {frame_time}")</span></span>
<span id="cb2-35"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-02-01-Graphs-for-Communication/index_files/figure-html/unnamed-chunk-2-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="bonus" class="level3">
<h3 class="anchored" data-anchor-id="bonus">Bonus</h3>
<p>Also compatible with other ggplot graphs!</p>
<p>In this example we see trend plots of continuous variables infections cases and days of patient hospital stays between 2019 and 2023.</p>
<p>Did you notice the length of patient stay in a hospital is not necessarily positive correlated with numbers of reported cases (except for the acute care hospital category)?</p>
<div class="cell" title="Patients Days vs Reported infection cases">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb3-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb3-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| title: Patients Days vs Reported infection cases </span></span>
<span id="cb3-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb3-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb3-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-width: 8</span></span>
<span id="cb3-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 8</span></span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">st &lt;- left_join(mrsa_combine, unique(us_counties), by= "subregion", relationship = "many-to-many") </span></span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">p &lt;-     </span></span>
<span id="cb3-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  st|&gt; </span></span>
<span id="cb3-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ggplot(aes(x= Patient_Days, y= Infections_Reported))+</span></span>
<span id="cb3-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    geom_point(alpha = 0.5, show.legend = FALSE, color = "#167bb2") +</span></span>
<span id="cb3-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    scale_size(range = c(2, 12)) +</span></span>
<span id="cb3-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme_bw()+</span></span>
<span id="cb3-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ggtitle(paste0("Length of Patients stay vs Reported infection cases")) +</span></span>
<span id="cb3-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(#axis.text.x=element_blank(),</span></span>
<span id="cb3-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          axis.text=element_text(size=12),</span></span>
<span id="cb3-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          legend.position = "bottom",</span></span>
<span id="cb3-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          axis.line = element_line(colour = "black"),</span></span>
<span id="cb3-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.grid.major = element_blank(),</span></span>
<span id="cb3-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.grid.minor = element_blank(),</span></span>
<span id="cb3-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.border = element_blank(),</span></span>
<span id="cb3-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.background = element_blank())+</span></span>
<span id="cb3-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          facet_wrap(~Hospital_Category_RiskAdjustment)+</span></span>
<span id="cb3-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    xlab("Days")+ylab("Cases")+labs(caption="Produced by CF Lee")</span></span>
<span id="cb3-27"></span>
<span id="cb3-28"></span>
<span id="cb3-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">p + transition_time(as.integer(Year))+  </span></span>
<span id="cb3-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  enter_fade() +</span></span>
<span id="cb3-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  exit_fade()+labs(title = "Year: {frame_time}")</span></span>
<span id="cb3-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2025-02-01-Graphs-for-Communication/index_files/figure-html/unnamed-chunk-3-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="resources" class="level2">
<h2 class="anchored" data-anchor-id="resources">Resources</h2>
<ol type="1">
<li>gganimate <a href="https://gganimate.com/">link</a></li>
<li>California Hospitals <a href="https://data.chhs.ca.gov/dataset/methicillin-resistant-staphylococcus-aureus-mrsa-bloodstream-infections-bsi-in-california-hospitals">data</a>.</li>
</ol>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R</category>
  <category>Data organization</category>
  <guid>https://github.com/clfee/home/posts/2025-02-01-Graphs-for-Communication/</guid>
  <pubDate>Sun, 01 Sep 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Automated Reporting: A Game-Changer for Data Analysis</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2024-08-16-automated-custom-reports/</link>
  <description><![CDATA[ 




<p>In the fast-paced world of data analysis, efficiency is key. Automated reporting is revolutionizing how we handle recurring data-rich reports, offering on-demand production and enhanced data exploration capabilities. This innovative approach not only saves time but also allows for deeper, more strategic insights. Parameterized reporting is a cutting-edge technique that enables simultaneous generation of multiple reports. This method not only boosts productivity but also significantly reduces errors associated with manual data handling.</p>
<p><strong>Goal :</strong> The objective of this report is to demonstrate how to create automated custom reports with <code>Quarto</code> and <code>Purrr</code> in R to track patient days from public data.</p>
<section id="real-world-application-california-mrsa-data-analysis" class="level2">
<h2 class="anchored" data-anchor-id="real-world-application-california-mrsa-data-analysis">Real-World Application: California MRSA Data Analysis</h2>
<p>To illustrate the power of automated reporting, let’s consider a case study using data from the California <a href="https://data.chhs.ca.gov/dataset/methicillin-resistant-staphylococcus-aureus-mrsa-bloodstream-infections-bsi-in-california-hospitals">California Health and Human Services Open Data Portal</a>. The demo uses the data from All California general acute care hospitals are required to report Methicillin-resistant Staphylococcus aureus (MRSA) bloodstream infection (BSI) cases that occur following hospitalization. MRSA is a serious, contagious bacterial infection that starts on your skin. It’s a type of staph infection that resists most common antibiotics, making it especially dangerous. Without treatment, MRSA can be deadly.</p>
</section>
<section id="step-by-step-guide" class="level2">
<h2 class="anchored" data-anchor-id="step-by-step-guide">Step-by-Step Guide</h2>
<section id="step-1-create-a-single-report-template" class="level3">
<h3 class="anchored" data-anchor-id="step-1-create-a-single-report-template">Step 1: Create a Single Report Template</h3>
<p>We began by preparing a single report template using data from 2022. This template serves as the foundation for all subsequent reports. Let’s prepare a single report as a template from the data in 2022.</p>
<section id="healthcare-associated-infection-report-2022" class="level4">
<h4 class="anchored" data-anchor-id="healthcare-associated-infection-report-2022">Healthcare Associated Infection Report, 2022</h4>
<section id="results" class="level5">
<h5 class="anchored" data-anchor-id="results">Results</h5>
<p><strong>Figure 1:</strong> Box plots of Standardized Infection Ratio (SIR) in 2022.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb1-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| label: Boxplots</span></span>
<span id="cb1-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb1-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">color_palette &lt;- c("#167bb2","#2e3b4f","#7A6C5D","#2e4f42","#A54657","#624151","#16b29b","#002c47","#9d72d0"</span></span>
<span id="cb1-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> mrsa_combine|&gt;</span></span>
<span id="cb1-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    filter(Year == params$year) |&gt;</span></span>
<span id="cb1-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    filter(County %in% c("San Diego" ,"San Francisco", "Los Angeles", "Sacramento", "Orange","Yolo" ))|&gt;</span></span>
<span id="cb1-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ggplot(aes(x= Hospital_Type, y= SIR, fill=Hospital_Type))+</span></span>
<span id="cb1-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">     geom_boxplot()+</span></span>
<span id="cb1-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    scale_fill_manual(values=color_palette)+</span></span>
<span id="cb1-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">       theme(</span></span>
<span id="cb1-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      legend.position="none",</span></span>
<span id="cb1-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      plot.title = element_text(size=11),</span></span>
<span id="cb1-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)</span></span>
<span id="cb1-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ) +</span></span>
<span id="cb1-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ggtitle(paste0("Standardized Infection Ratio (SIR) in ",params$year)) +</span></span>
<span id="cb1-21"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    xlab("")+ylab("")+labs(caption="Produced by CF Lee")+</span></span>
<span id="cb1-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   facet_wrap(~County)</span></span>
<span id="cb1-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2024-08-16-automated-custom-reports/index_files/figure-html/Boxplots-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p><strong>Figure 2:</strong> Patients Days vs Reported infection cases in 2022</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{r}</span></span>
<span id="cb2-2"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| code-fold: true</span></span>
<span id="cb2-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| warning: false</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> mrsa_combine$Months &lt;- as.numeric(mrsa_combine$Months)</span></span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> mrsa_combine |&gt; </span></span>
<span id="cb2-8"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    filter(Year == params$year) |&gt;</span></span>
<span id="cb2-9"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    mutate(Quarters = case_when(</span></span>
<span id="cb2-10"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                         Months &gt; 0 &amp; Months &lt; 4 ~ 1,</span></span>
<span id="cb2-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                         Months &gt; 3 &amp; Months &lt; 7 ~ 2,</span></span>
<span id="cb2-12"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                         Months &gt; 6 &amp; Months &lt; 10 ~ 3 , </span></span>
<span id="cb2-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                         Months &gt; 9 &amp; Months &lt; 13 ~ 4 , </span></span>
<span id="cb2-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">                                .default = 4))|&gt;</span></span>
<span id="cb2-15"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # group_by(Quarters,Hospital_Category_RiskAdjustment, Year)|&gt;</span></span>
<span id="cb2-16"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  #  summarise(mean = mean(na.omit(Infections_Reported)))|&gt; </span></span>
<span id="cb2-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-18"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ggplot(aes(x= Patient_Days, y= Infections_Reported, </span></span>
<span id="cb2-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">               colour = as.integer(Quarters)))+</span></span>
<span id="cb2-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">     geom_point(alpha = 0.7, show.legend = FALSE) +</span></span>
<span id="cb2-21"></span>
<span id="cb2-22"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme_bw()+</span></span>
<span id="cb2-23"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    ggtitle(paste0("Patients Days vs Reported infection cases in ",params$year)) +</span></span>
<span id="cb2-24"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    theme(#axis.text.x=element_blank(),</span></span>
<span id="cb2-25"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          axis.text=element_text(size=12),</span></span>
<span id="cb2-26"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          legend.position = "bottom",</span></span>
<span id="cb2-27"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          axis.line = element_line(colour = "black"),</span></span>
<span id="cb2-28"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.grid.major = element_blank(),</span></span>
<span id="cb2-29"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.grid.minor = element_blank(),</span></span>
<span id="cb2-30"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.border = element_blank(),</span></span>
<span id="cb2-31"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          panel.background = element_blank())+</span></span>
<span id="cb2-32"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">          facet_wrap(~Hospital_Category_RiskAdjustment)+</span></span>
<span id="cb2-33"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    xlab("Days")+ylab("Cases")+labs(caption="Produced by CF Lee")</span></span>
<span id="cb2-34"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2024-08-16-automated-custom-reports/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="960"></p>
</figure>
</div>
</div>
</div>
<p>Note: Patient Days is define as below. A count of the number of patients in the hospital during a time period, excluding IRFs and IPFs with their own CCN numbers. A rehabilitation unit (IRF) with its own CCN number reports separately from the rest of the hospital. To calculate patient days, the number of patients is recorded at the same time each day for each day of the month. At the end of the month, the daily counts are summed. See the NHSN website: http://www.cdc.gov/nhsn.</p>
</section>
</section>
<section id="step-2-generate-all-report-variations" class="level4">
<h4 class="anchored" data-anchor-id="step-2-generate-all-report-variations">Step 2: Generate All Report Variations</h4>
<p>After creating the single report, we used an R script to render the parameterized Quarto template with each defined parameter. This step allows for the simultaneous generation of all report variations, ensuring consistency and accuracy.To do this, write an R script to render the parameterized Quarto template with each of the defined parameters.</p>
<p>In our example the code looks like this,</p>
<pre><code># 1. First create a dataframe 

data &lt;- expand.grid(
  year = c(2019,2021:2023),
  record_date = Sys.Date(), 
  stringsAsFactors = FALSE)

df &lt;- data |&gt; 
  dplyr::mutate(
    output_format = "html",       # Output format (html, word, etc.)
    output_file = paste(          # Output file name
      year, "report.html",
      sep = "-"
    ),
    execute_params = purrr::map2( # Named list of parameters
      record_date, year, 
      \(record_date, year) list(record_date = record_date, year = year)
    )
  ) |&gt; 
  dplyr::select(-c(record_date, year))

df

# 2. Use purrr::pwalk() to map over each row of the dataframe and render each report variation.

purrr::pwalk(
  .l = df,                      # Dataframe to map over
  .f = quarto::quarto_render,   # Quarto render function
  input = "demo_autoreport.qmd",       # Named arguments of .f
  .progress = TRUE              # Optionally, show a progress bar
)</code></pre>
<p>By adopting this automated approach, analysts can shift their focus from report creation to data interpretation, leading to more impact decision-making and strategic planning.</p>
<p>Automatically generated reports, one after another!</p>
<div class="quarto-video"><video id="video_shortcode_videojs_video1" class="video-js vjs-default-skin vjs-fluid" controls="" preload="auto" data-setup="{}" title=""><source src="demo.mp4"></video></div>
</section>
</section>
</section>
<section id="resources" class="level2">
<h2 class="anchored" data-anchor-id="resources">Resources</h2>
<ol type="1">
<li><p>The raw data is obtained from California Health and Human Services Open Data Portal <a href="https://data.chhs.ca.gov/dataset/methicillin-resistant-staphylococcus-aureus-mrsa-bloodstream-infections-bsi-in-california-hospitals">datasets</a></p></li>
<li><p>Full code <a href="https://github.com/clfee">link</a></p></li>
</ol>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R</category>
  <category>Data organization</category>
  <guid>https://github.com/clfee/home/posts/2024-08-16-automated-custom-reports/</guid>
  <pubDate>Fri, 16 Aug 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Tying the data together</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/</link>
  <description><![CDATA[ 




<p><img src="https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/file_main.PNG" class="img-fluid"></p>
<p>Have you ever get tired of typing functions into vlookup? In this post, we will use tidyverse in R to modify, combine, search, and merge several datasets. We will also create a shiny app allowing users to download the organized data.</p>
<section id="background-challenges" class="level2">
<h2 class="anchored" data-anchor-id="background-challenges">Background &amp; challenges</h2>
<p>Recently, we reviewed our operational costs and found that a significant portion was allocated to credit card processing fees. After thorough research and negotiation, we secured a better deal with a new merchant service provider, saving about $1,500 USD per month and qualifying for next-day funding—a major win for our business.</p>
<p>However, our bookkeeper quickly identified an issue: the new provider’s reporting system is less straightforward. Unlike our previous provider, which offered a comprehensive master report with detailed order and funding information, the new service splits this data into three separate reports: Sales, Transaction, and Funded. To complicate matters, some data points, like order details, are labeled differently across the reports (e.g., order.ifo in Sales versus order_detail in Transaction). Additionally, a single order can have multiple funding references, making it challenging to track everything accurately.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/file_demo.PNG" class="img-fluid figure-img"></p>
<figcaption>Different column names and multiple funded entries for one order</figcaption>
</figure>
</div>
</section>
<section id="actions" class="level2">
<h2 class="anchored" data-anchor-id="actions">Actions</h2>
<p>To fully benefit from the cost savings and stick with the new provider, I decided to tackle this problem head-on using R and Shiny.</p>
<p>One of our goals was to create a master statement with all the action organized in one report as below. To do this, the steps are as below:</p>
<ol type="1">
<li>We first re-organized the raw report by renaming the column names.</li>
<li>Skipped the data where it is empty or contains not useful information.</li>
<li>Data merge based on the same column names.</li>
</ol>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/file_master.PNG" class="img-fluid figure-img"></p>
<figcaption>Desired master report</figcaption>
</figure>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Expand for Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'shiny' was built under R version 4.4.3</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># identify columns appear in both data sets</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderUI</span>({</span>
<span id="cb3-3">    var_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>()), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>()))</span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">selectInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var_ab"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select merge variable for Transaction and Sales"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choices =</span> var_names)</span>
<span id="cb3-5">  })</span></code></pre></div>
<div class="cell-output-display no-overflow-x">
<div id="outcd9adcd41e32b1d7" class="shiny-html-output"></div>
</div>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># merge data </span></span>
<span id="cb4-2">  merged_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb4-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_c</span>(), input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_ab, input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_bc)</span>
<span id="cb4-4">    temp_ab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<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;">full_join</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_ab)</span>
<span id="cb4-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(dplyr<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;">full_join</span>(temp_ab, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_c</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_bc))</span>
<span id="cb4-6">  })</span></code></pre></div>
</div>
</div>
</div>
</div>
<p>I also wanted to save time for our bookkeeper as they often need to cross-check the internal documents. This step takes time to find the correct range of the data, and input <code>Vlookup</code> functions before finding and labeling the matches in Excel. To streamline the process and to reduce the manual input errors, I replicated this function in the app (full code as below and on github).</p>
<p>The output result is shown below. This example shows that the order 891631 with two different retrieval references were received, the transactions were successful ,and the amount was deposited into the account. These records (114,115) also appeared in the uploaded internal file.</p>
<p><img src="https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/file_check.PNG" class="img-fluid"></p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Expand for Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb5-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>(),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_f</span>())</span>
<span id="cb5-3">    dt_org <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<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;">full_join</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>(),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_f</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_checklist)</span>
<span id="cb5-4">    dt_org <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt_org <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">Date and Time</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Date</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Count</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Merchant Reference Number</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Summary          Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Retrieval_Ref,</span>
<span id="cb5-6">              <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;">Transaction Gross</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Net</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Gross Amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Net Amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Currency,<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;">Reversal Flag</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb5-7">               <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;">Transaction Type</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb5-8">    counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt_org <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-9">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</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;">Summary Transaction Slip</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="cb5-11">      <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;">Check_merchantID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Merchant Reference Number</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;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-12">      <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;">Check_Retrieval_Ref =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(Retrieval_Ref), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">countx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-13">      <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;">has_Summary_Transaction_Slip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-14">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>count, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>countx)</span>
<span id="cb5-15"></span>
<span id="cb5-16">    not_in_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt_org <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-17">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</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;">Summary Transaction Slip</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="cb5-18">      <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;">Check_merchantID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-19">      <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;">Check_Retrieval_Ref =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-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;">has_Summary_Transaction_Slip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span>)</span>
<span id="cb5-21">     counts_summary <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;">apply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(counts,not_in_counts),<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,as.character)</span>
<span id="cb5-22">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(counts_summary)</span>
<span id="cb5-23">  })</span></code></pre></div>
<div class="cell-output-display">
<pre class="shiny-text-output noplaceholder" id="out94ad2b7505a70b8a"></pre>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>After the data merge is done, the files are ready to download to the local drive by pressing the <code>Download</code> buttons. This customized shiny app allows our accounting staff to run and download the report anytime/anywhere.</p>
<p><img src="https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/file_m2.PNG" class="img-fluid"></p>
<p>By developing a custom solution, I was able to streamline the reporting process, merge the data consistently, and keep our operations running smoothly—all while cutting down on expenses.</p>
<p><a href="https://github.com/clfee/Shiny-apps/blob/main/Data_organization.R">Full code on Github</a></p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center" data-bs-toggle="collapse" data-bs-target=".callout-3-contents" aria-controls="callout-3" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Expand for Full Code
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-3" class="callout-3-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(shiny)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'dplyr'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:stats':

    filter, lag</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union</code></pre>
</div>
<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;">library</span>(tidyr)</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readxl)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'readxl' was built under R version 4.4.3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(DT)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'DT'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:shiny':

    dataTableOutput, renderDataTable</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">ui <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fluidPage</span>(</span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">titlePanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"File Merger"</span>),</span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sidebarLayout</span>(</span>
<span id="cb15-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sidebarPanel</span>(</span>
<span id="cb15-5">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1. Convert Retrieval Reference Number to General"</span>),</span>
<span id="cb15-6">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fileInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"file_a"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transaction file (CSV or XLSX)"</span>,</span>
<span id="cb15-7">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accept =</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;">".csv"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".xlsx"</span>)),</span>
<span id="cb15-8">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2a. Convert Retrieval Ref to General"</span>),</span>
<span id="cb15-9">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2b. Remove all rows above Merchant ID "</span>),</span>
<span id="cb15-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fileInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"file_b"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sales (CSV or XLSX)"</span>,</span>
<span id="cb15-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accept =</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;">".csv"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".xlsx"</span>)),</span>
<span id="cb15-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3. Remove all rows above Summary Transaction Slip"</span>),</span>
<span id="cb15-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   Output: Merged File; Auto-Checked File "</span>),</span>
<span id="cb15-14">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fileInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"file_c"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Funded Deposit (CSV or XLSX)"</span>,</span>
<span id="cb15-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accept =</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;">".csv"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".xlsx"</span>)),</span>
<span id="cb15-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"------------------------------------------"</span>),</span>
<span id="cb15-17">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4. (Optional) Upload a payportal list. Output: Manual-Checked File"</span>),</span>
<span id="cb15-18">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Must contain a column called **Merchant Reference Number**!"</span>),</span>
<span id="cb15-19">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">checkboxInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"checklist"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Manual Check"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>),</span>
<span id="cb15-20">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fileInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"file"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Checklist (CSV or XLSX)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accept =</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;">".csv"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".xlsx"</span>)),</span>
<span id="cb15-21">      </span>
<span id="cb15-22">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uiOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"merge_vars_a_b"</span>),</span>
<span id="cb15-23">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uiOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"merge_vars_b_c"</span>),</span>
<span id="cb15-24">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uiOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"merge_vars_checklist"</span>),</span>
<span id="cb15-25">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">br</span>(),</span>
<span id="cb15-26">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#p("------------------------------------------"),</span></span>
<span id="cb15-27">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A: Contains Summary Transaction Slip &amp; Retrival Ref (matched &amp; unmatched)"</span>),</span>
<span id="cb15-28">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">downloadButton</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"downloadData"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Download Merged File"</span>),</span>
<span id="cb15-29">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#p("------------------------------------------"),</span></span>
<span id="cb15-30">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B: Contains non-empty Summary Transaction Slip, count matched Retrival Ref &amp; Merchant ref"</span>),</span>
<span id="cb15-31">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">downloadButton</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exportData"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Download Auto-Checkeds File"</span>),</span>
<span id="cb15-32">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#p("------------------------------------------"),</span></span>
<span id="cb15-33">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">p</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C: Using uploaded checklist to match and count non-empty Summary Transaction Slip, Retrival Ref &amp; Merchant ref"</span>),</span>
<span id="cb15-34">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">downloadButton</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"manual_input"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Download Manual-Checked File"</span>)</span>
<span id="cb15-35">    ),</span>
<span id="cb15-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mainPanel</span>(</span>
<span id="cb15-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabsetPanel</span>(</span>
<span id="cb15-38">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transaction List"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_a"</span>)),</span>
<span id="cb15-39">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sales List"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_b"</span>)),</span>
<span id="cb15-40">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Funded Deposit List"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_c"</span>)),</span>
<span id="cb15-41">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Merged File"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_d"</span>)),</span>
<span id="cb15-42">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Auto-Checked File"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_e"</span>)),</span>
<span id="cb15-43">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Checklist"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_f"</span>)),</span>
<span id="cb15-44">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tabPanel</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Manual-Checked File"</span>, DT<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;">dataTableOutput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_g"</span>))</span>
<span id="cb15-45">      )</span>
<span id="cb15-46">    )</span>
<span id="cb15-47">  )</span>
<span id="cb15-48">)</span>
<span id="cb15-49"></span>
<span id="cb15-50">server <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(input, output, session) {</span>
<span id="cb15-51">  </span>
<span id="cb15-52">  read_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(file) {</span>
<span id="cb15-53">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(file)) {</span>
<span id="cb15-54">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb15-55">    }</span>
<span id="cb15-56">    ext <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file_ext</span>(file<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name)</span>
<span id="cb15-57">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> (ext <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;">"csv"</span>) {</span>
<span id="cb15-58">      df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(file<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>datapath)</span>
<span id="cb15-59">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">if</span> (ext <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;">"xlsx"</span>) {</span>
<span id="cb15-60">      df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_excel</span>(file<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>datapath)</span>
<span id="cb15-61">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">else</span> {</span>
<span id="cb15-62">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb15-63">    }</span>
<span id="cb15-64">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(df)</span>
<span id="cb15-65">  }</span>
<span id="cb15-66">  </span>
<span id="cb15-67">  data_a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-68">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate</span>(</span>
<span id="cb15-69">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">need</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data set"</span>)</span>
<span id="cb15-70">    )</span>
<span id="cb15-71">    </span>
<span id="cb15-72">    da <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_data</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_a)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-73">      <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;">Retrieval_Ref =</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;">Retrieval Reference Number</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="cb15-74">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">Date and Time</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, Retrieval_Ref,<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;">Merchant Reference Number</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Amount) </span>
<span id="cb15-75">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(da)</span>
<span id="cb15-76">    </span>
<span id="cb15-77">  })</span>
<span id="cb15-78">  </span>
<span id="cb15-79">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove all rows above Merchant ID and change </span></span>
<span id="cb15-80">  data_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-81">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_b)</span>
<span id="cb15-82">    db <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_data</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_b)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb15-83">      <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;">Retrieval_Ref =</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;">Retrieval Ref.</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="cb15-84">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">Transaction Date</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, Retrieval_Ref, <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;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb15-85">             <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;">Payment Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Gross</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Net</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb15-86">    db </span>
<span id="cb15-87">  })</span>
<span id="cb15-88">  </span>
<span id="cb15-89">  </span>
<span id="cb15-90">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove all rows above Summary Transaction Slip</span></span>
<span id="cb15-91">  data_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-92">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_c)</span>
<span id="cb15-93">    dc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_data</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_c)</span>
<span id="cb15-94">    dc<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dc[<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;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Payment'</span>, dc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</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;">&amp;</span></span>
<span id="cb15-95">              <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;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sale'</span>, dc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</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;">&amp;</span></span>
<span id="cb15-96">              <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;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span>, dc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</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;">&amp;</span></span>
<span id="cb15-97">              <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;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Summary'</span>, dc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>),]</span>
<span id="cb15-98">    dc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">as.numeric</span>(dc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb15-99">    </span>
<span id="cb15-100">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#colnames(dc)[2:7] &lt;-  c("Summary Transaction Slip","Transaction Type","Transaction Count", "Reversal Flag",</span></span>
<span id="cb15-101">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                   "Currency","Gross Amount", "Net Amount")</span></span>
<span id="cb15-102">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(dc) </span>
<span id="cb15-103">  })</span>
<span id="cb15-104">  </span>
<span id="cb15-105">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>merge_vars_a_b <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;">renderUI</span>({</span>
<span id="cb15-106">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#req(data_a(), data_b())</span></span>
<span id="cb15-107">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate</span>(</span>
<span id="cb15-108">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">need</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data set"</span>)</span>
<span id="cb15-109">    )</span>
<span id="cb15-110">    var_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>()), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>()))</span>
<span id="cb15-111">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">selectInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var_ab"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select merge variable for Transaction and Sales"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choices =</span> var_names)</span>
<span id="cb15-112">  })</span>
<span id="cb15-113">  </span>
<span id="cb15-114">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>merge_vars_b_c <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;">renderUI</span>({</span>
<span id="cb15-115">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#req(data_b(), data_c())</span></span>
<span id="cb15-116">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate</span>(</span>
<span id="cb15-117">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">need</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data set"</span>)</span>
<span id="cb15-118">    )</span>
<span id="cb15-119">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#get(input$file_a, 'package:datasets')</span></span>
<span id="cb15-120">    var_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>()), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_c</span>()))</span>
<span id="cb15-121">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">selectInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var_bc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select merge variable for Sales and Funded Depos it"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choices =</span> var_names)</span>
<span id="cb15-122">  })</span>
<span id="cb15-123">  </span>
<span id="cb15-124">  </span>
<span id="cb15-125">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>merge_vars_checklist <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;">renderUI</span>({</span>
<span id="cb15-126">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate</span>(</span>
<span id="cb15-127">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">need</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>checklist <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data set"</span>)</span>
<span id="cb15-128">    )</span>
<span id="cb15-129">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#get(input$file_a, 'package:datasets')</span></span>
<span id="cb15-130">    var_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>()), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_f</span>()))</span>
<span id="cb15-131">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">selectInput</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var_checklist"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Select merge variable for Merge data and Checklist"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choices =</span> var_names)</span>
<span id="cb15-132">  })</span>
<span id="cb15-133">  data_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-134">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">validate</span>(</span>
<span id="cb15-135">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">need</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data set"</span>)</span>
<span id="cb15-136">    )</span>
<span id="cb15-137">    dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_data</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file)</span>
<span id="cb15-138">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(dt)</span>
<span id="cb15-139">  })</span>
<span id="cb15-140">  </span>
<span id="cb15-141">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT<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;">renderDataTable</span>({</span>
<span id="cb15-142">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#req(data_a())</span></span>
<span id="cb15-143">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>()),</span>
<span id="cb15-144">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb15-145">    )  </span>
<span id="cb15-146">  })</span>
<span id="cb15-147">  </span>
<span id="cb15-148">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT<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;">renderDataTable</span>({</span>
<span id="cb15-149">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#req(data_b())</span></span>
<span id="cb15-150">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>()),</span>
<span id="cb15-151">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb15-152">    )  </span>
<span id="cb15-153">  })</span>
<span id="cb15-154">  </span>
<span id="cb15-155">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT<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;">renderDataTable</span>({</span>
<span id="cb15-156">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#req(data_c())</span></span>
<span id="cb15-157">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_c</span>()),</span>
<span id="cb15-158">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb15-159">    )  </span>
<span id="cb15-160">  })</span>
<span id="cb15-161">  </span>
<span id="cb15-162">  merged_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-163">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_c</span>(), input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_ab, input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_bc)</span>
<span id="cb15-164">    temp_ab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<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;">full_join</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_a</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_b</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_ab)</span>
<span id="cb15-165">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(dplyr<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;">full_join</span>(temp_ab, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_c</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_bc))</span>
<span id="cb15-166">  })</span>
<span id="cb15-167">  </span>
<span id="cb15-168">  processed_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-169">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>())</span>
<span id="cb15-170">    df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb15-171">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-172">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-173">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</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;">Summary Transaction Slip</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="cb15-174">      <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;">Check_merchantID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Merchant Reference Number</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;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-175">      <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;">Check_Retrieval_Ref =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(Retrieval_Ref), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">countx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-176">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">Date and Time</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Date</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Count</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Merchant Reference Number</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Retrieval_Ref,</span>
<span id="cb15-177">             <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;">Check_merchantID</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Check_Retrieval_Ref,<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;">Transaction Gross</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Net</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Gross Amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Net Amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Currency,<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;">Reversal Flag</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb15-178">             <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;">Transaction Type</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb15-179">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(df,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,as.character)</span>
<span id="cb15-180">    </span>
<span id="cb15-181">  })</span>
<span id="cb15-182">  </span>
<span id="cb15-183">  manual_check <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactive</span>({</span>
<span id="cb15-184">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>(),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_f</span>())</span>
<span id="cb15-185">    dt_org <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<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;">full_join</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>(),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_f</span>(), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>var_checklist)</span>
<span id="cb15-186">    dt_org <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt_org <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-187">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</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;">Date and Time</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Date</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Count</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Merchant Reference Number</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Retrieval_Ref,</span>
<span id="cb15-188">              <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;">Transaction Gross</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Transaction Net</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Gross Amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">Net Amount</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Currency,<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;">Reversal Flag</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb15-189">               <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;">Transaction Type</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb15-190"></span>
<span id="cb15-191">    counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt_org <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-192">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Summary Transaction Slip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-193">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</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;">Summary Transaction Slip</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="cb15-194">      <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;">Check_merchantID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Merchant Reference Number</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;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-195">      <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;">Check_Retrieval_Ref =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(Retrieval_Ref), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">countx =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-196">      <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;">has_Summary_Transaction_Slip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-197">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>count, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>countx)</span>
<span id="cb15-198"></span>
<span id="cb15-199">    not_in_counts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dt_org <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-200">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</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;">Summary Transaction Slip</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="cb15-201">      <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;">Check_merchantID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-202">      <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;">Check_Retrieval_Ref =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-203">      <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;">has_Summary_Transaction_Slip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span>)</span>
<span id="cb15-204"></span>
<span id="cb15-205">     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#counts_summary &lt;- rbind(counts,not_in_counts)</span></span>
<span id="cb15-206">     counts_summary <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;">apply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(counts,not_in_counts),<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,as.character)<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#rbind(apply(counts,2,as.character),apply(not_in_counts,2,as.character))</span></span>
<span id="cb15-207">    </span>
<span id="cb15-208">    </span>
<span id="cb15-209">     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(counts_summary)</span>
<span id="cb15-210">    </span>
<span id="cb15-211">  })</span>
<span id="cb15-212">  </span>
<span id="cb15-213">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT<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;">renderDataTable</span>({</span>
<span id="cb15-214">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>())</span>
<span id="cb15-215">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>()),</span>
<span id="cb15-216">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb15-217">    )  </span>
<span id="cb15-218">  })</span>
<span id="cb15-219">  </span>
<span id="cb15-220">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DT<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;">renderDataTable</span>({</span>
<span id="cb15-221">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>())</span>
<span id="cb15-222">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">processed_data</span>()),</span>
<span id="cb15-223">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) </span>
<span id="cb15-224">    )  </span>
<span id="cb15-225">  })</span>
<span id="cb15-226">  </span>
<span id="cb15-227">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Render the data table only if the checkbox is checked</span></span>
<span id="cb15-228">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">renderDT</span>({</span>
<span id="cb15-229">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>checklist)</span>
<span id="cb15-230">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#manual_check()</span></span>
<span id="cb15-231">    </span>
<span id="cb15-232">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data_f</span>()),</span>
<span id="cb15-233">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb15-234">    )</span>
<span id="cb15-235">  })</span>
<span id="cb15-236">  </span>
<span id="cb15-237">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>table_g <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;">renderDT</span>({</span>
<span id="cb15-238">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">req</span>(input<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>checklist)</span>
<span id="cb15-239">    </span>
<span id="cb15-240">    DT<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;">datatable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">manual_check</span>()),</span>
<span id="cb15-241">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pageLength =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rownames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb15-242">    )</span>
<span id="cb15-243">  })</span>
<span id="cb15-244">  </span>
<span id="cb15-245">  <span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">######### Download ############</span></span>
<span id="cb15-246">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>downloadData <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;">downloadHandler</span>(</span>
<span id="cb15-247">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>() {</span>
<span id="cb15-248">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.Date</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"merged_data.csv"</span>)</span>
<span id="cb15-249">    },</span>
<span id="cb15-250">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">content =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(file) {</span>
<span id="cb15-251">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merged_data</span>()), file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb15-252">    }</span>
<span id="cb15-253">  )</span>
<span id="cb15-254">  </span>
<span id="cb15-255">  </span>
<span id="cb15-256">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>exportData <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;">downloadHandler</span>(</span>
<span id="cb15-257">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>() {</span>
<span id="cb15-258">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.Date</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"auto_checked.csv"</span>)</span>
<span id="cb15-259">    },</span>
<span id="cb15-260">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">content =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(file) {</span>
<span id="cb15-261">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">processed_data</span>()), file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb15-262">    }</span>
<span id="cb15-263">  )</span>
<span id="cb15-264">  </span>
<span id="cb15-265">  output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>manual_input <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;">downloadHandler</span>(</span>
<span id="cb15-266">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>() {</span>
<span id="cb15-267">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.Date</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"manual_checked.csv"</span>)</span>
<span id="cb15-268">    },</span>
<span id="cb15-269">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">content =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(file) {</span>
<span id="cb15-270">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>( <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">manual_check</span>()), file, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb15-271">    }</span>
<span id="cb15-272">  ) </span>
<span id="cb15-273">  </span>
<span id="cb15-274">}</span>
<span id="cb15-275"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shinyApp</span>(ui, server)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.</code></pre>
</div>
<div class="cell-output-display">
<p></p><div style="width: 100% ; height: 400px ; text-align: center; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;" class="muted well">Shiny applications not supported in static R Markdown documents</div><p></p>
</div>
</div>
</div>
</div>
</div>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R</category>
  <category>Shiny</category>
  <category>Data organization</category>
  <category>sql</category>
  <category>vlookup</category>
  <guid>https://github.com/clfee/home/posts/2024-08-01-tying-the-data-together/</guid>
  <pubDate>Thu, 01 Aug 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Creating a Serveless Dashboard</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2024-06-25-create-a-Quarto-dashboard/</link>
  <description><![CDATA[ 




<p><img src="https://github.com/clfee/home/posts/2024-06-25-create-a-Quarto-dashboard/dashboard.PNG" class="img-fluid"></p>
<p>A while ago I wrote about creating a serverless dashboard (with no server dependencies) using <a href="https://clfee.github.io/r/dashboard/crosstalk-dashboard/">Flexdashboard</a>. As some of the functions now are no longer supported, I found a good alternative called “Quarto Dashboards”. Quarto Dashboards allow to create dashboards using Python, R, Julia, and Observable. More details are <a href="https://quarto.org/docs/dashboards/">here</a>.</p>
<p>Dashboards can be created either using Jupyter notebooks (.ipynb) or using plain text markdown (.qmd). I am showing how to create one using RStudio.</p>
<ol type="1">
<li>Click File -&gt; New File -&gt; Quarto Document</li>
</ol>
<p><img src="https://github.com/clfee/home/posts/2024-06-25-create-a-Quarto-dashboard/demo.PNG" class="img-fluid" width="557"></p>
<ol start="2" type="1">
<li>A new <code>.qmd</code> file is created</li>
</ol>
<p>Here is the code for the visual version of the dashboard <a href="https://clfee.github.io/home/posts/2024-07-15-marketing-compaign-dashboard/">link</a></p>
<p>Here is the plain text&nbsp;<code>.qmd</code>&nbsp;version of the dashboard.</p>
<ol type="a">
<li>The document options define the title and author for the navigation bar as well as specifying the use of the dashboard format.</li>
<li>By default, dashboard pages are laid out first by row, then by column. In this demo, I changed this by specifying the&nbsp;<code>orientation: columns</code>&nbsp;document option:</li>
</ol>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">title<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Marketing Campaign at a Glance"</span></span>
<span id="cb1-2">author<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb1-3">    name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> Chris Lee</span>
<span id="cb1-4">    url<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> https<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span>clfee.github.io<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span></span>
<span id="cb1-5">date<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;">2024-07-15</span></span>
<span id="cb1-6">format<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> </span>
<span id="cb1-7">  dashboard<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb1-8">    orientation<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> columns</span>
<span id="cb1-9">    nav<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>buttons<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [github]</span>
<span id="cb1-10">    github<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> https<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">//</span>github.com<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>clfee</span>
<span id="cb1-11">logo<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;">"/assets/images/cmm1.PNG"</span></span>
<span id="cb1-12">theme<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> custom.scss</span>
<span id="cb1-13">editor_options<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> </span>
<span id="cb1-14">  chunk_output_type<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> console</span></code></pre></div>
<ol start="3" type="a">
<li>Each row in the dashboard that is not given an explicit height will determine its size by either filling available space or by flowing to its natural height.Here I changed the figure height by specifying the&nbsp;<code>#| fig-height: 6</code>&nbsp;</li>
</ol>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| title: Correlations</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-height: 6</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| padding: 0;</span></span>
<span id="cb2-4">dt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> train</span></code></pre></div>
<p>More details about <a href="https://github.com/clfee/Shiny-apps/tree/main/Dashboards/Marketing%20Campaign">full code</a>.</p>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R</category>
  <category>Quarto</category>
  <category>Dashboard</category>
  <guid>https://github.com/clfee/home/posts/2024-06-25-create-a-Quarto-dashboard/</guid>
  <pubDate>Tue, 25 Jun 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Image Classification (Part1)</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2021-08-31-malaria-cell-classification/</link>
  <description><![CDATA[ 




<p>I remember years ago seeing my colleague spent hours under a microscopes counting cells underwent of apoptosis or Dauer larva formation. I mean it is fun doing experiments in the lab but telling differences of these tiny worms would probably is the last thing I’d want to do. This task does take lots of valuable time from a researcher. Imagine, how many more novel anti-agents like this <a href="https://pubmed.ncbi.nlm.nih.gov/23049887/">article</a> Yongsoon could bring us if the deep learning techniques were ready to use back in 2011.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/assets/images/dauer.PNG" class="img-fluid figure-img"></p>
<figcaption>Kim Y, Sun H (2012) PLOS ONE 7(9): e45890</figcaption>
</figure>
</div>
<p>Thanks to the advancement in deep learning field, neural network model architectures can be readily reused and, in most cases, are tested across multiple applications to establish robustness. Here, I’m going to show how easy it is to implement transfer learning using Keras in Python for Malaria cell classification. The basic concept of transfer learning is using the knowledge (architecture or weights) gained from a neural network model that was trained to recognize animals to recognize cats. The dataset used here came from <a href="https://ceb.nlm.nih.gov/repositories/malaria-datasets/">NIH</a>, along with recent publications<a href="https://peerj.com/articles/6977/">1</a>,<a href="https://peerj.com/articles/4568/">2</a>.</p>
<section id="workflow" class="level4">
<h4 class="anchored" data-anchor-id="workflow">Workflow</h4>
<ul>
<li>Loading data and data pre-processing</li>
<li>Transfer learning and fine-tuning (DenseNet121)</li>
<li>Result evaluation</li>
</ul>
<p><img src="https://github.com/clfee/home/assets/images/flow1.PNG" class="img-fluid"></p>
</section>
<section id="data-overview" class="level4">
<h4 class="anchored" data-anchor-id="data-overview">Data Overview</h4>
<p>There are many ways to create train/valid/test data sets. Below is one of the methods using R to create csv files containing file paths and classifications from train and test folders.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># R code</span></span>
<span id="cb1-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">library(fs)</span></span>
<span id="cb1-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dataset_dir &lt;- "Data/cell_images/"</span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_dir   &lt;- paste0(dataset_dir, "/test/")</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># stored image paths in the image column</span></span>
<span id="cb1-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_parasite &lt;- dir_ls(path=paste0(test_dir, "parasite"),glob = "*.png")</span></span>
<span id="cb1-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_uninfected &lt;- dir_ls(path=paste0(test_dir, "uninfected"),glob = "*.png")</span></span>
<span id="cb1-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_par &lt;- as.data.frame(matrix('parasite', length(test_parasite), 1))</span></span>
<span id="cb1-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_unin &lt;- as.data.frame(matrix('uninfected', length(test_parasite), 1))</span></span>
<span id="cb1-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_par$image &lt;- test_parasite </span></span>
<span id="cb1-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test_unin$image &lt;- test_uninfected</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test &lt;- rbind(test_par,test_unin)</span></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colnames(test)[1] &lt;- 'label'</span></span>
<span id="cb1-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test$normal  &lt;- ifelse(test$label != 'parasite', 1,0)</span></span>
<span id="cb1-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test$parasite &lt;- ifelse(test$label == 'parasite', 1,0)</span></span></code></pre></div>
<p>And the csv file looks like this. <img src="https://github.com/clfee/home/assets/images/preview.PNG" class="img-fluid" alt="csv"></p>
<p>In reality, we don’t usually see many cells infected with parasites, therefore less than 1/3 of the infected samples were used in this exercise.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Python</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get ids for each label</span></span>
<span id="cb2-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_img_ids = list(new_df.uninfected.index.unique())</span></span>
<span id="cb2-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">train_ids, test_ids = train_test_split(all_img_ids, test_size=0.01, random_state=21)</span></span>
<span id="cb2-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">train_ids, valid_ids = train_test_split(train_ids, test_size=0.1, random_state=21)</span></span></code></pre></div>
<p>Making sure, the proportion of the infected cell is as expected after data split. <img src="https://github.com/clfee/home/assets/images/pct.PNG" class="img-fluid" alt="pct"></p>
<p>Let’s also check few images. The images come with different sizes. They will need to reshape and normalize before xx.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract numpy values from image column in data frame</span></span>
<span id="cb3-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">train_df = new_df.iloc[train_ids,:]</span></span>
<span id="cb3-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">images = train_df['image'].values</span></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract 9 random images </span></span>
<span id="cb3-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random_images = [np.random.choice(images) for i in range(9)]</span></span>
<span id="cb3-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">img_dir = 'C:/Users/your_image_folder'</span></span>
<span id="cb3-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">print('Display Random Images')</span></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adjust the size of your images</span></span>
<span id="cb3-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.figure(figsize=(20,10))</span></span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">for i in range(9)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb3-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    plt.subplot(3, 3, i + 1)</span></span>
<span id="cb3-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    img = plt.imread(os.path.join(img_dir, random_images[i]))</span></span>
<span id="cb3-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    plt.imshow(img)</span></span>
<span id="cb3-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    plt.axis('off')</span></span></code></pre></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/assets/images/random_img.PNG" class="img-fluid figure-img"></p>
<figcaption>random_images</figcaption>
</figure>
</div>
</section>
<section id="loading-data" class="level4">
<h4 class="anchored" data-anchor-id="loading-data">Loading data</h4>
<p>Next is building generators from the Keras framework. The purpose of building generator is that it allows to generate batches of tensor image data with real-time data augmentation(ex: random horizontal flipping of images). We also use the generator to transform the values in each batch so that their mean is 0 and their standard deviation is 1.Here is the information of <a href="https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/ImageDataGenerator#used-in-the-notebooks">ImageDataGenerator</a> and a short <a href="https://vijayabhaskar96.medium.com/tutorial-on-keras-imagedatagenerator-with-flow-from-dataframe-8bd5776e45c1">tutorial</a>. We’ll also need to build a sereperate generator for valid and test sets. Since each image will be normailized using mean and standard deviation derived from its own batch. In a real life scenario, we process one image at a time. And the incoming image is normalized using the statistics computed from the training set.</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Train generator</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def get_train_generator(df, image_dir, x_col, y_cols, shuffle=True, batch_size=8, seed=1, target_w = 224, target_h = 224)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </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;">"</span></span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Args</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train_df (dataframe)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> dataframe specifying training data.</span></span>
<span id="cb4-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_dir (str)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> directory where image files are held.</span></span>
<span id="cb4-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">x_col (str)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> name of column in df that holds filenames.</span></span>
<span id="cb4-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">y_cols (list)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> list of strings that hold y labels for images.</span></span>
<span id="cb4-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_size (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> size of sample to use for normalization statistics.</span></span>
<span id="cb4-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">batch_size (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> images per batch to be fed into model during training.</span></span>
<span id="cb4-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seed (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> random seed.</span></span>
<span id="cb4-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">target_w (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> final width of input images.</span></span>
<span id="cb4-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">target_h (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> final height of input images.</span></span>
<span id="cb4-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb4-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Returns</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb4-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">train_generator (DataFrameIterator)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> iterator over training set</span></span>
<span id="cb4-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </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;">"        </span></span>
<span id="cb4-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    print("getting train generator...") </span></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # normalize images</span></span>
<span id="cb4-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    image_generator = ImageDataGenerator(</span></span>
<span id="cb4-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        samplewise_center=True,</span></span>
<span id="cb4-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        samplewise_std_normalization= True)</span></span>
<span id="cb4-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb4-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    # flow from directory with specified batch size and target image size</span></span>
<span id="cb4-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    generator = image_generator.flow_from_dataframe(</span></span>
<span id="cb4-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            dataframe=df,</span></span>
<span id="cb4-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            directory=image_dir,</span></span>
<span id="cb4-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            x_col=x_col,</span></span>
<span id="cb4-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            y_col=y_cols,</span></span>
<span id="cb4-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            class_mode="raw",</span></span>
<span id="cb4-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            batch_size=batch_size,</span></span>
<span id="cb4-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            shuffle=shuffle,</span></span>
<span id="cb4-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            seed=seed,</span></span>
<span id="cb4-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            target_size=(target_w,target_h))</span></span>
<span id="cb4-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb4-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    return generator  </span></span></code></pre></div>
<p>Before, model building we’ll need to define a loss function to address class imbalance. We can give more weight for the less frequent class and less weight for the other one, see <a href="https://arxiv.org/pdf/1711.05225.pdf">here</a> . We can write the overall average cross-entropy loss over the entire training set D of size N as follows:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/assets/images/loss.PNG" class="img-fluid figure-img"></p>
<figcaption>loss</figcaption>
</figure>
</div>
<p>Next, we will use a pre-trained DenseNet121 model which we can load directly from Keras and then add two layers on top of it.</p>
<ul>
<li><p>Set include_top=False, to remove the orginal fully connect dense layer (so you can adjust the ouptut prediction clsses or<br>
activation function).</p></li>
<li><p>Use specific layer using get_layer(). For example: base_model.get_layer(‘conv5_block16_conv’)</p></li>
</ul>
<p>A GlobalAveragePooling2D layer to get the average of the last convolution layers from DenseNet121. The pooling layer typically uses a filter to extract representative features (e.g., maximum, average, etc.) for different locations. The method of extracting features from the pooling filter is called a pooling function. The commonly used pooling functions include the maximum pooling function, average pooling function, L2 normalization, and weighted average pooling function based on the distance from the center pixel. In short, the pooling layer summarizes all the feature information centered on each position of the input feature map, which makes it reasonable that the output data of the pooling layer is less than the input data. This method reduces the input data to the next layer and improves the computational efficiency of the CNN.</p>
<p>The output of the pooling layer is flattening to convert the pooled features maps into a single dimensional array. This is done in order for the data to be fed into densely connected hidden layers.</p>
<p>A Dense layer with sigmoid activation to get the prediction logits for each of our classes. We can set our custom loss function for the model by specifying the loss parameter in the compile() function.</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build model</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def create_dense121_model()</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb5-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    pretrained = 'fine_tuned.hdf5'</span></span>
<span id="cb5-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    train_df = pd.read_csv("train_df.csv")</span></span>
<span id="cb5-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    labels = ['uninfected', 'parasite']  </span></span>
<span id="cb5-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    class_pos = train_df.loc[:, labels].sum(axis=0)</span></span>
<span id="cb5-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    class_neg = len(train_df) - class_pos</span></span>
<span id="cb5-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    class_total = class_pos + class_neg</span></span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    pos_weights =  class_pos / class_total</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> #[0.5,class_pos / class_total]</span></span>
<span id="cb5-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    neg_weights =  class_neg / class_total</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> #[0.5,class_neg / class_total]</span></span>
<span id="cb5-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    print("Got loss weights")</span></span>
<span id="cb5-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def create_model(input_shape=(224, 224,3))</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb5-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape)</span></span>
<span id="cb5-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">       </span></span>
<span id="cb5-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # add a global spatial average pooling layer</span></span>
<span id="cb5-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        x = GlobalAveragePooling2D()(base_model.output)</span></span>
<span id="cb5-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        x = Flatten()(x)</span></span>
<span id="cb5-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        x = Dense(1024, activation='relu', name='dense_post_pool')(x)</span></span>
<span id="cb5-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        x = Dropout(0.8)(x)</span></span>
<span id="cb5-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # output has two neurons for the 2 classes (uninfected and parasite)</span></span>
<span id="cb5-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        predictions = Dense(len(labels), activation='sigmoid')(x)</span></span>
<span id="cb5-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        model = Model(inputs = base_model.input, outputs = predictions)</span></span>
<span id="cb5-27"></span>
<span id="cb5-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # freeze the earlier layers</span></span>
<span id="cb5-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        for layer in base_model.layers[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:-4]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb5-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            layer.trainable=False</span></span>
<span id="cb5-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span></span>
<span id="cb5-32"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        return model</span></span>
<span id="cb5-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def get_weighted_loss(neg_weights, pos_weights, epsilon=1e-7)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb5-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def weighted_loss(y_true, y_pred)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb5-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            y_true = tf.cast(y_true, tf.float32)</span></span>
<span id="cb5-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            #print(f'neg_weights : {neg_weights}, pos_weights: {pos_weights}')</span></span>
<span id="cb5-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            #print(f'y_true : {y_true}, y_pred: {y_pred}')</span></span>
<span id="cb5-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            # L(X, y) = −w * y log p(Y = 1|X) − w *  (1 − y) log p(Y = 0|X)</span></span>
<span id="cb5-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">            # from https://arxiv.org/pdf/1711.05225.pdf</span></span>
<span id="cb5-41"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            loss = 0</span></span>
<span id="cb5-42"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            </span></span>
<span id="cb5-43"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">for i in range(len(neg_weights))</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb5-44"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                loss -= (neg_weights[i] * y_true[:, i] * K.log(y_pred[:, i] + epsilon) + </span></span>
<span id="cb5-45"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                         pos_weights[i] * (1 - y_true[:, i]) * K.log(1 - y_pred[:, i] + epsilon))</span></span>
<span id="cb5-46"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            </span></span>
<span id="cb5-47"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            loss = K.sum(loss)</span></span>
<span id="cb5-48"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">            return loss</span></span>
<span id="cb5-49"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        return weighted_loss</span></span>
<span id="cb5-50"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-51"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">   </span></span>
<span id="cb5-52"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    model = create_model()</span></span>
<span id="cb5-53"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    model.load_weights(pretrained)</span></span>
<span id="cb5-54"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    print("Loaded Model")</span></span>
<span id="cb5-55"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb5-56"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    model.compile(optimizer='adam', loss= get_weighted_loss(neg_weights, pos_weights)) </span></span>
<span id="cb5-57"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    print("Compiled Model")   </span></span>
<span id="cb5-58"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">          </span></span>
<span id="cb5-59"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    return model</span></span></code></pre></div>
<p>Model is fine tuned using <a href="https://keras.io/api/callbacks/model_checkpoint/">ModelCheckpoint</a> and only the model’s weights will be saved.</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># CallBack </span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -------------------------------------------------------------------------------------------------</span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Callback Function 1</span></span>
<span id="cb6-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fname = 'dense121(V)_Epoch[{epoch:02d}].ValLoss[{val_loss:.3f}].hdf5'</span></span>
<span id="cb6-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fullpath = fname</span></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://keras.io/api/callbacks/model_checkpoint/</span></span>
<span id="cb6-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">callback_func1 = ModelCheckpoint(filepath=fullpath,             </span></span>
<span id="cb6-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                                monitor='val_loss',             </span></span>
<span id="cb6-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                                verbose=1,                      </span></span>
<span id="cb6-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                                save_best_only=True,            </span></span>
<span id="cb6-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                                save_weights_only=True,</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> # save weights       </span></span>
<span id="cb6-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                                mode='min',                     </span></span>
<span id="cb6-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                                period=1)                       </span></span>
<span id="cb6-14"></span>
<span id="cb6-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Callback Function 2</span></span>
<span id="cb6-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://keras.io/callbacks/#tensorboard</span></span>
<span id="cb6-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">callback_func2 = keras.callbacks.TensorBoard(log_dir='./logs/log2', histogram_freq=1)</span></span>
<span id="cb6-18"></span>
<span id="cb6-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Callback Function</span></span>
<span id="cb6-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">callbacks = []</span></span>
<span id="cb6-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">callbacks.append(callback_func1)</span></span>
<span id="cb6-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">callbacks.append(callback_func2)</span></span>
<span id="cb6-23"></span>
<span id="cb6-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Training and Plotting</span></span>
<span id="cb6-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -------------------------------------------------------------------------------------------------</span></span>
<span id="cb6-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">history = model.fit(train_generator, </span></span>
<span id="cb6-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                              validation_data=valid_generator,</span></span>
<span id="cb6-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                              steps_per_epoch=100, </span></span>
<span id="cb6-29"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                              validation_steps=25, </span></span>
<span id="cb6-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                              epochs = 15,</span></span>
<span id="cb6-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                              callbacks=callbacks)</span></span>
<span id="cb6-32"></span>
<span id="cb6-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.plot(history.history['loss'])</span></span>
<span id="cb6-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.plot(history.history['val_loss'])</span></span>
<span id="cb6-35"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.ylabel("loss")</span></span>
<span id="cb6-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.xlabel("epoch")</span></span>
<span id="cb6-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.title("Training Loss Curve")</span></span>
<span id="cb6-38"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plt.show()</span></span></code></pre></div>
<p>Train versus validation loss for all epochs is shown here. The orange and blue lines indicate train loss and validation loss respectively. We can see the model may be under-fitted. One way to overcome this is simply increase the number of epochs. Also with the callback function, we can re-use the best weights saved at 12th epoch.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/assets/images/history.PNG" class="img-fluid figure-img"></p>
<figcaption>history</figcaption>
</figure>
</div>
</section>
<section id="evaluation" class="level4">
<h4 class="anchored" data-anchor-id="evaluation">Evaluation</h4>
<p>The <a href="https://pubmed.ncbi.nlm.nih.gov/3753562/">ROC curve</a> is created by plotting the true positive rate against the false positive rate. We can see the model performs reasonable well.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/assets/images/ROC.PNG" class="img-fluid figure-img"></p>
<figcaption>ROC</figcaption>
</figure>
</div>
<p>We can try different approaches to improve the model perfromance, such as train the model for a longer time or use all the training data (since only 1/3 of the parasite data was used). We can also try a different base model, the previous <a href="https://peerj.com/articles/6977/">publication</a>, shows 99.32% accuracy with VGG-19 alone.</p>
</section>
<section id="visualize-class-activation-maps" class="level4">
<h4 class="anchored" data-anchor-id="visualize-class-activation-maps">Visualize class activation maps</h4>
<p>Next, I will show how to produce visual explanation using Grad-CAM. The purpose of doing this is as following:</p>
<ul>
<li>Debug your model and visually validate that it is “looking” and “activating” at the correct locations in an image.</li>
<li><a href="https://arxiv.org/abs/1610.02391">Grad-CAM</a> works by (1) finding the final convolutional layer in the network and then (2) examining the gradient information flowing into that layer.</li>
</ul>
</section>
<section id="notes" class="level4">
<h4 class="anchored" data-anchor-id="notes">Notes</h4>
<p>Note 1: AUC is the area below these ROC curves. Therefore, in other words, AUC is a great indicator of how well a classifier functions. Note 2: A good tutorial for to learn neural network image classification from <a href="https://medium.com/@saugata.paul1010/a-case-study-on-malaria-detection-using-cell-images-and-deep-convolution-neural-networks-in-keras-8d07356a3d05">scratch</a> and Andrew Ng’s deep learning <a href="https://www.coursera.org/specializations/deep-learning">course</a>.</p>
<p>Note 3:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># packages used </span></span>
<span id="cb7-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import os</span></span>
<span id="cb7-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import sklearn</span></span>
<span id="cb7-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import pandas as pd</span></span>
<span id="cb7-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import numpy as np</span></span>
<span id="cb7-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import seaborn as sns</span></span>
<span id="cb7-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import matplotlib.pyplot as plt</span></span>
<span id="cb7-8"></span>
<span id="cb7-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">import keras</span></span>
<span id="cb7-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras.applications.densenet import DenseNet121</span></span>
<span id="cb7-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras.models import Model</span></span>
<span id="cb7-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization, GlobalAveragePooling2D</span></span>
<span id="cb7-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras.callbacks import ModelCheckpoint, CSVLogger, LearningRateScheduler, ReduceLROnPlateau, EarlyStopping, TensorBoard</span></span>
<span id="cb7-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras import backend as K</span></span>
<span id="cb7-15"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras.preprocessing import image</span></span>
<span id="cb7-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from keras.preprocessing.image import ImageDataGenerator</span></span></code></pre></div>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>python</category>
  <category>Image Classification</category>
  <guid>https://github.com/clfee/home/posts/2021-08-31-malaria-cell-classification/</guid>
  <pubDate>Tue, 31 Aug 2021 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Image Classification (Part2)</title>
  <dc:creator>Chris Lee</dc:creator>
  <link>https://github.com/clfee/home/posts/2021-08-31-malaria-cell-classification - p2/</link>
  <description><![CDATA[ 




<p>When building a predictive model, there are two important criteria: predictive accuracy and interpretability, which generally have a trade-off relationship. In the previous <a href="https://clfee.github.io/python/image/classification/image-classification1">post</a>, we have shown that the DenseNet121 model can achieve high accuracy in detecting cells infected with parasites.</p>
<p>Here, I am going to introduce a powerful technique GRAD-CAM (gradient-weighted class activation mapping) to visualize which parts of an image are most important to the predictions of an image regression network. GRAD-CAM is a generalization of the CAM technique which determines the importance of each neuron in a network prediction by considering the gradients of the target flowing through the deep network. Unlike CAM which requires a particular kind of CNN architecture to perform global average pooling prior to prediction and forces us to change the base model retrain the network. In contrast, GRAD-CAM is accessing intermediate activations in the deep learning model and computing gradients with respect to the class output. For more details, please <a href="https://www.coursera.org/lecture/advanced-computer-vision-with-tensorflow/gradcam-u1Qub">see</a>.</p>
<p>Workflow: - Obtain predicted class/index - Determine which intermediate layer(s) to use. Lower-level convolution layers capture low level features such as edges, and lines. Higher-level layers usually have more abstract information. - Calculate the gradients with respect to the outout of the class/index - Generate a heatmap by weighing the convolution outputs with the computed gradients - Super-impose the heatmap to the original image</p>
<p><em>Load base model</em></p>
<p>We first load the base model and will only train the last 4 layers.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def build_model(input_shape=(224, 224,3),pos_weights,neg_weights)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # load the base DenseNet121 model</span></span>
<span id="cb1-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  base_model = DenseNet121(input_shape = input_shape, </span></span>
<span id="cb1-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                      weights='imagenet', </span></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                      include_top=False)</span></span>
<span id="cb1-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # add a GAP layer</span></span>
<span id="cb1-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  output = layers.GlobalAveragePooling2D()(base_model.output)</span></span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # output has two neurons for the 2 classes (uninfected and parasite)</span></span>
<span id="cb1-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  output = layers.Dense(2, activation='softmax')(output)</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # set the inputs and outputs of the model</span></span>
<span id="cb1-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  model = Model(base_model.input, output)</span></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # freeze the earlier layers</span></span>
<span id="cb1-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  for layer in base_model.layers[</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">:-4]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb1-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">      layer.trainable=False</span></span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  # configure the model for training</span></span>
<span id="cb1-21"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  model.compile(loss= get_weighted_loss(neg_weights, pos_weights), </span></span>
<span id="cb1-22"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                optimizer=adam, </span></span>
<span id="cb1-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                metrics=['accuracy'])</span></span>
<span id="cb1-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span></span>
<span id="cb1-25"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  return model</span></span>
<span id="cb1-26"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span></span></code></pre></div>
<p>We then create a new model that has the original model’s inputs, but two different outputs. The first output contains the activation layers outputs that in this case is the final convolutional layer in the original model. And the second output is the model’s prediction for the image.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode yml code-with-copy"><code class="sourceCode yaml"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">def get_CAM(model, processed_image, actual_label, layer_name)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span></span>
<span id="cb2-2"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </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;">"</span></span>
<span id="cb2-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    GradCAM method for visualizing input saliency.</span></span>
<span id="cb2-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Args</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model (Keras.model)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> model to compute cam for</span></span>
<span id="cb2-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image (tensor)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> input to model, shape (1, H, W, 3)</span></span>
<span id="cb2-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cls (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> class to compute cam with respect to</span></span>
<span id="cb2-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_name (str)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> relevant layer in model</span></span>
<span id="cb2-10"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">H (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> input height</span></span>
<span id="cb2-11"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">W (int)</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> input width</span></span>
<span id="cb2-12"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Return</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-13"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        heatmap()</span></span>
<span id="cb2-14"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </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;">"    </span></span>
<span id="cb2-15"></span>
<span id="cb2-16"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    model_grad = Model([model.inputs], </span></span>
<span id="cb2-17"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">                       </span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">[</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model.get_layer(layer_name).output</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">,</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> model.output</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">]</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb2-18"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-19"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with tf.GradientTape() as tape</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-20"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        conv_output_values, predictions = model_grad(processed_image)</span></span>
<span id="cb2-21"></span>
<span id="cb2-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # assign gradient tape to monitor the conv_output</span></span>
<span id="cb2-23"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        tape.watch(conv_output_values)</span></span>
<span id="cb2-24"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span></span>
<span id="cb2-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # use binary cross entropy loss, actual_label = 0 if uninfected</span></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # get prediction probability of infected  </span></span>
<span id="cb2-27"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        pred_prob = predictions[:,1] </span></span>
<span id="cb2-28"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span></span>
<span id="cb2-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # make sure actual_label is a float, like the rest of the loss calculation</span></span>
<span id="cb2-30"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        actual_label = tf.cast(actual_label, dtype=tf.float32)</span></span>
<span id="cb2-31"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # add a tiny value to avoid log of 0</span></span>
<span id="cb2-33"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        smoothing = 0.00001 </span></span>
<span id="cb2-34"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        # Calculate loss as binary cross entropy</span></span>
<span id="cb2-36"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        loss = -1 * (actual_label * tf.math.log(pred_prob + smoothing) + (1 - actual_label) * tf.math.log(1 - pred_prob + smoothing))</span></span>
<span id="cb2-37"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">        </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print(f"binary loss</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">{</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">loss</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")</span></span>
<span id="cb2-38"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-39"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    # get the gradient of the loss with respect to the outputs of the last conv layer</span></span>
<span id="cb2-40"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    grads_values = tape.gradient(loss, conv_output_values)</span></span>
<span id="cb2-41"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    grads_values = K.mean(grads_values, axis=(0,1,2))</span></span>
<span id="cb2-42"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-43"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    conv_output_values = np.squeeze(conv_output_values.numpy())</span></span>
<span id="cb2-44"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    grads_values = grads_values.numpy()</span></span>
<span id="cb2-45"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-46"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    # weight the convolution outputs with the computed gradients</span></span>
<span id="cb2-47"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    for i in range(grads_values.shape[-1]): </span></span>
<span id="cb2-48"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        conv_output_values[:,:,i] *= grads_values[i]</span></span>
<span id="cb2-49"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    heatmap = np.mean(conv_output_values, axis=-1)</span></span>
<span id="cb2-50"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-51"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    heatmap = np.maximum(heatmap, 0)</span></span>
<span id="cb2-52"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    heatmap /= heatmap.max()</span></span>
<span id="cb2-53"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb2-54"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    del model_grad, conv_output_values, grads_values, loss</span></span>
<span id="cb2-55"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   </span></span>
<span id="cb2-56"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    return heatmap</span></span></code></pre></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/clfee/home/assets/images/gradcam_res.PNG" class="img-fluid figure-img"></p>
<figcaption>Result</figcaption>
</figure>
</div>
<p>Note: Instead of using max pooling that only keeps the highest valued ones. Average pooling allows some of the lesser intensity pixels to pass on in the pooling layer. It is important as we look at the small size of the image once it reaches this layer, max pooling could leave us with very little information.</p>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>python</category>
  <category>Image Classification</category>
  <guid>https://github.com/clfee/home/posts/2021-08-31-malaria-cell-classification - p2/</guid>
  <pubDate>Tue, 31 Aug 2021 05:00:00 GMT</pubDate>
</item>
</channel>
</rss>
