########################################################################## # Installing packages. # The following checks if a package is installed. # The package is installed if it hasn't been installed before # If you have already installed these packages, you can skip these steps ########################################################################## If (!require(package = "TDAmapper")) { install.packages(pkgs = "TDAmapper") } If (!require(package = "fastcluster")) { install.packages(pkgs = "fastcluster") } If (!require(package = "igraph")) { install.packages(pkgs = "igraph") } ############################################################# # Load the libraries # A library only needs to be loaded once per session ############################################################# 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. require(fastcluster) # The igraph package is necessary to view simplicial complexes # (undirected graph) resulting from mapper1D(). library(igraph) ################################################## # We are now ready to run TDA mapper on some data ################################################## # Either create or load a dataset # You can change the DataSet to any dataset you choose # In other words, modify the command below to try TDAmapper # on different datasets. DataSet = cbind(runif(50, -1,1),runif(50, -1,1)) # Set the parameters needed for TDAmapper (listed below) distance_matrix = dist(DataSet) # Create distance matrix from DataSet filter <- DataSet[,1] # projection to first coordinate. # You can choose any filter function you like. # Filter function is a vector with same # number of entries as your dataset. # See below for some examples of filter functions. num_intervals <- 10 # Number of intervals. # For mapper1D, number of overlapping bins = num_intervals # For mapper2D, number of overlapping bins = num_intervals^2 percent_overlap <- 50 # The percent that these intervals overlap. num_bins_when_clustering <- 10 # A parameter for determining clusters. # We will discuss this parameter later. # Call the mapper1D to apply TDAmapper to dataset m1 <- mapper1D( distance_matrix, filter, num_intervals, percent_overlap, num_bins_when_clustering) # create and plot mapper graph g1 <- graph.adjacency(m1$adjacency, mode="undirected") plot(g1, layout = layout.auto(g1) ) title("Filter = projection to x-axis", sub =paste("num_intervals = ", num_intervals, "\n percent_overlap = ", percent_overlap, "\n num_bins_when_clustering =", num_bins_when_clustering) ) ########################################### # # Some other filter functions # f: Dataset to Real line # Thus x is a point in dataset, # and f(x) is a real number # ########################################### # f(x) = length of x filter <- sqrt(rowSums(DataSet^2)) # Project onto first principle component filter <- prcomp(DataSet, center = TRUE)