In the article Fitting Percentage of Body Fat to Simple Body Measurements, Johnson (1996) uses the data at http://jse.amstat.org/datasets/fat.dat.txt provided to him by Dr. A. Garth Fischer in a personal communication on October 5, 1994, as a multiple linear regression activity with his students. A subset of the variables at http://jse.amstat.org/datasets/fat.dat.txt is available in the R package mfp by Ambler and Benner (2022) and the data set is used frequently in the text Statistical Regression and Classification by Matloff (2017).
The purpose of this activity is to have the reader critically question, evaluate, and clean the original data provided at http://jse.amstat.org/datasets/fat.dat.txt. The guided activities will reinforce reading data into R using the fread()
function from the data.table package written by Dowle and Srinivasan (2022), creating graphs with both packages ggplot2 and plotly written by Wickham, Chang, et al. (2022) and Sievert et al. (2022), respectively, and creating new variables with functions such as mutate()
from the dplyr package written by Wickham, François, et al. (2022).
Type complete sentences to answer all questions inside the answer
tags provided in the R Markdown document. Round all numeric answers you report inside the answer tags to four decimal places. Use inline R
code to report numeric answers inside the answer
tags (i.e. do not hard code your numeric answers).
The article by Johnson (1996) defines body-fat determined with the brozek and siri methods as well as fat free weight using Equations (1), (2), and (3), respectively.
\[\begin{equation} \text{bodyfatBrozek} = \frac{457}{\text{density}} - 414.2 \tag{1} \end{equation}\]
\[\begin{equation} \text{bodyfatSiri} = \frac{495}{\text{density}} - 450 \tag{2} \end{equation}\]
\[\begin{equation}
\text{FatFreeWeight} = \left(1 -\frac{\text{brozek}}{100}\times \text{weight_lbs}\right)
\tag{3}
\end{equation}\]
Body Mass Index (BMI
) is defined in Equation (4).
\[\begin{equation} \text{BMI} = \frac{\text{kg}}{\text{m}^2} \tag{4} \end{equation}\]
Please use the following conversion factors with this project: 0.453592 kilos per pound and 2.54 centimeters per inch.
For example, a male who weighs 185 pounds and is 71 inches tall has a weight of 83.91 kg and a height of 1.8034 m.
\[\begin{equation} 185 \text{ pounds} \times \frac{0.453592 \text{ kg}}{\text{pounds}} = 83.9145 \text{ kg} \end{equation}\]
\[\begin{equation} 71 \text{ inches} \times \frac{2.54 \text{ cm}}{\text{inches}}\times \frac{1 \text {m}}{100 \text{ cm}} = 1.8034 \text{ m} \end{equation}\]
To read tabular data from the internet, one might use the read.table()
function or the fread()
function from the data.table
package. The general structure to read data from the internet is to provide the URL in the file
or input
arguments of the read.table()
or fread()
functions, respectively.
# Example structure
<- read.table(file = "http://some.url.com")
DF1 # load data.table package
library(data.table)
<- fread(input = "http://some.url.com") DF2
fread()
function from the data.table
package to read the data from http://jse.amstat.org/datasets/fat.dat.txt into an object named bodyfat
. The data dictionary for http://jse.amstat.org/datasets/fat.dat.txt is available at http://jse.amstat.org/datasets/fat.txt.col.names
argument of fread()
:
c("case", "brozek", "siri", "density", "age", "weight_lbs", "height_in", "bmi", "fat_free_weight",
"neck_cm", "chest_cm", "abdomen_cm", "hip_cm", "thigh_cm", "knee_cm", "ankle_cm", "biceps_cm",
"forearm_cm", "wrist_cm")
.# Type your code and comments inside the code chunk
# Obtaining the original data
read.table()
function to read the data from http://jse.amstat.org/datasets/fat.dat.txt into an object named bodyfat2
. Pass the same vector created in Problem 1 to the col.names
argument of read.table()
.# Type your code and comments inside the code chunk
# Obtaining the original data
The basic structure of a function in R is
<- function(argument1, argument2, ...){
function_name
expression }
The function curve()
draws a curve corresponding to a function of the interval [from, to]
. For example, to draw the function \(y = x^2\) from -3 to 3 use the following code to obtain Figure 1.
<- function(x){x^2}
quad curve(quad, from = -3, to = 3)
Figure 1: Using the function curve()
to draw a quadratic function
To superimpose a function on an open graphics device with curve()
, use the argument add = TRUE
. For example, to graph \(y = x^2\) with a blue line and \(y = 4 + \sin(8x)\) with a red line from \(-\pi\) to \(\pi\), use the following code:
<- function(x){x^2}
quad curve(quad, from = -pi, to = pi, col = "blue", ylab = "f(x)")
<- function(x){4 + sin(8*x)}
nsin curve(nsin, add = TRUE, col = "red")
brozek
and density
given in Equation (1) with a blue line as well as the relationship between siri
and density
given in Equation (2) with a red line. First, define functions fb
and fs
for Equations (1) and (2), respectively. Use the function curve()
to draw Equations (1) and (2) on the same graphics device for density values between 1.0 \(\text{gm/cm}^3\) and 1.10 \(\text{gm/cm}^3\).# Type your code and comments inside the code chunk
# Functions fb and fs
<- function(density){(457/density) - 414.2}
fb <- function(density){(495/density) - 450} fs
Code to construct the requested graph using ggplot2
code is given below and the result is visible in Figure 2.
library(ggplot2)
<- ggplot(data = data.frame(x = c(1, 1.1), y = c(0, 50)), aes(x = x, y = y))
p + stat_function(fun = fb, color = "blue") +
p stat_function(fun = fs, color = "red") +
labs(x = expression(paste("density in ", gm/cm^3)), y = "bodyfat") +
theme_bw()
Figure 2: Realtionship between bodyfat and density according to Brozek and Siri definitions
Type your answer here.
Since the relationship between body fat and density for both Equations (1) and (2) is linear, one way to check for unusual observations is to plot the actual values for brozek
versus density
or siri
versus density
using the data stored in bodyfat
or bodyfat2
. Points that do not fall along a straight line should be scrutinized for possible data entry errors or other possible explanations.
The plotly package allows the user to create interactive graphs or to convert ggplot2 graphs to plotly graphs using the ggplotly()
function. Given a data frame say DF
with variables X
and Y
, the following template code will create an interactive plotly scatterplot.
library(plotly)
library(ggplot2)
# Create ggplot scatterplot named p
<- ggplot(data = DF, aes(x = X, y = Y)) +
p geom_point()
# Convert ggplot scatterplot to plotly with ggplotly()
<- ggplotly(p)
p2 # Display graph
p2
Consider an interactive scatterplot showing the distance required to stop for vehicles traveling at different speeds using the cars
data frame.
library(plotly)
library(ggplot2)
# Create ggplot scatterplot named p
<- ggplot(data = cars, aes(x = speed, y = dist)) +
p geom_point()
# Convert ggplot scatterplot to plotly with ggplotly()
<- ggplotly(p)
p2 # Display graph
p2
plotly
scatterplots of siri
versus density
with case
mapped to color
, weight_lbs
versus height_in
with case
mapped to color
, and ankle_cm
versus weight_lbs
with case
mapped to color
to help identify potential outliers. Identify the case numbers for the outliers from your three plots.# Type your code and comments inside the code chunk
# Creating interactive scatterplot of siri versus density
# Change code below to your plot
ggplot(data = data.frame(x = 1, y = 1), aes(x = x, y = y)) +
geom_point()
Figure 3: Plot of siri
versus density
# Type your code and comments inside the code chunk
# Creating interactive scatterplot of weight_lbs versus height_in
# Change code below to your plot
ggplot(data = data.frame(x = 1, y = 1), aes(x = x, y = y)) +
geom_point()
Figure 4: Plot of weight_lbs
versus height_in
# Type your code and comments inside the code chunk
# Creating interactive scatterplot of ankle_cm versus weight_lbs
# Change code below to your plot
ggplot(data = data.frame(x = 1, y = 1), aes(x = x, y = y)) +
geom_point()
Figure 5: Interactive scatterplot of ankle_cm
versus weight_lbs
Type your complete sentence answer here using inline R code and delete this comment.
bodyfat
data frame named BF
consisting only of the cases identified as outliers in Problem 5. Use equation (2) to create a new variable in BF
named density_C
(computed density) based on the reported siri
values. Use equation (4) to reverse engineer the computed height in inches based on the values in weight_lbs
and bmi
using the conversion factors given at the start of the lab. Store the computed heights in inches in a variable named height_in_C
. Use the verb mutate
from the dplyr package to create both density_C
and height_in_C
. Show the values of the selected outliers for columns case
, density_C
, height_in
, height_in_C
, and ankle_cm
. What do you notice about the density
and density_C
values in BF
for the scatterplot you created in Figure 3? What do you notice about the height_in
values in BF
for the scatterplot you created in Figure 4? What do you notice about the ankle_cm
values in BF
for the scatterplot you created in Figure 5?# Type your code and comments inside the code chunk
# Isolating points of interest
Type your complete sentence answer here using inline R code and delete this comment.
density
values of bodyfat
based on your answers from Problem 6. Change the height_in
value for case 42 to the value you reverse engineered in Problem 6 rounding to the nearest half inch.# Type your code and comments inside the code chunk
# Updating computed bodyfat values and bmi measurements
# Replacing identified typos of density
# Replacing identified typos in height_in
# Replacing identified typos in ankle_cm
siri_C
, brozek_C
, and bmi_C
, that compute body-fat values rounded to one decimal place according to equations (2), (1), and (4), respectively. Replace the values in siri
and brozek
with the computed values in siri_C
and brozek_C
for case 182.# Type your code and comments inside the code chunk
brozek
and brozek_C
as well as differences between siri
and siri_C
greater than 0.11 to be some type of data entry problem. Use the verbs filter()
and select()
to show the values for case
, siri
, siri_C
, brozek
, brozek_C
, and density
that are considered data entry problems. If both the computed value for siri and brozek differ from the reported values of siri and brozek, it is likely a data entry problem with the density value. If one of either siri or brozek agrees with its computed value, the one that does not agree with the computed value is likely a data entry problem. Which cases seem to have data entry problems and for what reasons?# Type your code and comments inside the code chunk
# Identifying bodyfat typos for brozek and siri
Type your complete sentence answer here using inline R code and delete this comment.
bodyfat
data frame. Make sure to update the siri_C
, brozek_C
, and bmi_C
variables in bodyfat
using your corrected values from Problem 9. Write code to verify that there are no differences between brozek
and brozek_C
or any differences between siri
and siri_C
greater than 0.11.# Type your code and comments inside the code chunk
# Replacing bodyfat typos for brozek and siri
# Updating siri_C, brozek_C, and bmi_C
# Checking for typos according to given criteria
siri_C
versus density
with case
mapped to color
and brozek_C
versus density
with case
mapped to color
using the modified bodyfat
data frame. Superimpose equations (2) and (1) over their corresponding scatterplots. Comment on the scatterplots.# interactive plotly scatterplot siri_C vs. density
# Change code below to your plot
ggplot(data = data.frame(x = 1, y = 1), aes(x = x, y = y)) +
geom_point()
# interactive plotly scatterplot brozek_C vs. density
# Change code below to your plot
ggplot(data = data.frame(x = 1, y = 1), aes(x = x, y = y)) +
geom_point()
Type your complete sentence answer here using inline R code and delete this comment.
# Type your code and comments inside the code chunk
# Number of rounding discrepancies for siri
# Number of rounding discrepancies for brozek
# Number of rounding discrepancies for bmi
Type your complete sentence answer here using inline R code and delete this comment.
Type your complete sentence answer here using inline R code and delete this comment.
# Code for graph suggested in 13
# Change code below to your plot
ggplot(data = data.frame(x = 1, y = 1), aes(x = x, y = y)) +
geom_point()
bodyfatClean
that excludes the variables brozek
, siri
, and bmi
from the bodyfat
data frame. Use the function write.csv()
or write_csv()
to store the bodyfatClean
object as a CSV file.# writing bodyfat to bodyfatClean.csv
This material is released under an Attribution-NonCommercial-ShareAlike 3.0 United States license. Original author: Alan T. Arnholt