1 Introduction

In order to have a deeper understanding of nutritional data, in this project, I will use the comprehensive Food and Nutrient Database for Dietary Studies (FNDDS) from the U.S. Department of Agriculture (USDA). In this database we find a wide variety of ingredients, from pure vitamin D to dairy products.

The focus of this analysis will be on the core nutrients that are present in nutritional information labels. These include protein, total fat, total carbohydrates, energy, sugars, dietary fiber, calcium, iron, potassium, sodium, cholesterol, saturated fat, monounsaturated fat, and polyunsaturated fat. This nutritional information is essential for maintaining a good health. PCA enables us to reduce the feature space while still preserving the essential variance and patterns in the nutritional data, offering a distilled yet comprehensive view of the nutritional relationships and insights.

2 Dataset

The dataset used is the Food and Nutrient Database for Dietary Studies (FNDDS) from the U.S. Department of Agriculture (USDA), which includes data on the nutrient values for a large quantity of ingredients. This information is valuable to have a deeper understanding of the food we consume and which ingredients may be more suitable for each person’s diet. The version of the dataset corresponds to the FNDDS 2019-2020, which was published in October, 2022.

For this study I focused on the nutrients that are commonly found in the nutritional information labels, such as: protein, total.fat, total.carbohydrates, energy, sugars, dietary.fiber, calcium, iron, potassium, sodium, cholesterol, saturated.fat, monounsaturated.fat, polyunsaturated.fat.

df <- read.csv('fndds/fndds_ingredient_nutrient_value.csv')
nutrients <- read.csv('fndds/nutrient.csv') %>%
  select(nutrient_nbr, name) %>%
  rename(nutrient.name = name) %>%
  mutate(nutrient.name = tolower(gsub("[^[:alnum:] ]", "", nutrient.name))) %>%
  mutate(nutrient.name = gsub(" ", "_", nutrient.name))

df <- df %>%
  left_join(nutrients, by = c("Nutrient.code" = "nutrient_nbr"))

df <- df %>% filter(Nutrient.code %in% important_nutrient_codes)

ingredients <- df %>%
  pivot_wider(id_cols = c(ingredient.code, Ingredient.description),
              names_from = nutrient.name,
              values_from = Nutrient.value)

colnames(ingredients) <- c("ingredient.code", "ingredient.description", nutrient_mapping)

2.1 Summary

From a summary of the dataset, we observe a wide range of values for these nutrients, with protein content ranging from 0 to 88.32g, and fat content extending up to 100g (for oils and others), which reflects the diversity of ingredients. Carbohydrates vary significantly as well, with a median of 11.715g.

summary(ingredients[,c(3:ncol(ingredients))])
##     protein         total.fat        total.carbohydrates     energy     
##  Min.   : 0.000   Min.   :  0.0000   Min.   :  0.000     Min.   :  0.0  
##  1st Qu.: 1.150   1st Qu.:  0.3525   1st Qu.:  2.678     1st Qu.: 66.0  
##  Median : 5.400   Median :  3.7700   Median : 11.715     Median :194.5  
##  Mean   : 9.391   Mean   : 10.5197   Mean   : 24.501     Mean   :227.1  
##  3rd Qu.:14.297   3rd Qu.: 14.1250   3rd Qu.: 46.130     3rd Qu.:356.0  
##  Max.   :88.320   Max.   :100.0000   Max.   :100.000     Max.   :902.0  
##      sugars       dietary.fiber         calcium               iron          
##  Min.   : 0.000   Min.   :   0.000   Min.   :      0.0   Min.   :     0.00  
##  1st Qu.: 0.120   1st Qu.:   0.000   1st Qu.:     10.0   1st Qu.:     0.29  
##  Median : 2.580   Median :   0.900   Median :     25.0   Median :     0.92  
##  Mean   : 9.711   Mean   :   2.796   Mean   :    634.2   Mean   :    55.37  
##  3rd Qu.: 9.935   3rd Qu.:   2.800   3rd Qu.:     85.5   3rd Qu.:     2.26  
##  Max.   :99.800   Max.   :1000.000   Max.   :1000000.0   Max.   :100000.00  
##    potassium           sodium         cholesterol      saturated.fat     
##  Min.   :    0.0   Min.   :    0.0   Min.   :   0.00   Min.   : 0.00000  
##  1st Qu.:  104.0   1st Qu.:   16.0   1st Qu.:   0.00   1st Qu.: 0.05425  
##  Median :  194.0   Median :   98.0   Median :   0.00   Median : 0.99500  
##  Mean   :  288.7   Mean   :  375.8   Mean   :  29.67   Mean   : 3.44362  
##  3rd Qu.:  320.0   3rd Qu.:  452.0   3rd Qu.:  35.00   3rd Qu.: 4.07750  
##  Max.   :16500.0   Max.   :38700.0   Max.   :3100.00   Max.   :93.96600  
##  monounsaturated.fat polyunsaturated.fat
##  Min.   : 0.00000    Min.   : 0.0000    
##  1st Qu.: 0.03725    1st Qu.: 0.0860    
##  Median : 1.08450    Median : 0.5305    
##  Mean   : 3.74597    Mean   : 2.4513    
##  3rd Qu.: 4.66725    3rd Qu.: 2.1117    
##  Max.   :72.73400    Max.   :67.8490

