---
title: "Cardiac study report"
author: "Your Name"
date: "2 November 2026"
output:
  pdf_document:
    toc: true
    number_sections: true
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)

library(knitr)
library(ggplot2)
```

# Introduction

## Data description

These data come from a **cohort study** of the risk factors for cardiovascular disease.
A group of adults, aged between 55 and 75, were examined at the start of the study and a
range of *measurements* was taken on each of them. Each row of the dataset is one patient,
identified by a unique patient number (`patno`), and each column is one of those
measurements.

The four measurements used in this report are:

- age, in years
- systolic blood pressure, in mmHg
- HDL cholesterol, in mmol/l
- smoking status

```{r import}
cardiac <- read.table('data/cardiacdata.txt', header = TRUE, sep = "\t",
                      stringsAsFactors = TRUE)

cardiac$Fsex <- factor(cardiac$sex, levels = c(1, 2),
                       labels = c("Female", "Male"))

cardiac$Fsmoking <- factor(cardiac$smoking, levels = c(1, 2, 3),
                           labels = c("Current", "Ex", "Never"))
```

## Data validation

The dataset contains `r nrow(cardiac)` patients, with a mean age of
`r round(mean(cardiac$age), 1)` years. The youngest patient was
`r round(min(cardiac$age))` and the oldest was `r round(max(cardiac$age))`.

```{r validation}
cardiac$bmi[cardiac$bmi > 100] <- NA
cardiac$triglyceride[cardiac$triglyceride == 0] <- NA
cardiac$hdlchol[cardiac$hdlchol == 0] <- NA
```

Three variables contained values that are not biologically possible. Two patients had an
HDL cholesterol of 0 mmol/l and one had a triglyceride of 0 mmol/l, and nobody can have no
fat at all in their blood. One patient had a body mass index of 514.6, which is almost
certainly 51.46 with the decimal point in the wrong place. All four values were set to
`NA` rather than deleted or corrected. Deleting the patients would throw away their other
measurements, which are perfectly good, and correcting the body mass index would mean
putting a number into the data that nobody measured.

```{r alcohol-transform, fig.cap = 'Weekly alcohol consumption.'}
cardiac$alcohol_sqrt <- sqrt(cardiac$alcohol)

ggplot(cardiac, aes(x = alcohol)) +
  geom_histogram(bins = 15) +
  labs(x = "Alcohol (units per week)", y = "Number of patients") +
  theme_minimal()
```

```{r alcohol-sqrt, fig.cap = 'Weekly alcohol consumption after a square root transformation.'}
ggplot(cardiac, aes(x = alcohol_sqrt)) +
  geom_histogram(bins = 15) +
  labs(x = "Square root of alcohol units", y = "Number of patients") +
  theme_minimal()
```

A small number of patients drink a great deal more than the rest, which leaves weekly
alcohol units with a long tail to the right. Alcohol is recorded as a count of units, so
the square root is the usual transformation for it, and it spreads the values out much
more evenly.

## Results

```{r chol-plot, fig.cap = 'HDL cholesterol by smoking status.', fig.width = 4}
ggplot(cardiac, aes(x = Fsmoking, y = hdlchol)) +
  geom_boxplot() +
  labs(x = "Smoking status", y = "HDL cholesterol (mmol/l)") +
  theme_minimal()
```

Patients who have never smoked have a higher HDL cholesterol than either the current
smokers or the ex-smokers.

```{r summary-table}
smoke_summary <- aggregate(cardiac[, c("age", "systolic", "tchol")],
                           by = list(Smoking = cardiac$Fsmoking),
                           mean, na.rm = TRUE)

kable(smoke_summary, digits = 1,
      caption = "Mean age, systolic blood pressure and total cholesterol by smoking status.")
```

## An externally produced figure

```{r admissions-figure, out.width = "100%", fig.cap = 'Alcohol-related hospital admissions by council area.'}
include_graphics("output/ex6_admissions.png")
```

Alcohol-related hospital admissions vary between council areas. Glasgow City
has consistently the highest rate in Scotland over this period and Aberdeenshire one of
the lowest, with the national average falling between the two.

## Session information

```{r session-info}
sessionInfo()
```
