Are you a dairy lover or a cow hugger? How often do you purchase lactaid in order to consume dairy?

## In this project I will discuss nutritional composition of food, specifically dairy based products. The datset contains about 9000+ rows f food items, but sI wanted to explore items that mean something to me. The URL provided below is where the csv file came. Unfortunately the author of the dataset did not provide enough background to where the database originated. There are two variables that are integers and one that is a string. There are not really any NA’s present, luckily.

https://www.kaggle.com/datasets/thedevastator/now-with-more-nutrients

The variables I’ll observe and manipulate are: NDB_No = National Nutrient Database Number Energy_kcal = Energy measured in Kcal Protein_g = Protein measured in Grams Saturated_Fats_g = Saturated Fats measured in Grams Fat_g = Fats measured in Grams Carb_g = carbohydrates in Grams Fiber_g = Fiber in Grams Sugar_g = Sugar in Grams Calcium_mg = Calcium in milligrams Iron_mg = Iron in milligrams Magnesium_mg = Magnesium in milligrams Phosphorus_mg = Phosphorus in milligrams Potassium_mg = Potassium in milligrams Sodium_mg = Sodium in milligrams Zinc_mg = Zinc in milligrams Copper_mcg = micrograms Magnese_mg = Manganese in milligrams Selenium_mcg = Selenium in micrograms VitC_mg = Vitamin C in milligrams

The first chunk to start out is to load the libraries ad set the working directory

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.1     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyr)
setwd("C:/Users/earlb/OneDrive/Documents/Data Science 110")
food <-read_csv("FoodNutri.csv", locale=locale(encoding="latin1")) 
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 9318 Columns: 29
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (2): NDB_No, Descrip
## dbl (27): Energy_kcal, Protein_g, Saturated_fats_g, Fat_g, Carb_g, Fiber_g, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

The second chunk will show the first couple of columns of the dataset with the ‘head’ function and follow up with the ‘summary’ function to show possible NA’s, lengths of columns,

