-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
84 lines (65 loc) · 2.59 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
---
output:
md_document:
variant: gfm
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# pathway
[![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Travis-CI Build Status](https://travis-ci.org/mdlincoln/pathway.svg?branch=master)](https://travis-ci.org/mdlincoln/pathway)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/mdlincoln/pathway?branch=master&svg=true)](https://ci.appveyor.com/project/mdlincoln/pathway)
[![Coverage Status](https://img.shields.io/codecov/c/github/mdlincoln/pathway/master.svg)](https://codecov.io/github/mdlincoln/pathway?branch=master)
pathway finds a pathway of observations between two points in a matrix.
## Installation
You can install pathway from GitHub with:
```{r, eval=FALSE}
# install.packages("devtools")
devtools::install_github("mdlincoln/pathway")
```
## Example
```{r pathway}
library(pathway)
set.seed(34)
m <- matrix(rnorm(1000), nrow = 500, ncol = 2)
p1 <- 2L
p2 <- 11L
p <- pathway(m, p1, p2, n = 5)
# Returns both the ideal points between p1 and p2
p$line
# as well as the indices of the nearest neighbors
p$i
plot_pathway(m, p)
```
It is also possible to place conditional restraints on the solution that `pathway` finds by using `navigate_` functions.
For example, `navigate_unique` will not revisit the same point along a path, and `navigate_ordered` will only look at points that occurr in later rows in the matrix.
```{r naviagte}
p_ordered <- pathway(m, 5, 380, n = 5, navigator = navigate_ordered)
p_ordered$i
plot_pathway(m, p_ordered)
```
To use your own predicate function, define a function that returns a vector of indices to search and call it with `navigator = navigate(f)`
```{r custom_navigate}
obs_types <- sample(c("setosa", "versicolor", "virginica"), 500, replace = TRUE)
# A custom predicate function must take the original matrix, the list of
# previously-selected pathway points, along with p1 and p2.
different_species <- function(x, pi, p1, p2, obs_types) {
if (is.null(pi)) {
search_space <- 1:nrow(x)
} else {
# Only search observations that do not have the same species as the immediately previous one.
prev_type <- obs_types[tail(pi, 1)]
search_space <- which(obs_types != prev_type)
}
# Don't forget to exclude p1 and p2
setdiff(search_space, c(p1, p2))
}
p_species <- pathway(m, p1, p2, n = 8, navigator = navigate(different_species, obs_types))
obs_types[p_species$i]
```