-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathREADME.Rmd
More file actions
140 lines (97 loc) · 3.35 KB
/
README.Rmd
File metadata and controls
140 lines (97 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
output: github_document
bibliography: inst/REFERENCES.bib
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
dpi = 200,
knitr::opts_chunk$set(comment = NA)
)
```
# **BBcor**: Bayesian Bootstrapping Correlations
[](https://cran.r-project.org/package=BBcor)
[](https://cran.r-project.org/package=BBcor)
[](https://travis-ci.org/donaldRwilliams/BBcor)
The goal of BBcor is to provide an efficient way to obtain samples from the
posterior distribution of various correlation coefficients:
* Pearson (`method = "pearson"`)
* Spearman (`method = "spearman"`)
* Gaussian Rank (`method = "gaussian_rank"`)
* Kendall (`method = "kendall"`)
* Blomqvist (`method = "blomqvist"`; median correlation)
* Polychoric (`method = "polychoric"`)
The method is based on @rubin1981bayesian and described in @rodriguez2021painless.
## Installation
You can install the released version of BBcor from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("BBcor")
```
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("donaldRwilliams/BBcor")
```
## Example: Correlations
This is a basic example which shows you how to solve a common problem, i.e., estimating correlations:
```{r example1}
library(BBcor)
## basic example code
# data
Y <- mtcars[,1:5]
# sample posterior
bb_sample <- bbcor(Y, method = "spearman")
# correlation matrix
bb_sample$cor_mean
```
## Example: Partial Correlations
It is also possible to obtain partial correlations from the object `bb_sample`:
```{r example2}
# convert
pcors <- cor_2_pcor(bb_sample)
# partial correlation matrix
pcors$pcor_mean
```
Note that the objects `bb_sample` and `pcors` include a 3D array with the sampled
correlation or partial correlation matrices.
## Example: Posterior Samples
The posterior samples can be summarized as follows
```{r}
post_summary <- posterior_samples(pcors, summary = TRUE, cred = 0.95)
# print
post_summary
```
Note that setting `summary = FALSE` returns the posterior samples in a data frame.
## Example: Comparing Correlations
Comparisons can then be made using the `compare` function using a string to specify which comparisons to be made
```{r}
comparisons <- c("mpg--cyl > mpg--disp",
"mpg--disp - mpg--hp = 0")
post_comparisons <- compare(comparisons,
obj = pcors,
cred = 0.9)
post_comparisons
```
```r
plot(post_comparisons) +
ggplot2::theme_classic()
```
```{r echo=FALSE, out.height="20%", out.width="75%", fig.align='center'}
knitr::include_graphics("inst/comparison-plot.png", dpi=300)
```
or with a contrast matrix
```{r}
contrast_mat <- matrix(c(1, -1, 0,
0, 1, -1),
nrow = 2,
byrow = TRUE)
post_comparisons <- compare(c("mpg--cyl", "mpg--disp", "mpg--hp"),
obj = pcors,
contrast = contrast_mat,
cred = 0.9)
post_comparisons
```
## References