head(food)
## # A tibble: 6 × 29
##   NDB_No Descrip     Energy_kcal Protein_g Saturated_fats_g Fat_g Carb_g Fiber_g
##   <chr>  <chr>             <dbl>     <dbl>            <dbl> <dbl>  <dbl>   <dbl>
## 1 1001   butter wit…         717      0.85             51.4  81.1   0.06       0
## 2 1002   butter whi…         718      0.49             45.4  78.3   2.87       0
## 3 1003   butter oil…         876      0.28             61.9  99.5   0          0
## 4 1004   cheese blue         353     21.4              18.7  28.7   2.34       0
## 5 1005   cheese bri…         371     23.2              18.8  29.7   2.79       0
## 6 1006   cheese brie         334     20.8              17.4  27.7   0.45       0
## # ℹ 21 more variables: Sugar_g <dbl>, Calcium_mg <dbl>, Iron_mg <dbl>,
## #   Magnesium_mg <dbl>, Phosphorus_mg <dbl>, Potassium_mg <dbl>,
## #   Sodium_mg <dbl>, Zinc_mg <dbl>, Copper_mcg <dbl>, Manganese_mg <dbl>,
## #   Selenium_mcg <dbl>, VitC_mg <dbl>, Thiamin_mg <dbl>, Riboflavin_mg <dbl>,
## #   Niacin_mg <dbl>, VitB6_mg <dbl>, Folate_mcg <dbl>, VitB12_mcg <dbl>,
## #   VitA_mcg <dbl>, VitE_mg <dbl>, VitD2_mcg <dbl>
summary(food)
##     NDB_No            Descrip           Energy_kcal      Protein_g     
##  Length:9318        Length:9318        Min.   :  0.0   Min.   : 0.000  
##  Class :character   Class :character   1st Qu.: 86.0   1st Qu.: 2.333  
##  Mode  :character   Mode  :character   Median :183.0   Median : 8.100  
##                                        Mean   :220.9   Mean   :11.322  
##                                        3rd Qu.:330.6   3rd Qu.:19.870  
##                                        Max.   :902.0   Max.   :88.320  
##                                                                        
##  Saturated_fats_g     Fat_g            Carb_g          Fiber_g     
##  Min.   : 0.000   Min.   :  0.00   Min.   :  0.00   Min.   : 0.00  
##  1st Qu.: 0.136   1st Qu.:  0.82   1st Qu.:  0.00   1st Qu.: 0.00  
##  Median : 1.319   Median :  4.80   Median :  8.72   Median : 0.40  
##  Mean   : 3.305   Mean   : 10.20   Mean   : 21.50   Mean   : 2.15  
##  3rd Qu.: 4.000   3rd Qu.: 13.21   3rd Qu.: 32.45   3rd Qu.: 2.50  
##  Max.   :95.600   Max.   :100.00   Max.   :100.00   Max.   :79.00  
##                                                                    
##     Sugar_g         Calcium_mg         Iron_mg         Magnesium_mg   
##  Min.   : 0.000   Min.   :   0.00   Min.   :  0.000   Min.   :  0.00  
##  1st Qu.: 0.000   1st Qu.:   9.00   1st Qu.:  0.490   1st Qu.: 10.00  
##  Median : 0.460   Median :  19.93   Median :  1.300   Median : 20.00  
##  Mean   : 6.523   Mean   :  73.63   Mean   :  2.665   Mean   : 33.63  
##  3rd Qu.: 5.487   3rd Qu.:  64.00   3rd Qu.:  2.598   3rd Qu.: 30.00  
##  Max.   :99.800   Max.   :7364.00   Max.   :123.600   Max.   :781.00  
##                                                       NA's   :1       
##  Phosphorus_mg     Potassium_mg       Sodium_mg          Zinc_mg      
##  Min.   :   0.0   Min.   :    0.0   Min.   :    0.0   Min.   : 0.000  
##  1st Qu.:  36.0   1st Qu.:  117.0   1st Qu.:   31.0   1st Qu.: 0.230  
##  Median : 131.0   Median :  225.0   Median :   79.0   Median : 0.800  
##  Mean   : 154.6   Mean   :  271.9   Mean   :  294.2   Mean   : 1.906  
##  3rd Qu.: 216.0   3rd Qu.:  334.0   3rd Qu.:  381.0   3rd Qu.: 2.610  
##  Max.   :9918.0   Max.   :16500.0   Max.   :38758.0   Max.   :90.950  
##                                                                       
##    Copper_mcg       Manganese_mg       Selenium_mcg        VitC_mg        
##  Min.   : 0.0000   Min.   :  0.0000   Min.   :   0.00   Min.   :   0.000  
##  1st Qu.: 0.0300   1st Qu.:  0.0010   1st Qu.:   0.10   1st Qu.:   0.000  
##  Median : 0.0790   Median :  0.0250   Median :   4.30   Median :   0.000  
##  Mean   : 0.1771   Mean   :  0.5126   Mean   :  12.91   Mean   :   8.635  
##  3rd Qu.: 0.1460   3rd Qu.:  0.2320   3rd Qu.:  21.50   3rd Qu.:   2.780  
##  Max.   :15.0500   Max.   :328.0000   Max.   :1917.00   Max.   :2732.000  
##                                                                           
##    Thiamin_mg      Riboflavin_mg        Niacin_mg          VitB6_mg      
##  Min.   : 0.0000   Min.   : 0.00000   Min.   :  0.000   Min.   :  0.000  
##  1st Qu.: 0.0300   1st Qu.: 0.04225   1st Qu.:  0.383   1st Qu.:  0.037  
##  Median : 0.0750   Median : 0.14500   Median :  2.000   Median :  0.125  
##  Mean   : 0.2015   Mean   : 0.22660   Mean   :  3.301   Mean   :  1.900  
##  3rd Qu.: 0.2167   3rd Qu.: 0.25300   3rd Qu.:  4.939   3rd Qu.:  0.366  
##  Max.   :23.3750   Max.   :17.50000   Max.   :127.500   Max.   :316.000  
##                                                                          
##    Folate_mcg        VitB12_mcg        VitA_mcg           VitE_mg        
##  Min.   :   0.00   Min.   : 0.000   Min.   :    0.00   Min.   :  0.0000  
##  1st Qu.:   0.00   1st Qu.: 0.000   1st Qu.:    0.00   1st Qu.:  0.0000  
##  Median :   7.00   Median : 0.010   Median :    1.00   Median :  0.1300  
##  Mean   :  36.65   Mean   : 1.135   Mean   :   89.13   Mean   :  0.9083  
##  3rd Qu.:  19.00   3rd Qu.: 1.110   3rd Qu.:   19.00   3rd Qu.:  0.4825  
##  Max.   :2875.00   Max.   :98.890   Max.   :30000.00   Max.   :224.0000  
##                                                        NA's   :6         
##    VitD2_mcg       
##  Min.   :  0.0000  
##  1st Qu.:  0.0000  
##  Median :  0.0000  
##  Mean   :  0.3439  
##  3rd Qu.:  0.0000  
##  Max.   :250.0000  
## 

