Skip to content

Steven-Nanga/SampleSizeR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SampleSizeR

A Multi-Design Sample Size Calculator for Public Health Research

Launch App

MIT License R Shiny Status


A fully functional, modular, and extensible R Shiny application that calculates sample sizes for different study designs using established statistical formulas used in public health research, DHS-type surveys, and clinical studies.

Try it now: https://mnakjp-steven-nanga.shinyapps.io/SampleSizeR/


Who Is This For?

  • Epidemiologists designing cross-sectional or cohort studies
  • Biostatisticians computing sample sizes for grant proposals
  • DHS/MICS analysts planning multi-stage cluster surveys
  • Clinical researchers designing RCTs and case-control studies
  • Public health students learning sample size methodology

Features

Category Details
10 Study Designs Single Proportion, Two Proportions, Mean Estimation, Two Means, Case-Control, Cohort, Cluster Sampling, Finite Population Correction, DHS Survey, Power Analysis
Reactive Calculations Results update instantly as parameters change
Step-by-Step Explanations Every calculation displays the formula and detailed derivation steps
Sensitivity Analysis Interactive plots with current-value markers showing how sample size responds to parameter changes
Plain-Language Interpretation Each result includes a human-readable explanation tailored to the study design
Preset Scenarios Quick-load common epidemiological scenarios (DHS Vaccination Coverage, HIV Prevalence, RCT Drug vs Placebo, etc.)
Cross-Design Comparison Side-by-side bar chart and table comparing results across all active designs
Calculation History Session log of all calculations with timestamps
Export Download structured CSV or generate HTML reports with full methodology documentation
Tooltips Info icons on every parameter with contextual statistical explanations
DHS Survey Mode Specialized multi-stage calculator combining cluster sampling, DEFF, FPC, and non-response adjustments

Quick Start

Use Online (No Installation)

Visit the live app:

https://mnakjp-steven-nanga.shinyapps.io/SampleSizeR/

Run Locally

# 1. Install dependencies
install.packages(c("shiny", "shinydashboard", "ggplot2", "dplyr", "DT",
                    "scales", "rmarkdown", "knitr"))

# 2. Clone and run
shiny::runGitHub("SampleSizeR", "mnakjp-steven-nanga")

Or clone the repo manually:

git clone https://github.com/mnakjp-steven-nanga/SampleSizeR.git
shiny::runApp("SampleSizeR")

Project Structure

SampleSizeR/
├── global.R                    # Package loading, module sourcing, design registry
├── ui.R                        # Dashboard UI layout (shinydashboard)
├── server.R                    # Server logic, output wiring, downloads
├── helpers/
│   ├── formulas.R              # All sample size calculation formulas (11 functions)
│   └── utils.R                 # Validation, formatting, tooltips, presets, interpretation
├── modules/
│   ├── mod_single_proportion.R # Cross-sectional study
│   ├── mod_two_proportions.R   # Comparative / RCT
│   ├── mod_mean_estimation.R   # Single mean estimation
│   ├── mod_two_means.R         # Independent samples comparison
│   ├── mod_case_control.R      # Case-control with OR calculation
│   ├── mod_cohort.R            # Cohort study with RR calculation
│   ├── mod_cluster_sampling.R  # Cluster design with DEFF/ICC
│   ├── mod_finite_population.R # FPC with multiple base methods
│   ├── mod_dhs_survey.R        # Full DHS/MICS-style survey
│   └── mod_power_based.R       # Power analysis
├── www/
│   └── custom.css              # Custom styling (Material-inspired theme)
├── report_template.Rmd         # R Markdown template for HTML reports
├── LICENSE                     # MIT License
└── README.md

Formulas Implemented

1. Single Population Proportion

$$n = \frac{Z^2 \cdot p(1-p)}{d^2}$$

2. Two Proportion Comparison

$$n = \frac{(Z_{\alpha/2}+Z_\beta)^2 [p_1(1-p_1)+p_2(1-p_2)]}{(p_1-p_2)^2}$$

3. Mean Estimation

$$n = \frac{Z^2 \sigma^2}{d^2}$$

4. Two Means Comparison

$$n = \frac{(Z_{\alpha/2}+Z_\beta)^2 \cdot 2\sigma^2}{(\mu_1-\mu_2)^2}$$

5. Case-Control Study (Kelsey et al.)

$$n_{cases} = \frac{(Z_{\alpha/2}+Z_\beta)^2 \bar{p}(1-\bar{p})(r+1)}{r(p_1-p_2)^2}$$

