| Title: | Linear Splines to Generate Decision Boundaries |
|---|---|
| Description: | Create two dimensional datasets with decision boundaries set by linear splines. An HTML widget enables users to draw the splines on a web page and generate a JSON file that can be used to generate datasets. |
| Authors: | Janith Wanniarachchi [aut, cre, cph] |
| Maintainer: | Janith Wanniarachchi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.1 |
| Built: | 2026-09-23 08:59:07 UTC |
| Source: | https://github.com/janithwanni/sillysplines |
This function generates samples from a uniform distribution to be used as noise variables. This can be used to assess the effect of high-dimensions on a model fit, and explainers.
add_noise_vars(data, n_vars = 2)add_noise_vars(data, n_vars = 2)
data |
A matrix or data frame with three columns (x, y, class). |
n_vars |
Integer; number of additional variables to generate. |
A data frame with three+n_vars columns, like:
Random uniform coordinate
Random uniform coordinate
From the original data x
From the original data y
Binary class label ("Above" or "Below")
coords <- matrix(c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7), ncol = 2, byrow = TRUE) df <- create_data(coord = coords, n_samples = 500, seed = 717) df <- add_noise_vars(df, 2) head(df)coords <- matrix(c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7), ncol = 2, byrow = TRUE) df <- create_data(coord = coords, n_samples = 500, seed = 717) df <- add_noise_vars(df, 2) head(df)
Classifies data points as being above or below a boundary defined by a set of coordinates. The function uses piecewise linear interpolation to determine the boundary's y-value at each x-coordinate in the data, then classifies points based on whether they lie above or below this boundary.
classify_boundary(data, boundary_coord, classes = c("Above", "Below"))classify_boundary(data, boundary_coord, classes = c("Above", "Below"))
data |
A data frame containing at least two columns named 'x' and 'y' representing the coordinates of points to be classified. |
boundary_coord |
A two-column matrix or data frame where the first column contains x-coordinates and the second column contains y-coordinates defining the boundary line. Points should be ordered by x-coordinate for proper interpolation. |
classes |
A character vector of length 2 specifying the class labels.
Default is |
The function uses stats::approxfun() with rule = 2 for piecewise linear
interpolation. This means:
Between boundary points, linear interpolation is used
For x-values outside the range of boundary coordinates, the boundary is extrapolated linearly using the slope at the nearest endpoint
Points exactly on the boundary (y == boundary_y) are classified as "Below" (the second class).
A character vector of the same length as the number of rows in
data, containing class labels for each point.
approxfun for details on interpolation
# Create sample data data <- data.frame( x = c(1, 2, 3, 4, 5), y = c(2, 4, 3, 5, 1) ) # Define a simple linear boundary boundary <- data.frame( x = c(1, 5), y = c(2, 4) ) # Classify points classify_boundary(data, boundary) # Use custom class labels classify_boundary(data, boundary, classes = c("High", "Low")) # Complex boundary with multiple segments boundary_complex <- data.frame( x = c(1, 2, 3, 4, 5), y = c(1, 3, 2, 4, 3) ) classify_boundary(data, boundary_complex)# Create sample data data <- data.frame( x = c(1, 2, 3, 4, 5), y = c(2, 4, 3, 5, 1) ) # Define a simple linear boundary boundary <- data.frame( x = c(1, 5), y = c(2, 4) ) # Classify points classify_boundary(data, boundary) # Use custom class labels classify_boundary(data, boundary, classes = c("High", "Low")) # Complex boundary with multiple segments boundary_complex <- data.frame( x = c(1, 2, 3, 4, 5), y = c(1, 3, 2, 4, 3) ) classify_boundary(data, boundary_complex)
Generates a synthetic two-dimensional dataset using random uniform values. Each point is assigned a class label based on whether it lies above all coordinate thresholds provided. Coordinates can be supplied directly as a matrix/data frame or indirectly via a JSON file containing an array of coordinate pairs.
create_data( coord_filepath = NULL, coord = NULL, n_samples = 10000, type = "uniform", seed = 1835, gap = 0, r = 0 )create_data( coord_filepath = NULL, coord = NULL, n_samples = 10000, type = "uniform", seed = 1835, gap = 0, r = 0 )
coord_filepath |
A character string specifying a file path to a JSON
file containing a list/array of coordinate pairs. Optional if |
coord |
A matrix or data frame with two columns (x and y) specifying
threshold coordinates. Optional if |
n_samples |
Integer; number of synthetic 2D points to generate. |
type |
String; How should the points be generated. Can be either 'uniform' (default), 'normal', or 'grid' |
seed |
Integer; Random seed to be used for generating dataset. |
gap |
Numeric; Value close to 0 defining gap at the boundary, less than 0.2 |
r |
Numeric; Correlation for generating normal sample, between (-0.9, 0.9), default 0. |
A point is labelled class = 1 if it is above all coordinate
thresholds such that:
Otherwise, the point is labelled 0.
A data frame with three columns:
Random uniform x-coordinate
Random uniform y-coordinate
Binary class label ("Above" or "Below")
coords <- matrix(c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7), ncol = 2, byrow = TRUE) df <- create_data(coord = coords, n_samples = 500, seed = 717) head(df)coords <- matrix(c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7), ncol = 2, byrow = TRUE) df <- create_data(coord = coords, n_samples = 500, seed = 717) head(df)
This creates a simple htmlwidget to be used
sillysplines(width = 640, height = 640, elementId = "app")sillysplines(width = 640, height = 640, elementId = "app")
width |
width of container in pixels |
height |
height of container in pixels |
elementId |
The id to be used for the container element |
Returns an htmlWidget to be used within an HTML document
sillysplines()sillysplines()
Output and render functions for using sillysplines within Shiny applications and interactive Rmd documents.
sillysplinesOutput(outputId, width = "100%", height = "400px") renderSillysplines(expr, env = parent.frame(), quoted = FALSE)sillysplinesOutput(outputId, width = "100%", height = "400px") renderSillysplines(expr, env = parent.frame(), quoted = FALSE)
outputId |
output variable to read from |
width, height
|
Must be a valid CSS unit (like |
expr |
An expression that generates an HTML widget (or a promise of an HTML widget). |
env |
The environment in which to evaluate |
quoted |
Is @return Returns a |
Returns an htmlwidgets::shinyWidgetOutput to be used within the UI
of a shiny app.
library(shiny) app <- shinyApp( ui = fluidPage(sillysplinesOutput('splines')), server = function(input, output) { splines_widget <- sillysplines() output$splines= renderSillysplines(splines_widget) } ) if (interactive()) applibrary(shiny) app <- shinyApp( ui = fluidPage(sillysplinesOutput('splines')), server = function(input, output) { splines_widget <- sillysplines() output$splines= renderSillysplines(splines_widget) } ) if (interactive()) app
This function removes points outside of a elliptical region
trim_shape(data, r = 0.6)trim_shape(data, r = 0.6)
data |
A matrix or data frame with three columns (x, y, class). |
r |
Numeric; Correlation defining the shape, between -1, 1; default 0.6 |
A data frame with three+n_vars columns, like:
Random uniform coordinate
Random uniform coordinate
Binary class label ("Above" or "Below")
coords <- matrix(c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7), ncol = 2, byrow = TRUE) df <- create_data(coord = coords, n_samples = 500, seed = 717) df <- trim_shape(df) plot(df$x, df$y, col=ifelse(df$class == "Above", "red", "yellow"))coords <- matrix(c(0.2, 0.3, 0.4, 0.5, 0.6, 0.7), ncol = 2, byrow = TRUE) df <- create_data(coord = coords, n_samples = 500, seed = 717) df <- trim_shape(df) plot(df$x, df$y, col=ifelse(df$class == "Above", "red", "yellow"))