Let us look at the class of some of the variables we will use, like protein, energy, fat, and sugar. The main components we concern ourselves about.

class(food$Energy_kcal)
## [1] "numeric"
class(food$Protein_g)
## [1] "numeric"
class(food$Fat_g)
## [1] "numeric"
class(food$Sugar_g)
## [1] "numeric"
summary(food$Energy_kcal, food$Protein_g, food$Fat_g, food$Sugar_g)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0    86.0   183.0   220.9   330.6   902.0

As you can see the four variables are all numeric. Meaning we can create visualizations that mainly use nunumeric variables to expressdata

With the below chunk, I have mutated with ‘ifelse’ to create new columns that went from numeric to catergorical variables.

food <-   mutate(food, new_col = ifelse(Phosphorus_mg < 100,"low","high"))
food <-   mutate(food, ener_col = ifelse(Energy_kcal< 150,"low","high"))
food <-   mutate(food, pro_col = ifelse(Protein_g < 10.0,"low","high"))






p1 <- food %>%
  filter(NDB_No <= 1306) %>%
  ggplot(aes(x = Protein_g, y = Fat_g, colour = ener_col)) +
geom_point()+
ggtitle("Does Protein Equate to Fat?") +
xlab("Amount of Protein(grams)") +
ylab("Amount in Fat(grams")
p1

Since setting protein levels to catergories, high or low levels of protein, high levels of fats does not have as much as protein. Low levels of protein are also low in fat

# This chunk, ggplotly will cause our p1 plot to be interactive. Points will be able to show which item in corresponds to.
  ggplotly(p1)

As you can see above, we see there is a ton of foods, majority dairy, that are high in fat and low in protein. There are a small number of dairy based items that are high in protein and very low in fat.

We have observed the fats, sugars, protein, and energy from the dairy items. Now let’s take a look at how, if any, these items contain the main sources of nution we need from food. Essential vitamins and amino acids. The new data frame will be called ‘Nutrious’, filtering our for only the dairy items and using mutate to encompass the nutrious element columns of the dataset.

Nutrious <- food %>%
  filter(NDB_No <= 1306) %>%
  mutate(Selenium_mcg, Magnesium_mg, Potassium_mg, Fiber_g, Zinc_mg, Copper_mcg, Thiamin_mg, Riboflavin_mg, Niacin_mg, VitB6_mg, VitC_mg, VitB12_mcg, VitA_mcg, VitE_mg, VitD2_mcg) 


Nutrious <-   mutate(Nutrious, categ = ifelse(Protein_g < 20,"low","high"))



Nutrious
## # A tibble: 1,526 × 33
##    NDB_No Descrip    Energy_kcal Protein_g Saturated_fats_g Fat_g Carb_g Fiber_g
##    <chr>  <chr>            <dbl>     <dbl>            <dbl> <dbl>  <dbl>   <dbl>
##  1 1001   butter wi…         717      0.85             51.4  81.1   0.06       0
##  2 1002   butter wh…         718      0.49             45.4  78.3   2.87       0
##  3 1003   butter oi…         876      0.28             61.9  99.5   0          0
##  4 1004   cheese bl…         353     21.4              18.7  28.7   2.34       0
##  5 1005   cheese br…         371     23.2              18.8  29.7   2.79       0
##  6 1006   cheese br…         334     20.8              17.4  27.7   0.45       0
##  7 1007   cheese ca…         300     19.8              15.3  24.3   0.46       0
##  8 1008   cheese ca…         376     25.2              18.6  29.2   3.06       0
##  9 1009   cheese ch…         404     22.9              18.9  33.3   3.09       0
## 10 1010   cheese ch…         387     23.4              19.5  30.6   4.78       0
## # ℹ 1,516 more rows
## # ℹ 25 more variables: Sugar_g <dbl>, Calcium_mg <dbl>, Iron_mg <dbl>,
## #   Magnesium_mg <dbl>, Phosphorus_mg <dbl>, Potassium_mg <dbl>,
## #   Sodium_mg <dbl>, Zinc_mg <dbl>, Copper_mcg <dbl>, Manganese_mg <dbl>,
## #   Selenium_mcg <dbl>, VitC_mg <dbl>, Thiamin_mg <dbl>, Riboflavin_mg <dbl>,
## #   Niacin_mg <dbl>, VitB6_mg <dbl>, Folate_mcg <dbl>, VitB12_mcg <dbl>,
## #   VitA_mcg <dbl>, VitE_mg <dbl>, VitD2_mcg <dbl>, new_col <chr>, …
Nutrious %>%
  ggplot(aes(x =Saturated_fats_g, y = Sugar_g), position = "identity") +
  geom_point() +
  ggtitle("Sugar vs Fat") +
  xlab("Saturated Fats(grams)") +
  ylab("Sugar Present(grams)") +
  facet_wrap(~categ)

