funnel_plot  R Documentation 
An implementation of funnel plots for indirectly standardised ratios, as described by Spiegelhalter (2005) <https://doi.org/10.1002/sim.1970/>.
There are several parameters for the input, with the assumption that you will want smooth,
overdispersed, funnel control limits. Limits may be inflated for overdispersion based on the methods of DerSimonian & Laird (1986), buy calculating a between unit standard deviation (\tau
)
and constructing an additive random effects models, originally used for metaanalyses of clinical trials data.
funnel_plot(
numerator,
denominator,
group,
data_type = "SR",
limit = 99,
label = "outlier",
highlight = NA,
draw_unadjusted = FALSE,
draw_adjusted = TRUE,
sr_method = "SHMI",
trim_by = 0.1,
title = "Untitled Funnel Plot",
multiplier = 1,
x_label = "Expected",
y_label,
x_range = "auto",
y_range = "auto",
plot_cols = c("#FF7F0EFF", "#FF7F0EFF", "#1F77B4FF", "#1F77B4FF", "#9467BDFF",
"#9467BDFF", "#2CA02CFF", "#2CA02CFF"),
theme = funnel_clean(),
label_outliers,
Poisson_limits,
OD_adjust,
xrange,
yrange,
SHMI_rounding = TRUE
)
numerator 
A vector of the numerator (observed events/counts) values. Used as numerator of the Yaxis 
denominator 
A vector of denominator (predicted/population etc.) Used as denominator of the Yaxis and the scale of the xaxis 
group 
A vector of group names as character or factor. Used to aggregate and group points on plots 
data_type 
A string identifying the type of data used for in the plot, the adjustment used and the reference point. One of: "SR" for indirectly standardised ratios, such SHMI, "PR" for proportions, or "RC" for ratios of counts. Default is "SR". 
limit 
Plot limits, accepted values are: 95 or 99, corresponding to 95% or 99.8% quantiles of the distribution. Default=99,and applies to OD limits if both OD and Poisson are used. 
label 
Whether to label outliers, highlighted groups, both or none. Default is "outlier", by accepted values are:

highlight 
Single or vector of points to highlight, with a different colour and point style. Should correspond to values specified to 'group'. Default is NA, for no highlighting. 
draw_unadjusted 
Draw control limits without overdispersion adjustment. (default=FALSE) 
draw_adjusted 
Draw overdispersed limits using hierarchical model, assuming at group level, as described in Spiegelhalter (2012).
It calculates a second variance component ' for the 'between' standard deviation ( 
sr_method 
Method for adjustment when using indirectly standardised ratios (type="SR") Either "CQC" or "SHMI" (default). There are a few methods for standardisation. "CQC"/Spiegelhalter
uses a squareroot transformation and Winsorises (rescales the outer most values to a particular percentile).
SHMI, instead, uses logtransformation and doesn't Winsorise, but truncates the distribution before assessing overdisperison .
Both methods then calculate a dispersion ratio ( 
trim_by 
Proportion of the distribution for winsorisation/truncation. Default is 10 % (0.1). Note, this is applied in a twosided fashion, e.g. 10% refers to 10% at each end of the distribution (20% winsorised/truncated) 
title 
Plot title 
multiplier 
Scale relative risk and funnel by this factor. Default to 1, but 100 sometime used, e.g. in some hospital mortality ratios. 
x_label 
Title for the funnel plot xaxis. Usually expected deaths, readmissions, incidents etc. 
y_label 
Title for the funnel plot yaxis. Usually a standardised ratio. 
x_range 
Manually specify the yaxis min and max, in form c(min, max), e.g. c(0, 200). Default, "auto", allows function to estimate range. 
y_range 
Manually specify the yaxis min and max, in form c(min, max), e.g. c(0.7, 1.3). Default, "auto", allows function to estimate range. 
plot_cols 
A vector of 8 colours for funnel limits, in order: 95% Poisson (lower/upper), 99.8% Poisson (lower/upper), 95% ODadjusted (lower/upper), 99.8% ODadjusted (lower/upper). Default has been chosen to avoid red and green which can lead to subconscious value judgements of good or bad. Default is hex colours: c("#FF7F0EFF", "#FF7F0EFF", "#1F77B4FF","#1F77B4FF", "#9467BDFF", "#9467BDFF", "#2CA02CFF", "#2CA02CFF") 
theme 
a ggplot theme function. This can be a canned theme such as theme_bw(), a theme() with arguments, or your own custom theme function. Default is new funnel_clean(), but funnel_classic() is original format. 
label_outliers 
Deprecated. Please use the 'label' argument instead. 
Poisson_limits 
Deprecated. Please use the 'draw_unadjusted' argument instead. 
OD_adjust 
Deprecated. Please use the 'draw_adjusted' argument instead. 
xrange 
Deprecated. Please use the 'x_range' argument instead. 
yrange 
Deprecated. Please use the 'y_range' argument instead. 
SHMI_rounding 
TRUE/FALSE, for SHMI calculation (standardised ratio, with SHMI truncation etc.), should you round the expected values to 2 decimal places (TRUE) or not (FALSE) 
Outliers are marked based on the grouping, and the limits chosen, corresponding to either 95% or 99.8% quantiles of the normal distribution.
Labels can attached using the 'label' argument.
Overdispersion can be factored in based on the methods in Spiegelhalter et al. (2012), set 'draw_adjusted' to FALSE to suppress this.
To use Poisson limits set 'draw_unadjusted=TRUE'.
The plot colours deliberately avoid redambergreen colouring, but you could extract this from the ggplot object and change manually if you like.
Future versions of 'funnelplotr' may allow users to change this.
A fitted 'funnelplot' object. A 'funnelplot' object is a list containing the following components:
print 
Prints the number of points, outliers and whether the plot has been adjusted, and prints the plot 
plot 
A ggplot object with the funnel plot and the appropriate limits 
limits_lookup 
A lookup table with selected limits for drawing a plot in software that requires limits. 
aggregated_data 
A data.frame of the the aggregated dataset used for the plot. 
outlier 
A data frame of outliers from the data. 
tau2 
The betweengroups standard deviation, 
phi 
The dispersion ratio, 
draw_adjusted 
Whether overdispersionadjusted limits were used. 
draw_unadjusted 
Whether unadjusted Poisson limits were used. 
DerSimonian & Laird (1986) Metaanalysis in clinical trials. <doi:10.1016/01972456(86)900462>
Spiegelhalter (2005) Funnel plots for comparing institutional performance <doi:10.1002/sim.1970>
Spiegelhalter et al. (2012) Statistical methods for healthcare regulation: rating, screening and surveillance: <doi:10.1111/j.1467985X.2011.01010.x>
NHS Digital (2020) SHMI Methodology v .134 https://digital.nhs.uk/dataandinformation/publications/cihub/summaryhospitallevelmortalityindicatorshmi
# We will use the 'medpar' dataset from the 'COUNT' package.
# Little reformatting needed
library(COUNT)
data(medpar)
medpar$provnum<factor(medpar$provnum)
medpar$los<as.numeric(medpar$los)
mod< glm(los ~ hmo + died + age80 + factor(type)
, family="poisson", data=medpar)
# Get predicted values for building ratio
medpar$prds< predict(mod, type="response")
# Draw plot, returning just the plot object
fp<funnel_plot(denominator=medpar$prds, numerator=medpar$los,
group = medpar$provnum, limit=95, title="An example funnel plot")
# Methods for viewing/extracting
print(fp)
plot(fp)
summary(fp)
limits(fp)
outliers(fp)
source_data(fp)
phi(fp)
tau2(fp)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.