Created
November 27, 2023 03:58
-
-
Save ernestguevarra/db4487213980774fa59a5120ecd62c05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Return of the BMI exercise - 28 November 2023 -------------------------------- | |
## Load libraries ---- | |
library(dplyr) ## for data manipulation | |
library(openxlsx) ## for reading XLSX files | |
library(zscorer) ## for calculating z-scores | |
## Load project-specific functions ---- | |
source(list.files("R", full.names = TRUE)) | |
## Read data ---- | |
### Read CSV file from GitHub teaching_datasets repository ---- | |
nut_data <- read.table( | |
file = "https://raw.githubusercontent.com/OxfordIHTM/teaching_datasets/main/school_nutrition.csv", | |
sep = ",", header = TRUE | |
) | |
### Download CSV file into data directory and then read the data ---- | |
download.file( | |
url = "https://raw.githubusercontent.com/OxfordIHTM/teaching_datasets/main/school_nutrition.csv", | |
destfile = "data/school_nutrition.csv" | |
) | |
nut_data <- read.table(file = "data/school_nutrition.csv", sep = ",", header = TRUE) | |
### Read XLSX file from GitHub teaching datasets repository ---- | |
nut_data_xlsx <- read.xlsx(xlsxFile = "https://raw.githubusercontent.com/OxfordIHTM/teaching_datasets/main/school_nutrition.xlsx") | |
### Download XLSX file into data directory and then read the data ---- | |
download.file( | |
url = "https://raw.githubusercontent.com/OxfordIHTM/teaching_datasets/main/school_nutrition.xlsx", | |
destfile = "data/school_nutrition.xlsx" | |
) | |
nut_data_xlsx <- read.xlsx(xlsxFile = "data/school_nutrition.xlsx") | |
## Calculate BMI ---- | |
### Calculate BMI using basic operations and assigning to the bmi variable ---- | |
nut_data$bmi <- nut_data$weight / (nut_data$height / 100) ^ 2 | |
### Calculate BMI using the function created for this purpose ---- | |
nut_data$bmi <- calculate_bmi(nut_data$weight, nut_data$height / 100) | |
### Calculate BMI using dplyr functions to add to the nut_data object ---- | |
nut_data <- nut_data %>% | |
mutate(bmi = calculate_bmi(weight, height / 100)) | |
## Classify BMI ---- | |
### Calculate bmi-for-age z-score ---- | |
nut_data$age_days <- nut_data$age_months * (365.25 / 12) | |
nut_data <- addWGSR( | |
data = nut_data, | |
sex = "sex", | |
firstPart = "weight", | |
secondPart = "height", | |
thirdPart = "age_days", | |
index = "bfa" | |
) | |
### Classify BMI-for-age z-score using ifelse ---- | |
nut_data$bmi_classification <- ifelse( | |
nut_data$bfaz > 2, "obese", | |
ifelse( | |
nut_data$bfaz > 1 & nut_data$bfaz <= 2, "overweight", | |
ifelse( | |
nut_data$bfaz >= -3 & nut_data$bfaz < -2, "thin", | |
ifelse( | |
nut_data$bfaz < -3, "severely thin", "normal" | |
) | |
) | |
) | |
) | |
### Classify BMI-for-age z-score using dplyr ---- | |
nut_data <- nut_data %>% | |
mutate( | |
bmiz_classification = case_when( | |
bfaz > 2 ~ "obese", | |
bfaz > 1 & bfaz <= 2 ~ "overweight", | |
bfaz >= -3 & bfaz < -2 ~ "thin", | |
bfaz < -3 ~ "severely thin", | |
.default = "normal" | |
) | |
) | |
# Functions to calculate and work with BMI ------------------------------------- | |
## Calculate BMI ---- | |
calculate_bmi <- function(weight, height) { | |
weight / height ^ 2 | |
} | |
calculate_bmi <- function(weight, height) { | |
bmi <- weight / height ^ 2 | |
msg <- paste("The BMI is ", bmi, ".", sep = "") | |
print(msg) | |
bmi | |
} | |
calculate_bmi <- function(weight, height) { | |
bmi <- weight / height ^ 2 | |
msg <- paste("The BMI is ", bmi, ".", sep = "") | |
message(msg) | |
return(bmi) | |
} | |
calculate_bmi <- function(weight, height, verbose = FALSE) { | |
bmi <- weight / height ^ 2 | |
msg <- paste("The BMI is ", bmi, ".", sep = "") | |
if (verbose) message(msg) | |
return(bmi) | |
} | |
classify_bmi <- function(bmi) { | |
ifelse( | |
bmi >= 30, "obese", | |
ifelse( | |
bmi < 30 & bmi >= 25, "overweight", | |
ifelse( | |
bmi < 25 & bmi >= 18.5, "normal", "underweight" | |
) | |
) | |
) | |
} | |
classify_bmi <- function(bmi) { | |
cut( | |
x = bmi, | |
breaks = c(0, 18.5, 25, 30, Inf), | |
labels = c("underweight", "normal", "overweight", "obese"), | |
include.lowest = FALSE, right = TRUE | |
) | |
} | |
classify_bmi_children <- function(bmiz) { | |
ifelse( | |
bmiz > 2, "obese", | |
ifelse( | |
bmiz <= 2 & bmiz > 1, "overweight", | |
ifelse( | |
bmiz <= 1 & bmiz >= -2, "normal", | |
ifelse( | |
bmiz >= -3 & bmiz < -2, "thin", "severely thin" | |
) | |
) | |
) | |
) | |
} | |
classify_bmi_children <- function(bmiz) { | |
cut( | |
x = bmiz, | |
breaks = c(-Inf, -3, -1.9999, 1, 2, Inf), | |
labels = c("severely thin", "thin", "normal", "overweight", "obese"), | |
include.lowest = FALSE, right = TRUE | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment