-
Notifications
You must be signed in to change notification settings - Fork 0
/
r_SB.R
35 lines (34 loc) · 809 Bytes
/
r_SB.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#' Spearman-Brown Split half reliability
#'
#' @param x A correlation or numeric vector
#' @param y A numeric vector
#' @param var.equal Assume equal var of `x` and `y`? (ignored if `y` is not `NULL`)
#'
#' @examples
#' r_SB(1:30, -exp(1 / 1:30), var.equal = TRUE)
#'
#' r_SB(1:30, -exp(1 / 1:30), var.equal = FALSE)
#'
#' r_SB(0.57)
#'
#' @export
r_SB <- function(x, y = NULL, var.equal = TRUE) {
if (!is.null(y)) {
r <- stats::cov(cbind(x, y))
if (var.equal) {
r <- stats::cov2cor(r)
}
if (r[1, 2] < 0) {
stop("Correlation is negative!")
}
mean(r[!(!upper.tri(r) & !lower.tri(r))]) / mean(r)
} else {
if (x < 0) {
stop("Correlation is negative!")
}
if (x > 1) {
stop("Correlation cannot be greater than 1!")
}
(2 * x) / (1 + x)
}
}