forked from GRousselet/ccmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
192 lines (154 loc) · 4.68 KB
/
README.Rmd
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
```{r, include = FALSE}
# library(ccmc)
devtools::load_all()
library(tibble)
library(ggplot2)
library(tidyr)
library(dplyr)
```
# ccmc
Cluster Correction for Multiple Comparisons (v. 0.0.0.9000)
The goal of ccmc is to correct for multiple comparisons of dependent tests using cluster-based statistics. The rationale for this approach is described [here](https://garstats.wordpress.com/2018/09/06/cluster/).
## Installation
The package can be installed using these commands:
```{r, eval = FALSE}
install.packages("devtools")
devtools::install_github("GRousselet/ccmc")
```
## Examples
### No effect: 5 independent groups
```{r}
set.seed(21)
N <- 40 # observations per group
Ng <- 5 # number of groups
x <- matrix(rnorm(N*Ng), ncol = Ng)
trimbt.ccmc(x,nullval=0,tr=0,alpha=.05,bt=FALSE,nboot=599)
```
### Cluster of length 3 with 5 independent groups
Make data
```{r}
set.seed(21)
N <- 40 # observations per group
Ng <- 5 # number of groups
x <- matrix(rnorm(N*Ng), ncol = Ng)
x[,3:5] <- x[,3:5] + 1
```
Illustrate data
```{r}
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# data frame of independent observations
df <- as.tibble(x)
colnames(df) <- c("c1", "c2", "c3", "c4", "c5")
df <- gather(df, condition, value)
df$condition <- factor(df$condition)
# data frame of means
tmp <- as_tibble(x)
colnames(tmp) <- c("c1", "c2", "c3", "c4", "c5")
df.mean <- tidyr::gather(dplyr::summarise_all(tmp, funs(mean)),condition, value)
df.mean$condition <- as.factor(df.mean$condition)
ggplot(df, aes(x=condition, y=value, colour=condition)) + theme_grey() +
geom_jitter() +
scale_colour_manual(values=cbPalette) +
geom_line(data = df.mean, aes(group = 1), colour = "black", size = 1) +
geom_point(data = df.mean, colour = "black", size = 3) +
geom_abline(slope = 0, intercept = 0) +
theme(axis.title = element_text(size = 18),
axis.text = element_text(size = 14, colour="black"),
legend.position="none")
```
Mass cluster test
```{r}
trimbt.ccmc(x,nullval=0,tr=0,alpha=.05,bt=FALSE,nboot=599)
```
Use bootstrap-t thresholds
```{r, eval = FALSE}
trimbt.ccmc(x,nullval=0,tr=0,alpha=.05,bt=TRUE,nboot=599)
```
Get cluster statistics for cluster 1:
```{r, eval = FALSE}
out <- trimbt.ccmc(x,nullval=0,tr=0,alpha=.05,bt=TRUE,nboot=599)
c2sum <- sum(out$tval[out$cluster.map==1]^2)
```
### Cluster of length 2: 5 dependent groups
Make data
```{r}
require(MASS)
set.seed(21)
Ng <- 5 # n groups
Np <- 30 # n participants per group
rho = 0.75 # correlation between repeated measures
ES <- c(0, 1, 1, 0, 0) # true effects
sigma <- 1 # population standard deviation
# variance-covariance matrix
Sigma <- diag(nrow = Ng)
Sigma[Sigma == 0] <- rho
x <- mvrnorm(Np, ES, Sigma)
```
Illustrate data
```{r}
df <- as.tibble(x)
colnames(df) <- c("c1", "c2", "c3", "c4", "c5")
df <- gather(df, condition, value)
df$participant <- factor(rep(seq(1, Np), Ng))
df$condition <- factor(df$condition)
# data frame of means
tmp <- as_tibble(x)
colnames(tmp) <- c("c1", "c2", "c3", "c4", "c5")
# apply(data, 2, mean) # Mean per group
df.mean <- tidyr::gather(dplyr::summarise_all(tmp, funs(mean)),condition, value)
df.mean$condition <- as.factor(df.mean$condition)
ggplot(df, aes(x=condition, y=value)) +
geom_line(aes(group=participant, colour=participant)) +
geom_line(data = df.mean, aes(group = 1), size = 1) +
geom_point(data = df.mean, aes(group = 1), size = 3) +
geom_abline(slope = 0, intercept = 0) +
theme(axis.title = element_text(size = 18),
axis.text = element_text(size = 14, colour="black"))
```
Mass cluster test
```{r}
trimbt.ccmc(x,nullval=0,tr=0,alpha=.05,bt=FALSE,nboot=599)
```
## Threshold Free Cluster Enhancement
TFCE demo
```{r}
x.seq <- seq(1,29)
data <- c(0,0,0,0,seq(1,11),seq(10,1),0,0,0,0)
data.tfce <- tfce(data)
df <- tibble(x = rep(x.seq,2),
y = c(data/max(data), data.tfce/max(data.tfce)),
data = factor(c(rep("Original",29),rep("TFCE",29)))
)
ggplot(df, aes(x=x, y=y, colour = data)) +
geom_point(size = 2) +
geom_line(size = 0.5) +
scale_colour_manual(values=c("black","orange"))
```
TFCE bootstrap data
```{r}
nboot <- 1000
Ng <- 5
boot.data <- matrix(rnorm(Ng*nboot), ncol = nboot)
boot.tfce <- tfce(abs(boot.data))
```
Correct for multiple comparisons using TFCE
```{r}
set.seed(21)
N <- 40 # observations per group
Ng <- 5 # number of groups
x <- matrix(rnorm(N*Ng), ncol = Ng)
x[,3:5] <- x[,3:5] + 1
trimbt.tfce(x,nullval=0,tr=0,alpha=.05,nboot=599)
```