---
title: "Worked Exemplar: Temperature and Heart Rate by Gender"
author: "Dr. Barry Monk"
date: "2023-02-01"
output: html_document
---

```{r setup, include=FALSE}
library(dplyr)
TH <- read.csv("https://barrymonk.com/datasets/MATH3610/temp_heartrate.csv")
```

## Study

In a study, the biological gender, body temperature (in degrees Fahrenheit), and heart rate were recorded for 553 people. We address the following questions:

1. What is the overall shape of the temperature data?
2. Is the mean temperature close to 98.6?
3. Is there a difference between temperatures for males and females?
4. Does there appear to be a relationship between heart rate and temperature?

## Data Preparation

We filter the data into male and female subsets.

```{r}
MTH <- filter(TH, Bio.Gender == "male")
FTH <- filter(TH, Bio.Gender == "female")
```

## 1. What is the overall shape of the temperature data?

```{r}
hist(TH$Body.Temp, main = "Distribution of Body Temperatures", xlab = "Temperature (°F)", col = "steelblue")
boxplot(TH$Body.Temp, horizontal = TRUE, main = "Boxplot of Body Temperatures")
```

**The distribution of body temperatures appears to be approximately symmetric and bell-shaped. The boxplot shows some slight skewness but no extreme outliers. Overall, the data looks roughly normal.**

## 2. Is the mean temperature close to 98.6?

```{r}
mean(TH$Body.Temp)
```

**The mean body temperature in this sample is approximately 98.25 degrees, which is noticeably lower than the commonly cited 98.6 degrees.**

## 3. Is there a difference between temperatures for males and females?

```{r}
mean(MTH$Body.Temp)
mean(FTH$Body.Temp)
```

**The average male body temperature is approximately 98.1°F and the average female body temperature is approximately 98.4°F. It appears that female body temperatures are slightly higher on average.**

```{r}
sd(MTH$Body.Temp)
sd(FTH$Body.Temp)
```

**The standard deviations are similar for both groups (about 0.70 for males and 0.74 for females), so the spread is comparable.**

```{r}
boxplot(MTH$Body.Temp, FTH$Body.Temp, names = c("Male", "Female"),
        horizontal = TRUE, main = "Body Temperature by Gender", col = c("lightblue", "salmon"))
```

**The side-by-side boxplots confirm that female temperatures tend to be slightly higher. Both distributions are roughly symmetric.**

## 4. Does there appear to be a relationship between heart rate and temperature?

```{r}
plot(TH$Body.Temp, TH$Heart.Rate, main = "Heart Rate vs. Body Temperature",
     xlab = "Body Temperature (°F)", ylab = "Heart Rate (bpm)", pch = 16, col = "darkgreen")
```

**There may be a slight positive association between body temperature and heart rate, but the scatterplot shows a lot of scatter.**

```{r}
cor(TH$Body.Temp, TH$Heart.Rate)
```

**The correlation coefficient is r = 0.254, confirming a weak positive linear association. Higher body temperatures are associated with somewhat higher heart rates, but the relationship is not strong.**
