-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c507c12
commit 74604cb
Showing
10 changed files
with
1,302 additions
and
409 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"hash": "82416872553d5b88e2beed9b56456679", | ||
"result": { | ||
"markdown": "---\ntitle: \"What's a Bland-Altman Plot? In Base R\"\nauthor: \"Steven P. Sanderson II, MPH\"\ndate: \"2023-10-25\"\ncategories: [rtip, viz]\n---\n\n\n# Introduction\n\nBefore we dive into the code, let's briefly understand what a Bland-Altman plot is. It's a graphical method to visualize the agreement between two measurement techniques, often used in fields like medicine or any domain with comparative measurements. The plot displays the differences between two measurements (Y-axis) against their means (X-axis).\n\n# Step 1: Data Preparation\n\nStart by loading your data into R. In our example, we'll create some synthetic data for illustration purposes. You'd replace this with your real data.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Creating example data\nmethod_A <- c(10, 12, 15, 18, 22, 25)\nmethod_B <- c(9.5, 11, 14, 18, 22, 24.5)\n\n# Calculate the differences and means\ndiff_values <- method_A - method_B\nmean_values <- (method_A + method_B) / 2\n\ndf <- data.frame(method_A, method_B, mean_values, diff_values)\n```\n:::\n\n\n# Step 2: Calculate Average Difference and CI\n\nNow that we have our data prepared, let's create the Bland-Altman plot.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmean_diff <- mean(df$diff_values)\nmean_diff\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 0.5\n```\n:::\n\n```{.r .cell-code}\nlower <- mean_diff - 1.96 * sd(df$diff_values)\nupper <- mean_diff + 1.96 * sd(df$diff_values)\n\nlower\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] -0.3765386\n```\n:::\n\n```{.r .cell-code}\nupper\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 1.376539\n```\n:::\n:::\n\n\n# Step 3: Creating the Bland-Altman Plot\n\nWe are going to do this in base R.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Create a scatter plot\nplot(df$mean_values, df$diff_values, \n xlab = \"Mean of Methods A and B\",\n ylab = \"Difference (Method A - Method B)\",\n main = \"Bland-Altman Plot\",\n ylim = c(lower + (lower * .1), upper * 1.1))\n\n# Add a horizontal line at the mean difference\nabline(h = mean(diff_values), col = \"red\", lty = 2)\n\n# Add Confidence Intervals\nabline(h = upper, col = \"blue\", lty = 2)\nabline(h = lower, col = \"blue\", lty = 2)\n```\n\n::: {.cell-output-display}\n![](index_files/figure-html/unnamed-chunk-3-1.png){width=672}\n:::\n:::\n\n\nThis code will generate a simple Bland-Altman plot, and here's what each part does:\n\n- `plot()`: Creates the scatter plot with means on the X-axis and differences on the Y-axis.\n- `abline(h = mean(diff_values), col = \"red\", lty = 2)`: Adds a red dashed line at the mean difference.\n- `abline(h = upper, col = \"green\", lty = 2)`: Adds blue dashed lines representing the 95% limits of agreement.\n\n# Step 4: Interpretation\n\nNow that you've generated your Bland-Altman plot, let's interpret it:\n\n- The red line represents the mean difference between the two methods.\n- The blue dashed lines show the 95% limits of agreement, which help you assess the spread of the differences.\n\nIf most data points fall within the blue lines, it indicates good agreement between the two methods. If data points are scattered widely outside the lines, there may be a systematic bias or inconsistency between the methods.\n\n# Step 5: Exploration\n\nI encourage you to try this out with your own data. Replace the example data with your measurements and see what insights your Bland-Altman plot reveals.\n\nIn conclusion, creating a Bland-Altman plot in R is a valuable technique to visualize agreement or bias between two measurement methods. It's an essential tool for quality control and validation in various fields. I hope this step-by-step guide helps you get started. Happy plotting!", | ||
"supporting": [ | ||
"index_files" | ||
], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: "What's a Bland-Altman Plot? In Base R" | ||
author: "Steven P. Sanderson II, MPH" | ||
date: "2023-10-25" | ||
categories: [rtip, viz] | ||
--- | ||
|
||
# Introduction | ||
|
||
Before we dive into the code, let's briefly understand what a Bland-Altman plot is. It's a graphical method to visualize the agreement between two measurement techniques, often used in fields like medicine or any domain with comparative measurements. The plot displays the differences between two measurements (Y-axis) against their means (X-axis). | ||
|
||
# Step 1: Data Preparation | ||
|
||
Start by loading your data into R. In our example, we'll create some synthetic data for illustration purposes. You'd replace this with your real data. | ||
|
||
```{r} | ||
# Creating example data | ||
method_A <- c(10, 12, 15, 18, 22, 25) | ||
method_B <- c(9.5, 11, 14, 18, 22, 24.5) | ||
# Calculate the differences and means | ||
diff_values <- method_A - method_B | ||
mean_values <- (method_A + method_B) / 2 | ||
df <- data.frame(method_A, method_B, mean_values, diff_values) | ||
``` | ||
|
||
# Step 2: Calculate Average Difference and CI | ||
|
||
Now that we have our data prepared, let's create the Bland-Altman plot. | ||
|
||
```{r} | ||
mean_diff <- mean(df$diff_values) | ||
mean_diff | ||
lower <- mean_diff - 1.96 * sd(df$diff_values) | ||
upper <- mean_diff + 1.96 * sd(df$diff_values) | ||
lower | ||
upper | ||
``` | ||
|
||
# Step 3: Creating the Bland-Altman Plot | ||
|
||
We are going to do this in base R. | ||
|
||
```{r} | ||
# Create a scatter plot | ||
plot(df$mean_values, df$diff_values, | ||
xlab = "Mean of Methods A and B", | ||
ylab = "Difference (Method A - Method B)", | ||
main = "Bland-Altman Plot", | ||
ylim = c(lower + (lower * .1), upper * 1.1)) | ||
# Add a horizontal line at the mean difference | ||
abline(h = mean(diff_values), col = "red", lty = 2) | ||
# Add Confidence Intervals | ||
abline(h = upper, col = "blue", lty = 2) | ||
abline(h = lower, col = "blue", lty = 2) | ||
``` | ||
|
||
This code will generate a simple Bland-Altman plot, and here's what each part does: | ||
|
||
- `plot()`: Creates the scatter plot with means on the X-axis and differences on the Y-axis. | ||
- `abline(h = mean(diff_values), col = "red", lty = 2)`: Adds a red dashed line at the mean difference. | ||
- `abline(h = upper, col = "green", lty = 2)`: Adds blue dashed lines representing the 95% limits of agreement. | ||
|
||
# Step 4: Interpretation | ||
|
||
Now that you've generated your Bland-Altman plot, let's interpret it: | ||
|
||
- The red line represents the mean difference between the two methods. | ||
- The blue dashed lines show the 95% limits of agreement, which help you assess the spread of the differences. | ||
|
||
If most data points fall within the blue lines, it indicates good agreement between the two methods. If data points are scattered widely outside the lines, there may be a systematic bias or inconsistency between the methods. | ||
|
||
# Step 5: Exploration | ||
|
||
I encourage you to try this out with your own data. Replace the example data with your measurements and see what insights your Bland-Altman plot reveals. | ||
|
||
In conclusion, creating a Bland-Altman plot in R is a valuable technique to visualize agreement or bias between two measurement methods. It's an essential tool for quality control and validation in various fields. I hope this step-by-step guide helps you get started. Happy plotting! |