Learn R Programming

Safestats: an R package for safe, anytime-valid inference.

Safestats is for designing and applying safe hypothesis tests. It can be used for designing hypothesis tests in the prospective or randomised controlled trial (RCT) setting, where the tests can be used under optional stopping and experiments often can be stopped early. The 'pilot' functions in the package also enable using the safe tests in the observational/ retrospective setting. For examples and explanation about which test to choose for which study setup, our vignette can be used (see below). The current version includes safe t-tests and tests of two proportions. The initial paper on the theory of safe testing and a worked-out example for the t-test can be found in this paper. More on the theory behind the development of the safe tests for proportions can be found here.

Installation

The development version can be found on GitHub, which can be installed with the devtools package from CRAN by entering in R:

devtools::install_github("AlexanderLyNL/safestats", build_vignettes = TRUE)

Once a stable version is finished, you can install it from CRAN in R through:

install.packages("safestats")

Usage

Load the safestats package:

library("safestats")

For an extensive overview of all functions and their usage, we recommend following the safestats vignette tutorial:

browseVignettes("safestats")

Copy Link

Version

Install

install.packages('safestats')

Monthly Downloads

224

Version

0.8.7

License

LGPL (>= 3)

Maintainer

Alexander Ly

Last Published

November 24th, 2022

Functions in safestats (0.8.7)

computeBetaSafeT

Helper function: Computes the type II error of the safeTTest based on the minimal clinically relevant standardised mean difference and nPlan.
computeBetaSafeZ

Helper function: Computes the type II error based on the minimal clinically relevant mean difference and nPlan
computeConfidenceBoundForLogOddsTwoProportions

Estimate an upper or lower bound for a safe confidence sequence on the logarithm of the odds ratio for two proportions.
checkAndReturnsEsMinParameterSide

Checks consistency between the sided of the hypothesis and the minimal clinically relevant effect size or safe test defining parameter. Throws an error if the one-sided hypothesis is incongruent with the
checkAndReturnsNPlan

Check consistency between nPlan and the testType for one and two-sample z and t-tests
computeLogrankZ

Helper function to computes the logrank statistic for 'Surv' objects of type "right" and "counting" with the hypergeometric variance.
computeConfidenceIntervalZ

Helper function: Computes the safe confidence sequence for a z-test
computeNEff

Help function to compute the effective sample size based on a length 2 vector of samples
computeMinEsBatchSafeZ

Computes the smallest mean difference that is detectable with chance 1-beta, for the provided sample size
computeEsMinSafeT

Helper function: Computes the minimal clinically relevant standardised mean difference for the safe t-test nPlan and beta.
computeNPlanBatchSafeT

Helper function: Computes the planned sample size for the safe t-test based on the minimal clinically relevant standardised effect size, alpha and beta.
computeLogrankBetaFrom

Helper function: Computes the type II error under optional stopping based on the minimal clinically relevant hazard ratio and the maximum number of nEvents.
checkDoubleArgumentsDesignObject

Helper function to check whether arguments are specified in a function at a higher level and already provided in the design object.
computeLogrankNEvents

Helper function: Computes the planned sample size based on the minimal clinical relevant hazard ratio, alpha and beta under optional stopping.
computeBetaBatchSafeZ

Helper function: Computes the type II error based on the minimal clinically relevant effect size and sample size.
computeNPlanBatchSafeZ

Helper function: Computes the planned sample size based on the minimal clinical relevant mean difference, alpha and beta.
computeConfidenceIntervalT

Helper function: Computes the safe confidence sequence for the mean in a t-test
computeStatsForLogrank

Computes the sufficient statistics needed to compute 'logrankSingleZ'
computeNPlanSafeZ

Helper function: Computes the planned sample size based on the minimal clinical relevant mean difference, alpha and beta
computeConfidenceBoundsForDifferenceTwoProportions

Estimate Lower and Upper Bounds on the Confidence Sequence (Interval) for the Difference Divergence Measure for Two Proportions
designPilotSafeZ

Designs a Safe Z-Test Based on Planned Samples nPlan
designSafeLogrank

Designs a Safe Logrank Test Experiment
designSafeT

