ratesci is an R package to compute confidence intervals and tests for:
A number of different methods are offered, but in each case, the
recommended default is based on asymptotic score methodology (from
(Wilson 1927), (Miettinen and Nurminen
1985) and (Tango 1998)), but
including skewness corrections following the principles of (Gart and
Nam 1988). The resulting family of skewness-corrected
asymptotic score (SCAS) methods (Laud 2017), (and Laud
2025, under review) ensures equal-tailed coverage (or central location),
in other words for a nominal 95% confidence interval, the one-sided
non-coverage probability is (on average) close to 2.5% on each side.
Stratified calculations are also catered for (e.g. meta-analysis,
including random effects). Most of the above list is covered by
scoreci()
, with the exception of clustered proportions (which are
handled by clusterpci()
) and paired contrasts (pairbinci()
). Options
are included for omitting the skewness correction to obtain legacy
methods such as stratified or unstratified Miettinen-Nurminen and Wilson
intervals, Tango intervals for paired data, or chi-squared, CMH,
Farrington-Manning or McNemar tests.
In each case, the asymptotic score methods provide a matching 2-sided test for association, and also a 1-sided hypothesis test against any specified null parameter value for a non-inferiority test. Both tests are guaranteed to be coherent with the interval. The test for association is a variant of (and in many cases identical to) a chi-squared test or CMH test, and the non-inferiority test is analogous to a Farrington-Manning test, all with improved control of type I error achieved by the bias and skewness corrections. For paired proportions, the test for association is a variant of the McNemar test, incorporating an ‘N-1’ adjustment which appears to avoid any violations of the nominal significance level.
Another family of methods offered by the package, with reasonable
performance for large (single-stratum) sample sizes (but without a
matching hypothesis test), uses the Method of Variance Estimates
Recovery (MOVER), also known as Square-and-Add (Newcombe 2012, chap.
7). These methods combine intervals calculated
separately for each proportion. The recommended default gives the
MOVER-J method, using Jeffreys equal-tailed intervals instead of the
Wilson method preferred by Newcombe. This improves on traditional
approximate methods with respect to one-sided and two-sided coverage,
particularly for the RR contrast, but does not match the performance of
the SCAS method. As the Jeffreys interval is based on a Bayesian
conjugate prior, the MOVER approach allows the option to incorporate
prior beliefs about the rates in each group - by default, the
non-informative Jeffreys $Beta(0.5, 0.5)$ priors are used (or
corresponding Gamma priors for Poisson rates). MOVER intervals are
available in moverci()
for all contrasts of independent binomial and
Poisson rates, and in pairbinci()
for the paired binomial contrasts.
For those wishing to achieve strictly conservative coverage, continuity adjustments are provided as approximations to “exact” methods, with the option to adjust the strength of the adjustment (as the Yates correction is widely recognised to be an over-conservative adjustment). The performance of these adjustments has not been extensively evaluated, but they appear to be more successful for SCAS than for MOVER, in terms of achieving conservative coverage.
An online calculator based on this package is available here. Plots illustrating the coverage properties of selected methods can be found here, and here.
The current official (i.e. CRAN) release can be installed within R with:
install.packages("ratesci")
The latest development version of the package can be installed with:
# install.packages("pak")
pak::pak("petelaud/ratesci")
This builds the package from source based on the current version on GitHub
Below is a basic example which shows you how to request a confidence
interval for the difference between proportions 5/56 - 0/29. The $call
output element shows that the default settings give an interval for the
risk difference (contrast = "RD"
), for binomial proportions
(distrib = "bin"
), at a 95% confidence level. Variance bias correction
(bcf
) and skewness correction (skew
) are applied, continuity
adjustment (cc
) is not. This is the skewness-corrected asymptotic
score (“SCAS”) confidence interval. (For Miettinen-Nurminen, use
skew = FALSE
, for Gart-Nam, use bcf = FALSE
.)
library(ratesci)
scoreci(x1 = 5, n1 = 56, x2 = 0, n2 = 29)
#> $estimates
#> lower est upper level x1 n1 x2 n2 p1hat p2hat p1mle p2mle
#> [1,] -0.01862 0.09169 0.1867 0.95 5 56 0 29 0.08929 0 0.09169 0
#>
#> $pval
#> chisq pval2sided theta0 scorenull pval_left pval_right
#> [1,] 3.025 0.082 0 1.739 0.959 0.041
#>
#> $call
#> distrib contrast level bcf skew cc
#> "bin" "RD" "0.95" "TRUE" "TRUE" "FALSE"
An example of a paired analysis follows, using the data from Table II of
(Fagerland, Lydersen, and Laake 2014). Here the
bias and skewness corrections are again applied by default. Omitting
both would produce the Tango asymptotic score interval for
contrast = "RD"
, or the Tang method for contrast = "RR"
.
pairbinci(x = c(1, 1, 7, 12))
#> $data
#> Test_2
#> Test_1 Success Failure
#> Success 1 1
#> Failure 7 12
#>
#> $estimates
#> lower est upper level p1hat p2hat p1mle p2mle phi_hat phi_c
#> [1,] -0.5281 -0.2859 -0.01842 0.95 0.09524 0.381 0.0952 0.3811 0.07954 0
#> psi_hat
#> [1,] 1.714
#>
#> $pval
#> chisq pval2sided theta0 scorenull pval_left pval_right
#> [1,] 4.286 0.03843 0 -2.07 0.01922 0.9808
#>
#> $call
#> contrast method level bcf skew cc
#> "RD" "Score" "0.95" "TRUE" "TRUE" "FALSE"
ratesci contains the following functions:
For comparisons of rates (contrasts RD, RR and OR):
scoreci()
: for score-based confidence intervals including SCAS,
Miettinen-Nurminen and Gart-Nam, with or without stratification.scasci()
: wrapper function to compute SCAS intervals.tdasci()
: wrapper function to compute TDAS stratified intervals
incorporating random effects.moverci()
: for the MOVER methods, including Newcombe and MOVER-J.moverbci()
: wrapper function to compute MOVER-B intervals.pairbinci()
: for paired binomial data, including SCAS, asymptotic
score and MOVER methods for RD and RR, and transformed binomial
intervals for conditional OR.For single binomial or Poisson rates:
scaspci()
: non-iterative SCAS method for a single rate. For
stratified calculations use scoreci()
with contrast = "p"
.jeffreysci()
: wrapper function to compute Jeffreys interval for a
single rate (with option to incorporate prior information).rateci()
: wrapper function for selected methods for a single rate,
including SCAS, Jeffreys, midp and Clopper-Pearson/Garwood.clusterpci()
: Saha’s Wilson-based interval for a single proportion
based on clustered data, with a skewness-corrected version.Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.