# We compare the implemented algorithm against a naive brute-force approach.
bruteForcePB <- function(x, y) {
n <- length(x)
medind1 <- floor(((n * (n - 1)) / 2 + 2) / 2) # upper median
medind2 <- floor((n + 2) / 2)
temp <- t(sapply(1:n, function(z) apply(cbind(x, y), 1 ,
function(k) (k[2] - y[z]) /
(k[1] - x[z]))))
PBslope <- sort(abs(as.vector(temp[lower.tri(temp)])))[medind1]
PBintercept <- sort(y - x * PBslope)[medind2]
return(list(intercept = PBintercept, slope = PBslope))
}
n = 100
set.seed(2)
x = rnorm(n)
y = x + rnorm(n)
t0 <- proc.time()
PB.fast <- PassingBablok(x, y, NULL, FALSE)
t1 <- proc.time()
t1 - t0
t0 <- proc.time()
PB.naive <- bruteForcePB(x, y)
t1 <- proc.time()
t1 - t0
PB.fast$slope - PB.naive$slope
Run the code above in your browser using DataLab