This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated th
#Obesity in Individuals
###What is obesity? It is considered a chronic, progressive and complex disease associated with environmental and genetic risk factors. Obesity is closely related to the main causes of morbidity and disability. The consequences of obesity range from metabolic to psychological and social, with a significant impact on quality of life and life expectancy.
###Why did we choose this topic? Because obesity has become one of the most relevant challenges of public health. The exponential growth of obesity during the last three decades has turned it into a worldwide epidemic, affecting both developed and developing countries, with a greater impact on the latter.
###Importance of this project. In Latin America, Mexico is positioned within the first five countries with the highest prevalence of obesity in all age and gender groups, with a prevalence in adults over 20 years of age of 20.60% in men and 32.70% in women, and in children under 20 years of age of 10.50% in boys and 9.8% in girls.
Source: World Health Organization (WHO)
###Project scheme: Data Collection Data Exploration Data Distribution Outlier Detection Chi-square Analysis Evaluation of Distribution
##Load Libraries
```r
library(tidyverse)
```
```
## Warning: package 'tidyverse' was built under R version 4.3.3
```
```
## Warning: package 'readr' was built under R version 4.3.3
```
```
## Warning: package 'forcats' was built under R version 4.3.3
```
```
## Warning: package 'lubridate' was built under R version 4.3.3
```
```r
library(fastDummies)
```
```
## Warning: package 'fastDummies' was built under R version 4.3.3
```
```r
library(caret)
```
```
## Warning: package 'caret' was built under R version 4.3.3
```
```r
library(glmnet)
```
```
## Warning: package 'glmnet' was built under R version 4.3.3
```
```r
library(rpart)
```
```
## Warning: package 'rpart' was built under R version 4.3.3
```
```r
library(rpart.plot)
```
```
## Warning: package 'rpart.plot' was built under R version 4.3.3
```
```r
library(randomForest)
```
```
## Warning: package 'randomForest' was built under R version 4.3.3
```
```r
library(e1071)
```
```
## Warning: package 'e1071' was built under R version 4.3.3
```
```r
library(ipred)
```
```
## Warning: package 'ipred' was built under R version 4.3.3
```
```r
library(ggplot2)
```
##About Project #The name of the dataset is “Estimation of Obesity Levels Based on Eating Habits and Physical Condition” gotten from UC Irvine Machine Learning Repository. The dataset calculated obesity level based off their height and weight from individuals from the countries of Mexico, Peru and Colombia alongside recording their physical characteristics and eating habit. The goal of this Project is to build a capable model that can accurately predict the obesity of an individual. Hence, I am looking for a model with high accuracy and precision. The Obesity target variable will be generated from a column called “NObeyesdad” to foretell if an individual is “obese” or “not_obese”. The approach followed: Data Collection, Data Exploration, Data Preparation,Modeling and Evaluation. The steps are explained before coding is done. The models used are SVM, Linear Regression and Decision Tree as they are well suited to handle classification problems. #Dataset:Estimation of obesity levels based on eating habits and physical condition
## Data Collection #In this step, dataset is obtained from the source and downloaded into R.
obesity_data <- read.csv("C:\\Users\\Rox\\OneDrive\\Documentos\\Rox\\MBA\\MCAA\\ObesityDataSet_raw_and_data_sinthetic.csv")
head(obesity_data,3)
## Gender Age Height Weight family_history_with_overweight FAVC FCVC NCP
## 1 Female 21 1.62 64 yes no 2 3
## 2 Female 21 1.52 56 yes no 3 3
## 3 Male 23 1.80 77 yes no 2 3
## CAEC SMOKE CH2O SCC FAF TUE CALC MTRANS
## 1 Sometimes no 2 no 0 1 no Public_Transportation
## 2 Sometimes yes 3 yes 3 0 Sometimes Public_Transportation
## 3 Sometimes no 2 no 2 1 Frequently Public_Transportation
## NObeyesdad
## 1 Normal_Weight
## 2 Normal_Weight
## 3 Normal_Weight
## Data Exploration #Exploring the dataset to get a general understanding of its characteristics
### Checking Data Structure and Summary
#checking structure
str(obesity_data)
## 'data.frame': 2111 obs. of 17 variables:
## $ Gender : chr "Female" "Female" "Male" "Male" ...
## $ Age : num 21 21 23 27 22 29 23 22 24 22 ...
## $ Height : num 1.62 1.52 1.8 1.8 1.78 1.62 1.5 1.64 1.78 1.72 ...
## $ Weight : num 64 56 77 87 89.8 53 55 53 64 68 ...
## $ family_history_with_overweight: chr "yes" "yes" "yes" "no" ...
## $ FAVC : chr "no" "no" "no" "no" ...
## $ FCVC : num 2 3 2 3 2 2 3 2 3 2 ...
## $ NCP : num 3 3 3 3 1 3 3 3 3 3 ...
## $ CAEC : chr "Sometimes" "Sometimes" "Sometimes" "Sometimes" ...
## $ SMOKE : chr "no" "yes" "no" "no" ...
## $ CH2O : num 2 3 2 2 2 2 2 2 2 2 ...
## $ SCC : chr "no" "yes" "no" "no" ...
## $ FAF : num 0 3 2 2 0 0 1 3 1 1 ...
## $ TUE : num 1 0 1 0 0 0 0 0 1 1 ...
## $ CALC : chr "no" "Sometimes" "Frequently" "Frequently" ...
## $ MTRANS : chr "Public_Transportation" "Public_Transportation" "Public_Transportation" "Walking" ...
## $ NObeyesdad : chr "Normal_Weight" "Normal_Weight" "Normal_Weight" "Overweight_Level_I" ...
#Checking the summary of the dataset
summary(obesity_data)
## Gender Age Height Weight
## Length:2111 Min. :14.00 Min. :1.450 Min. : 39.00
## Class :character 1st Qu.:19.95 1st Qu.:1.630 1st Qu.: 65.47
## Mode :character Median :22.78 Median :1.700 Median : 83.00
## Mean :24.31 Mean :1.702 Mean : 86.59
## 3rd Qu.:26.00 3rd Qu.:1.768 3rd Qu.:107.43
## Max. :61.00 Max. :1.980 Max. :173.00
## family_history_with_overweight FAVC FCVC
## Length:2111 Length:2111 Min. :1.000
## Class :character Class :character 1st Qu.:2.000
## Mode :character Mode :character Median :2.386
## Mean :2.419
## 3rd Qu.:3.000
## Max. :3.000
## NCP CAEC SMOKE CH2O
## Min. :1.000 Length:2111 Length:2111 Min. :1.000
## 1st Qu.:2.659 Class :character Class :character 1st Qu.:1.585
## Median :3.000 Mode :character Mode :character Median :2.000
## Mean :2.686 Mean :2.008
## 3rd Qu.:3.000 3rd Qu.:2.477
## Max. :4.000 Max. :3.000
## SCC FAF TUE CALC
## Length:2111 Min. :0.0000 Min. :0.0000 Length:2111
## Class :character 1st Qu.:0.1245 1st Qu.:0.0000 Class :character
## Mode :character Median :1.0000 Median :0.6253 Mode :character
## Mean :1.0103 Mean :0.6579
## 3rd Qu.:1.6667 3rd Qu.:1.0000
## Max. :3.0000 Max. :2.0000
## MTRANS NObeyesdad
## Length:2111 Length:2111
## Class :character Class :character
## Mode :character Mode :character
##
##
##
### Data Distribution #We are going to check the distribution of the variables
#Remove Height and Weight variables
obesity_df <- obesity_data[,-c(3,4)]
head(obesity_df,3)
## Gender Age family_history_with_overweight FAVC FCVC NCP CAEC SMOKE CH2O
## 1 Female 21 yes no 2 3 Sometimes no 2
## 2 Female 21 yes no 3 3 Sometimes yes 3
## 3 Male 23 yes no 2 3 Sometimes no 2
## SCC FAF TUE CALC MTRANS NObeyesdad
## 1 no 0 1 no Public_Transportation Normal_Weight
## 2 yes 3 0 Sometimes Public_Transportation Normal_Weight
## 3 no 2 1 Frequently Public_Transportation Normal_Weight
#Creating barplot for character variables
obesity_df %>%
select(-c(14,15)) %>%
keep(is.character) %>%
gather() %>%
ggplot(aes(x = value)) +
facet_wrap(~ key, scales = "free") +
geom_bar(stat = "count", fill = "skyblue") +
theme_minimal()
##Based on the graphics we can observe that most people sometimes consume food between meals as well as alcohol. Most people have a family history of being overweight. An interesting discovering was that most people frequently consume vegetables; however, most of them don’t have a calorie consumption monitoring
#Create NObeyesdad count barplot
ggplot(data = obesity_df) +
geom_bar(aes(x = NObeyesdad, fill = NObeyesdad)) +
ggtitle("Barplot of Obesity Level frequency") +
xlab("Obesity Level") +
ylab("Frequency") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Create the visualizations for MTRANS
ggplot(data = obesity_df) +
geom_bar(aes(x = MTRANS, fill = MTRANS)) +
ggtitle("Barplot of Transportation frequency") +
xlab("MTRANS") +
ylab("Frequency") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
#Create a histogram for Age
hist(obesity_df$Age)
#Create a histogram for FCVC
hist(obesity_df$FCVC)
#Create a histogram for NCP
hist(obesity_df$NCP)
#Create a histogram for CH20
hist(obesity_df$CH2O)
#Create a histogram for FAF
hist(obesity_df$FAF)
#Create a histogram for FCVC
hist(obesity_df$TUE)
##There are closely equal distribution of the obesity level. The highest level is Obesity Type I and the lowest level is Insufficient_Weight. For Family History and High calric food intake, majority of the respondent chose Yes while for SMOKE (If you smoke) and SCC(Caloric monitoring), majority of the respondent chose No. The highest mode of transportation for this participant is Public Transportation. While for the consumption of food (CAEC) and consumption of Alcohol(CALC), majority of the participant chose Sometimes.
### Chi-square Analysis #Chi-square analysis is used to check the correlation between two categorical variables. I will be checking the correlation of NObeyesdad against other column
#Checking the NObeyesdad column against other column
chisq.test(obesity_df$NObeyesdad, obesity_df$Gender, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$Gender
## X-squared = 657.75, df = 6, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$Age, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$Age
## X-squared = 9547.1, df = 8406, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$family_history_with_overweight, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$family_history_with_overweight
## X-squared = 621.98, df = 6, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$FAVC, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$FAVC
## X-squared = 233.34, df = 6, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$FCVC, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$FCVC
## X-squared = 5945, df = 4854, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$NCP, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$NCP
## X-squared = 4231.1, df = 3804, p-value = 1.112e-06
chisq.test(obesity_df$NObeyesdad, obesity_df$CAEC, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$CAEC
## X-squared = 802.98, df = 18, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$SMOKE, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$SMOKE
## X-squared = 32.138, df = 6, p-value = 1.535e-05
chisq.test(obesity_df$NObeyesdad, obesity_df$CH2O, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$CH2O
## X-squared = 7955.1, df = 7602, p-value = 0.002359
chisq.test(obesity_df$NObeyesdad, obesity_df$SCC, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$SCC
## X-squared = 123.02, df = 6, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$FAF, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$FAF
## X-squared = 7678.3, df = 7134, p-value = 4.275e-06
chisq.test(obesity_df$NObeyesdad, obesity_df$TUE, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$TUE
## X-squared = 7119.2, df = 6768, p-value = 0.001472
chisq.test(obesity_df$NObeyesdad, obesity_df$CALC, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$CALC
## X-squared = 338.58, df = 18, p-value < 2.2e-16
chisq.test(obesity_df$NObeyesdad, obesity_df$MTRANS, correct=FALSE)
##
## Pearson's Chi-squared test
##
## data: obesity_df$NObeyesdad and obesity_df$MTRANS
## X-squared = 292.59, df = 24, p-value < 2.2e-16
##Looking at all the variables, the p-value is lesser than 0.05. This means that all the variables are dependent with NObeyesdad variable.
### Evaluation of Distribution # We are only going to be evalutaing the Age Column
#Create a QQPlot Distribution
obesity_df %>%
keep(is.numeric) %>%
select(Age) %>%
gather() %>%
ggplot(aes(sample = value)) +
facet_wrap(~ key, scales = "free") +
geom_qq() +
stat_qq_line(color = "red") +
theme_minimal() +
labs(title = "QQ Plot for Age", x = "Theoretical Quantiles", y = "Sample Quantiles")
##From the QQplot, the age variable is not normally distributed
###Conclusion #Obesity is a worldwide concern disease and it’s important to analyze its distribution and characteristics. This project aims to provide a panorama about the relationship between some variables such as consumption of food between meals, consumption of alcohol, family history, transportation media. Based on this data a possible application would be the creation of models to predict obesity.