2.2 Histogram

From the following histogram plot, we notice that most of the nutrients don’t seem to follow a normal distribution. Moreover, most of the ingredients seem to have close to 0 minerals, which is visible from the histogram of these minerals. Protein and total fats seem to follow a similar distribution. I’ll take a closer look at these nutrients in the next steps.

library(ggplot2)

histogram_list <- list()

for (i in 3:ncol(ingredients)) {
  column_name <- names(ingredients)[i]
  p <- ggplot(ingredients, aes_string(x = column_name)) +
        geom_histogram(bins = 30, fill = "blue", color = "black") +
        ggtitle(column_name) +
        theme_minimal() +
        theme(plot.title = element_text(hjust = 0.5, size = 15))
  
  # Add the plot to the list
  histogram_list[[i-2]] <- p
}

# Use the 'gridExtra' package to arrange and display all histograms
if (!require("gridExtra")) {
  install.packages("gridExtra")
}
library(gridExtra)


do.call(grid.arrange, c(histogram_list, ncol = 3))

2.3 Data normalization

Before continuing with the analysis, to avoid issues with the PCA process, I will address the skewness and magnitude of the data that is visible in the histogram plots. For this I’ll use the preProcess function from the caret library to normalize the data.

library(corrplot)
library(caret)

numeric_ing = ingredients[,c(3:ncol(ingredients))]

preproc <- preProcess(numeric_ing, method=c("center", "scale"))
ing_norm <- predict(preproc, numeric_ing)

2.4 Correlation Plot

From the correlation plot we can observe that energy has a correlation of 0.81 with total fats and greater than 0.6 with the specific fats. Protein has a weak positive correlation with Potassium, Energy, Cholesterol and some fats. Carbohydrates have a correlation of 0.63 with Sugars and 0.49 with Energy.

library(corrplot)

cor<-cor(ing_norm[,1:ncol(ing_norm)], method="pearson") 
corrplot(cor)

3 PCA

For dimension reduction, I will use the Principal Component Analysis (PCA) method, which is useful for minimizing the information loss.

## Importance of components:
##                           PC1    PC2     PC3     PC4     PC5     PC6     PC7
## Standard deviation     1.9139 1.4119 1.14373 1.02082 1.00272 1.00019 0.99605
## Proportion of Variance 0.2616 0.1424 0.09344 0.07443 0.07182 0.07146 0.07087
## Cumulative Proportion  0.2616 0.4040 0.49747 0.57190 0.64372 0.71518 0.78604
##                            PC8     PC9    PC10   PC11    PC12    PC13    PC14
## Standard deviation     0.96574 0.82612 0.79567 0.6114 0.60344 0.08791 0.03950
## Proportion of Variance 0.06662 0.04875 0.04522 0.0267 0.02601 0.00055 0.00011
## Cumulative Proportion  0.85266 0.90141 0.94663 0.9733 0.99934 0.99989 1.00000

From the summary of the PCA we can observe that the first two components explain 40.4% of the variance, and the first 9 components explain 90.14% of the variance. The last 2 components explain little to no variance.

3.1 Number of components

To determine the optimal number of components to keep, I will use a scree plot to visualize the eigenvalues. From this plot, we could conclude that we only need 3 components to explain most of the variance. However, from the previous PCA summary table, combined with the scree plot, I will move forward with 8 components that will explain 85.26% of the variance, and in the scree plot we see that even though is past the elbow point, there are further drops in explained variance past this point.

fviz_eig(ing_pca, choice = "eigenvalue", ncp = ncol(ing_pca[[2]]), addlabels = TRUE,   main = "Scree plot (Eigenvalues)")

3.2 Components analysis

To take a closer look at the variables, in the following plot we can observe the first two components. The first principal component (PC1), explaining 26.2% of the variance, is heavily influenced by energy and fats, which suggests that these factors contribute significantly to the variance in our diets.

The second principal component (PC2), explains 14.2% of the variance, is related to sugars and total carbohydrates, while protein and cholesterol are in the opposite side of this dimension (this is aligned with the small negative correlation between sugars and protein). Finally, minerals such as potassium have shorter vectors in these two components, suggesting a smaller influence over the variance captured by these two components.

fviz_pca_var(ing_pca, col.var="contrib", repel = TRUE, gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),)

Taking a look at the individual components, those with a orange color are the ones most well represented by the components.

#ing_data <- ingredients[,c(3:ncol(ingredients))]
#rownames(ing_data) <- ingredients$ingredient.description
#ing.pca <- prcomp(ing_data, center = TRUE, scale = TRUE)

fviz_pca_ind(ing_pca, col.ind = "cos2", gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"), repel = FALSE, label=c("none"))

4 Summary

From this analysis, we can determine that 85.26% of the variance can be explained with eight dimensions. However, five of these dimensions have an eigenvalue of 1 or less, suggesting that they explain the variance of no more than one variable each. The major contribution to variance explanation can be attributed to the first three components, especially PC1 and PC2, which are closely associated with energy, fats, and carbohydrates, respectively, as shown in the components analysis plot.