18  Exercises: Hallo Medical Statistics

TipHallo Medical Statistics

Pre-requicite for these exercises was covered within the session and is therefore not repeated here. The exercises build on this material.

18.1 Learning objectives

The learning objectives for this session are:

  • Udvise forståelse for usikkerheds- og sandsynlighedsbegrebet samt grundliggende begreber indenfor biostatistik
  • Udvise kendskab til de grundliggende overordnede studietyper samt at skelne mellem forklarende, eksplorative og prædiktive studier
  • Redegøre for forskellige typer af tilfældig og ikke tilfældig variation
  • Udvise forståelse for statistiske værktøjers begrænsninger og muligheder
  • Forstå statistiske problemstillinger, der er centrale for medicin med industrial specialisering og forstå, hvordan de biostatistiske værktøjer kan appliceres på disse problemstillinger

18.2 Exercises: “Just send a quick summary”

You have been asked to prepare a quick summary of a dataset of 303 chest pain patients to support a potential clinical trial collaboration. “Nothing complicated—just mean and standard deviation for a few variables.” You open RStudio…

18.2.1 A: Let’s start orienting ourselves in the dataset.

Exercise 1A: Import the Cleveland Heart Disease dataset into R.

  • What is the name of your dataset?
  • How many rows and columns does it have?
  • How many variables does it have?
  • How many participants does it have?
  • Is there any pattern here?

View the Cleveland Heart Disease dataset from the website. Import the data in your RStudio session. How many variables does it have?

Click for the solution
  1. Depends on your naming choice — could be hd_data as on the website.
  2. 303 by 15
  3. 15
  4. 303
  5. yes… 👀
Click for a tip.
hd_data <- medisr::hd_data
View(hd_data)

Exercise 2A: View the dataset.

  • List 3 variables you think are numeric
  • List 2 variables you think are categorical

Why does this distinction matter when calculating a mean?

Click for the solution
  1. Age, RestBP, Chol, MaxHR
  2. Sex, ChestPain, Fbs, RestECG, ExAng, Slope, Ca, Thal, HD
  3. 👀

Exercise 3A:

What happens when you take the mean of a character string? e.g., mean("hi")? Why do you think this happens?

Click for the solution
  1. error to protect you — don’t get scared from the red text warning 😄

Exercise 4A:

Extract one variable (e.g., Age or Cholesterol) and:

  • Print it to see all it’s values
  • Calculate its mean
Click for the solution
hd_data$Age # 1
  [1] 63 67 67 37 41 56 62 57 63 53 57 56 56 44 52 57 48 54 48 49 64 58
 [23] 58 58 60 50 58 66 43 40 69 60 64 59 44 42 43 57 55 61 65 40 71 59
 [45] 61 58 51 50 65 53 41 65 44 44 60 54 50 41 54 51 51 46 58 54 54 60
 [67] 60 54 59 46 65 67 62 65 44 65 60 51 48 58 45 53 39 68 52 44 47 53
 [89] 53 51 66 62 62 44 63 52 59 60 52 48 45 34 57 71 49 54 59 57 61 39
[111] 61 56 52 43 62 41 58 35 63 65 48 63 51 55 65 45 56 54 44 62 54 51
[133] 29 51 43 55 70 62 35 51 59 59 52 64 58 47 57 41 45 60 52 42 67 55
[155] 64 70 51 58 60 68 46 77 54 58 48 57 52 54 35 45 70 53 59 62 64 57
[177] 52 56 43 53 48 56 42 59 60 63 42 66 54 69 50 51 43 62 68 67 69 45
[199] 50 59 50 64 57 64 43 45 58 50 55 62 37 38 41 66 52 56 46 46 64 59
[221] 41 54 39 53 63 34 47 67 54 66 52 55 49 74 54 54 56 46 49 42 41 41
[243] 49 61 60 67 58 47 52 62 57 58 64 51 43 42 67 76 70 57 44 58 60 44
[265] 61 42 52 59 40 42 61 66 46 71 59 64 66 39 57 58 57 47 55 35 61 58
[287] 58 58 56 56 67 55 44 63 63 41 59 57 45 68 57 57 38
mean(hd_data$Age) # 2
[1] 54.43894

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
# 1
hd_data |>
  pull(Age)
  [1] 63 67 67 37 41 56 62 57 63 53 57 56 56 44 52 57 48 54 48 49 64 58
 [23] 58 58 60 50 58 66 43 40 69 60 64 59 44 42 43 57 55 61 65 40 71 59
 [45] 61 58 51 50 65 53 41 65 44 44 60 54 50 41 54 51 51 46 58 54 54 60
 [67] 60 54 59 46 65 67 62 65 44 65 60 51 48 58 45 53 39 68 52 44 47 53
 [89] 53 51 66 62 62 44 63 52 59 60 52 48 45 34 57 71 49 54 59 57 61 39
[111] 61 56 52 43 62 41 58 35 63 65 48 63 51 55 65 45 56 54 44 62 54 51
[133] 29 51 43 55 70 62 35 51 59 59 52 64 58 47 57 41 45 60 52 42 67 55
[155] 64 70 51 58 60 68 46 77 54 58 48 57 52 54 35 45 70 53 59 62 64 57
[177] 52 56 43 53 48 56 42 59 60 63 42 66 54 69 50 51 43 62 68 67 69 45
[199] 50 59 50 64 57 64 43 45 58 50 55 62 37 38 41 66 52 56 46 46 64 59
[221] 41 54 39 53 63 34 47 67 54 66 52 55 49 74 54 54 56 46 49 42 41 41
[243] 49 61 60 67 58 47 52 62 57 58 64 51 43 42 67 76 70 57 44 58 60 44
[265] 61 42 52 59 40 42 61 66 46 71 59 64 66 39 57 58 57 47 55 35 61 58
[287] 58 58 56 56 67 55 44 63 63 41 59 57 45 68 57 57 38
# 2
hd_data |>
  pull(Age) |>
  mean()
[1] 54.43894

18.2.2 B: Now you start doing the actual request from the email.

Exercise 1B:

Calculate the mean and standard deviation for:

  • Age
  • Chol
  • RestBP
  • MaxHR
hd_data |>
  pull(Age) |>
  mean()
[1] 54.43894
hd_data |>
  pull(Age) |>
  sd()
[1] 9.038662
# etc....

Exercise 2B:

Check if there are missing values in the dataset.

  • How many are there and for which variables (tip: view it or use is.nan())?
  • What happens if you try to calculate the mean when missing values are present?
  • Can you find out how to fix the above issue by reading the documentation?
Click for the solution
  1. Ca and Thal has.
  2. You get NA— see
  3. na.rm — see ?mean argument 3

Exercise 3B:

List all the variable types the mean() function can take the mean of. There should be 5. Seek help inside RStudio.

Click for the solution
  1. type ?mean It says that “Currently there are methods for 1. numeric, 2. logical 2. date, 4. date-time and 5. time interval”.

18.2.3 C: You reflect on your day at work during your evening at home…

Exercise 1C:

Consider the age mean and standard deviation you previously calculated (in exercise 1B). How can you interpret this? What can we say from this?

Click for the solution
  1. mean 54.43894 sd 9.038662 - the group was between 72.5 and 36.5 years old so a good spread of adults.

Exercise 2C:

A colleague suggested calculating the mean of Sex.

  • What would that number represent?
  • Is it meaningful? Why or why not?
Click for the solution
  1. It is meaningful — it’s the percent of males.

Exercise 3C:

Why is it important to report both mean and standard deviation, and not just the mean? Think in a clinical context.

Click for the solution
  1. standard deviation tells about variability in data. There could be outliers to the mean.