Skip to content

Commit

Permalink
new post
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Oct 25, 2023
1 parent c507c12 commit 74604cb
Show file tree
Hide file tree
Showing 10 changed files with 1,302 additions and 409 deletions.
16 changes: 16 additions & 0 deletions _freeze/posts/2023-10-25/index/execute-results/html.json
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.
484 changes: 261 additions & 223 deletions docs/index.html

Large diffs are not rendered by default.

444 changes: 260 additions & 184 deletions docs/index.xml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/listings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
"listing": "/index.html",
"items": [
"/posts/2023-10-25/index.html",
"/posts/2023-10-24/index.html",
"/posts/2023-10-23/index.html",
"/posts/2023-10-20/index.html",
Expand Down
669 changes: 669 additions & 0 deletions docs/posts/2023-10-25/index.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion docs/search.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/index.html</loc>
<lastmod>2023-10-24T12:16:58.676Z</lastmod>
<lastmod>2023-10-25T12:25:39.869Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/rtip-2023-04-06/index.html</loc>
Expand Down Expand Up @@ -516,4 +516,8 @@
<loc>https://www.spsanderson.com/steveondata/posts/2023-10-24/index.html</loc>
<lastmod>2023-10-24T12:17:18.506Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2023-10-25/index.html</loc>
<lastmod>2023-10-25T12:25:47.359Z</lastmod>
</url>
</urlset>
82 changes: 82 additions & 0 deletions posts/2023-10-25/index.qmd
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!

0 comments on commit 74604cb

Please sign in to comment.