From 0e870401f2c80503c4e9deeae7531fb76e771c20 Mon Sep 17 00:00:00 2001 From: Daniele Melotti Date: Mon, 5 Dec 2022 23:30:55 +0800 Subject: [PATCH 1/6] Created new script titled interactive_similarity.R --- R/interactive_similarity.R | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 R/interactive_similarity.R diff --git a/R/interactive_similarity.R b/R/interactive_similarity.R new file mode 100644 index 0000000..e69de29 From ea89543fd6f3409f9c8a5e6204067f52d05627e6 Mon Sep 17 00:00:00 2001 From: Daniele Melotti Date: Mon, 5 Dec 2022 23:42:47 +0800 Subject: [PATCH 2/6] Created new script titled similarity_plot.R --- R/similarity_interactive.R | 14 ++++++++++++++ R/{interactive_similarity.R => similarity_plot.R} | 0 2 files changed, 14 insertions(+) create mode 100644 R/similarity_interactive.R rename R/{interactive_similarity.R => similarity_plot.R} (100%) diff --git a/R/similarity_interactive.R b/R/similarity_interactive.R new file mode 100644 index 0000000..33ac81e --- /dev/null +++ b/R/similarity_interactive.R @@ -0,0 +1,14 @@ +interactive_similarity <- function() { + + # Step 1: insert points + + + # Step 2: interactive visualization + manipulate::manipulate( + plot_t_test(diff, sd, n, alpha), + diff = manipulate::slider(0, 4, step = 0.1, initial = 0.5), + sd = manipulate::slider(1, 5, step = 0.1, initial = 4), + n = manipulate::slider(2, 500, step = 1, initial = 100), + alpha = manipulate::slider(0.01, 0.1, step = 0.01, initial = 0.05) + ) +} \ No newline at end of file diff --git a/R/interactive_similarity.R b/R/similarity_plot.R similarity index 100% rename from R/interactive_similarity.R rename to R/similarity_plot.R From 5ce532cd2d4e55e2b7d54a0b7ca346abe9123d13 Mon Sep 17 00:00:00 2001 From: Daniele Melotti Date: Tue, 6 Dec 2022 09:08:55 +0800 Subject: [PATCH 3/6] minor fixes --- R/similarity_interactive.R | 17 ++++++++++++++++- R/similarity_plot.R | 10 ++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/R/similarity_interactive.R b/R/similarity_interactive.R index 33ac81e..f4be241 100644 --- a/R/similarity_interactive.R +++ b/R/similarity_interactive.R @@ -1,7 +1,22 @@ interactive_similarity <- function() { # Step 1: insert points - + function(points=data.frame(), ...) { + cat("Click on the plot to create data points; hit [esc] to stop") + repeat { + plot_regr(points, ...) + + click_loc <- locator(1) + if (is.null(click_loc)) break + + if(nrow(points) == 0 ) { + points <- data.frame(x=click_loc$x, y=click_loc$y) + } else { + points <- rbind(points, c(click_loc$x, click_loc$y)) + } + } + + return(points) # Step 2: interactive visualization manipulate::manipulate( diff --git a/R/similarity_plot.R b/R/similarity_plot.R index e69de29..752ae46 100644 --- a/R/similarity_plot.R +++ b/R/similarity_plot.R @@ -0,0 +1,10 @@ +plot_sim <- function(points) { + max_x <- 50 + if (nrow(points) == 0) { + plot(NA, xlim=c(-5,max_x), ylim=c(-5,max_x), xlab="x", ylab="y") + return() + } + plot(points, xlim=c(-5,max_x), ylim=c(-5,max_x), pch=19, cex=2, col="gray") + if (nrow(points) < 2) return() + +} \ No newline at end of file From 63c2a7c40327ae8ab049b093b73c1547a1e544d1 Mon Sep 17 00:00:00 2001 From: Daniele Melotti Date: Mon, 12 Dec 2022 21:35:30 +0800 Subject: [PATCH 4/6] Added interactive_similarity() and plot_sim() to package, created empty docs --- NAMESPACE | 2 ++ R/similarity_interactive.R | 24 +++++++++--------------- R/similarity_plot.R | 10 +++++++++- man/interactive_similarity.Rd | 11 +++++++++++ man/plot_sim.Rd | 14 ++++++++++++++ 5 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 man/interactive_similarity.Rd create mode 100644 man/plot_sim.Rd diff --git a/NAMESPACE b/NAMESPACE index 0f0a877..85c8600 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,10 +5,12 @@ export(interactive_matrix_inverse) export(interactive_pca) export(interactive_regression) export(interactive_sampling) +export(interactive_similarity) export(interactive_t_test) export(machine_precision) export(plot_logit) export(plot_matrix_inverse) export(plot_regr) export(plot_sampling) +export(plot_sim) export(plot_t_test) diff --git a/R/similarity_interactive.R b/R/similarity_interactive.R index f4be241..7508487 100644 --- a/R/similarity_interactive.R +++ b/R/similarity_interactive.R @@ -1,10 +1,13 @@ -interactive_similarity <- function() { - - # Step 1: insert points - function(points=data.frame(), ...) { +#' compstatslib interactive_similarity() function +#' +#' @return +#' @export +#' +#' @examples +interactive_similarity <- function(points=data.frame(), ...) { cat("Click on the plot to create data points; hit [esc] to stop") repeat { - plot_regr(points, ...) + plot_sim(points, ...) click_loc <- locator(1) if (is.null(click_loc)) break @@ -17,13 +20,4 @@ interactive_similarity <- function() { } return(points) - - # Step 2: interactive visualization - manipulate::manipulate( - plot_t_test(diff, sd, n, alpha), - diff = manipulate::slider(0, 4, step = 0.1, initial = 0.5), - sd = manipulate::slider(1, 5, step = 0.1, initial = 4), - n = manipulate::slider(2, 500, step = 1, initial = 100), - alpha = manipulate::slider(0.01, 0.1, step = 0.01, initial = 0.05) - ) -} \ No newline at end of file +} diff --git a/R/similarity_plot.R b/R/similarity_plot.R index 752ae46..ff88dc9 100644 --- a/R/similarity_plot.R +++ b/R/similarity_plot.R @@ -1,3 +1,11 @@ +#' compstatslib plot_sim() function +#' +#' @param points +#' +#' @return +#' @export +#' +#' @examples plot_sim <- function(points) { max_x <- 50 if (nrow(points) == 0) { @@ -7,4 +15,4 @@ plot_sim <- function(points) { plot(points, xlim=c(-5,max_x), ylim=c(-5,max_x), pch=19, cex=2, col="gray") if (nrow(points) < 2) return() -} \ No newline at end of file +} diff --git a/man/interactive_similarity.Rd b/man/interactive_similarity.Rd new file mode 100644 index 0000000..79ad6ab --- /dev/null +++ b/man/interactive_similarity.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/similarity_interactive.R +\name{interactive_similarity} +\alias{interactive_similarity} +\title{compstatslib interactive_similarity() function} +\usage{ +interactive_similarity(points = data.frame(), ...) +} +\description{ +compstatslib interactive_similarity() function +} diff --git a/man/plot_sim.Rd b/man/plot_sim.Rd new file mode 100644 index 0000000..7ad9b83 --- /dev/null +++ b/man/plot_sim.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/similarity_plot.R +\name{plot_sim} +\alias{plot_sim} +\title{compstatslib plot_sim() function} +\usage{ +plot_sim(points) +} +\arguments{ +\item{points}{} +} +\description{ +compstatslib plot_sim() function +} From 4eab25e8b615fbc62f5e1934ad855d249d530aaf Mon Sep 17 00:00:00 2001 From: Daniele Melotti Date: Mon, 12 Dec 2022 22:18:22 +0800 Subject: [PATCH 5/6] Added arrows to plot --- R/similarity_interactive.R | 1 + R/similarity_plot.R | 2 ++ 2 files changed, 3 insertions(+) diff --git a/R/similarity_interactive.R b/R/similarity_interactive.R index 7508487..2cd8fd8 100644 --- a/R/similarity_interactive.R +++ b/R/similarity_interactive.R @@ -20,4 +20,5 @@ interactive_similarity <- function(points=data.frame(), ...) { } return(points) + } diff --git a/R/similarity_plot.R b/R/similarity_plot.R index ff88dc9..60ee51d 100644 --- a/R/similarity_plot.R +++ b/R/similarity_plot.R @@ -13,6 +13,8 @@ plot_sim <- function(points) { return() } plot(points, xlim=c(-5,max_x), ylim=c(-5,max_x), pch=19, cex=2, col="gray") + arrows(x0 = 0, y0 = 0, x1 = points[, 1], y1 = points[, 2], col = "cornflowerblue") + if (nrow(points) < 2) return() } From a43cad531755902960bdb003493a4dffe02fafd8 Mon Sep 17 00:00:00 2001 From: Daniele Melotti Date: Tue, 13 Dec 2022 00:55:43 +0800 Subject: [PATCH 6/6] Latest changes --- R/similarity_plot.R | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/R/similarity_plot.R b/R/similarity_plot.R index 60ee51d..1dc1a82 100644 --- a/R/similarity_plot.R +++ b/R/similarity_plot.R @@ -7,14 +7,31 @@ #' #' @examples plot_sim <- function(points) { - max_x <- 50 + max_x <- 5 if (nrow(points) == 0) { - plot(NA, xlim=c(-5,max_x), ylim=c(-5,max_x), xlab="x", ylab="y") + plot(NA, xlim=c(0,max_x), ylim=c(0,max_x), xlab="ac1", ylab="ac2") return() } - plot(points, xlim=c(-5,max_x), ylim=c(-5,max_x), pch=19, cex=2, col="gray") + plot(points, xlim=c(0,max_x), ylim=c(0,max_x), pch=19, cex=2, col="gray") arrows(x0 = 0, y0 = 0, x1 = points[, 1], y1 = points[, 2], col = "cornflowerblue") - + if (nrow(points) < 2) return() + data_matrix <- as.matrix(points) + + recommend(data_matrix) +} + +recommend <- function(data_matrix) { + cosines <- cosine(data_matrix) + + data_transpose <- t(data_matrix) + + data_means <- apply(data_transpose, 2, mean) + data_means_matrix <- t(replicate(nrow(data_matrix), data_means)) + data_means_matrix_mc <- data_transpose - data_means_matrix + + cors <- cosine(data_means_matrix_mc) + + c(cosines, cors) }