library(MethComp)
library(dplyr)
library(ggplot2)
## Load systolic blood pressure data
data(sbp)
## Summarize
sbp %>%
dplyr::group_by(meth,repl) %>%
summarize(mean_y = mean(y))
## Source: local data frame [9 x 3]
## Groups: meth
##
## meth repl mean_y
## 1 J 1 128.5
## 2 J 2 127.3
## 3 J 3 126.4
## 4 R 1 128.3
## 5 R 2 127.6
## 6 R 3 126.1
## 7 S 1 144.8
## 8 S 2 142.7
## 9 S 3 141.5
## Use rater J, replications 1 and 3
sbp2 <- subset(sbp, meth == "J" & repl %in% c(1,3))
## Plot
ggplot(data = sbp2,
mapping = aes(x = repl, y = y, group = item)) +
layer(geom = "line") +
theme_bw() +
theme(legend.key = element_blank())
## Using MethComp package
BlandAltman(sbp[sbp$repl == 3,"y"], sbp[sbp$repl == 1,"y"],
x.name = "sbp3", y.name = "sbp1")
## NOTE:
## 'AB.plot' and 'BlandAltman' are deprecated,
## and likely to disappear in a not too distant future,
## use 'BA.plot' instead.
##
## Limits of agreement:
## sbp3 - sbp1 2.5% limit 97.5% limit SD(diff)
## -2.545 -25.355 20.265 11.405
## Manually
(diffs <- sbp[sbp$repl == 3,"y"] - sbp[sbp$repl == 1,"y"])
## [1] 7 0 6 -4 -12 2 -14 -2 12 -8 4 14 -2 0 -4 -8 2 2 -2 -6 -12 -14 -8 -24 4 2 -6
## [28] 2 0 -16 -8 2 -4 8 18 4 4 -10 0 -6 -12 -12 -2 -10 0 -14 8 -16 -12 14 -4 -12 6 -6
## [55] -10 4 -2 10 -12 -14 -16 -10 -10 2 8 -2 14 0 10 22 26 2 -6 0 6 -2 12 0 -4 -6 -24
## [82] -22 6 -6 -10 13 2 6 -4 -14 2 -16 0 12 -8 4 12 -4 0 -2 -4 -2 2 -4 -4 -16 -4 -6
## [109] -20 4 2 -8 2 0 -14 -6 6 -4 -6 16 6 4 -12 0 -8 -14 -12 -4 -10 2 -14 8 -14 -14 12
## [136] -4 -10 8 -4 -12 2 0 12 -10 -14 -18 -12 -8 -2 4 0 14 2 12 22 26 -2 -8 -4 6 -6 14
## [163] 2 -4 -6 -24 -22 8 -4 -8 2 7 3 8 -16 -3 -10 5 3 -15 4 2 -9 -9 -1 -5 11 -5 -26
## [190] -10 -15 -9 -19 10 -4 2 1 -7 -2 -6 3 -7 -7 -1 4 -1 11 -8 5 -15 -11 -12 -8 -21 4 -5
## [217] -4 -40 -15 12 27 -8 5 -23 -8 12 -2 2 -6 -19 -39 24 -13 -6 21 -21 -49 43 -3 14 20 8 -11
## [244] -5 3 -7 -14 -8 -7 11 -24 13 12 -3 7
(d_bar <- mean(diffs))
## [1] -2.545
(sd_diff <- sd(diffs))
## [1] 11.41
qnorm(0.975)
## [1] 1.96
d_bar + c(-1,+1) * qnorm(0.975) * sd_diff
## [1] -24.90 19.81
## Plot
plot(density(diffs))
abline(v = d_bar)
abline(v = d_bar + c(-1,+1) * qnorm(0.975) * sd_diff)