Restaurant Reviews as Foodborne Illness Indicators
Contributed by Brain Saindon. He took NYC Data Science Academy 12 week full time Data Science Bootcamp program between Sept 23 to Dec 18, 2015. The post was based on his fourth class project(due at 8th week of the program).
CDC estimates that every year, nearly 1 in 6 Americans get sick from a foodborne illness. If you have ever experienced this type of sickness, you understand that foodborne illness is serious and unpleasant. In fact the CDC also estimates that 3,000 Americans die from a foodborne illness every year. You may have experienced a foodborne illness shortly after visiting a new restaurant and have decided to never again go back to that restaurant. During your two days of sickness you may have wished that you had decided to go to an alternative restaurant to prevent your sickness from happening. Is there any way that you could have known whether you would have experienced a foodborne illness from that restaurant before eating there?
This is a difficult question to tackle using only publicly available sources. However, we can reframe our question into one that we can more directly answer with publicly available data: Can you use restaurant reviews as a public health indicator? More specifically, can you use publicly available restaurant ratings to infer how well a restaurant handles their food? This project leverages publicly available data to explore whether there is a relationship between a restaurants rating and their food handling practices by asking two main questions.
- Do restaurants with A food grades (administered by the NYC DOH) have higher restaurant ratings compared to restaurants with B food grades?
- Do restaurants with a higher count of words associated with foodborne illness in their reviews tend to have higher restaurant ratings compared to restaurants with a lower count of words associated with foodborne illness?
- Using 106 restaurants as a sample size, these results show that restaurants with higher restaurant grades (administered by the NYC DOH) tend to have higher restaurant ratings. The graph below shows that B graded restaurants have an average rating of 3.7 whereas A grade restaurants have an average rating of 4.25.
- Using the same sample size of 106 restaurants, these results show that restaurants with a no words related to foodborne illnesses in their comments tend to have higher average restaurant ratings as compared to restaurants that have 1 or more words related to foodborne illnesses in their comments. The graph below shows that the average rating for restaurants with no foodborne illness flags have is about 4.8 whereas the average rating for restaurants with more than 1 foodborne illness flag is around 3.6.
In order to answer the two main question, I used two primary data sources from the NYC DOH and yelp. I used Python for the entire process from data extraction to descriptive analysis. Below outlines specific data sources and the specific code flow used to develop this project. My python code is available on Github.
- Scrape Yelp restaurant Reviews with Beautiful Soup
- 3988 Reviews –200 Restaurants
- Download NYC DOH Restaurant Inspection Results
Code Approach - Python
- Scrape the 20 most recent yelp reviews from the top 200 restaurants in Manhattan. This led to a total of 3,988 reviews from 200 restaurants.
- Create a Foodborne Illness flag if any of the following words associated with foodborne illness appeared in the comment text: ['ill', 'foodborne', 'sick', 'vomit', 'sickness', 'poisoning', 'headache', 'fever', 'cramp']
- Download Restaurant grade data and select the latest restaurant grade per restaurant.
- Merge restaurant reviews to restaurant grades data by phone. 106 restaurants matched.
- Perform summary statistics to answer the projects main questions.
Python Code Available On Github
Python Code Details
See below for snippets of my python code developed for this project. For a complete view of my code, click on my github link above.
Initially, I import several packages which will be leveraged downstream:
First, I create a 'master list' of the URLs required to extract the names of the to 200 restaurants on Yelp. This list will run through a downstream code.
Next, I run the above url list through a for loop in order to create a list of urls which will include the direct link to each restaurant's first page of reviews.
Once I have a list of the urls for the top 200 restaurants, I run the final_url_list through a loop and leverage BeautifulSoup in order to extract relevant data for this analysis. Specifically, the below loop will extract the following information from each restaurant's top 20 reviews:
The next lines of code will create a foodborne illness flag if any of the following words appear in a restaurants review: [' ill', 'foodborne', 'sick', 'vomit', 'sickness', 'poisoning', 'headache', 'fever', 'cramp']
A necessary part of this analysis, is the NYC DOH data which contains all information on the restaurant grades within the city. The code below calls in this data as a pandas dataframe.
For each restaurant, I only want the latest restaurant grade. The code below selects the latest restaurant grade administered to each restaurant.
Time to match the data sources (ratings and DOH restaurant grades) so that we can prepare begin our analysis:
In order to move forward with the analysis, I must obtain each restaurant's average rating and the number of comments containing a foodborne illness flag for each restaurant:
The below code merges both summary measures into one data frame:
Now that we have the data ready for analysis, we first create a histograms of average restaurant rating group by restaurant grades. This is the first image presented above.
For a second visualization, I created another histogram plot of average restaurant ratings grouped by number of comments which included a foodborne illness flag.
Conclusion & Next Steps
The results of this project suggest that average restaurant ratings may indicate how well a restaurant performs during a food inspection administered by the NYC Department of Health. Additionally, we see that restaurants with a higher amount of foodborne illness flags (as derived from the text within a restaurant's reviews) tend to have lower average restaurant ratings. The results of this project only suggest that restaurant ratings may be correlated with foodborne illness outbreaks. In order to further investigate a possible association between restaurant reviews and foodborne illness outbreaks, additional statistical and study design methods must be considered to improve the validity and robustness of this project:
- More Data
- Additional review sources: Incorporate restaurant reviews from other sources in addition to yelp.
- Additional Restaurants: Include more than 200 restaurants in order to have a bigger sample size.
- Improve match rate between restaurant grade data and restaurant review data: Use restaurant name and address.
- Refine foodborne illness flag (FBI) classification: Perform a more thorough literature review to identify the best words to use for the FBI flag derivation.
- Apply additional statistical methods and machine learning techniques:
- Apply t-test/ANOVA to identify whether the difference in average restaurant rating is statistically different between restaurant grade groups/fbi flag groups.
- Explore the ability to prospectively predict foodborne illness outbreak or restaurant grade given previous restaurant review ratings. Potential supervised algorithms that will add value to this recommendation are:
- Logistic Regression
- Classification Tree
- Support Vector Machine
It is important to not assume that poor restaurant reviews indicate whether an individual will experience a foodborne illness from these results. With the above recommendations implemented, one can move closer to identifying whether restaurant ratings are statistically associated with foodborne illness outbreaks.