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 new file mode 100644 index 0000000..2cd8fd8 --- /dev/null +++ b/R/similarity_interactive.R @@ -0,0 +1,24 @@ +#' 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_sim(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) + +} diff --git a/R/similarity_plot.R b/R/similarity_plot.R new file mode 100644 index 0000000..1dc1a82 --- /dev/null +++ b/R/similarity_plot.R @@ -0,0 +1,37 @@ +#' compstatslib plot_sim() function +#' +#' @param points +#' +#' @return +#' @export +#' +#' @examples +plot_sim <- function(points) { + max_x <- 5 + if (nrow(points) == 0) { + plot(NA, xlim=c(0,max_x), ylim=c(0,max_x), xlab="ac1", ylab="ac2") + return() + } + 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) +} 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 +}