# Plot Differences in Two Measurements-Bland-Altman Plot in R

Plot Differences in Measurements, the discrepancies in measurements between two different instruments or measurement methodologies are visualized using a Bland-Altman plot.

It can be used to compare the accuracy of two instruments or approaches while measuring the same build.

## Bland-Altman plot in R

This tutorial describes how to create a Bland-Altman plot in R.

Logistic Regression R- Tutorial » Detailed Overview »

### Step 1: dataset creation

Suppose if we have one product and the smoothness values measured based on two different instruments (A and B) from the 20 subjects.

Let’s create a data frame,

```data <- data.frame(A=c(6, 5, 3, 5, 6, 6, 5, 4, 7, 8, 9,
10, 11, 13, 10, 4, 15, 8, 22, 5),
B=c(5, 4, 3, 5, 5, 6, 8, 6, 4, 7, 7, 11,
13, 5, 10, 11, 14, 8, 9, 4))```
`head(data)`
```  A B
1 6 5
2 5 4
3 3 3
4 5 5
5 6 5
6 6 6```

### Step 2: Difference in Measurements

Next, we’ll add two new columns to the data frame, one for each product’s average measurement and the other for the difference in measurements:

Naive Bayes Classification in R » Prediction Model »

create a new column for average measurement

`data\$avg <- rowMeans(data)`

create a new column for differences

```data\$diff <- data\$A - data\$B
data```
```    A  B  avg diff
1   6  5  5.5    1
2   5  4  4.5    1
3   3  3  3.0    0
4   5  5  5.0    0
5   6  5  5.5    1
6   6  6  6.0    0
7   5  8  6.5   -3
8   4  6  5.0   -2
9   7  4  5.5    3
10  8  7  7.5    1
11  9  7  8.0    2
12 10 11 10.5   -1
13 11 13 12.0   -2
14 13  5  9.0    8
15 10 10 10.0    0
16  4 11  7.5   -7
17 15 14 14.5    1
18  8  8  8.0    0
19 22  9 15.5   13
20  5  4  4.5    1```

Deep Neural Network in R » Keras & Tensor Flow

### Step 3: Calculate the Confidence Interval

The average difference in measurements between the two instruments, as well as the upper and lower 95 percent confidence interval bounds for the average difference, need to calculate.

find an average difference

```mean_diff <- mean(data\$diff)
mean_diff
0.85```

find lower 95% confidence interval limits

`lower <- mean_diff - 1.96*sd(data\$diff)`

find upper 95% confidence interval limits

`upper <- mean_diff + 1.96*sd(data\$diff)`

The average difference turns out to be 0.85 and the 95% confidence interval for the average difference is [-6.997089, 8.697089].

eXtreme Gradient Boosting in R » Ultimate Guide »

### Step 4: Create the Bland-Altman Plot

Let’s make use of ggplot to create a Bland-Altman plot in R.

```  library(ggplot2)
ggplot(data, aes(x = avg, y = diff)) +
geom_point(size=2) +
geom_hline(yintercept = mean_diff) +
geom_hline(yintercept = lower, color = "red", linetype="dashed") +
geom_hline(yintercept = upper, color = "red", linetype="dashed") +
ggtitle("Bland-Altman Plot") +
ylab("Difference Between Instruments") +
xlab("Average")+theme_bw()``` The plot’s x-axis shows the average measurement of the two instruments, while the y-axis shows the difference in measurements between them.

The average difference in measurements between the two instruments is represented by the black line, while the 95 percent confidence interval limits for the average difference are represented by the two red dashed lines.

Repeated Measures of ANOVA in R Complete Tutorial »

#### You may also like... 