compstatslib is a collection of interactive gadgets and
plotting functions for visualizing data sets and statistical concepts in
two and three dimensions.
Some of it works on your own data: explore any data frame as a rotatable 3D point cloud, or fit a moderated (interaction) regression and rotate its fitted surface to see how the interaction twists it away from a plane. The rest simulates a concept rather than plotting your data — sampling distributions, confidence intervals, t-statistics, matrix inversion — and is built for in-class demonstration, homework, and self-study.
Every interactive gadget prints the plot_*() call that
reproduces its final view, viewing angle included. Exploration in the
viewer pane becomes one line you can paste into a script, an Rmd, or a
figure-generating file.
The 3D visualizations are the part of the package meant to grow
beyond the classroom, toward figures good enough for textbooks and
manuscripts. They are not there yet — see docs/future-work.md
for the specific gaps.
Three kinds of function are provided:
They are grouped below by what they are for, since that varies more than the interaction style does.
These accept arbitrary data frames and model formulas, with control over axes, color mapping, aspect ratio, and viewing angle.
interactive_scatter3d() Interactive Shiny gadget for
exploring three numeric columns of a data frame as a rotatable 3D point
cloud. Column pickers swap x / y / z (and an optional color mapping) at
runtime; aspect, opacity, and marker-size sliders tune the view.
Rotation and zoom persist across slider/picker changes within the
gadget. On Done, prints a reproducible plot_scatter3d(...)
call to the console — including the captured camera position — so the
exact rotation and zoom can be pasted into an Rmd or script.plot_scatter3d() Non-interactive counterpart that
returns a plotly htmlwidget for a 3D scatterplot of three
numeric columns. Supports optional color mapping (numeric → continuous
scale; factor / character → discrete palette), aspect-ratio control,
marker opacity / size, custom axis titles, and an explicit
camera argument for reproducing a specific view captured
from the gadget.interactive_moderation_3d() Interactive Shiny gadget
that fits a moderated regression and renders the fitted surface as a
rotatable 3D wireframe. Two sliders control the viewing angle, so you
can see how an interaction term twists the surface relative to an
additive (planar) model.plot_moderation_3d() Non-interactive counterpart that
returns a lattice::wireframe trellis object for the
moderation surface. Accepts any model formula (y ~ x * z,
y ~ x + z, or larger models with extra controls — pass
iv and mod to choose which two predictors are
plotted; the rest are held at typical values).moderation_data Bundled synthetic dataset used as the
default example for the two functions above; calibrated to make the
interaction effect visually obvious. Includes an unrelated noise
variable w for demonstrating multi-predictor formulas.These plot a dataframe of x / y points that
you supply, together with a fitted model. They are sized for small data
— points you click in by hand or a modest dataframe — rather than for
arbitrary data: plot_regression() draws in a fixed −5 to 50
window, and plot_pca() expects exactly two columns named
x and y.
interactive_regression() Interactive visualization
function that lets you point-and-click to add data points, while it
automatically plots and updates a regression line and associated
statistics.plot_regression() Plotting function that takes a
dataframe of points (x, y) and plots them with a regression line and
associated statistics.interactive_logit() Interactive visualization function
that lets you point-and-click to add data points, while it automatically
plots and updates a logistic regression line and associated
statistics.plot_logit() Plotting function that takes a dataframe
of points (x, y) and plots them with a logistic regression curve and
associated statistics. The x-axis range adapts to the data you
pass.interactive_pca() Interactive visualization function
that lets you point-and-click to add data points, while it automatically
plots and updates principal component vectors.plot_pca() Plotting function that takes a dataframe of
points (x, y) and plots them with their principal component vectors.
Supports optional mean-centering.These do not plot your data. They simulate a process, or draw a geometric object, so that a concept can be watched rather than described.
interactive_t_test() Interactive visualization function
that will show you a simulation of null and alternative distributions of
the t-statistic. You will be able to play with the different parameters
that affect hypothesis tests in order to see how their variation
influences the null t and alternative t distributions, as well as
statistical power.plot_t_test() Non-interactive visualization that plots
null and alternative t distributions of a t-test. Shows the rejection
zone and statistical power as shaded areas under the curves. Accepts
parameters for the test difference, standard deviation, sample size,
significance level, and an optional type I/II error matrix overlay.interactive_sampling() Interactive sampling simulation
that will sample given population data to show how a sampling statistic
is distributed across repetitions of sampling exercise.plot_sampling() Plotting function that shows the
distribution of a population alongside samples drawn from it and the
distribution of a given sampling statistic (e.g., mean or median).plot_sample_ci() Simulated visualization of samples
drawn from a given population function, with each sample’s confidence
intervals displayed.interactive_matrix_inverse() Interactive function that
allows one to manipulate a matrix inversion.plot_matrix_inverse() Plotting function that visualizes
a matrix and its inverse as vector pairs, showing their geometric
relationship.machine_precision() Code function that shows how to
find the smallest number your computer can effectively representEvery interactive_*() gadget hands its final state back
when you click Done, and prints the
plot_*() call that reproduces what was on screen. Assign
the result and you can either paste that call into a script or feed the
object straight back:
result <- interactive_moderation_3d()
#> plot_moderation_3d(formula = y ~ x * z, data = moderation_data, z_rot = 125)
do.call(plot_moderation_3d, result) # same surface, same viewing angleGadgets whose state is a set of points return a dataframe you can use
as one (nrow(), [, passing it to the plot
function). Gadgets whose state is a set of settings return a plain named
list suitable for do.call(). Derived results a user would
not retype — the prcomp() fit from
interactive_pca(), the accumulated draws from
interactive_sampling() — ride along as attributes.
You can install the current development version from GitHub using the devtools
package:
# install.packages("devtools")
devtools::install_github("compstatslib/compstatslib")Feel free to send open issues or send pull requests. Happy hacking!
compstatslib is maintained by Soumya Ray.
Daniele Melotti is a co-author of the package. Several of the plotting and interactive functions grew out of work he did as a student under Soumya Ray’s supervision, and were then folded back into the package.