What is good food?
๐ Background
You and your friend have gotten into a debate about nutrition. Your friend follows a high-protein diet and does not eat any carbohydrates (no grains, no fruits). You claim that a balanced diet should contain all nutrients but should be low in calories. Both of you quickly realize that most of what you know about nutrition comes from mainstream and social media.
Being the data scientist that you are, you offer to look at the data yourself to answer a few key questions.
๐พ The data
You source nutrition data from USDA's FoodData Central website. This data contains the calorie content of 7,793 common foods, as well as their nutritional composition. Each row represents one food item, and nutritional values are based on a 100g serving. Here is a description of the columns:
- FDC_ID: A unique identifier for each food item in the database.
- Item: The name or description of the food product.
- Category: The category or classification of the food item, such as "Baked Products" or "Vegetables and Vegetable Products".
- Calories: The energy content of the food, presented in kilocalories (kcal).
- Protein: The protein content of the food, measured in grams.
- Carbohydrate: The carbohydrate content of the food, measured in grams.
- Total fat: The total fat content of the food, measured in grams.
- Cholesterol: The cholesterol content of the food, measured in milligrams.
- Fiber: The dietary fiber content of the food, measured in grams.
- Water: The water content of the food, measured in grams.
- Alcohol: The alcohol content of the food (if any), measured in grams.
- Vitamin C: The Vitamin C content of the food, measured in milligrams.
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(plotly))
df_food <- readr::read_csv('nutrition.csv', show_col_types = FALSE)
df_food๐ช Competition challenge
Create a report that covers the following:
Which fruit has the highest vitamin C content? What are some other sources of vitamin C?Describe the relationship between the calories and water content of a food item.What are the possible drawbacks of a zero-carb diet? What could be the drawbacks of a very high-protein diet?According to the Cleveland Clinic website, a gram of fat has around 9 kilocalories, and a gram of protein and a gram of carbohydrate contain 4 kilocalories each. Fit a linear model to test whether these estimates agree with the data.Analyze the errors of your linear model to see what could be the hidden sources of calories in food.
Which Food has the Highest Vitamin-C?
df_food %>%
select(Item, `Vitamin C`) %>%
filter(`Vitamin C` != 'null') %>%
mutate(`Vitamin C` = str_replace(`Vitamin C`, " mg",""),
`Vitamin C (mg)` = as.numeric(`Vitamin C`)) %>%
select(-`Vitamin C`) %>%
arrange(desc(`Vitamin C (mg)`)) %>%
head(5) Baby food has the highest vitamin-C content. Runner ups are flavored powdered-beverages, frozen red peppers, and west indian cherries
How Do Calories Correlate with Water Content?
df_food %>% select(Calories, Water) %>%
mutate(water_g = as.numeric(str_replace(Water, " g", "")),
kcal = as.numeric(str_replace(Calories," kcal", ""))) %>%
select(-Water, -Calories) %>%
cor()Water content and calories are negatively correlated. Expect that, on average, foods with higher water content have fewer calories.
Comparing High-Protein and Low-Carb Diets
suppressPackageStartupMessages(library(reshape2))
df_food %>%
select(-FDC_ID, -Item, -Category) %>%
mutate(Calories = as.numeric(str_replace(Calories, " kcal", "")),
Protein = as.numeric(str_replace(Protein, " g", "")),
Carbohydrate = as.numeric(str_replace(Carbohydrate, " g", "")),
`Total fat` = as.numeric(str_replace(`Total fat`, " g", "")),
Cholesterol = as.numeric(str_replace(Cholesterol, " mg", "")),
Fiber = as.numeric(str_replace(Fiber, " g", "")),
Water = as.numeric(str_replace(Water, " g", "")),
Alcohol = as.numeric(str_replace(Alcohol, " g", "")),
`Vitamin C` = as.numeric(str_replace(`Vitamin C`, " mg", ""))) %>%
sapply(., function(x) replace(x, is.na(x), 0)) %>%
cor() %>%
melt() %>%
ggplot(., aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
scale_fill_gradient2(low = 'blue',
mid = 'white',
high = 'red',
midpoint = 0,
limits = c(-1, 1)) +
labs(title = 'Correlation Matrix',
x = '',
y = '') +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
A consequence of a high protein diet may be consuming fewer carbohydrates and higher cholesterol. Consequences of a low carb diet may include fewer calories, eating more protein, eating less fiber, and higher water content in your foods.
How many Calories do Fat, Protein, and Carbohydrates contain?
#Selecting necessary columns and reformating to numerical
df <- df_food %>%
select(Calories, Protein, Carbohydrate, `Total fat`) %>%
mutate(Calories = as.numeric(str_replace(Calories, " kcal", "")),
Protein = as.numeric(str_replace(Protein, " g", "")),
Carbohydrate = as.numeric(str_replace(Carbohydrate, " g", "")),
`Total fat` = as.numeric(str_replace(`Total fat`, " g", ""))) %>%
#Replacing missing values with 0
sapply(., function(x) replace(x, is.na(x), 0)) %>%
data.frame()
#Creating the linear model
model <- lm(data = df, formula = Calories ~ . + 0)
model
#Gathering predicted values
df$pred_cal <- predict(model, df)
#Plotting predicted values versus actual and drawing the regression line
ggplot(df, aes(x = pred_cal, y = Calories)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x, color = "red") +
theme_classic() +
labs(title = 'Regression Line of Calories ~ Fat + Protein + Carbs',
x = 'Predicted Calories',
y = 'Actual Calories')โ
โ