Bland Altman dùng để đánh giá agreement (tương đồng/tương hợp) sự khác biệt giữa 2 đo lường. Biểu đồ với trục hoành là trung bình cộng của 2 đo lường và trục hoành là sự khác biệt (hiệu số/sai phân) của chúng. Từ đó đánh giá được sự khác biệt trung bình và giới hạn tương đồng (limits of agreement).

Gọi dữ liệu

library(foreign)
dt = read.dta("C:\\Users\\Nguyen\\Desktop\\LVTN.dta")

Dùng package BlandAltmanLeh

library(BlandAltmanLeh)
bland.altman.plot(dt$aph, dt$vph, main = "The difference of pH between ABG and VBG", xlab = "Means", ylab = "Differences", conf.int=.95, pch=19)

## NULL

Vẽ trên nền ggplot2

library(ggplot2)
p <- bland.altman.plot(dt$aph, dt$vph, graph.sys="ggplot2", conf.int=.95)
p

Chia theo phân nhóm

ba <- bland.altman.stats(dt$aph, dt$vph)
names(ba)
##  [1] "means"         "diffs"         "groups"        "based.on"     
##  [5] "lower.limit"   "mean.diffs"    "upper.limit"   "lines"        
##  [9] "CI.lines"      "two"           "critical.diff"
plot(ba$means, ba$diffs, col=dt$dia1, sub=paste("critical difference is", round(ba$critical.diff,4)), main="The difference of pH between ABG and VBG", xlab = "Means", ylab = "Differences", pch=19)
abline(h = ba$lines, lty=c(2,4,2), col=c("darkgreen","blue","darkgreen"), lwd=c(2,1,2))
legend(x = "topright", legend = c("TB sequel","TB"), fill = 1:2)

Vẽ bằng package ggplot2

library(BlandAltmanLeh)
library(ggplot2)
library(ggExtra)
ba.stats <- bland.altman.stats(dt$aph, dt$vph)
names(ba.stats)
##  [1] "means"         "diffs"         "groups"        "based.on"     
##  [5] "lower.limit"   "mean.diffs"    "upper.limit"   "lines"        
##  [9] "CI.lines"      "two"           "critical.diff"
p = ggplot(data = dt, aes(x = (dt$aph+dt$vph)/2, y = dt$aph-dt$vph, fill = dt$dia1, col = dt$dia1))
p = p + geom_point()
p = p + geom_hline(yintercept = ba.stats$lines, lty = c(2,4,2), lwd = c(1,1,1), color = c("darkorange", "darkblue", "darkorange"))
p = p + theme(legend.position = "bottom")
p
## Warning: Removed 2 rows containing missing values (geom_point).

#Thêm mô tả 2 trục bằng density

ggMarginal(p, type="density", groupColour=T, groupFill=T)
## Warning: Removed 2 rows containing missing values (geom_point).

#Thêm mô tả 2 trục bằng histogram

ggMarginal(p, type="histogram", groupColour=T, groupFill=T)
## Warning: Removed 2 rows containing missing values (geom_point).

#Thêm mô tả 2 trục bằng boxplot

ggMarginal(p, type="boxplot", groupColour=T, groupFill=T)
## Warning: Removed 2 rows containing missing values (geom_point).