From b04972d307f28d11e23303f631765d2de8d42c4e Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Fri, 4 Dec 2026 13:51:08 -0500 Subject: [PATCH] various updated and refactors --- README.md | 4 +- _coverage/build.R | 2 +- _quarto.yml | 10 +-- comparisons/bin-prop-test.qmd | 13 ++-- comparisons/numeric-rank-test.qmd | 12 +-- coverage.qmd | 91 ----------------------- index.qmd | 87 ++-------------------- methodology.qmd | 37 ---------- testing.qmd | 118 ++++++++++++++++++++++++++++++ 9 files changed, 146 insertions(+), 228 deletions(-) delete mode 100644 coverage.qmd delete mode 100644 methodology.qmd create mode 100644 testing.qmd diff --git a/README.md b/README.md index b34816b..7122fb5 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ When segue gains a new exported function, add its `.qmd` file to a section under ## Test coverage -`coverage.qmd` shows segue's test coverage, overall and per file, with a [covr](https://covr.r-lib.org/) report for each line. The numbers are computed here, not read from Codecov. You need covr 4.0.0 or later. +`testing.qmd` shows segue's test coverage, overall and per file, with a [covr](https://covr.r-lib.org/) report for each line. The numbers are computed here, not read from Codecov. You need covr 4.0.0 or later. Then, from this folder, pass a branch, tag, or commit SHA of `posit-dev/segue` (the default is `main`): @@ -94,7 +94,7 @@ The script exports that commit from `_packages/segue` into a temporary folder (s | `assets/coverage-badge.svg` | Badge for the whole package | | `assets/coverage/.svg` | One badge per file in segue's `R/` folder, e.g. `assets/coverage/sas_round.svg` | -Then render `coverage.qmd` again. A badge can be used on any page, e.g. `![](../assets/coverage/sas_round.svg)` on a comparison page. +Then render `testing.qmd` again. A badge can be used on any page, e.g. `![](../assets/coverage/sas_round.svg)` on a comparison page. ## Links to function documentation diff --git a/_coverage/build.R b/_coverage/build.R index 1557c90..8e30459 100644 --- a/_coverage/build.R +++ b/_coverage/build.R @@ -1,4 +1,4 @@ -# Computes the test coverage of segue and saves it for coverage.qmd. +# Computes the test coverage of segue and saves it for testing.qmd. # # Run from the root of this project: # diff --git a/_quarto.yml b/_quarto.yml index 43976a9..2120386 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -57,12 +57,10 @@ website: href: reference/index.qmd - text: "Comparisons" href: comparisons/index.qmd - - text: "Methodology" - href: methodology.qmd - text: "Design" - href: design.qmd - - text: "Test Coverage" - href: coverage.qmd + href: design.qmd + - text: "Software Validity Testing" + href: testing.qmd right: - text: "segue on GitHub" href: https://github.com/posit-dev/segue @@ -134,4 +132,4 @@ format: fig-dpi: 160 bibliography: segue.bib -csl: chicago-author-date-inline.csl \ No newline at end of file +csl: chicago-author-date-inline.csl diff --git a/comparisons/bin-prop-test.qmd b/comparisons/bin-prop-test.qmd index 01ec878..ae09716 100644 --- a/comparisons/bin-prop-test.qmd +++ b/comparisons/bin-prop-test.qmd @@ -5,6 +5,13 @@ format: html `bin_prop_test()` is an interface to a set of statistial methods to test that $H_0: \pi = \pi_0$ where $\pi_0$ is the null value. There is a section below that descibes the results for each `method`. +## Resources: + +- [Help Page Details Section](../reference/bin_prop_test.qmd#details) +- Code coverage: + - 100% + - 100% + ## Experimental Setup ```{r} @@ -27,10 +34,6 @@ Since this function takes qualitative data as the input, we can look at the resu To measure the difference, we consider the absolute difference in the resulting p-values. -Resources: - -- [Help Page Details Section](../reference/bin_prop_test.qmd#details) -- link to file code coverage ## Exact Test (R-based): `method = "exact"` @@ -230,4 +233,4 @@ score_complete |> labs(x = "Abs Difference (segue - SAS)", title = "Score Test") + scale_x_continuous(labels = label_scientific(digits = 2))+ theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, vjust = 1)) -``` \ No newline at end of file +``` diff --git a/comparisons/numeric-rank-test.qmd b/comparisons/numeric-rank-test.qmd index b8b104d..bd9f70d 100644 --- a/comparisons/numeric-rank-test.qmd +++ b/comparisons/numeric-rank-test.qmd @@ -14,6 +14,13 @@ One method estimates a stratified statistic, so a thrid qualitative variable is These comparisons are specific to asymptotic test statistics since that is the method common to segue and SAS. + +## Resources: + +- [Help Page Details Section](../reference/numeric_rank_test.qmd#details) +- Code coverage: + - 100% + ## Experimental Setup ```{r} @@ -54,11 +61,6 @@ The simulation and rounding were computed in R. To measure the difference, we consider the absolute difference in the resulting p-values. -Resources: - -- [Help Page Details Section](../reference/numeric_rank_test.qmd#details) -- link to file code coverage - ## Wilcoxon Rank Sum Test `method = "wilcoxon"` diff --git a/coverage.qmd b/coverage.qmd deleted file mode 100644 index b366ddf..0000000 --- a/coverage.qmd +++ /dev/null @@ -1,91 +0,0 @@ ---- -title: "Test coverage" -subtitle: "How much of segue's code its unit tests run." -resources: - - assets/coverage/*.svg ---- - -```{r} -#| label: setup -#| include: false -source("R/setup.R") - -cov <- readRDS("data/coverage.rds") -repo <- "https://github.com/posit-dev/segue" - -if (is.na(cov$sha)) { - commit <- "a local working tree" - file_url <- function(f) { - rep(NA_character_, length(f)) - } -} else { - short <- substr(cov$sha, 1, 7) - commit <- sprintf("[`%s`](%s/tree/%s)", short, repo, cov$sha) - file_url <- function(f) { - sprintf("%s/blob/%s/%s", repo, cov$sha, f) - } -} - -if (is.na(cov$date)) { - commit_date <- format(cov$computed, "%Y-%m-%d") -} else { - commit_date <- substr(cov$date, 1, 10) -} -``` - -![](assets/coverage-badge.svg){fig-alt="Coverage badge"} - -These numbers come from running segue's tests with [covr](https://covr.r-lib.org/) `r cov$versions[["covr"]]` on segue `r cov$versions[["segue"]]`, `r cov$ref_type` `r cov$ref` (commit `r commit`, `r commit_date`), using R `r cov$versions[["R"]]`. - -Overall, the tests run **`r pct(cov$percent, 1)`** of segue's lines of R code. - -## By file - -```{r} -#| label: by-file -#| echo: false -by_file <- cov$summary[order(cov$summary$percent, cov$summary$file), ] - -badges <- sprintf( - '%s', - tools::file_path_sans_ext(basename(by_file$file)), - pct(by_file$percent, 0) -) -urls <- file_url(by_file$file) -files <- ifelse( - is.na(urls), - sprintf("%s", by_file$file), - sprintf('%s', urls, by_file$file) -) - -show_table( - data.frame( - File = files, - Lines = comma(by_file$lines), - Covered = comma(by_file$covered), - Coverage = badges - ), - align = c("l", "r", "r", "l") -) -``` - -## Line by line - -Click a file to see which lines the tests run. - -```{r} -#| label: report -#| echo: false -covr::report_html(cov$coverage) -``` - -## Update these numbers - -The coverage is computed when this site is built, not read from a coverage service. To compute it for a branch, tag, or commit of segue, run this from the root of the site: - -```bash -Rscript _coverage/build.R # main -Rscript _coverage/build.R v0.1.0 # a tag, a commit SHA, or a branch -``` - -Then render this page again. diff --git a/index.qmd b/index.qmd index eface12..3a2bae7 100644 --- a/index.qmd +++ b/index.qmd @@ -1,90 +1,15 @@ --- title: "segue" -pagetitle: "segue: SAS analyses, written in R" -toc: false -body-classes: home -page-layout: full -listing: - id: home-comparisons - contents: - - "comparisons/*.qmd" - - "!comparisons/index.qmd" - template: comparisons/_rows.ejs - sort: "order" - fields: [title, description, sas, r, agreement, agreement-label] - filter-ui: false - sort-ui: false --- -::: {.hero} -# Your SAS analyses, written in R. +segue is an R package that is intended to help new R users coming to SAS, especially those in clinical statistics groups within the pharmaceutical industry. -[Each page here takes a SAS step you already know, shows the R code that does the same job with the segue package, and reports how closely the results agree. Every comparison is checked by running both programs on thousands of inputs.]{.lede} +The idea is to give users a set of well-named, extensively documented functions that serve as SAS analogs. They are neither SAS-like nor base R-like; they generally follow conventions developed by the [tidyverse](https://tidyverse.org/) and [tidymodels](https://www.tmwr.org/) groups that focus on how humans interact with software. See the [**Design**](design.qmd) page for more information on our philosophy. -::: {.actions} -[Browse the comparisons](comparisons/index.qmd){.btn-go} -[Methodology](methodology.qmd){.btn-quiet} -::: +These functions can help SAS users become acclimated to R in the short term, but they can be used as longer-term tools for a validated clinical pipeline. -::: {.hero-translation} -::: {.translation} -::: {.pane .sas what="PROC FREQ" result-label="XP2_BIN" result="0.56971121420377"} -```sas -proc freq data=trial; - tables response - / binomial(p=0.25 level="yes"); - exact binomial; - weight count; -run; -``` -::: +The [**Reference**](reference/index.qmd) section echoes each function's help file in HTML format. The function documentation there describes any _methodological_ differences between segue/R and SAS. -::: {.pane .r what="segue" result-label="p.value" result="0.56971121420377"} -```r -library(segue) +If you are interested in how the _results_ of segue's function compare to those of SAS, the [**Comparisons**](comparisons/index.qmd) page shows fairly extensive numerical analysis and comparisons between the software platforms. In many cases, repeated simulations of clinical data are used to characterize how your results (e.g., p-values) might differ. -bin_prop_test( - response, - method = "exact_sas", - null_value = 0.25 -) -``` -::: -::: -::: -::: - -::: {.section-head} -## Comparisons - -[See all](comparisons/index.qmd) -::: - -::: {#home-comparisons} -::: - -::: {.section-head} -## Comparison methodology - -[Read the details](methodology.qmd) -::: - -::: {.steps} -::: {.step} -### Run SAS on many inputs - -A SAS program runs the analysis on a grid of inputs and edge cases, and saves the results as `.sas7bdat` files. -::: - -::: {.step} -### Run R on the same values - -R reads those files bit for bit with haven, so both programs start from identical numbers. -::: - -::: {.step} -### Compare every result - -Each page reports how many results are identical, the largest difference, and the reason for any gap. -::: -::: +Finally, the [**Software Validity Testing**](testing.qmd) page gives an overview of how this package is tested and specific details for each line of code in the package sources. diff --git a/methodology.qmd b/methodology.qmd deleted file mode 100644 index 82524a4..0000000 --- a/methodology.qmd +++ /dev/null @@ -1,37 +0,0 @@ ---- -title: "Comparison methodology" -subtitle: "Every number on this site comes from running SAS and R on the same inputs." ---- - -The goal of each comparison is to answer one question: if you replace this SAS code with the R code, do your results change? - -## The method - -1. **Run SAS on many inputs.** Each comparison starts with a SAS program that covers far more cases than a typical analysis: every combination of a grid of inputs, plus hand-picked edge cases. SAS saves the results in `.sas7bdat` files. -2. **Run R on the same inputs.** R reads the SAS data sets with `haven::read_sas()`. A `.sas7bdat` file stores numbers as 8-byte doubles, so R gets the exact same bits that SAS used. We don't use CSV files, because writing a number as text and reading it back can change its last digits. -3. **Compare every result.** We report how many results are identical, and how large the differences are when they are not. When a difference has a cause, such as a different definition of a p-value, the comparison page explains it. - -## Versions - -| | | -|---|---| -| SAS | SAS Viya `V.04.00M0P060826`, run from SAS Studio, Linux X64 | -| SAS options | `DECIMALCONV=COMPATIBLE` (the default) unless a page says otherwise | -| R | R 4.6.1, on Apple Silicon (aarch64) | -| segue | the version on GitHub when the page was built | - -The R code on each page runs when the site is built, so the R output you see comes from the current version of segue. - -## What "agree" means - -Floating-point results can differ in the last few bits for reasons that don't matter, such as adding numbers in a different order. So each page reports what it can: - -- **Identical**: the same double, bit for bit. We use this for functions such as `sas_round()`, where the goal is to reproduce SAS exactly. -- **Largest difference**: the biggest absolute difference between the R and SAS values. We use this for statistical tests, where both programs compute the same formula with their own numerical code. A difference of about 1e-15 is the rounding error of a double. - -## Get segue - -```r -# install.packages("pak") -pak::pak("posit-dev/segue") -``` diff --git a/testing.qmd b/testing.qmd new file mode 100644 index 0000000..121ba17 --- /dev/null +++ b/testing.qmd @@ -0,0 +1,118 @@ +--- +title: "How segue is tested" +resources: + - assets/coverage/*.svg +--- + +```{r} +#| label: setup +#| include: false +source("R/setup.R") + +cov <- readRDS("data/coverage.rds") +repo <- "https://github.com/posit-dev/segue" + +if (is.na(cov$sha)) { + commit <- "a local working tree" + file_url <- function(f) { + rep(NA_character_, length(f)) + } +} else { + short <- substr(cov$sha, 1, 7) + commit <- sprintf("[`%s`](%s/tree/%s)", short, repo, cov$sha) + file_url <- function(f) { + sprintf("%s/blob/%s/%s", repo, cov$sha, f) + } +} + +if (is.na(cov$date)) { + commit_date <- format(cov$computed, "%Y-%m-%d") +} else { + commit_date <- substr(cov$date, 1, 10) +} +``` + +When validating the correctness of software computations and interfaces, test cases are used to evaluate the software and determine if the outcomes are correct. For example, [unit tests](https://en.wikipedia.org/wiki/Unit_testing) are a common method to ensure that things work as intended. + +segue, like many R packages, uses a conventional approach: + +- The [testthat](https://testthat.r-lib.org/) package executes and evaluates the tests. +- Tests are run whenever a change is proposed and/or accepted to the package. +- The test suite is run under many different versions of R and all major operating systems (i.e., Windows, macOS, and Linux). +- Reverse dependency checks are used to manage interdependent packages. + + +For more information, see the ["Testing basics"](https://r-pkgs.org/testing-basics.html) chapter in _R Packages_. + +## Unit tests and code coverage + +One overall method for determining how extensive the package's tests are is code coverage. This is the proportion of lines of code that are covered by at least one unit test. It is not a perfect metric, but it can give an overall sense of how thoroughly developers have tested their work (at least formally). + +The actual coverage for segue is shown in a section below. + +## Reverse dependency checks + +segue, once on [CRAN](https://en.wikipedia.org/wiki/R_package#Comprehensive_R_Archive_Network_(CRAN)), also benefits from *reverse dependency checks*. CRAN enforces these checks to ensure that a change in one package does not harm other packages that depend on it. For example, suppose segue uses a function from the [coin](https://CRAN.R-project.org/package=coin) package for permutation tests. segue would have unit tests to ensure its computations produce the same values as coin. If the coin maintainers make a change that affects segue's tests, the CRAN maintainers usually insist that everyone be aware of the changes and that segue be updated appropriately (or that the coin maintainers fix things on their end, depending on the case). + +Often, the convention in the R community is for package maintainers to pre-check their reverse dependencies and work something out with the downstream packages. In many cases, one maintainer will inform another about a breakage by submitting the necessary updates to the downstream package(s) it affects. + +For example, a change to the caret package where one package that caret depended on was switched for another that was more actively maintained. One package that depends on caret, called mikropml, had one of its unit tests fail because the new code was _more accurate_. Before sending the new caret version to CRAN, the maintainer described the change in a [GitHub issue](https://github.com/SchlossLab/mikropml/issues/373) and then submitted a change to the mikropml maintainers (as a pull request). That discussion can be found [here](https://github.com/SchlossLab/mikropml/pull/374) and allowed enough time for all parties to avoid any issues. + +## Coverage results + +![](assets/coverage-badge.svg){fig-alt="Coverage badge"} + +These numbers come from running segue's tests with [covr](https://covr.r-lib.org/) `r cov$versions[["covr"]]` on segue `r cov$versions[["segue"]]`, `r cov$ref_type` `r cov$ref` (commit `r commit`, `r commit_date`), using R `r cov$versions[["R"]]`. + +Overall, the tests run **`r pct(cov$percent, 1)`** of segue's lines of R code. + +### By file + +```{r} +#| label: by-file +#| echo: false +by_file <- cov$summary[order(cov$summary$percent, cov$summary$file), ] + +badges <- sprintf( + '%s', + tools::file_path_sans_ext(basename(by_file$file)), + pct(by_file$percent, 0) +) +urls <- file_url(by_file$file) +files <- ifelse( + is.na(urls), + sprintf("%s", by_file$file), + sprintf('%s', urls, by_file$file) +) + +show_table( + data.frame( + File = files, + Lines = comma(by_file$lines), + Covered = comma(by_file$covered), + Coverage = badges + ), + align = c("l", "r", "r", "l") +) +``` + +### Line by line + +Click a file to see which lines the tests run. + +```{r} +#| label: report +#| echo: false +covr::report_html(cov$coverage) +``` + +### Update these numbers + +The coverage is computed when this site is built, not read from a coverage service. To compute it for a branch, tag, or commit of segue, run this from the root of the site: + +```bash +Rscript _coverage/build.R # main +Rscript _coverage/build.R v0.1.0 # a tag, a commit SHA, or a branch +``` + +Then render this page again.