From eda4bdc3e075170e2cf6c39f5b6f378585bdd85f Mon Sep 17 00:00:00 2001 From: Stuart Wheater Date: Mon, 3 Nov 2025 00:14:29 +0000 Subject: [PATCH] Initial 'mdPatternDS' tests --- man/mdPatternDS.Rd | 54 +++++++++++++++++++++++++++ tests/testthat/test-arg-mdPatternDS.R | 41 ++++++++++++++++++++ tests/testthat/test-smk-mdPatternDS.R | 37 ++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 man/mdPatternDS.Rd create mode 100644 tests/testthat/test-arg-mdPatternDS.R create mode 100644 tests/testthat/test-smk-mdPatternDS.R diff --git a/man/mdPatternDS.Rd b/man/mdPatternDS.Rd new file mode 100644 index 00000000..1084565e --- /dev/null +++ b/man/mdPatternDS.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mdPatternDS.R +\name{mdPatternDS} +\alias{mdPatternDS} +\title{Missing data pattern with disclosure control} +\usage{ +mdPatternDS(x) +} +\arguments{ +\item{x}{a character string specifying the name of a data frame or matrix +containing the data to analyze for missing patterns.} +} +\value{ +A list containing: +\item{pattern}{The missing data pattern matrix with disclosure control applied} +\item{valid}{Logical indicating if all patterns meet disclosure requirements} +\item{message}{A message describing the validity status} +} +\description{ +This function is a serverside aggregate function that computes the +missing data pattern using mice::md.pattern and applies disclosure control to +prevent revealing small cell counts. +} +\details{ +This function calls the mice::md.pattern function to generate a matrix +showing the missing data patterns in the input data. To ensure disclosure control, +any pattern counts that are below the threshold (nfilter.tab, default=3) are +suppressed. + +\strong{Suppression Method:} + +When a pattern count is below threshold: +- Row name is changed to "suppressed()" where N is the threshold +- All pattern values in that row are set to NA +- Summary row is also set to NA (prevents back-calculation) + +\strong{Output Matrix Structure:} + +- Rows represent different missing data patterns (plus a summary row at the bottom) +- Row names contain pattern counts (or "suppressed()" for invalid patterns) +- Columns show 1 if variable is observed, 0 if missing +- Last column shows total number of missing values per pattern +- Last row shows total number of missing values per variable + +\strong{Note for Pooling:} + +When this function is called from ds.mdPattern with type='combine', suppressed +patterns are excluded from pooling to prevent disclosure through subtraction. +This means pooled counts may underestimate the true total when patterns are +suppressed in some studies. +} +\author{ +Xavier EscribĂ  montagut for DataSHIELD Development Team +} diff --git a/tests/testthat/test-arg-mdPatternDS.R b/tests/testthat/test-arg-mdPatternDS.R new file mode 100644 index 00000000..f6b4f941 --- /dev/null +++ b/tests/testthat/test-arg-mdPatternDS.R @@ -0,0 +1,41 @@ +#------------------------------------------------------------------------------- +# Copyright (c) 2025 ProPASS Consortium. All rights reserved. +# +# This program and the accompanying materials +# are made available under the terms of the GNU Public License v3.0. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#------------------------------------------------------------------------------- + +# +# Set up +# + +context("mdPatternDS::arg::setup") + +# +# Tests +# + +context("mdPatternDS::arg::x NULL") +test_that("mdPatternDS x NULL", { + x <- NULL + + expect_error(mdPatternDS(x), "The input object must be of type 'data.frame' or 'matrix'. Current type: NULL") +}) + +context("mdPatternDS::arg::x not valid variable") +test_that("mdPatternDS x not variable", { + x <- "not a variable" + + expect_error(mdPatternDS(x), "Object 'not a variable' does not exist on the server") +}) + +# +# Done +# + +context("mdPatternDS::arg::shutdown") + +context("mdPatternDS::arg::done") diff --git a/tests/testthat/test-smk-mdPatternDS.R b/tests/testthat/test-smk-mdPatternDS.R new file mode 100644 index 00000000..33e65055 --- /dev/null +++ b/tests/testthat/test-smk-mdPatternDS.R @@ -0,0 +1,37 @@ +#------------------------------------------------------------------------------- +# Copyright (c) 2025 ProPASS Consortium. All rights reserved. +# +# This program and the accompanying materials +# are made available under the terms of the GNU Public License v3.0. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +#------------------------------------------------------------------------------- + +# +# Set up +# + +context("mdPatternDS::smk::setup") + +# +# Tests +# + +context("mdPatternDS::smk::sample data.frame") +test_that("mdPatternDS: sample data.frame", { + x_val <- data.frame(v1 = c(0.0, 1.0, 2.0, 3.0, 4.0), v2 = c(4.0, 3.0, 2.0, 1.0, 0.0)) + x <- "x_val" + + res <- mdPatternDS(x) + + expect_length(res, 3) +}) + +# +# Done +# + +context("mdPatternDS::smk::shutdown") + +context("mdPatternDS::smk::done")