Designs a Safe Experiment to Test Means with a T Test
designSafeTwoProportions

Designs a Safe Experiment to Test Two Proportions in Stream Data
generateNormalData

Generates Normally Distributed Data Depending on the Design
generateSurvData

Generate Survival Data which Can Be Analysed With the `survival` Package
designFreqZ

Design a Frequentist Z-Test
logrankSingleEExact

Helper function computes single component of the exact logrank e-value
logrankSingleZ

Helper function computes single component of the logrank statistic
designPilotSafeT

Designs a Safe T-Test Based on Planned Samples nPlan
defineTTestN

Computes a Sequence of (Effective) Sample Sizes
plotConfidenceSequenceTwoProportions

Plot bounds of a safe confidence sequence of the difference or log odds ratio for two proportions against the number of data blocks in two data streams ya and yb.
plot.safeTSim

Plots a 'safeTSim' Object
plotHistogramDistributionStoppingTimes

Plots the Histogram of Stopping Times
plot.safe2x2Sim

Plots Results of Simulations for Comparing Hyperparameters for Safe Tests of Two Proportions
computeNPlanSafeT

Helper function: Computes the planned sample size of the safe t-test based on the minimal clinical relevant standardised mean difference.
extractNameFromArgs

Helper function: Get all names as entered by the user
designSafeZ

Designs a Safe Z Experiment
plotSafeTDesignSampleSizeProfile

Plots the Sample Sizes Necessary for a Tolerable Alpha and Beta as a Function of deltaMin
print.safe2x2Sim

Prints Results of Simulations for Comparing Hyperparameters for Safe Tests of Two Proportions
designFreqT

Design a Frequentist T-Test
safeTTestStatTDensity

safeTTestStat() based on t-densities
safeTwoProportionsTest

Perform a Safe Test for Two Proportions with Stream Data
getArgs

Helper function: Get all arguments as entered by the user
safeZ10Inverse

Computes the Inverse of the Two-Sided Safe Z-Test
safeZTest

Safe Z-Test
print.safeTest

Print Method for Safe Tests
isTryError

Checks Whether a Vector of Object Inherits from the Class 'try-error'
getNameTestType

Gets the Label of the Test
rLogrank

Randomly samples from a logrank distribution
safeLogrankTest

Safe Logrank Test
safeTTest

Safe Student's T-Test.
safeTTestStatAlpha

safeTTestStat() Subtracted with 1/alpha.
safeTTestStat

Computes E-Values Based on the T-Statistic
simulate.safeDesign

Simulate Early Stopping Experiments for the T Test
simulateCoverageDifferenceTwoProportions

Simulate the coverage of a safe confidence sequence for differences between proportions for a given distribution and safe design.
sampleStoppingTimesSafeZ

Simulate stopping times for the safe z-test
sampleStoppingTimesSafeT

Simulate stopping times for the safe z-test
simulateTwoProportions

Compare Different Hyperparameter Settings for Safe Tests of Two Proportions.
tryOrFailWithNA

Tries to Evaluate an Expression and Fails with NA
selectivelyContinueTTestCombineData

Selectively Continue Experiments that Did Not Lead to a Null Rejection for a (Safe) T-Test
setSafeStatsPlotOptionsAndReturnOldOnes

Sets 'safestats' Plot Options and Returns the Current Plot Options.
getNameAlternative

Gets the Label of the Alternative Hypothesis
returnOne

Auxiliary function for sampling of the logrank simulations to return the integer 1 event per time.
replicateTTests

Simulate Early Stopping Experiments
print.safeTSim

Prints a safeTSim Object
print.safeDesign

Print Method for Safe Tests
safeZTestStat

Computes E-Values Based on the Z-Statistic
simulateOptionalStoppingScenarioTwoProportions

Simulate an optional stopping scenario according to a safe design for two proportions
simulateIncorrectStoppingTimesFisher

Simulate incorrect optional stopping with fisher's exact test's p-value as the stopping rule.
sampleLogrankStoppingTimes

Simulate stopping times for the exact safe logrank test
computeBootObj

Computes the bootObj for sequential sampling procedures regarding nPlan, beta, the implied target