Skip to content

Instantly share code, notes, and snippets.

@danlewer
Last active March 16, 2019 20:58
Show Gist options
  • Save danlewer/189a29ad130bdbd3137c8c28465db66b to your computer and use it in GitHub Desktop.
Save danlewer/189a29ad130bdbd3137c8c28465db66b to your computer and use it in GitHub Desktop.
Bootstrap confidence interval for difference in means with paired data
# function
mbPaired <- function(x, y, B = 1000) {
lx <- length(x)
ly <- length(y)
if (lx != ly) stop('x and y differ in length')
# sample indices
M <- sample(seq_len(lx), size = lx * B, replace = T)
# calculate differences and mean of differences
diffs <- x[M] - y[M]
diffs <- matrix(diffs, ncol = B)
diffs <- colMeans(diffs)
#results
c(mean(x) - mean(y), quantile(diffs, c(0.025, 0.975)))
}
# example
x <- exp(seq(0, 5, length.out = 100))
y <- log(1:100) ^ 2
mbPaired(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment