Using Data to Identify harmful ingredients and added sugars in food
The skills the authors demonstrated here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
Are there harmful ingredients in your food? How about added sugars? We will be using data collected to see what ingredients are harmful and which have extra sugars.
The harmful ingredients found in various foods may cause cancer, reproductive problems, or developmental problems. In California, a warning is found on foods if they cause any of these concerns (it's known as Proposition 65). There is a summary of six of these harmful ingredients at the end of this article, in a section titled "Additional Information."
Many foods contain added sugar. As a result, they are major sources of empty calories, that is calories that deliver no nutritional benefits. While fruits also contain sugar, that natural source tends to also deliver vitamins that the body needs. By July 2018, all food manufacturers must note on their nutrition labels how many grams of added sugars are found in their product, as noted on the FDA's website.
The Environmental Working Group has a cornucopia of useful information on their website (ewg.org). In addition to reviews, the site includes detailed information on ingredients and nutrition facts for thousands of foods. The structure of the EWG website is divided into categories, subcategories, and, finally, individual foods. I scraped the data to find out which foods contain the most harmful ingredients and added sugars.
I decided to use Scrapy, a package in Python, to scrape ewg.org. With Scrapy, a user tells the spider where to visit and where to extract the data from.
For each piece of data, an individual xpath was created, which notes where the data is located on the web page. The items extracted were the categories, subcategories, scores, nutrients, and ingredients.
Starting on ewg.org/foodscores, the categories and subcategories were extracted through their respective xpaths. Scraping this website did not provide any severe challenges such as re-routing to other sites or problems due to prohibited scraping.
Item 2: The secondary page is where a listing of many items in a category or subcategory can be found.
The issues that were resolved included finding well-hidden xpaths, creating a list of the xpaths of categories and subcategories for the spider to visit (since four categories did not have subcategories), and extracting scores for each of the individual foods from their respective image URLs.
Once I obtained all the data, I converted the serving units to grams for consistent measurements in comparing the various products. I also calculated the amount of sugar, carbohydrates, fat, and protein by weight, and I used regular expression to find the names and counts of harmful ingredients and added sugars for each observation.
The extraction resulted in a data set of 46,000 foods and their scores, ingredients, and nutrients.
To start off the analysis, I was interested in seeing the relationship of the number of ingredients in an item to the number of harmful ingredients in that item. As the scatterplot shows, when the number of ingredients increases, the number of harmful ingredients also increases.
Item 4: As the number of ingredients rises, the number of harmful ingredients also rises.
After seeing this, I was curious about differences among different categories of food.
Item 5: I created a stacked bar plot and plotted the median values of the total number of ingredients (the blue bar), the number of added sugars (the orange bar), and the number of harmful ingredients (the green bar).
Frozen food, baked goods (including breads, cakes, and pastries) and prepared meals have the highest number of ingredients and the highest number of harmful ingredients. The average number of total ingredients is 25 and the average number of harmful ingredients is 2.
Categories with the lowest number of ingredients includes herbs, seasonings, fruits, vegetables, beans, rice, pasta, and grains.
Desserts, baked goods, candy, peanut butter, jams, jellies, and baking supplies had the most added sugars, averaging 3 to 4 added sugars each.
Then I was interested in seeing the scores of these categories and comparing those to our findings above.
In the bar plot below, we see that three of the four worst scoring foods are the foods with the highest amounts of added sugars (baking supplies, desserts, and candy). The foods with the best scores are fruits, vegetables, beans, baby food, rice, pasta, and grains.
Item 6: EWG's median scores for each food category.
To get a better understanding of the Environmental Working Group's scores, I decided to look at the score distribution for processed food.
On the left hand side, we have the distribution of minimally and moderately processed foods, which is fairly normal. Most scores are in the center (scoring a 5) and the number of foods for each score decreases as we move away from the center. We see a slight uptick on the left side, around scores 1 to 1.5, as there are many fruits and vegetables with those ratings.
On the right hand side, we have the distribution of highly processed foods, where the score is most often a 10 (the worst score). There is also a fairly stable distribution of scores from 6 to 9.
Item 7: The score distribution of minimally/moderately processed foods (left side) and the distribution of highly processed foods (right side).
Since frozen foods are tied with baked goods for the most ingredients (averaging 25 ingredients per product), I wanted to see if there were differences in the subcategories of frozen foods.
As we see here, frozen desserts and pizzas are the worst for you, but there is a quite a drop off when we look at frozen fruits and vegetables which are very good for you, despite being frozen.
After understanding the composition of the scores, I decided to find the best and worst rated foods by subcategory. I created bar plots showing the mean score by subcategory along with the count of foods in that subcategory.
Flours and corn meal, fresh fruits and vegetables, frozen fruits and vegetables, and pasta and noodles rank the highest. Flours & corn meal only had 12 reviews, as compared to a range of 389 to 1,154 reviews for the other subcategories mentioned.
Item 9: Foods with the best scores / Item 10: Count of scores for the best foods
The subcategories with the worst mean scores are baking decorations (frosting, sprinkles), half and half & creamers, processed and deli meats, and canned meat. The canned meat subcategory only has 16 reviews while the others have between 335 and 1,149 reviews.
Item 11: Foods with the worst scores / Item 12: Count of scores for the worst foods
After seeing processed meat rank high on the list for worst products, I wanted to see how many harmful ingredients are found in processed meat. As we see in the plot below, each kind of processed meat has, on average, two to three harmful ingredients in it (namely, nitrates and nitrites are found in processed meat). Processed meat should be consumed in moderation, if at all.
I also wanted to take a closer look at items that are marketed as "healthy," to see if they actually are. I decided to compare bars - cereal bars, candy bars, granola bars, and trail mix bars - to see how much sugar by weight is in each of these and how their scores compare to their sugar content.
Candy bars take the lead at 42% sugar by weight, while many others follow around 36%. Granola and trail mix bars have the least amount of sugar, 23% and 26%, respectively, yet the amount of sugar they contain is somewhat of a concern.
The scores correspond fairly well to the amount of sugar by weight, as granola and trail mix bars maintain their ranks on the bottom of the list. Other attributes making up the EWG score are how processed each item is, how many added sugars are in them, and nutritional content (if any).
Item 14: Sugar by weight for various bars (Candy, trail mix, and granola bars) / Item 15: Scores of various bars
An Anova test was used to test the differences in group means. Since the test resulted in a p-value less than 0.05, we reject the null value that the food subcategories are the same. In other words, it would be a very rare chance that the group means of scores among the bars are the same.
In conclusion, when deciding what to buy, know that in general, the fewer the ingredients, the better. You should check that the product is free from harmful ingredients or added sugars, you can look at the scores on ewg.org, or you can look at categories to watch out for in the stacked bar plot above.
Examples of foods that should be limited are desserts, candy, baked goods, and processed meats because they are high in harmful ingredients and added sugars.
Future research would look at sales data to see what people usually purchase, what criteria are usually evaluated and how these criteria are prioritized. I would also be interested in seeing what role demographics play in the selection of healthy or unhealthy foods and to see if convenience is a large factor in what a person purchases.
The list of harmful ingredients includes:
- Nitrates and nitrites, deemed carcinogens by the World Health Organization, are often added to processed meats.
- Potassium bromate is deemed a carcinogen by the International Agency for Research on Cancer. This ingredient is banned from Brazil, Canada, and the United Kingdom, yet it can still be found in certain brands of flour and breads.
- Propyl paraben is found in pastries. It is an endocrine disruptor linked to lowered sperm counts and decreased fertility.
- Butylated hydroxyanisole (BHA), added to chips and preserved meat, is an item that the The National Institute of Health notes is "reasonably anticipated to be a human carcinogen".
- Butylated hydroxytoluene (BHT), an endocrine disruptor, is found in cereal.
- Propyl gallate is associated with tumors and may be an endocrine distruptor. It is found in sausage and lard.
For more information on each of these harmful ingredients and for information on the other six, please visit Environmental Health Watch's web page titled "Dirty Dozen Guide to Food Additives."
The code used to create this Shiny app is publicly available at gist.github.com/fouadyared
The skills I demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.