Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample code for clustered and nonclustered bootstrapping #168

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
90 changes: 90 additions & 0 deletions R/cor_boot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
cor_boot <- function(data,
x,
y,
method = "pearson",
ci = 0.95,
R = 2000,
cluster = NULL,
...) {

if (!is.null(cluster)) {

Check warning on line 10 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_boot.R,line=10,col=7,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.

Check warning on line 10 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_boot.R,line=10,col=7,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
bs_data <- data[, c(x, y, cluster)]
bs_data <- split(bs_data, bs_data[[cluster]])
# clustered bootstrap
bs_results <- clusterboot(
bs_data = bs_data,
method = method,
R = R,
...
)
} else {
# nonclustered bootstrap
bs_results <- singleboot(
bs_data = data[, c(x, y)],
method = method,
R = R,
...
)
}

bs_ci <- boot::boot.ci(bs_results, conf = ci, type = "perc", index = 2)
bs_ci <- bs_ci$percent[4:5]

out <- data.frame(
"Parameter1" = x,

Check warning on line 34 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_boot.R,line=34,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).

Check warning on line 34 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_boot.R,line=34,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
"Parameter2" = y,

Check warning on line 35 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_boot.R,line=35,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).

Check warning on line 35 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_boot.R,line=35,col=5,[keyword_quote_linter] Only quote named arguments to functions if necessary, i.e., if the name is not a valid R symbol (see ?make.names).
r = z_to_r(bs_results$t0[2]),
SE = sd(z_to_r(bs_results$t[, 2])),
CI_low = z_to_r(bs_ci[[1]]),
CI_high = z_to_r(bs_ci[[2]]),
Method = method,
stringsAsFactors = FALSE
)

out
}

## Non-clustered Bootstrap
singleboot <- function(bs_data, method, R, ...) {
boot::boot(
data = bs_data,
statistic = singleboot_stat,
R = R,
method = method,
...
)
}

r_to_z <- function(r) {
0.5 * log((1 + r) / (1 - r))
}

z_to_r <- function(z) {
(exp(2 * z) - 1) / (exp(2 * z) + 1)
}

singleboot_stat <- function(data, index, method) {
r <- stats::cor(data[index, ], method = method)
z <- r_to_z(r)
z
}

## Cluster Bootstrap
clusterboot <- function(bs_data, method, R, ...) {
boot::boot(
data = bs_data,
statistic = clusterboot_stat,
R = R,
method = method,
...
)
}

clusterboot_stat <- function(data, index, method) {
resample <- do.call(rbind, data[index])
r <- stats::cor(resample[, 1:2], method = method)
z <- r_to_z(r)
z
}

Check warning on line 89 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_boot.R,line=89,col=1,[trailing_blank_lines_linter] Remove trailing blank lines.

Check warning on line 89 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_boot.R,line=89,col=1,[trailing_blank_lines_linter] Remove trailing blank lines.

Check warning on line 90 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_boot.R,line=90,col=1,[trailing_blank_lines_linter] Remove trailing blank lines.

Check warning on line 90 in R/cor_boot.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_boot.R,line=90,col=1,[trailing_blank_lines_linter] Remove trailing blank lines.
Loading