stat.bootstrap <- function(id, val, func = mean, N.bootstrap = 10000) { ## Purpose: Calculate bootstrap-based 95% confidence interval for data with repeated measures ## Arguments: ## id: uniquely identifies a subject (patient) ## val: a numeric vector to be summarized. It contains repeated measures per subject. ## func: a function for calculating the summary statistic from a numeric vector. Default to "mean". ## N.bootstrap: number of bootstrap samples. Default to 10000. ## Return: Point estimate, bootstrap percentile 95% CI, histogram for the bootstrap distribution of the target statistic ## Author: Feiming Chen ## ________________________________________________ fname <- deparse(substitute(func)) cat("Summary Statistic:", fname, "=", func(val), "\n") id.uniq <- unique(id) N <- length(id.uniq) r <- rep(NA, N.bootstrap) for (i in 1:N.bootstrap) { s <- sample(id.uniq, N, replace = TRUE) v <- c() for (j in s) v <- c(v, val[id == j]) r[i] <- func(v) } hist(r, xlab = fname, main = paste("Bootstrap Distribution: ", fname)) cat("95% Bootstrap Percentile Confidence Interval:\n") quantile(r, c(0.025, 0.975)) } if (F) { # Unit Test id <- c(1, 1, 1, 2, 2, 3) val <- c(3, 3, 3, 4, 4, 5) stat.bootstrap(id, val) ## Summary Statistic: mean = 3.6667 ## 95% Bootstrap Percentile Confidence Interval: ## 2.5% 97.5% ## 3 5 stat.bootstrap(id, val, func = sd) ## Summary Statistic: sd = 0.8165 ## 95% Bootstrap Percentile Confidence Interval: ## 2.5% 97.5% ## 0.0000 1.0954 }
Wednesday, June 30, 2021
Bootstrap Confidence Interval for Data with Repeated Measures
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment