| Title: | Optimal Histogram Binning Using Shimazaki-Shinomoto Method |
|---|---|
| Description: | Implements the Shimazaki-Shinomoto method for optimizing the bin width of a histogram. This method minimizes the mean integrated squared error (MISE) and features a 'C++' backend for high performance and shift-averaging to remove edge-position bias. Ideally suits for time-dependent rate estimation and identifying intrinsic data structures. Supports both 1D and 2D data distributions. For more details see Shimazaki and Shinomoto (2007) "A Method for Selecting the Bin Size of a Time Histogram" <doi:10.1162/neco.2007.19.6.1503>. |
| Authors: | Daniil Popov [aut, cre] |
| Maintainer: | Daniil Popov <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.3 |
| Built: | 2026-05-16 09:23:10 UTC |
| Source: | https://github.com/celebithil/sshist |
Plot method for sshist objects
## S3 method for class 'sshist' plot(x, ...)## S3 method for class 'sshist' plot(x, ...)
x |
An object of class sshist. |
... |
Additional arguments passed to plot. |
No return value, called for side effects. Creates a two-panel plot showing: (1) the cost function curve with the optimal number of bins highlighted, and (2) the optimal histogram with the selected bin edges.
Plot method for sshist_2d objects
## S3 method for class 'sshist_2d' plot(x, ...)## S3 method for class 'sshist_2d' plot(x, ...)
x |
An object of class sshist. |
... |
Additional arguments passed to plot. |
No return value, called for side effects. Creates a two-panel plot showing: (1) a heatmap of the cost function landscape across different bin combinations with the optimal point highlighted in red, and (2) the optimal 2D histogram visualization using the selected bin numbers for both dimensions.
Print method for sshist objects
## S3 method for class 'sshist' print(x, ...)## S3 method for class 'sshist' print(x, ...)
x |
An object of class sshist. |
... |
Additional arguments passed to print. |
Returns the input object x invisibly. The method is called for its
side effect of printing a summary of the Shimazaki-Shinomoto histogram
optimization results, including the optimal number of bins, bin width, and
minimum cost value.
Print method for sshist_2d objects
## S3 method for class 'sshist_2d' print(x, ...)## S3 method for class 'sshist_2d' print(x, ...)
x |
An object of class sshist. |
... |
Additional arguments passed to print. |
Returns the input object x invisibly. The method is called for its
side effect of printing a summary of the 2D Shimazaki-Shinomoto histogram
optimization results, including the optimal number of bins for both X and Y
dimensions, bin widths, and minimum cost value.
Computes the optimal bin width and number of bins using the Shimazaki-Shinomoto method. This implementation uses Rcpp for high performance and includes shift-averaging to remove edge-position bias.
sshist(x, n_max = NULL, sn = 30)sshist(x, n_max = NULL, sn = 30)
x |
A numeric vector of data. Missing values (NA) will be removed. |
n_max |
An integer specifying the maximum number of bins to test.
If |
sn |
Integer. The number of shifts to use for averaging (default is 30). |
An object of class "sshist" containing:
The optimal number of bins.
The optimal bin width.
The sequence of break points for the optimal histogram.
A numeric vector of the cost function values.
The vector of N values tested.
The original data (cleaned).
Shimazaki, H. and Shinomoto, S., 2007. A method for selecting the bin size of a time histogram. Neural Computation, 19(6), pp.1503-1527.
# 1. Basic usage with base graphics data(faithful) res <- sshist(faithful$waiting) plot(res) # 2. Usage with ggplot2 if (requireNamespace("ggplot2", quietly = TRUE)) { library(ggplot2) df <- data.frame(waiting = faithful$waiting) ggplot(df, aes(x = waiting)) + geom_histogram(breaks = res$edges, fill = "lightblue", color = "black") + ggtitle(paste("Optimal Shimazaki-Shinomoto Bins (N =", res$opt_n, ")")) + theme_minimal() }# 1. Basic usage with base graphics data(faithful) res <- sshist(faithful$waiting) plot(res) # 2. Usage with ggplot2 if (requireNamespace("ggplot2", quietly = TRUE)) { library(ggplot2) df <- data.frame(waiting = faithful$waiting) ggplot(df, aes(x = waiting)) + geom_histogram(breaks = res$edges, fill = "lightblue", color = "black") + ggtitle(paste("Optimal Shimazaki-Shinomoto Bins (N =", res$opt_n, ")")) + theme_minimal() }
Computes the optimal number of bins for a 2-dimensional histogram. Includes checks for data resolution and biased variance calculation.
sshist_2d(x, y = NULL, n_min = 2, n_max = 200)sshist_2d(x, y = NULL, n_min = 2, n_max = 200)
x |
Numeric vector (coord X) or a 2-column matrix. |
y |
Numeric vector (coord Y). Optional if x is a matrix. |
n_min |
Integer. Minimum number of bins (default 2). |
n_max |
Integer. Maximum number of bins (default 200). Algorithm will reduce this automatically if data resolution restricts it. |
An object of class "sshist_2d" containing optimal parameters.
# 1. Basic usage with base graphics set.seed(42) x <- rnorm(500) y <- rnorm(500) res <- sshist_2d(x, y) plot(res) # 2. Usage with ggplot2 if (requireNamespace("ggplot2", quietly = TRUE)) { library(ggplot2) df <- data.frame(x = x, y = y) ggplot(df, aes(x = x, y = y)) + geom_bin2d(bins = c(res$opt_nx, res$opt_ny)) + scale_fill_viridis_c() + ggtitle(paste0("Optimal 2D Bins: ", res$opt_nx, "x", res$opt_ny)) + theme_minimal() }# 1. Basic usage with base graphics set.seed(42) x <- rnorm(500) y <- rnorm(500) res <- sshist_2d(x, y) plot(res) # 2. Usage with ggplot2 if (requireNamespace("ggplot2", quietly = TRUE)) { library(ggplot2) df <- data.frame(x = x, y = y) ggplot(df, aes(x = x, y = y)) + geom_bin2d(bins = c(res$opt_nx, res$opt_ny)) + scale_fill_viridis_c() + ggtitle(paste0("Optimal 2D Bins: ", res$opt_nx, "x", res$opt_ny)) + theme_minimal() }