6. Cohort Study

$$n_{exposed} = \frac{(Z_{\alpha/2}+Z_\beta)^2 [p_1(1-p_1)+p_2(1-p_2)/r]}{(p_1-p_2)^2}$$

7. Cluster Sampling Adjustment

$$n_{adj} = n \times DEFF \qquad \text{where} \quad DEFF = 1 + (m-1) \times ICC$$

8. Finite Population Correction

$$n_f = \frac{n}{1 + \frac{n-1}{N}}$$

9. Non-Response Adjustment

$$n_{final} = \frac{n}{1 - r}$$

10. Power-Based Sample Size

$$n = \frac{(Z_{\alpha/2}+Z_\beta)^2 \cdot p_0(1-p_0)}{\delta^2}$$


Architecture

SampleSizeR uses a registry-driven architecture that makes it trivial to add new study designs:

design_registry (global.R)
       │
       ├──► ui.R reads the registry → auto-generates sidebar items, tab panels,
       │                               value boxes, plots, tables, export buttons
       │
       └──► server.R reads the registry → auto-wires module servers, renders
                                           outputs, handles downloads, logs history

Each module follows a consistent contract:

  • UI function: mod_<name>_ui(id) returns a tagList of input widgets
  • Server function: mod_<name>_server(id) returns list(result = reactive(...), sensitivity = reactive(...))

Adding a new design requires zero changes to ui.R or server.R.


Extending the Application

Adding a New Study Design

  1. Add the formula to helpers/formulas.R:

    calc_new_design <- function(confidence, ...) {
      z <- get_z_value(confidence)
      n <- # ... your formula ...
      list(
        n       = ceiling(n),
        formula = "$$n = ...$$",
        steps   = c("Step 1: ...", "Step 2: ..."),
        params  = list(confidence = confidence, ...)
      )
    }
  2. Create a module in modules/mod_new_design.R:

    mod_new_design_ui <- function(id) {
      ns <- NS(id)
      tagList(
        # ... input widgets using ns() for namespacing ...
      )
    }
    
    mod_new_design_server <- function(id) {
      moduleServer(id, function(input, output, session) {
        result <- reactive({ ... })
        sensitivity <- reactive({ ... })
        list(result = result, sensitivity = sensitivity)
      })
    }
  3. Register in global.R:

    design_registry$new_design <- list(
      label    = "New Design",
      desc     = "Description",
      icon     = "icon-name",
      ui_func  = mod_new_design_ui,
      srv_func = mod_new_design_server
    )
  4. Source the module in global.R:

    source("modules/mod_new_design.R")
  5. Add a sidebar entry in ui.R (optional — the tab itself is auto-generated).

The UI tab, value boxes, interpretation, sensitivity plots, parameter table, export buttons, history logging, and comparison chart are all auto-wired from the registry.

Adding Preset Scenarios

Add entries to the presets list in helpers/utils.R:

presets$new_design <- list(
  "Custom"             = list(),
  "Scenario Name"      = list(confidence = 0.95, param1 = value1, ...)
)

References

  • Cochran, W.G. (1977). Sampling Techniques, 3rd Edition. Wiley.
  • Kelsey, J.L., Whittemore, A.S., Evans, A.S., & Thompson, W.D. (1996). Methods in Observational Epidemiology, 2nd Edition. Oxford University Press.
  • ICF International. DHS Survey Design Manual. The DHS Program.
  • Lwanga, S.K. & Lemeshow, S. (1991). Sample Size Determination in Health Studies: A Practical Manual. WHO.
  • Fleiss, J.L., Levin, B., & Paik, M.C. (2003). Statistical Methods for Rates and Proportions, 3rd Edition. Wiley.

Contributing

Contributions are welcome! To contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-design)
  3. Add your formula, module, and registry entry following the patterns above
  4. Test locally with shiny::runApp()
  5. Submit a pull request

Please ensure all formulas include proper citations and step-by-step documentation.


License

This project is licensed under the MIT License — see the LICENSE file for details.

You are free to use, modify, and distribute this software for any purpose, including commercial use.

Disclaimer: This tool is intended for planning purposes. Final sample size decisions should be reviewed by a qualified biostatistician and account for practical field considerations.


Built with R Shiny • Designed for epidemiologists, biostatisticians, and DHS analysts
Launch AppFormulasExtendContribute

About

A Multi-Design Sample Size Calculator for Public Health Research, DHS Surveys, and Clinical Studies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors