-
Notifications
You must be signed in to change notification settings - Fork 6
/
README.Rmd
244 lines (195 loc) · 9.78 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
---
output: github_document
---
<!-- 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-",
out.width = "100%",
dev = "png", dpi = 300
)
```
# ggtda
[![Coverage Status](https://img.shields.io/codecov/c/github/tdaverse/ggtda/main.svg)](https://codecov.io/github/tdaverse/ggtda?branch=main)
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![CRAN version](http://www.r-pkg.org/badges/version/ggtda)](https://CRAN.R-project.org/package=ggtda)
[![CRAN Downloads](http://cranlogs.r-pkg.org/badges/grand-total/ggtda)](https://CRAN.R-project.org/package=ggtda)
## Overview
The **ggtda** package provides **ggplot2** layers for the visualization of constructions and statistics arising from topological data analysis.
## Installation
The development version can be installed used the **remotes** package:
```r
# install from GitHub
remotes::install_github("tdaverse/ggtda", vignettes = TRUE)
```
For an introduction to package functionality, read the vignettes:
```r
# read vignettes
vignette(topic = "visualize-persistence", package = "ggtda")
vignette(topic = "illustrate-constructions", package = "ggtda")
vignette(topic = "grouped-list-data", package = "ggtda")
```
We aim to submit to [CRAN](https://CRAN.R-project.org) in Spring 2024!
## Example
```{r}
# attach {ggtda}
library(ggtda)
```
### Sample data set
This example illustrates **ggtda** features using an artificial point cloud $X$ sampled with noise from a circle:
```{r, fig.height=3}
# generate a noisy circle
n <- 36
set.seed(0)
t <- stats::runif(n = n, min = 0, max = 2*pi)
d <- data.frame(
x = cos(t) + stats::rnorm(n = n, mean = 0, sd = .2),
y = sin(t) + stats::rnorm(n = n, mean = 0, sd = .2)
)
# plot the data
ggplot(d, aes(x, y)) + geom_point() + coord_equal() + theme_bw()
```
### Topological constructions
**ggtda** provides stat and geom layers for common TDA constructions.
To illustrate, pick a proximity, or threshold, to consider points in the cloud to be neighbors:
```{r}
# choose a proximity threshold
prox <- 2/3
```
The homology $H_k(X)$ of a point cloud is uninteresting ($H_0(X) = \lvert X \rvert$ and $H_k(X) = 0$ for $k > 0$). The most basic space of interest to the topological data analyst is the union of a _ball cover_ $B_r(X)$ of $X$---a ball of common radius $r$ around each point. The common radius will be $r =$ `prox / 2`.
The figure below compares the ball cover (left) with the _Vietoris_ (or _Rips_) _complex_ ${VR}_r(X)$ constructed using the same proximity (right).
The complex comprises a simplex at each subset of points having diameter at most `prox`---that is, each pair of which are within `prox` of each other.
A key result in TDA is that the homology of the ball union is "very close" to that of the complex.
```{r, fig.height=3}
# visualize disks of fixed radii and the Vietoris complex for this proximity
p_d <- ggplot(d, aes(x = x, y = y)) +
coord_fixed() +
geom_disk(radius = prox/2, fill = "aquamarine3") +
geom_point() +
theme_bw()
p_sc <- ggplot(d, aes(x = x, y = y)) +
coord_fixed() +
stat_simplicial_complex(diameter = prox, fill = "darkgoldenrod") +
theme_bw() +
theme(legend.position = "none")
# combine the plots
gridExtra::grid.arrange(
p_d, p_sc,
layout_matrix = matrix(c(1, 2), nrow = 1)
)
```
This cover and simplex clearly contain a non-trivial 1-cycle (loop), which makes $H_1(B_r(X)) = H_1({VR}_r(X)) = 1$.
But detecting this feature depended crucially on the choice of `prox`, and there's no guarantee with new data that this choice will be correct or even that a single best choice exists.
Instead, we tend to be interested in considering those features that persist across many values of `prox`.
The GIF below[^TDAvis] illustrates this point: Observe how features appear and disappear as the disk covers grow:
[^TDAvis]: The GIF and many features of **ggtda** were originally developed in the separate package [**TDAvis**](https://github.com/jamesotto852/TDAvis).
<img src="man/figures/Small-Clouds.gif" width="100%" />
### Persistent homology
Persistent homology (PH) encodes the homology group ranks across the full range $0 \leq r < \infty$, corresponding to the full filtration of simplicial complexes constructed on the point cloud.
We use [**ripserr**](https://cran.r-project.org/package=ripserr) to compute the PH of the point cloud $X$:
```{r}
# compute the persistent homology
ph <- ripserr::vietoris_rips(as.matrix(d), dim = 1)
print(ph)
```
The loop is detected, though we do not yet know whether its persistence stands out from that of other features.
To prepare for `ggplot()`, we convert the result to a data frame and its numeric `dimension` column to a factor:
```{r}
pd <- as.data.frame(ph)
pd <- transform(pd, dimension = as.factor(dimension))
head(pd)
tail(pd)
```
### Persistence plots
**ggtda** also provides stat and geom layers for common visualizations of persistence data.
We visualize these data using a barcode (left) and a persistence diagram (right).
In the barcode, the dashed line indicates the cutoff at the proximity `prox`; in the persistence diagram, the fundamental box contains the features that are detectable at this cutoff.
```{r, fig.height=3}
# visualize the persistence data, indicating cutoffs at this proximity
p_bc <- ggplot(pd, aes(start = birth, end = death)) +
geom_barcode(linewidth = 1, aes(color = dimension, linetype = dimension)) +
labs(x = "Diameter", y = "Homological features",
color = "Dimension", linetype = "Dimension") +
geom_vline(xintercept = prox, color = "darkgoldenrod", linetype = "dotted") +
theme_barcode()
max_prox <- max(pd$death)
p_pd <- ggplot(pd) +
coord_fixed() +
stat_persistence(aes(start = birth, end = death,
colour = dimension, shape = dimension)) +
geom_abline(slope = 1) +
labs(x = "Birth", y = "Death", color = "Dimension", shape = "Dimension") +
lims(x = c(0, max_prox), y = c(0, max_prox)) +
geom_fundamental_box(
t = prox,
fill = "darkgoldenrod", color = "transparent"
) +
theme_persist()
# combine the plots
gridExtra::grid.arrange(
p_bc, p_pd,
layout_matrix = matrix(c(1, 2), nrow = 1)
)
```
The barcode lines are color- and linetype-coded by feature dimension: the 0-dimensional features, i.e. the gaps between connected components, versus the 1-dimensional feature, i.e. the loop.
These groups of lines do not overlap, which means that the loop exists only in the persistence domain where all the data points are part of the same connected component. Our choice of `prox` is between the birth and death of the loop, which is why the complex above recovers it.
The persistence diagram shows that the loop persists for longer than any of the gaps. This is consistent with the gaps being artifacts of the sampling procedure but the loop being an intrinsic property of the underlying space.
### Multiple data sets
TDA usually involves comparisons of topological data between spaces.
To illustrate such a comparison, we construct a larger sample and examine the persistence of its cumulative subsets:
```{r}
# larger point cloud sampled from a noisy circle
set.seed(0)
n <- 180
t <- stats::runif(n = n, min = 0, max = 2*pi)
d <- data.frame(
x = cos(t) + stats::rnorm(n = n, mean = 0, sd = .2),
y = sin(t) + stats::rnorm(n = n, mean = 0, sd = .2)
)
# list of cumulative point clouds
ns <- c(12, 36, 60, 180)
dl <- lapply(ns, function(n) d[seq(n), ])
```
First we construct a nested data frame containing these subsets and plot their Vietoris complexes.
(We specify the [**simplextree**](https://github.com/peekxc/simplextree) engine and restrict to 2-simplices to reduce runtime.)
```{r, fig.height=5}
# formatted as grouped data
dg <- do.call(rbind, dl)
dg$n <- rep(ns, vapply(dl, nrow, 0L))
# faceted plots of cumulative simplicial complexes
ggplot(dg, aes(x, y)) +
coord_fixed() +
facet_wrap(facets = vars(n), labeller = label_both) +
stat_simplicial_complex(
diameter = prox, dimension_max = 2L,
engine = "simplextree",
fill = "darkgoldenrod"
) +
theme_bw() +
theme(legend.position = "none")
```
The Vietoris complexes on these subsets for the fixed proximity are not a filtration; instead they show us how increasing the sample affects the detection of homology at that threshold.
Notice that, while a cycle exists at $n = 36$, the "true" cycle is only detected at $n = 60$.
We can also conveniently plot the persistence diagrams from all four cumulative subsets, this time using a list-column of data sets passed to the `dataset` aesthetic:
```{r, fig.height=5}
# nested data frame of samples of different cumulative sizes
ds <- data.frame(n = ns, d = I(dl))
print(ds)
# faceted plot of persistence diagrams
ggplot(ds, aes(dataset = d)) +
coord_fixed() +
facet_wrap(facets = vars(n), labeller = label_both) +
stat_persistence(aes(colour = after_stat(factor(dimension)),
shape = after_stat(factor(dimension)))) +
geom_abline(slope = 1) +
labs(x = "Birth", y = "Death", color = "Dimension", shape = "Dimension") +
lims(x = c(0, max_prox), y = c(0, max_prox)) +
theme_persist()
```
The diagrams reveal that a certain sample is necessary to distinguish bona fide features from noise, as only occurs here at $n = 36$.
While the true feature retains about the same persistence (death value less birth value) from diagram to diagram, the persistence of the noise gradually lowers.
## Contribute
To contribute to **ggtda**, you can create issues for any bugs you find or any suggestions you have on the [issues page](https://github.com/tdaverse/ggtda/issues).
If you have a feature in mind you think will be useful for others, you can also [fork this repository](https://help.github.com/en/articles/fork-a-repo) and [create a pull request](https://help.github.com/en/articles/creating-a-pull-request).