| Title: | Power of Interrupted Time Series (ITS) Studies |
|---|---|
| Description: | Provides tools for estimating the statistical power of Interrupted Time Series (ITS) designs, with a focus on healthcare applications. The package supports prospective power calculations before a study begins, and retrospective assessments of whether a completed study was adequately powered. It includes functions to estimate nuisance parameters (baseline, residual standard deviation, autocorrelation) from data observed before the intervention, and to estimate power via Monte Carlo simulation for single-site and multi-site designs. Utility functions for design optimisation sweeps and publication- ready plots are also provided. |
| Authors: | David de Lorenzo [aut, cre] (ORCID: <https://orcid.org/0000-0003-2042-0961>, affiliation: UCL Great Ormond Street Institute of Child Health, London, UK; Neotree, London, UK (https://neotree.org/)) |
| Maintainer: | David de Lorenzo <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-30 13:20:50 UTC |
| Source: | https://github.com/cran/PITS |
The package provides a complete workflow for ITS power analysis:
Step 1 - Estimate nuisance parameters from pre-intervention data
Use estimate_its_params() to estimate baseline, sigma (residual SD),
and rho (AR(1) autocorrelation) from a pre-intervention time series.
Individual functions estimate_baseline(), estimate_sigma(),
estimate_rho(), and estimate_trend() are also available.
Step 2 - Calculate power via Monte Carlo simulation
Use calculate_power() (single site) or calculate_power_multi()
(multiple sites) to estimate the probability of detecting a specified
intervention effect for a given study design.
Step 3 - Optimise the design
Use power_sweep() to evaluate power across a range of post-intervention
durations, and run_power_grid() for full factorial sensitivity analyses.
Visualise results with plot_power_curve() and plot_power_heatmap().
Convenience wrappers
run_its_power() replicates the interactive experience of the original
its_power_tool.R script. estimate_and_calculate() chains parameter
estimation and power calculation in a single call.
Tools for estimating the statistical power of Interrupted Time Series (ITS) designs, with a focus on healthcare applications.
Lopez Bernal J, et al. (2017). Interrupted time series regression for the evaluation of public health interventions: a tutorial. Int J Epidemiol 46:348-355. doi:10.1093/ije/dyw098
Zhang F, et al. (2011). Simulation-based power calculation for designing interrupted time series analyses of health policy interventions. J Clin Epidemiol 64:1252-1261.
Wagner AK, et al. (2002). Segmented regression analysis of interrupted time series studies in medication use research. J Clin Pharm Ther 27:299-309.
Maintainer: David de Lorenzo [email protected] (ORCID) (affiliation: UCL Great Ormond Street Institute of Child Health, London, UK; Neotree, London, UK (https://neotree.org/))
Authors:
David de Lorenzo [email protected] (ORCID) (affiliation: UCL Great Ormond Street Institute of Child Health, London, UK; Neotree, London, UK (https://neotree.org/))
Useful links:
Report bugs at https://github.com/drdaviddelorenzo/PITS/issues
Creates a data frame of all combinations of the supplied parameter vectors,
suitable for passing to run_power_grid(). Useful for sensitivity analyses
and generating figures showing how power varies across parameter space.
build_param_grid( n_post, level_change, sigma, rho, n_pre = 24L, baseline = 15, slope_change = 0 )build_param_grid( n_post, level_change, sigma, rho, n_pre = 24L, baseline = 15, slope_change = 0 )
n_post |
Integer vector. Post-intervention durations to evaluate. |
level_change |
Numeric vector. Effect sizes to evaluate. |
sigma |
Numeric vector. Residual SDs to evaluate. |
rho |
Numeric vector. Autocorrelation values to evaluate. |
n_pre |
Integer. Pre-intervention duration (fixed). Default 24. |
baseline |
Numeric. Baseline outcome (fixed). Default 15. |
slope_change |
Numeric. Slope change (fixed). Default 0. |
A data frame with one row per parameter combination.
grid <- build_param_grid( n_post = c(12, 18, 24, 30), level_change = c(-1, -2, -3), sigma = c(1.5, 2.5, 3.5), rho = c(0.2, 0.4, 0.6) ) nrow(grid) # 4 * 3 * 3 * 3 = 108 combinationsgrid <- build_param_grid( n_post = c(12, 18, 24, 30), level_change = c(-1, -2, -3), sigma = c(1.5, 2.5, 3.5), rho = c(0.2, 0.4, 0.6) ) nrow(grid) # 4 * 3 * 3 * 3 = 108 combinations
Runs a Monte Carlo simulation to estimate the probability of detecting a specified intervention effect in a single-site ITS study.
calculate_power( n_pre, n_post, baseline, level_change, slope_change = 0, sigma, rho, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 1000L, seed = 123L, pre_trend = 0 )calculate_power( n_pre, n_post, baseline, level_change, slope_change = 0, sigma, rho, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 1000L, seed = 123L, pre_trend = 0 )
n_pre |
Integer. Number of pre-intervention time points. |
n_post |
Integer. Number of post-intervention time points. This is
the primary design lever: use |
baseline |
Numeric. Mean outcome in the pre-intervention period. |
level_change |
Numeric. Expected immediate step change at the
intervention point (your minimum clinically meaningful effect). Set to
0 when |
slope_change |
Numeric. Expected change in trend per time unit after
the intervention. Default 0. Set to 0 when |
sigma |
Numeric. Residual standard deviation. Estimate from
pre-intervention data using |
rho |
Numeric. AR(1) autocorrelation coefficient in |
test |
Character. Effect to test: |
alpha |
Numeric. Significance threshold. Default 0.05. |
n_sim |
Integer. Number of Monte Carlo replications. Use 500 for a quick check, 1000 for a reportable estimate, 2000+ for publication. |
seed |
Integer or |
pre_trend |
Numeric. Pre-intervention trend per time unit. Default 0. |
A named list:
Numeric. Estimated power (proportion of simulations
with ).
Numeric. Power as a percentage.
Character. Qualitative label (see
interpret_power()).
Numeric vector. Raw p-values from all n_sim
replications (NA = non-convergence).
Integer. Number of replications that converged.
Integer. Total replications requested.
Named list. Input parameters, for traceability.
power_sweep(), calculate_power_multi(), estimate_its_params()
result <- calculate_power( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 500, seed = 42 ) print(result$power_pct)result <- calculate_power( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 500, seed = 42 ) print(result$power_pct)
Runs a Monte Carlo simulation to estimate the probability of detecting a common intervention effect across multiple sites (e.g. hospitals), using a mixed-effects segmented regression model.
calculate_power_multi( sites, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 1000L, seed = 123L )calculate_power_multi( sites, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 1000L, seed = 123L )
sites |
A list of named parameter lists, one per site. Each element
must contain: |
test |
Character. Effect to test: |
alpha |
Numeric. Significance threshold. Default 0.05. |
n_sim |
Integer. Number of Monte Carlo replications. Use 500 for a quick check, 1000 for a reportable estimate, 2000+ for publication. |
seed |
Integer or |
The model is a linear mixed-effects model with site-specific random intercepts and AR(1) autocorrelation within each site:
where are site random intercepts and
follows AR(1) within each site.
Same structure as calculate_power(), plus:
Integer. Number of sites.
Character vector. Site names.
calculate_power(), power_sweep()
sites <- list( list(name = "Hospital A", n_pre = 24, n_post = 24, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4), list(name = "Hospital B", n_pre = 24, n_post = 24, baseline = 18, level_change = -3, slope_change = 0, sigma = 3.0, rho = 0.4) ) result <- calculate_power_multi(sites, n_sim = 200, seed = 42) result$power_pctsites <- list( list(name = "Hospital A", n_pre = 24, n_post = 24, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4), list(name = "Hospital B", n_pre = 24, n_post = 24, baseline = 18, level_change = -3, slope_change = 0, sigma = 3.0, rho = 0.4) ) result <- calculate_power_multi(sites, n_sim = 200, seed = 42) result$power_pct
Produces a 2x2 panel of diagnostic plots for pre-intervention data: the observed series with fitted trend, residuals over time, a Q-Q normality plot, and the residual ACF. These help assess whether the ITS model assumptions are plausible.
diagnose_params( data, outcome_col = "outcome", time_col = "time", main = "Pre-intervention diagnostics" )diagnose_params( data, outcome_col = "outcome", time_col = "time", main = "Pre-intervention diagnostics" )
data |
Pre-intervention data: a data frame or numeric vector. See
|
outcome_col |
Character. Column name for the outcome. Default
|
time_col |
Character. Column name for time. Default |
main |
Character. Panel title prefix. |
Invisibly, a list with elements params (estimated parameters)
and residuals (residual vector).
data("example_cfr_data") diagnose_params(example_cfr_data)data("example_cfr_data") diagnose_params(example_cfr_data)
Convenience function that chains estimate_its_params() and
calculate_power(). Supply a pre-intervention dataset, a
level_change, and a target n_post, and receive a power
estimate.
estimate_and_calculate( data, level_change, n_post, outcome_col = "outcome", time_col = "time", verbose = TRUE, ... )estimate_and_calculate( data, level_change, n_post, outcome_col = "outcome", time_col = "time", verbose = TRUE, ... )
data |
Pre-intervention data: a data frame with columns |
level_change |
Numeric. Minimum clinically meaningful effect size (your clinical hypothesis). This is not estimated from data. |
n_post |
Integer. Planned post-intervention follow-up duration. |
outcome_col |
Character. Name of the outcome column. Default
|
time_col |
Character. Name of the time column. Default |
verbose |
Logical. Print parameter and power summaries. Default
|
... |
Additional arguments passed to |
Output from calculate_power().
estimate_its_params(), calculate_power(), run_its_power()
data("example_cfr_data") # Small n_sim for a fast example; use 1000+ for a reportable estimate. result <- estimate_and_calculate( data = example_cfr_data, level_change = -1.0, n_post = 24, n_sim = 100, verbose = FALSE ) result$power_pctdata("example_cfr_data") # Small n_sim for a fast example; use 1000+ for a reportable estimate. result <- estimate_and_calculate( data = example_cfr_data, level_change = -1.0, n_post = 24, n_sim = 100, verbose = FALSE ) result$power_pct
Fits a linear trend to the pre-intervention series and returns the intercept
at , which represents the baseline (mean) outcome at the start
of the observation period.
estimate_baseline(outcome, time = seq_along(outcome))estimate_baseline(outcome, time = seq_along(outcome))
outcome |
Numeric vector of outcome values in the pre-intervention period, ordered chronologically. Minimum 12 observations; 24+ recommended. |
time |
Integer vector of time indices. Defaults to
|
The baseline is the OLS intercept from the linear model
.
When the pre-intervention trend is close to zero (as expected for a
stable outcome), this is approximately equal to the mean of outcome.
A single numeric value: the estimated baseline outcome.
estimate_its_params() for extracting all parameters at once.
data("example_cfr_data") estimate_baseline(example_cfr_data$outcome)data("example_cfr_data") estimate_baseline(example_cfr_data$outcome)
Convenience wrapper that estimates all four nuisance parameters needed for
ITS power calculation from a pre-intervention data frame or numeric vector.
The output can be passed directly to calculate_power() or
run_its_power().
estimate_its_params( data, outcome_col = "outcome", time_col = "time", verbose = TRUE )estimate_its_params( data, outcome_col = "outcome", time_col = "time", verbose = TRUE )
data |
Either:
|
outcome_col |
Character. Name of the outcome column when |
time_col |
Character. Name of the time column when |
verbose |
Logical. If |
All four nuisance parameters are estimated jointly by maximum likelihood
from a single generalised least squares model with AR(1) errors, fitted with
nlme::gls() and nlme::corAR1():
This is the recommended approach because and are
mutually dependent: ordinary least squares estimates without
accounting for autocorrelation (biased upward when ) and then
estimates from serially correlated residuals. Fitting both from
the same likelihood avoids this inconsistency and yields parameters that are
directly compatible with the simulation engine used by calculate_power().
If GLS fails to converge - rare, and usually only for very short series
(fewer than about 12 observations) - the function falls back to the OLS
two-step estimator and records this in the returned method element.
The individual helpers estimate_baseline(), estimate_sigma(),
estimate_rho() and estimate_trend() use the simpler OLS two-step and are
provided for quick, single-parameter checks; for power calculation, prefer
the jointly estimated values returned here.
This function does not estimate level_change or
slope_change. Those are clinical hypotheses - the minimum effects
you would consider meaningful to detect - and must be set based on clinical
judgement or published evidence, not derived from your own data.
A named list with elements:
Integer. Number of pre-intervention observations.
Numeric. Estimated baseline (model intercept).
Numeric. Estimated residual standard deviation.
Numeric. Estimated AR(1) autocorrelation.
Numeric. Estimated pre-intervention trend (slope).
Character. Estimation method actually used: "GLS-ML"
(the default) or "OLS" (the fallback, used only when GLS fails
to converge).
estimate_baseline(), estimate_sigma(), estimate_rho(),
estimate_trend(), calculate_power(), diagnose_params()
data("example_cfr_data") params <- estimate_its_params(example_cfr_data, verbose = TRUE) str(params) # Use directly in calculate_power(): # calculate_power( # n_pre = params$n_pre, # n_post = 24, # baseline = params$baseline, # level_change = -1.0, # your clinical hypothesis # sigma = params$sigma, # rho = params$rho # )data("example_cfr_data") params <- estimate_its_params(example_cfr_data, verbose = TRUE) str(params) # Use directly in calculate_power(): # calculate_power( # n_pre = params$n_pre, # n_post = 24, # baseline = params$baseline, # level_change = -1.0, # your clinical hypothesis # sigma = params$sigma, # rho = params$rho # )
Estimates the first-order autocorrelation coefficient () from the
residuals of a linear trend fit to the pre-intervention series.
estimate_rho(outcome, time = seq_along(outcome))estimate_rho(outcome, time = seq_along(outcome))
outcome |
Numeric vector of outcome values in the pre-intervention period, ordered chronologically. Minimum 12 observations; 24+ recommended. |
time |
Integer vector of time indices. Defaults to
|
The estimate is the Pearson correlation between consecutive residuals:
Positive autocorrelation is nearly universal in routine health data and reduces effective sample size, lowering power relative to a naive calculation that assumes independence.
Typical ranges by aggregation frequency:
Daily: 0.7-0.9
Weekly: 0.5-0.8
Monthly: 0.3-0.5
Quarterly: 0.1-0.4
If outcome has fewer than 3 observations, NA is returned with
a warning.
A single numeric value in : the estimated AR(1)
autocorrelation coefficient.
estimate_its_params() for extracting all parameters at once.
data("example_cfr_data") estimate_rho(example_cfr_data$outcome)data("example_cfr_data") estimate_rho(example_cfr_data$outcome)
Fits a linear trend to the pre-intervention series and returns the standard
deviation of the residuals, used as the noise parameter in
ITS power calculations.
estimate_sigma(outcome, time = seq_along(outcome))estimate_sigma(outcome, time = seq_along(outcome))
outcome |
Numeric vector of outcome values in the pre-intervention period, ordered chronologically. Minimum 12 observations; 24+ recommended. |
time |
Integer vector of time indices. Defaults to
|
is the standard deviation of the detrended pre-intervention
series. It captures how much the outcome varies from one time point to the
next after accounting for any underlying trend. Larger means
noisier data and lower power.
As a rough guide, is typically 10-20\
monthly hospital rates. If your estimate is much larger, consider whether
the outcome series is stable or whether aggregation to a lower frequency
would reduce noise.
A single positive numeric value: the estimated residual SD
().
estimate_its_params() for extracting all parameters at once.
data("example_cfr_data") estimate_sigma(example_cfr_data$outcome)data("example_cfr_data") estimate_sigma(example_cfr_data$outcome)
Fits a linear trend to the pre-intervention series and returns the slope (change per time unit). Ideally this should be close to zero, indicating a stable pre-intervention period.
estimate_trend(outcome, time = seq_along(outcome))estimate_trend(outcome, time = seq_along(outcome))
outcome |
Numeric vector of outcome values in the pre-intervention period, ordered chronologically. Minimum 12 observations; 24+ recommended. |
time |
Integer vector of time indices. Defaults to
|
A non-trivial pre-intervention trend (e.g. )
may indicate that the outcome was not stable before the intervention. This
can bias ITS estimates and should be discussed in study planning.
A single numeric value: the estimated trend (slope) per time unit.
estimate_its_params() for extracting all parameters at once.
data("example_cfr_data") estimate_trend(example_cfr_data$outcome)data("example_cfr_data") estimate_trend(example_cfr_data$outcome)
Monthly case fatality rate (CFR) observations from a hypothetical hospital over a 24-month pre-intervention period. This dataset is used in the package vignettes to illustrate the full PITS workflow: parameter estimation followed by power calculation. It represents the motivating example from the accompanying paper, in which a hospital evaluates whether a clinical decision support system (CDSS) reduces case fatality rate and wishes to determine how long post-intervention follow-up is needed to detect a meaningful effect.
example_cfr_dataexample_cfr_data
A data frame with 24 rows and 2 variables:
Integer. Sequential monthly time index, 1 to 24.
Numeric. Case fatality rate, expressed as a percentage (for example, 3.5 represents 3.5 per cent).
The pre-intervention CFR is approximately 3.6 per cent, with low residual variability (sigma approximately 0.2) and moderate positive autocorrelation (rho approximately 0.3 to 0.5), consistent with monthly hospital data.
Simulated dataset generated for illustrative purposes.
data("example_cfr_data") head(example_cfr_data) plot(example_cfr_data$time, example_cfr_data$outcome, type = "o", xlab = "Month", ylab = "CFR (per cent)", main = "Pre-intervention CFR") # Estimate parameters: params <- estimate_its_params(example_cfr_data)data("example_cfr_data") head(example_cfr_data) plot(example_cfr_data$time, example_cfr_data$outcome, type = "o", xlab = "Month", ylab = "CFR (per cent)", main = "Pre-intervention CFR") # Estimate parameters: params <- estimate_its_params(example_cfr_data)
Writes the output of calculate_power() or power_sweep() to timestamped
files in the specified directory.
export_results(result, dir, prefix = "pits")export_results(result, dir, prefix = "pits")
result |
Output from |
dir |
Character. Output directory, supplied by the user; created if it
does not exist. There is no default: you must choose where files are
written (e.g. a project subdirectory, or |
prefix |
Character. File name prefix. Default |
Invisibly, a named character vector of file paths written.
result <- calculate_power(n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 100) # Write to a temporary directory (use your own path in practice): export_results(result, dir = tempdir())result <- calculate_power(n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 100) # Write to a temporary directory (use your own path in practice): export_results(result, dir = tempdir())
Fits a Gaussian segmented-regression model with AR(1) autocorrelation
correction using nlme::gls(), and returns the p-value for the
coefficient(s) specified by test.
fit_its_model(data, test = c("level", "slope", "both"))fit_its_model(data, test = c("level", "slope", "both"))
data |
A data frame as returned by |
test |
Character. Which effect to test:
|
The model fitted is:
Estimation is by maximum likelihood (method = "ML") to support
likelihood-ratio tests. For the "both" option, the full model is
compared against a model with only a linear trend ().
A single numeric p-value, or NA if the model failed to
converge.
simulate_its_data(), calculate_power()
df <- simulate_its_data( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4 ) fit_its_model(df, test = "level")df <- simulate_its_data( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4 ) fit_its_model(df, test = "level")
Converts a numeric power estimate to a short descriptive label using conventional thresholds.
interpret_power(power)interpret_power(power)
power |
Numeric. Power estimate in |
A character string: "Adequate (>= 80%)", "Borderline
(60-79%)", or "Underpowered (< 60%)".
interpret_power(0.85) interpret_power(0.70) interpret_power(0.45)interpret_power(0.85) interpret_power(0.70) interpret_power(0.45)
Generates and plots one realisation of an ITS dataset, showing the pre-intervention trend, the post-intervention trajectory, the intervention breakpoint, and the fitted segmented regression lines. Useful for illustrating the ITS model in papers and presentations.
plot_its_example( n_pre = 24L, n_post = 24L, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4, seed = 42L, xlab = "Time", ylab = "Outcome", main = "Simulated ITS example", pre_col = "steelblue", post_col = "firebrick" )plot_its_example( n_pre = 24L, n_post = 24L, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4, seed = 42L, xlab = "Time", ylab = "Outcome", main = "Simulated ITS example", pre_col = "steelblue", post_col = "firebrick" )
n_pre |
Integer. Pre-intervention time points. |
n_post |
Integer. Post-intervention time points. |
baseline |
Numeric. Baseline outcome. |
level_change |
Numeric. Level change at the intervention. |
slope_change |
Numeric. Slope change after the intervention. Default 0. |
sigma |
Numeric. Residual SD. |
rho |
Numeric. AR(1) autocorrelation. |
seed |
Integer. Random seed for reproducibility. Default 42. |
xlab |
Character. x-axis label. |
ylab |
Character. y-axis label. |
main |
Character. Plot title. |
pre_col |
Character. Colour for pre-intervention points. |
post_col |
Character. Colour for post-intervention points. |
Invisibly, the simulated data frame.
plot_its_example( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4 )plot_its_example( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4 )
Produces a line plot of estimated power against n_post, as returned
by power_sweep(). Highlights the 80\
adequate duration.
plot_power_curve( sweep_result, target = 80, xlab = "Post-intervention time points (n_post)", ylab = "Estimated power (%)", main = "ITS power curve", col = "steelblue", ... )plot_power_curve( sweep_result, target = 80, xlab = "Post-intervention time points (n_post)", ylab = "Estimated power (%)", main = "ITS power curve", col = "steelblue", ... )
sweep_result |
A data frame as returned by |
target |
Numeric. Power target line (0-100). Default 80. |
xlab |
Character. x-axis label. Default
|
ylab |
Character. y-axis label. Default |
main |
Character. Plot title. Default |
col |
Character. Line colour. Default |
... |
Additional arguments passed to |
Invisibly NULL. Called for its side-effect (plot).
power_sweep(), plot_power_heatmap()
# Small n_sim for a fast example; use 1000+ for a reportable estimate. sweep <- power_sweep( sweep_post = c(12, 24, 36), n_pre = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 50, seed = 42, verbose = FALSE ) plot_power_curve(sweep)# Small n_sim for a fast example; use 1000+ for a reportable estimate. sweep <- power_sweep( sweep_post = c(12, 24, 36), n_pre = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 50, seed = 42, verbose = FALSE ) plot_power_curve(sweep)
Displays estimated power as a colour-coded grid, with one parameter on each axis. Useful for visualising how power responds to combinations of effect size, follow-up duration, noise, or autocorrelation.
plot_power_heatmap( grid_result, x = "n_post", y = "level_change", xlab = x, ylab = y, main = "ITS power heatmap", palette = NULL )plot_power_heatmap( grid_result, x = "n_post", y = "level_change", xlab = x, ylab = y, main = "ITS power heatmap", palette = NULL )
grid_result |
A data frame as returned by |
x |
Character. Name of the column to use as the x-axis variable. |
y |
Character. Name of the column to use as the y-axis variable. |
xlab |
Character. x-axis label. Defaults to |
ylab |
Character. y-axis label. Defaults to |
main |
Character. Plot title. Default |
palette |
Character vector of colours for the gradient from 0 to 100\ power. Defaults to a white-steelblue ramp. |
Invisibly NULL. Called for its side-effect (plot).
run_power_grid(), build_param_grid()
grid <- build_param_grid( n_post = c(12, 24, 36), level_change = c(-1, -2, -3), sigma = 2.5, rho = 0.4 ) results <- run_power_grid(grid, n_sim = 100, verbose = FALSE) plot_power_heatmap(results, x = "n_post", y = "level_change")grid <- build_param_grid( n_post = c(12, 24, 36), level_change = c(-1, -2, -3), sigma = 2.5, rho = 0.4 ) results <- run_power_grid(grid, n_sim = 100, verbose = FALSE) plot_power_heatmap(results, x = "n_post", y = "level_change")
Runs calculate_power() for a vector of post-intervention durations,
returning a data frame of power estimates. Use this to identify the minimum
n_post needed to achieve power.
power_sweep(sweep_post = c(6L, 12L, 18L, 24L, 30L, 36L), ..., verbose = TRUE)power_sweep(sweep_post = c(6L, 12L, 18L, 24L, 30L, 36L), ..., verbose = TRUE)
sweep_post |
Integer vector. |
... |
All other arguments passed to |
verbose |
Logical. If |
A data frame with columns:
Integer. Follow-up duration evaluated.
Numeric. Estimated power (0-1).
Numeric. Power as a percentage.
Character. Qualitative label.
Integer. Number of converged replications.
calculate_power(), plot_power_curve()
# Small n_sim for a fast example; use 1000+ for a reportable estimate. sweep <- power_sweep( sweep_post = c(12, 24, 36), n_pre = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 100, seed = 42, verbose = FALSE ) plot_power_curve(sweep)# Small n_sim for a fast example; use 1000+ for a reportable estimate. sweep <- power_sweep( sweep_post = c(12, 24, 36), n_pre = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 100, seed = 42, verbose = FALSE ) plot_power_curve(sweep)
Displays a formatted summary of the output from calculate_power() or
calculate_power_multi().
## S3 method for class 'pits_power_result' print(x, ...)## S3 method for class 'pits_power_result' print(x, ...)
x |
A |
... |
Ignored. |
Invisibly x.
Displays a formatted table of power estimates across post-intervention
durations, as returned by power_sweep().
## S3 method for class 'pits_sweep_result' print(x, ...)## S3 method for class 'pits_sweep_result' print(x, ...)
x |
A |
... |
Ignored. |
Invisibly x.
Convenience wrapper that replicates the interactive experience of
its_power_tool.R. Runs the power simulation and optionally a design
sweep, prints formatted output to the console, and optionally saves results.
run_its_power( n_pre, n_post, baseline, level_change, slope_change = 0, sigma, rho, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 1000L, seed = 123L, sweep = FALSE, sweep_post = c(6L, 12L, 18L, 24L, 30L, 36L), save_output = FALSE, output_dir = NULL )run_its_power( n_pre, n_post, baseline, level_change, slope_change = 0, sigma, rho, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 1000L, seed = 123L, sweep = FALSE, sweep_post = c(6L, 12L, 18L, 24L, 30L, 36L), save_output = FALSE, output_dir = NULL )
n_pre |
Integer. Pre-intervention time points. |
n_post |
Integer. Post-intervention time points (primary design). |
baseline |
Numeric. Baseline outcome. |
level_change |
Numeric. Expected level change (your clinical hypothesis). |
slope_change |
Numeric. Expected slope change. Default 0. |
sigma |
Numeric. Residual SD. |
rho |
Numeric. AR(1) autocorrelation. |
test |
Character. Effect to test. Default |
alpha |
Numeric. Significance threshold. Default 0.05. |
n_sim |
Integer. Monte Carlo replications. Default 1000. |
seed |
Integer. Random seed. Default 123. |
sweep |
Logical. If |
sweep_post |
Integer vector. |
save_output |
Logical. If |
output_dir |
Character. Directory for saved files, required only when
|
Invisibly, a list with elements result (from
calculate_power()) and, if sweep = TRUE, sweep
(from power_sweep()).
calculate_power(), power_sweep(), estimate_its_params()
run_its_power( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 100, sweep = TRUE )run_its_power( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4, n_sim = 100, sweep = TRUE )
Applies calculate_power() to each row of a parameter grid as produced by
build_param_grid(), returning a data frame with the estimated power for
each combination.
run_power_grid( grid, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 500L, seed = 123L, verbose = TRUE )run_power_grid( grid, test = c("level", "slope", "both"), alpha = 0.05, n_sim = 500L, seed = 123L, verbose = TRUE )
grid |
A data frame as returned by |
test |
Character. Effect to test: |
alpha |
Numeric. Significance threshold. Default 0.05. |
n_sim |
Integer. Monte Carlo replications per cell. Default 500. |
seed |
Integer. Base random seed; each row adds its row index. |
verbose |
Logical. If |
The input grid data frame with columns appended:
power, power_pct, interpretation, n_converged.
build_param_grid(), plot_power_heatmap()
grid <- build_param_grid( n_post = c(12, 24, 36), level_change = c(-2, -3), sigma = 2.5, rho = 0.4 ) results <- run_power_grid(grid, n_sim = 100, verbose = FALSE) plot_power_heatmap(results, x = "n_post", y = "level_change")grid <- build_param_grid( n_post = c(12, 24, 36), level_change = c(-2, -3), sigma = 2.5, rho = 0.4 ) results <- run_power_grid(grid, n_sim = 100, verbose = FALSE) plot_power_heatmap(results, x = "n_post", y = "level_change")
Generates one realisation of an ITS time series under the alternative
hypothesis (i.e. with a true intervention effect), with AR(1)
autocorrelated errors. Used internally by calculate_power().
simulate_its_data( n_pre, n_post, baseline, level_change, slope_change, sigma, rho, pre_trend = 0 )simulate_its_data( n_pre, n_post, baseline, level_change, slope_change, sigma, rho, pre_trend = 0 )
n_pre |
Integer. Number of pre-intervention time points. |
n_post |
Integer. Number of post-intervention time points. |
baseline |
Numeric. Mean outcome at |
level_change |
Numeric. Immediate step change in outcome at the intervention point. Set to 0 if testing slope only. |
slope_change |
Numeric. Change in trend per time unit after the intervention. Set to 0 if testing level only. |
sigma |
Numeric. Residual standard deviation (noise). |
rho |
Numeric. AR(1) autocorrelation coefficient. Must be in
|
pre_trend |
Numeric. Pre-intervention trend (slope) per time unit. Default 0 (stable pre-period). |
The data-generating process is the standard segmented-regression ITS model:
where ,
,
= level_change, = slope_change,
and follows an AR(1) process with innovation SD
.
A data frame with columns:
Integer time index, 1 to .
Binary intervention indicator (0 = pre, 1 = post).
Time elapsed since intervention (0 in pre-period).
Simulated outcome values.
calculate_power(), fit_its_model()
df <- simulate_its_data( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4 ) plot(df$time, df$y, type = "o", pch = 16, xlab = "Time", ylab = "Outcome") abline(v = 24.5, lty = 2, col = "red")df <- simulate_its_data( n_pre = 24, n_post = 24, baseline = 15, level_change = -3, slope_change = 0, sigma = 2.5, rho = 0.4 ) plot(df$time, df$y, type = "o", pch = 16, xlab = "Time", ylab = "Outcome") abline(v = 24.5, lty = 2, col = "red")
Simulates a pre-intervention time series with known parameters. Useful for testing and for vignette examples when real data are unavailable.
simulate_predata( n = 24L, baseline = 15, sigma = 2.5, rho = 0.4, trend = 0, seed = 42L )simulate_predata( n = 24L, baseline = 15, sigma = 2.5, rho = 0.4, trend = 0, seed = 42L )
n |
Integer. Number of time points to generate. Default 24. |
baseline |
Numeric. Mean outcome at |
sigma |
Numeric. Residual standard deviation. Default 2.5. |
rho |
Numeric. AR(1) autocorrelation. Default 0.4. |
trend |
Numeric. Linear trend per time unit. Default 0. |
seed |
Integer or |
A data frame with columns time and outcome.
pre <- simulate_predata(n = 24, baseline = 15, sigma = 2.5, rho = 0.4) plot(pre$time, pre$outcome, type = "o")pre <- simulate_predata(n = 24, baseline = 15, sigma = 2.5, rho = 0.4) plot(pre$time, pre$outcome, type = "o")
Returns an extended summary including the p-value distribution across Monte Carlo replications.
## S3 method for class 'pits_power_result' summary(object, ...)## S3 method for class 'pits_power_result' summary(object, ...)
object |
A |
... |
Ignored. |
Invisibly, a list with power, params, and
pvalue_quantiles.
Checks that a set of ITS parameters is internally consistent and within plausible ranges. Issues warnings for unusual but permitted values.
validate_params( n_pre, n_post, baseline, level_change, sigma, rho, alpha = 0.05, n_sim = 1000L )validate_params( n_pre, n_post, baseline, level_change, sigma, rho, alpha = 0.05, n_sim = 1000L )
n_pre |
Integer. Pre-intervention time points. |
n_post |
Integer. Post-intervention time points. |
baseline |
Numeric. Baseline outcome. |
level_change |
Numeric. Expected level change. |
sigma |
Numeric. Residual SD. |
rho |
Numeric. AR(1) autocorrelation. |
alpha |
Numeric. Significance threshold. |
n_sim |
Integer. Number of simulations. |
Invisibly TRUE if all checks pass; stops with an error on
critical failures.
validate_params(n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4)validate_params(n_pre = 24, n_post = 24, baseline = 15, level_change = -3, sigma = 2.5, rho = 0.4)