library("TDA") # create data set containing 100 randomly choosen # points from a circle of radius r = 1 circle1 = circleUnif(100, r = 1) # save circle1 data to current working directory # as a text file write.table(circle1, "circleOUT.txt", sep=" ", row.names = FALSE, col.names = FALSE) # plot circle1 data with aspect ratio = 1 plot(circle1, asp=1) # save plot to current working directory # see http://stackoverflow.com/questions/7144118/how-to-save-a-plot-as-image-on-the-disk dev.copy(png,filename="plotcircle1.png"); dev.off (); ## Apply a linear transformation to circl1 data # create 2x2 matrix A <- matrix(c(1, 2, 2, 0), nrow=2, ncol=2) C <- t(circle1) # take the transpose of circle1 data M <- A %*% C # multiply A and C E <- eigen(A) # calculate eigenvalues and eigenvectors of A E # display E (note its data structure) plot(t(M), asp=1) # note M is the image of circle C under map A # Add an eigenvector to this plot (red asterisk) #points(c(0,E$values[1]*E$vectors[1,1]),c(0,E$values[1]*E$vectors[2,1])) arrows(0,0,E$values[1]*E$vectors[1,1],E$values[1]*E$vectors[2,1], col="red", lwd = 3) #points(c(0,E$values[2]*E$vectors[1,2]),c(0,E$values[2]*E$vectors[2,2])) arrows(0,0,E$values[2]*E$vectors[1,2],E$values[2]*E$vectors[2,2], col="blue", lwd = 3) plot(t(M), asp=1) # note M is the image of circle C under map A # Add an eigenvector to this plot (red asterisk) points(t(E$values[1]*E$vectors[,1]), pch=8, cex = 2, col=rgb(1, 0, 0)) # Add another eigenvector to this plot (blue asterisk) points(t(E$values[2]*E$vectors[,2]), pch=8, cex = 2, col=rgb(0, 0, 1)) # Add origin (0, 0) to plot (green +). points(t(c(0, 0)), pch=3, cex = 2, col=rgb(0, 1, 0)) # Create data for our next homework sphere <- sphereUnif(100, 2, r = 1) disk1 <- subset(sphere, select = -c(3) ) disk2 <- cbind(disk1[,1] + 4, disk1[,2]) disk3 <- cbind(disk1[,1], disk1[,2] + 4) disk4 <- disk1 + 4 noise1 <- cbind(runif(100, -1,5), runif(100, -1,5)) noisy_disk <- rbind(disk1, disk2, disk3, disk4, noise1) plot(noisy_disk, asp = 1) line <- cbind(runif(40, -.1,.1), runif(40, 1,3)) new <- rbind(noisy_disk, line) plot(new, asp = 1) ########################### ## Create more datasets ## ########################### # note the following is incorrect knotdata <- read.csv("trefoilknot.txt", header = FALSE) # note the following is the correct way to read in knot data knotdata <- read.csv("trefoilknot.txt", sep = " ", header = FALSE) # Grab first 2 columns for fun twodim_data <- cbind(knotdata[1], knotdata[2]) plot(twodim_data) # Create distance matrix d <- dist(knotdata) ########################################################################## ########################################################################### ## examples below 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)) ######################################################################## ## 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) ) ) plot(X[,1], X[,2])