Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Bland- Altman statistical function #1135

Merged
merged 20 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Collate:
'analyze_variables.R'
'analyze_vars_in_cols.R'
'argument_convention.R'
'bland_altman.R'
'combination_function.R'
'compare_variables.R'
'control_incidence_rate.R'
Expand Down
79 changes: 79 additions & 0 deletions R/bland_altman.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

Melkiades marked this conversation as resolved.
Show resolved Hide resolved
#' Bland Altman analysis
#'
#' @description 'r lifecycle::badge("stable")'
shajoezhu marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @inheritParams argument_convention
#' @param y ('numeric')\cr vector of numbers we want to analyze.
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @name s_bland_altman
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
#' @details
#' https://doi.org/10.1016/S0140-6736(86)90837-8
#' @references
#' @article{bland1986statistical,
# title={Statistical methods for assessing agreement between two methods of clinical measurement},
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
# author={Bland, J Martin and Altman, DouglasG},
# journal={The lancet},
# volume={327},
# number={8476},
# pages={307--310},
# year={1986},
# publisher={Elsevier}
# }
# https://cran.r-project.org/web/packages/Rdpack/vignettes/Inserting_bibtex_references.pdf

#' @examples
#' x <- seq(1, 60, 5)
#' y <- seq(5, 50, 4)
#' conf_level <- 0.9
#' s_bland_altman(x, y, conf_level = conf_level)


s_bland_altman <- function(x, y, conf_level = 0.95){
checkmate::assert_numeric(x, min.len = 1, any.missing = TRUE)
checkmate::assert_numeric(y, len = length(x), any.missing = TRUE)
checkmate::assert_numeric(conf_level, lower = 0, upper = 1, any.missing = TRUE)

alpha <- 1 - conf_level

ind <- complete.cases(x, y) # use only pairwise complete observations, and check if x and y have the same length
x <- x[ind]
y <- y[ind]
n <- length(n) # number of 'observations'
shajoezhu marked this conversation as resolved.
Show resolved Hide resolved

if(n ==0){
shajoezhu marked this conversation as resolved.
Show resolved Hide resolved
stop("there is no valid paired data")
}

difference <- x - y # vector of differences
average <- (x + y) / 2 # vector of means
difference_mean <- mean(difference) # mean difference
difference_sd <- sd(difference) # SD of differences
al <- qnorm(1 - alpha / 2) * difference_sd
upper_agreement_limit <- difference_mean + al # agreement limits
lower_agreement_limit <- difference_mean - al


difference_se <- difference_sd / sqrt(n) # standard error of the mean
al_se <- difference_sd * sqrt(3) / sqrt(n) # standard error of the agreement limit
tvalue <- qt(1 - alpha / 2, n - 1) # t value for 95% CI calculation
difference_mean_ci <- difference_se * tvalue
al_ci <- al_se * tvalue
upper_agreement_limit_ci <- c(upper_agreement_limit - al_ci, upper_agreement_limit + al_ci)
lower_agreement_limit_ci <- c(lower_agreement_limit - al_ci, lower_agreement_limit + al_ci)


list(
difference_mean = difference_mean,
ci_mean = difference_mean + c(-1, 1) * difference_mean_ci,
difference_sd = difference_sd,
difference_se = difference_se,
upper_agreement_limit = upper_agreement_limit,
lower_agreement_limit = lower_agreement_limit,
agreement_limit_se = al_se,
upper_agreement_limit_ci = upper_agreement_limit_ci,
lower_agreement_limit_ci = lower_agreement_limit_ci,
t_value = tvalue,
n = n
)
}
30 changes: 30 additions & 0 deletions man/s_bland_altman.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions tests/testthat/test-bland-altman.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
testthat::test_that("unequal length vector gives correct error", {
testthat::expect_error(s_bland_altman (x = 1:5, y = 1:6, 0.95))
})

testthat::test_that("infeasible input gives correct error", {
testthat::expect_error(s_bland_altman (x = c('a', 'b', 'c'), y = 1:3, 0.95))
testthat::expect_error(s_bland_altman (x = 1:3, y = 4:6, 2))
})


testthat::test_that("works correctly", {
shajoezhu marked this conversation as resolved.
Show resolved Hide resolved
set.seed(1)
x <- rnorm(20)
y <- rnorm(20)
res <- s_bland_altman(x, y, 0.9)
expect <- list(
difference_mean = mean(x) - mean(y),
ci_mean = c(-0.3414723, 0.7354631),
difference_sd = 1.392664,
difference_se = 0.3114091,
upper_agreement_limit = 2.487724,
lower_agreement_limit = -2.093733,
agreement_limit_se = 0.5393764,
upper_agreement_limit_ci = c(1.555070, 3.420377),
lower_agreement_limit_ci = c(-3.026386,-1.161079),
t_value = 1.729133,
n = 20L
)
expect_identical(res, expect, tolerance = 1e-5)


Melkiades marked this conversation as resolved.
Show resolved Hide resolved
})


testthat::test_that("works correctly with NA element in either vectors", {
set.seed(1)
x <- rnorm(20)
y <- rnorm(20)
x <- c(NA_real_, 2, x, NA_real_)
y <- c(1, NA_real_, y, 2)
res <- s_bland_altman(x, y, 0.9)
expect <- list(
difference_mean = 0.1969954,
ci_mean = c(-0.3414723, 0.7354631),
difference_sd = 1.392664,
difference_se = 0.3114091,
upper_agreement_limit = 2.487724,
lower_agreement_limit = -2.093733,
agreement_limit_se = 0.5393764,
upper_agreement_limit_ci = c(1.555070, 3.420377),
lower_agreement_limit_ci = c(-3.026386,-1.161079),
t_value = 1.729133,
n = 20L
)
expect_identical(res, expect)
})