#----------------------------------------------------------------------------
# Pearson product-moment correlation coefficient
# Example 1a: Approximate distribution method
ci.cor(mtcars[, c("mpg", "drat", "qsec")])
# Alternative specification
ci.cor(mpg, drat, qsec, data = mtcars)
# Example 1b: Joint moments method
ci.cor(mtcars[, c("mpg", "drat", "qsec")], adjust = "joint")
#----------------------------------------------------------------------------
# Spearman's rank-order correlation coefficient
# Example 2a: Fieller et al. (1957) approximate standard error
ci.cor(mtcars[, c("mpg", "drat", "qsec")], method = "spearman")
# Example 2b: Bonett and Wright (2000) approximate standard error
ci.cor(mtcars[, c("mpg", "drat", "qsec")], method = "spearman", se = "bonett")
# Example 2c: Rank-based inverse normal (RIN) transformation
ci.cor(mtcars[, c("mpg", "drat", "qsec")], method = "spearman", se = "rin")
#----------------------------------------------------------------------------
# Kendall's Tau
# Example 3a: Kendall's Tau-b
ci.cor(mtcars[, c("mpg", "drat", "qsec")], method = "kendall-b")
# Example 3b: Kendall's Tau-c
ci.cor(mtcars[, c("mpg", "drat", "qsec")], method = "kendall-c")
if (FALSE) {
#----------------------------------------------------------------------------
# Bootstrap Confidence Interval (CI)
# Example 4a: Bias-corrected (BC) percentile bootstrap CI
ci.cor(mtcars[, c("mpg", "drat", "qsec")], boot = "bc")
# Example 4b: Bias-corrected and accelerated (BCa) bootstrap CI,
# 5000 bootstrap replications, set seed of the pseudo-random number generator
ci.cor(mtcars[, c("mpg", "drat", "qsec")], boot = "bca", R = 5000, seed = 123)
#----------------------------------------------------------------------------
# Grouping and Split Variable
# Example 5a: Grouping variable
ci.cor(mpg, drat, qsec, data = mtcars, group = "vs")
# Alternative specification
ci.cor(mtcars[, c("mpg", "drat", "qsec")], group = mtcars$vs)
# Example 5b: Split variable
ci.cor(mpg, drat, qsec, data = mtcars, split = "am")
# Alternative specification
ci.cor(mtcars[, c("mpg", "drat", "qsec")], split = mtcars$am)
# Example 5c: Grouping and split variable
ci.cor(mpg, drat, qsec, data = mtcars, group = "vs", split = "am")
# Alternative specification
ci.cor(mtcars[, c("mpg", "drat", "qsec")], group = mtcars$vs, split = mtcars$am)
#----------------------------------------------------------------------------
# Write Output
# Example 6a: Text file
ci.cor(mtcars[, c("mpg", "drat", "qsec")], write = "CI_Cor_Text.txt")
# Example 6b: Excel file
ci.cor(mtcars[, c("mpg", "drat", "qsec")], write = "CI_Cor_Excel.xlsx")
#----------------------------------------------------------------------------
# Plot Confidence Intervals
# Example 7a: Pearson product-moment correlation coefficient
ci.cor(mpg, drat, qsec, data = mtcars, plot = "ci")
# Example 7b: Grouping variable
ci.cor(mpg, drat, qsec, data = mtcars, group = "vs", plot = "ci")
# Example 7c: Split variable
ci.cor(mpg, drat, qsec, data = mtcars, split = "am", plot = "ci")
# Example 7d: Save plot as PDF file
ci.cor(mpg, drat, qsec, data = mtcars, plot = "ci", saveplot = "CI_Cor.pdf",
width = 9, height = 6)
# Example 7e: Save plot as PNG file
ci.cor(mpg, drat, qsec, data = mtcars, plot = "ci", saveplot = "CI_Cor.png",
width = 9, height = 6)
# Example 7f: Plot in line with the default setting
# Load ggplot2 package
library(ggplot2)
# Extract plot data
plotdata <- ci.cor(mtcars[, c("mpg", "drat", "qsec")], plot = "ci",
output = FALSE)$plot$plotdata
# Draw plot
ggplot(plotdata, aes(x, y)) +
geom_hline(yintercept = 0, linetype = "dashed", color = "gray50") +
geom_errorbar(aes(ymin = low, ymax = upp), width = 0.3) +
geom_point(size = 2.5) +
scale_x_discrete(name = "") +
scale_y_continuous(name = "Correlation Coefficient", limits = c(-1, 1),
breaks = seq(-1, 1, by = 0.25)) +
theme_bw() +
labs(title = NULL, subtitle = NULL) +
theme(plot.subtitle = element_text(hjust = 0.5),
plot.title = element_text(hjust = 0.5),
plot.margin = unit(c(5.5, 5.5, 5.5, 5.5), "pt"),
axis.text = element_text(size = 10),
axis.title = element_text(size = 10))
#----------------------------------------------------------------------------
# Plot Bootstrap Samples
# Example 8a: Pearson product-moment correlation coefficient
ci.cor(mpg, drat, qsec, data = mtcars, boot = "bc", plot = "boot")
# Example 8b: Grouping variable
ci.cor(mpg, drat, qsec, data = mtcars, group = "vs", boot = "bc", plot = "boot")
# Example 8c: Split variable
ci.cor(mpg, drat, qsec, data = mtcars, split = "am", boot = "bc", plot = "boot")
# Example 8d: Save plot as PDF file
ci.cor(mpg, drat, qsec, data = mtcars, boot = "bc", plot = "boot",
saveplot = "CI_Cor_Boot.pdf", width = 14, height = 9)
# Example 8e: Save plot as PNG file
ci.cor(mpg, drat, qsec, data = mtcars, boot = "bc", plot = "boot",
saveplot = "CI_Cor_Boot.png", width = 14, height = 9)
# Example 8f: Plot in line with the default setting
# Load ggplot2 package
library(ggplot2)
# Extract plot data
plotdata <- ci.cor(mtcars[, c("mpg", "drat", "qsec")], boot = "bc", plot = "boot",
output = FALSE)$plot$plotdata
ggplot(plotdata, aes(y)) +
geom_histogram(aes(y = after_stat(density)), color = "black", alpha = 0.4, fill = "gray85") +
geom_density(color = "#0072B2", linewidth = 0.5) +
geom_vline(xintercept = 0, color = "gray70") +
geom_vline(aes(xintercept = point), linewidth = 0.6) +
geom_vline(aes(xintercept = low), linewidth = 0.6, linetype = "dashed") +
geom_vline(aes(xintercept = upp), linewidth = 0.6, linetype = "dashed") +
facet_wrap(~ x, scales = "free_y") +
scale_x_continuous(name = "Correlation Coefficient", expand = c(0.02, 0),
limits = c(-1, 1), breaks = seq(-1, 1, by = 0.25)) +
scale_y_continuous(name = "Probability Density, f(x)",
expand = expansion(mult = c(0L, 0.05))) +
theme_bw() +
theme(plot.subtitle = element_text(hjust = 0.5),
strip.text = element_text(size = 11),
plot.title = element_text(hjust = 0.5),
plot.margin = unit(c(5.5, 5.5, 5.5, 5.5), "pt"),
axis.text = element_text(size = 10),
axis.title = element_text(size = 11))
}
Run the code above in your browser using DataLab