Christine
Christine

Reputation: 11

Exclude Outliers in a Bland Altman Plot in R

I would like adapt my Bland Altman plot since it doesn't look good. I would like to exclude outliers or only include the Confidence Interval.

I used this code:

library(BlandAltmanLeh)
library(ggplot2)

S_plot <- bland.altman.plot(m1, m2, graph.sys = "ggplot2")

print(S_plot + ggtitle("Bland Altman plot Stress"))

Is there a specific command i can use? Thanks in advance

enter image description here

Upvotes: 0

Views: 516

Answers (1)

tjebo
tjebo

Reputation: 23807

I don't endorse this type of visualisation - a Bland Altman plot is made to examine and test the repeatability of your method. Making it look "prettier" by removing outliers does yourself and science a disservice.

If you really wanna do this, you will need to manually calculate the limits of agreement. You can do this as per below, (or, even easier, you employ the function BlandAltmanLeh::bland.altman.stats from the package which you are using.). You will then need to change the limits of your coordinates of your plot.

library(BlandAltmanLeh)
set.seed(42)
df <- data.frame(x1 = rnorm(20), x2 = rnorm(20))

## differences 
diffs <- with(df, x1 - x2)
## mean and sd
mean_diff <- mean(diffs, na.rm = T)
sd_diff <- sd(diffs, na.rm = T)
## calculate LoA
upper_LOA <- mean_diff + 1.96*sd_diff
lower_LOA <- mean_diff - 1.96*sd_diff
LoA <- c(lower_LOA, upper_LOA)

## or, even easier with BlandAltmanLeh::bland.altman.stats
LoA_2 <-bland.altman.stats(df$x1, df$x2)$lines[c("lower.limit", "upper.limit")]
all(LoA == LoA_2)
#> [1] TRUE

# after plotting, change the coordinate limits
## requires you to load ggplot2
bland.altman.plot(df$x1, df$x2, graph.sys = "ggplot2") +
  ggplot2::coord_cartesian(ylim = LoA)
#> Loading required namespace: ggplot2

Created on 2022-06-14 by the reprex package (v2.0.1)

Upvotes: 1

Related Questions