If you want to compare the results of two methods to measure a continuous outcome, you will find that correlation/regression displays or models the relationship between the two sets of outcomes. But what you really want to know and analyze, is the difference between the two sets of outcomes. The method is best known by the names of the two authors who published on this topic in the Lancet: Bland and Altman.

Take a look at the dataset TwoTests1.

Set the working directory:

read the file into R using:

> TwoTests1 <- read.csv(file="TwoTests1.csv",head=TRUE, sep=",")

look at the data using:

> TwoTests1

The data shows you three columns: a case identifier, and two rows of test results of what could very well be a continuous variable.

In a few steps you can calculate the:

- difference between each pair of test results
- the mean of the paired test results

define the function (all these steps will be this function):

> bland_altman_plot <- function(x,y,xlab="Average testresult", ylab="Deviation of experimental test")

assign and compute the required data, R awaits the steps of the function. Copy-paste the entire function into R:

{ x <- TwoTests1$Test1 y <- TwoTests1$Test2 d <- ((TwoTests1$Test1 + TwoTests1$Test2)/2) diff <- x - y plot(diff ~ d,pch=16,ylim=c(-60,60),xlab=xlab,ylab=ylab) abline(h=mean(diff)-c(-2,0,2)*sd(diff),lty=2) }

call the function:

> bland_altman_plot(d,diff,xlab="Test 1",ylab="Difference Test1 and Test2")

The last command will result in the plot shown:

Adapted from:

https://stat.ethz.ch/pipermail/r-help/2008-July/166921.html

and

http://rstats.tiddlyspot.com/#Bland-Altman

previous: T test in R

next: Pitman’s T in R

by

papilionaceae704 Mar 2012at 15:41Many useful, Thank you so much

by

Bas12 Jul 2012at 18:14Sorry, but you need to get some reading on functions! This is not a good example for novice R programmers.

First you ask for x, y as an input in your function, and you don’t do anything with that input because you define the same variables 3 lines down.

bland_altman_plot <- function(x,y,xlab="Average testresult", ylab="Deviation of experimental test")

{

x <- TwoTests1$Test1

y <- TwoTests1$Test2

d <- ((TwoTests1$Test1 + TwoTests1$Test2)/2)

diff <- x – y

plot(diff ~ d,pch=16,ylim=c(-60,60),xlab=xlab,ylab=ylab)

abline(h=mean(diff)-c(-2,0,2)*sd(diff),lty=2)

}

by

noob05 Oct 2012at 20:47doesnt work:

Error in bland_altman_plot(d, diff, xlab = “Test 1”, ylab = “Difference Test1 and Test2”) :

object ‘TwoTests1’ not found

by

admin10 Nov 2012at 22:28I wasn’t aware people actually read and comment here.

@noob: the code above still works fine, just make sure you save the TwoTest1 dataset and read it into R, first. Then run the code above.

@Bas, it works, no matter how bad the code is.

by

Jeff05 Dec 2012at 22:03I think this is how the code should read:

bland_altman_plot <- function(x,y,xlab="Average testresult", ylab="Deviation of experimental test")

{

d <- ((x + y)/2)

diff <- x – y

plot(diff ~ d,pch=16,ylim=c(-60,60),xlab=xlab,ylab=ylab)

abline(h=mean(diff)-c(-2,0,2)*sd(diff),lty=2)

}

by

Weismann10 Dec 2012at 01:51Many thanks for this good example. How can I save the bland-altmann plot to .jpg??

Many thamks

by

admin02 Jan 2013at 22:25@Jeff: thanks, now I get what Bas was talking about. Your example is exactly right.

@Weismann: I haven’t done a post on such output yet. But I am sure you can find it elswhere.

by

Firas05 Nov 2013at 23:46you can use MedCalc program. It is more easy than other programs.