# install.packages("TDAmapper") library("TDAmapper") # The fastcluster package is not necessary. By loading the # fastcluster package, the fastcluster::hclust() function # automatically replaces the slower stats::hclust() function # whenever hclust() is called. install.packages("fastcluster") require(fastcluster) # The igraph package is necessary to view simplicial complexes # (undirected graph) resulting from mapper1D(). library(igraph) ## examples modified from https://cran.r-project.org/web/packages/TDAmapper/README.html ###################################################### ## Example 1: Using mapper1D to identify a figure 8 ## ###################################################### #plot 100 points from a figure 8 plot(x=2*cos(0.5*(1:100)), y=sin(1:100)) # Apply mapper to these 100 points m1 <- mapper1D( distance_matrix = dist(data.frame( x=2*cos(0.5*(1:100)), y=sin(1:100) )), filter_values = 2*cos(0.5*(1:100)), num_intervals = 10, percent_overlap = 50, num_bins_when_clustering = 10) # create and plot mapper graph g1 <- graph.adjacency(m1$adjacency, mode="undirected") plot(g1, layout = layout.auto(g1) ) ########################################################## ## Example 2: Using mapper2D to identify an oval as S^1 ## ########################################################## m2 <- mapper2D( distance_matrix = dist(data.frame( x=2*cos(0.5*(1:100)), y=sin(1:100) )), filter_values = list( 2*cos(1:100), sin(1:100) ), num_intervals = c(5,5), percent_overlap = 50, num_bins_when_clustering = 10) g2 <- graph.adjacency(m2$adjacency, mode="undirected") plot(g2, layout = layout.auto(g2) ) ######################################################################## ## Example 3: using mapper1D to identify two independent spirals as two line segments ############################################################### # sample 200 points from two intertwined spirals t <- runif(100, min=1, max=6.3) # theta X <- data.frame( x = c( t*cos(t), -t*cos(t) ), y = c( t*sin(t), -t*sin(t) ) ) d <- dist(X) plot(X[,1], X[,2]) # create variables used in mapper filter <- X[,2] # height projection num_intervals <- 10 percent_overlap <- 50 num_bins_when_clustering <- 10 # Apply mapper m3 <- mapper1D( distance_matrix = d, filter_values = filter, # num_intervals = 10, # use default # percent_overlap = 50, # use default # num_bins_when_clustering = 10 # use default ) # create and plot mapper graph g3 <- graph.adjacency(m3$adjacency, mode="undirected") plot(g3, layout = layout.auto(g3) ) ########################### ## Create more datasets ## ########################### # note the following is incorrect knotdata <- read.csv("TDA/MAPPER/trefoilknot.txt", header = FALSE) # note the following is the correct way to read in knot data knotdata <- read.csv("TDA/MAPPER/trefoilknot.txt", sep = " ", header = FALSE) # Grab first 2 columns for fun 2dim_data <- cbind(knotdata[1], knotdata[2]) # Create distance matrix d <- dist(knotdata) # Create filter filter <- knotdata[1] # run mapper m3 <- mapper1D( distance_matrix = d, filter_values = filter) # create and plot mapper graph g3 <- graph.adjacency(m3$adjacency, mode="undirected") plot(g3, layout = layout.auto(g3) ) #### more data #### footballdata <- read.csv("http://www.repole.com/sun4cast/stats/cfb20140906.csv") data <- cbind(footballdata$ScoreOff, footballdata$RushAttOff) plot(data) ######### WARNING ######### ## you should clean your data before analyzing it