Last active
March 16, 2019 20:58
-
-
Save danlewer/189a29ad130bdbd3137c8c28465db66b to your computer and use it in GitHub Desktop.
Bootstrap confidence interval for difference in means with paired data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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