<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/to-eat-or-not-to-eat-where-is-the-question/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.nycdatascience.com/student-works/r-shiny/eat-not-eat-question/"> R – NYC Data Science Academy Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>According to NYC Health, “<em>Each year, thousands of New York City residents become sick from consuming foods or drinks that are contaminated with harmful bacteria, viruses or parasites”<a target="_blank" rel="nofollow" href="http://www1.nyc.gov/nyc-resources/service/1723/food-safety-or-poisoning-complaint"><span>[1]</span></a>.</em> Since a common source of food poisoning stems from eating out at insalubrious restaurants, I decided to create a Shiny App where users can easily search and access information on restaurants that were temporary closed due to hazardous sanitary violations in the past 5 years. The code for this project can be found on <a target="_blank" rel="nofollow" href="https://github.com/yvlau92">Github</a>. My Shiny can be found <a target="_blank" rel="nofollow" href="https://yvonnelau.shinyapps.io/nyc-restaurant-closures/">here</a>.</p>
<h2><strong>I. Introduction</strong></h2>
<p>NYC’s Department of Health and Mental Hygiene (DOHMH) conducts unannounced inspections of restaurants at least once a year to check for a variety of issues, such as compliance in food handling, food temperature, personal hygiene, and vermin control. According to its scoring system, each violation of a regulation gets a restaurant a certain number of points, which are then added to an overall score at the end of the inspection. The higher the score, the worst a restaurant performs. Each score is converted to a letter grade (e.g. A/B/C), which must be prominently posted at the entrance of a restaurant.</p>
<p>For my Shiny project, I was interested in investigating restaurant closures within NYC from 2013 to 2017. For example, some of my initial questions were:</p>
<ul>
<li>Do Manhattan restaurants have a lower proportion of closed restaurants than those in the Bronx?</li>
<li>Are Salad Shops in Staten Island more likely to have been closed than ones in Manhattan?</li>
<li>Is the length of temporary closure related to the overall score at the sanitary inspection (i.e higher score leading to longer closure timeframe)?</li>
</ul>
<p> </p>
<h2><strong>II. Dataset &amp; Cleanup</strong></h2>
<p>The Restaurant Inspection Results dataset is provided by the NYC DOHMH and can be found at NYC Open Data. It consists of about 400,000 entries from inspections conducted between 2012 and 2017, and includes at a high level information on restaurants location, cuisine, inspection dates and individual violations.</p>
<p>A version of January 17,2016 of the dataset was used for this shiny app. Some initial cleanup was performed on the data, including: removing rows with no scores and negative scores; changing format of dates; fixing borough naming issues; and shortening text values.</p>
<p>The code below was used to generate 3 new columns for the analysis of restaurant closures:</p>
<ul>
<li><span>n_infractions</span>: numbers of infractions committed at a particular date of inspection</li>
<li><span>n_closures</span>: number of closures a restaurant has had within the past 5 years.</li>
<li><span>days_diff</span>: number of days a restaurant took to reopen after being closed by the Health Department</li>
</ul>
<p>View the code on <a target="_blank" rel="nofollow" href="https://gist.github.com/yvlau92/d326a2d3ec129f68c0795835ad7246a7">Gist</a>.</p>
<p>Information on latitude and longitude was also added to the dataset for the map feature of my shiny app through the geocode function found at the ggmap library. For full details on my code to clean up this dataset, please refer to my github account.</p>
<h2></h2>
<p> </p>
<h2><strong>III. Analysis</strong></h2>
<h3><strong>A. Overall Distribution of Grades over the years</strong></h3>
<p>Since letter grades are what most NYC residents are familiar with, I first wanted to visualize the breakdown of restaurant grades and how that proportion has changed over the years. Looking at all 5 Boroughs combined, a few observations can be made:</p>
<ul>
<li>Most restaurants perform well at the restaurant inspection, with more than 80% of restaurants receiving an A grade.</li>
<li>The proportion of A grades has been increasing over the years, signaling a move by restaurants to become more hygienic.</li>
</ul>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/overall.png"><img sizes="(max-width: 955px) 100vw, 955px" alt="overall" class="wp-image-19006" src="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/overall.png?resize=456%2C118" data-recalc-dims="1"></a></p>
<p>The distribution of grades for each Borough found on my Shiny App also shows that, in terms of grades, there is no much differentiation across neighborhoods with most restaurants receiving A grades</p>
<h3></h3>
<p> </p>
<h3><strong>B. Proportion of closures</strong></h3>
<p>As most restaurants performed well at the sanitary inspections, I decided to focus my analysis on restaurant closures, as those were places that, independent of score received, committed some kind of sanitary violation that posed a serious threat to the health of customers.</p>
<h4><strong>B.1 Proportion of closures by Borough</strong></h4>
<p>The graph below provides some interesting insights:</p>
<ul>
<li>Overall, less than 2 % of restaurants in NYC have been shut down due to sanitary violation, reflecting the overall trend of clean restaurants seen on the overall analysis of restaurants.</li>
<li>The Bronx performs slightly worse than other Boroughs with 1.7% of restaurants closed at some point in the past 5 years, 0.5% higher than Manhattan, the best performer.</li>
</ul>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/borough.png"><img sizes="(max-width: 958px) 100vw, 958px" alt="borough" class="wp-image-19007" src="https://i2.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/borough.png?resize=456%2C129" data-recalc-dims="1"></a></p>
<p><strong> </strong></p>
<h4><strong>B.2. Proportion of closures by Borough and Cuisine</strong></h4>
<p>Since not much differentiation could be seen at the Borough level, I decided to take my analysis a level deeper, and investigate the proportion of closures by Borough and Cuisine.</p>
<p>The graph below displays the results of my analysis for the top 6 worst performers with highest closure proportions. On my Shiny App, users have the option to view more or less bars if they wish. This segmentation by both Borough and Cuisine offered some interesting insights:</p>
<ul>
<li>Four out the top six worst performers are in the Bronx area, which corroborates with the initial findings that the Bronx had the highest closure proportion among Boroughs</li>
<li>Restaurants on this grapg such as Salad Stores in Staten Island are places that should be potentially avoided since its closure proportion over the past 5 years is of 23.1% is much higher than the NYC average of 1.1%.</li>
</ul>
<p><img sizes="(max-width: 960px) 100vw, 960px" alt="borough and cuisine" class="wp-image-19008" src="https://i0.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/borough-and-cuisine.png?resize=456%2C129" data-recalc-dims="1"></p>
<h3></h3>
<p> </p>
<h3><strong>C. Length of closure vs Inspection Score</strong></h3>
<p>Another question I was interested in was whether inspection scores had any relationship with length of closure. Even though around 85% of restaurants only took less than a week to reopen, I was still expecting restaurants with a higher score to take a few more days to fix its problems and reopen.</p>
<p>The boxplot below shows the distribution of scores by closure length. However, there was no apparent relationship between the two variables aforementioned, with mean of scores around 46 across different categories of length of closure</p>
<h3><a target="_blank" rel="nofollow" href="https://i1.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/boxplot.png"><img sizes="(max-width: 969px) 100vw, 969px" alt="boxplot" class="wp-image-19009" src="https://i1.wp.com/blog.nycdatascience.com/wp-content/uploads/2017/02/boxplot.png?resize=456%2C223" data-recalc-dims="1"></a></h3>
<p> </p>
<h2></h2>
<h2><strong>IV. Conclusion</strong></h2>
<h3><strong>Findings</strong></h3>
<ul>
<li>Overall, dining out in NYC is pretty safe, with more than 80% of the restaurants receiving A grade. The proportion of A grade has also been increasing in the past 5 years, signaling a move by restaurants to become more hygienic</li>
<li>At a borough level, Bronx should be avoided if you do not want to dine out somewhere with a poor record of closures issued by the Health Department. At a borough and cuisine level, stay clear from Staten Island Salad Shops, which have about 23%(nyc average ~ 1%) of closure proportion.</li>
<li>There is no particular relationship between length of closure and inspection score. More than 85% of restaurants reopen within only a week after closing.</li>
</ul>
<h3><strong>Further Improvements/Analysis:</strong></h3>
<ul>
<li>Look over seasonal trends of closures</li>
<li>Analyze Inspection scores by type of violations</li>
<li>Include extra information on closed restaurants with a graph showing evolution of inspections scores</li>
</ul>
<p> </p>
<h2><strong>V. Other Features of the Restaurant Closures Shiny App</strong></h2>
<p>Aside from the graphs mentioned on this blog post, my shiny app also includes a map where one can find further details and location of restaurants shut down after poor performance in sanitary inspections within the past five years. There is also a tab labeled “Overview of All Restaurants”, where users can check the distribution of scores by Borough and find a Heat Map which displays the distribution of scores by Borough and Cuisine.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://blog.nycdatascience.com/student-works/r-shiny/eat-not-eat-question/">To Eat, or Not to Eat…WHERE is the question</a> appeared first on <a target="_blank" rel="nofollow" href="http://blog.nycdatascience.com/">NYC Data Science Academy Blog</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.nycdatascience.com/student-works/r-shiny/eat-not-eat-question/"> R – NYC Data Science Academy Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/L6GzFpZ44uM"></a></div>