Design Principles for segue
This document describes the motivation and design goals of segue. It’s not mandatory reading but should help users understand our goals and motivations.
Why Segue
segue is not a technical marvel; it is repackaging existing tools in R and mostly focuses on non-technical aspects of software:
- discoverability: make it easier to find what you need.
-
understandable: make documentation and error messages helpful and informative.
- compatibility: how does it work relative to more familiar tools.
- trustworthy: explicitly describe how the code is validated.
The package name implies that we are trying to get users somewhere they are not. Our motivation is to help users who are transitioning from SAS to R (perhaps not by choice). Moving between the two systems can be jarring, and segue can help someone getting started in R. While the package emphasizes closely reproducing previous SAS work, it is intended as a gateway to R.
Make R functions discoverable
R code lives either in the base R packages (e.g., stats, tools, etc.) or in contributed packages. The latter contains the vast majority of the code that you will interface with. The problem with both sources is that their interfaces are often unpredictable, inconsistent, and/or opaque. For example, as a graduate student new to the language, I looked for a matrix inversion function (maybe called inv()). This does not exist, but the function to solve a system of equations (solve()) can do it.
R is based on the S language, and R and S have mostly the same syntax. S was first developed in the 1970s, and a lot has changed since then. segue takes the stance that most users work with data frames, so our interfaces use these instead of raw vectors. We also have more capacity for naming functions than in the previous century, so segue tries to be explicit. For example, it is difficult to know that the difference in test statistics produced by binom.test() and prop.test() from their names. We also optimize for tab-completion.
R packages, on average, are not much better. For example, the code to sort a data frame by a variable (descending order) is very different:
# Base R
data_sorted <- data[, order(-data$x), drop = FALSE]
# data.table package
data_sorted <- setorder[data, -x]
# dplyr package
data_sorted <- arrange(data, desc(x))Packages also differ in implementation when it comes to statistical choices. For example, some hypothesis tests use continuity corrections by default, and others do not.
Coming from SAS, this can be perplexing. SAS has the advantage of a single, monolithic way to compute models and statistics, so you never really have the option to use a different test configuration.
Better R interfaces
Outside of images and text data, almost all data is tabular; rows are data instances or samples, and columns are variables. Most modern tools use a syntax centered on the general concept of a data frame. segue is centered around data frames.
Base R can assume that the data interface should be vectors or summary statistics. For example, tests for a proportion have the interface:
binom.test(number_of_events, total_samples)which is not helpful if your data are in columns of a data frame. Another example is that the function that does a Chi-squared test of association has the syntax:
# Abbreviated syntax:
chisq.test(x, y = NULL, ...)and the help file states:
-
xcan be “a numeric vector or matrix.xandycan also both be factors.” (it can also be a table object). -
yis “a numeric vector; ignored ifxis a matrix.”
That is helpful but barely actionable.
The t-test function is slightly better since it has multiple interfaces:
However, all interfaces preclude running these computations many times, conditional on some “by” variable.
Interfaces for data analysis software are discussed in the “Why Tidiness is Important for Modeling” in the Kuhn and Silge (2022). That references the idea of designing for humans described in Wickham et al. (2019). These philosophical aspects of software design also inform segue’s design.
Avoid “Right” and “Wrong”
R and SAS can differ a lot in how they do computations. Some of these differences matter in a meaningful way; others don’t.
Our principle for segue is to
- Don’t view any implementation as the gold standard.
- Accommodate both approaches whenever possible but choose a sensible default.
- Outline the differences in defaults and optional arguments.
- Investigate apples-to-apples numerical comparisons to give users a sense of how substantial any differences in results are.