The chunk above is a facet wrap. A type of graph where two variables are wrapped around a catergorical one. Since Protein amount was converted from numeric, then I was able to complete the plot.

library(DataExplorer)
## Warning: package 'DataExplorer' was built under R version 4.2.3
plot_correlation(Nutrious)
## 2 features with more than 20 categories ignored!
## NDB_No: 1526 categories
## Descrip: 1526 categories

The Correlation meter above shows us the correlation comparison between the variables in the Nutritous data frame. One thing that stick out is the Energy(kcal) variable with a positive correlation as it compares to the other variables.

Fit1 <- lm(Energy_kcal ~ Protein_g + Fat_g + Carb_g + Fiber_g + Sugar_g + Calcium_mg + Iron_mg + Magnesium_mg + Phosphorus_mg + Potassium_mg + Zinc_mg + Sodium_mg + Copper_mcg + Manganese_mg + Selenium_mcg + VitC_mg, data = Nutrious)
summary(Fit1)
## 
## Call:
## lm(formula = Energy_kcal ~ Protein_g + Fat_g + Carb_g + Fiber_g + 
##     Sugar_g + Calcium_mg + Iron_mg + Magnesium_mg + Phosphorus_mg + 
##     Potassium_mg + Zinc_mg + Sodium_mg + Copper_mcg + Manganese_mg + 
##     Selenium_mcg + VitC_mg, data = Nutrious)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -45.192  -2.114   0.106   2.511  35.480 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   -0.2045323  0.2643113  -0.774 0.439152    
## Protein_g      4.2794032  0.0240245 178.127  < 2e-16 ***
## Fat_g          8.8281172  0.0123726 713.524  < 2e-16 ***
## Carb_g         4.0236445  0.0163595 245.951  < 2e-16 ***
## Fiber_g       -0.7075592  0.0538868 -13.130  < 2e-16 ***
## Sugar_g       -0.1245494  0.0270138  -4.611 4.35e-06 ***
## Calcium_mg     0.0029084  0.0009995   2.910 0.003670 ** 
## Iron_mg       -0.2207107  0.0657475  -3.357 0.000808 ***
## Magnesium_mg  -0.0455163  0.0036494 -12.472  < 2e-16 ***
## Phosphorus_mg -0.0039825  0.0015448  -2.578 0.010033 *  
## Potassium_mg  -0.0012765  0.0006798  -1.878 0.060610 .  
## Zinc_mg        1.1120497  0.1984197   5.605 2.48e-08 ***
## Sodium_mg     -0.0010151  0.0004013  -2.529 0.011535 *  
## Copper_mcg    -8.3338182  0.4831769 -17.248  < 2e-16 ***
## Manganese_mg  -2.7134034  0.2512639 -10.799  < 2e-16 ***
## Selenium_mcg   0.0035882  0.0029781   1.205 0.228447    
## VitC_mg       -0.0092796  0.0022528  -4.119 4.01e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.912 on 1509 degrees of freedom
## Multiple R-squared:  0.9988, Adjusted R-squared:  0.9988 
## F-statistic: 7.742e+04 on 16 and 1509 DF,  p-value: < 2.2e-16

With the multiple linear regression fit above, the true contributors to the Energy model are Protein, Fat, Carbs, Fiber, the two that are not and can be plucked from the model to possibly increase our R-squared is Selenium and Phosphorus. Even tho our model has a 99%.

To conclude this project, I’ve learned a lot about nutrition. From some of the nutrious composition of dairy, to how much fat really is in dairy, and the energy and protein amount in dairy. I’ll love to come back to this dataset and merge it with one that might break down the selected food items into possible GMO’s present or emulsifier chemicals. That way I will be able to have the statistical proof about not consuming dairy products. In my project the main question(s) that arose to me are, “How much energy are these food items given off once broken down?”, “Is there a connection between protein and carbs?” These were the reasons I mutated those variables to catergorical variables.