Skip to content

Commit 5f3628d

Browse files
committed
Tweaks from checks
1 parent edceab3 commit 5f3628d

14 files changed

Lines changed: 97 additions & 66 deletions

DESCRIPTION

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: FunnelPlotR
22
Type: Package
3-
Title: Funnel plots for comparing institutional performance
4-
Version: 0.2.9999
3+
Title: Funnel Plots for Comparing Institutional Performance
4+
Version: 0.3
55
Authors@R: c(
66
person("Chris", "Mainey", ,"chris.mainey@uhb.nhs.uk", role= c("aut", "cre"),
77
comment = c(ORCID ="0000-0002-3018-6171")
@@ -15,10 +15,10 @@ URL: https://chrismainey.github.io/FunnelPlotR, https://github.com/chrismainey/F
1515
BugReports: https://github.com/chrismainey/FunnelPlotR/issues
1616
Encoding: UTF-8
1717
LazyData: true
18-
Imports: ggrepel,
18+
Imports: dplyr,
19+
ggrepel,
20+
ggplot2,
1921
scales
20-
Depends: ggplot2,
21-
dplyr
2222
RoxygenNote: 7.1.1
2323
Suggests:
2424
testthat (>= 2.1.0),

NEWS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# FunnelPlotR 0.2.9999
1+
# FunnelPlotR 0.3.0
22

3-
- Added methods for ratio of counts and proportions
4-
- Renamed several arguments
3+
- Added methods for ratios of counts and proportions
4+
- Renamed several arguments - please read the help file!
55
- Added 'themes' as people don't like the ggplot grey as default
66
- Broke process into much smaller functions for easier editing and speed
77
- Tweaked vignette to make it work
8-
- Removed unecceary rlang dependency
8+
- Removed unnecessary rlang dependency
99

1010
# FunnelPlotR 0.2.3
1111

R/funnel_plot.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @param group A vector of group names as character or factor. Used to aggregate and group points on plots
1010
#' @param data_type A string identifying the type of data used for in the plot, the adjustment used and the reference point. One of: "SR" forindirectly standardised ratios, such SHMI, "PR" for proportions, or "RC" for ratios of counts. Default is "SR".
1111
#' @param title Plot title
12-
#' @param label_outliers Add group labels to outliers on plot. Accepted values are: 95 or 99 corresponding to 95\% or 99.8\% quantiles of the distribution. Default=99
12+
#' @param label_outliers Add group labels to outliers on plot. 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.
1313
#' @param Poisson_limits Draw exact Poisson limits, without overdispersion adjustment. (default=FALSE)
1414
#' @param OD_adjust Draw overdispersed limits using hierarchical model, assuming at group level, as described in Spiegelhalter (2012) <doi:https://doi.org/10.1111/j.1467-985X.2011.01010.x>.
1515
#' It calculates a second variance component ' for the 'between' standard deviation (Tau2), that is added to the 'within' standard deviation (sigma) (default=TRUE)
@@ -18,7 +18,7 @@
1818
#' SHMI, instead, uses log-transformation and doesn't Winsorise, but truncates the distribution before assessing overdisperison.
1919
#' Both methods then calculate a dispersion ratio (phi) on this altered dataset. This ratio is then used to scale the full dataset,
2020
#' and the plot is drawn for the full dataset.
21-
#' @param winsorise_by Proportion of the distribution for winsorisation/truncation. Default is 10 \% (0.1). Note, this is applied in a two-sided
21+
#' @param trim_by Proportion of the distribution for winsorisation/truncation. Default is 10 \% (0.1). Note, this is applied in a two-sided
2222
#' fashion, e.g. 10\% refers to 10\% at each end of the distribution (20\% winsorised/truncated)
2323
#' @param multiplier Scale relative risk and funnel by this factor. Default to 1, but 100 sometime used, e.g. in some hospital mortality ratios.
2424
#' @param x_label Title for the funnel plot x-axis. Usually expected deaths, readmissions, incidents etc.
@@ -71,7 +71,7 @@
7171

7272

7373
funnel_plot <- function(numerator, denominator, group, data_type = "SR", label_outliers = 99,
74-
Poisson_limits = FALSE, OD_adjust = TRUE, sr_method = "SHMI", winsorise_by = 0.1,
74+
Poisson_limits = FALSE, OD_adjust = TRUE, sr_method = "SHMI", trim_by = 0.1,
7575
title="Untitled Funnel Plot", multiplier = 1, x_label = "Expected",
7676
y_label ,xrange = "auto", yrange = "auto",
7777
return_elements=c("plot", "data", "limits"), theme = funnel_clean()){
@@ -131,7 +131,11 @@ funnel_plot <- function(numerator, denominator, group, data_type = "SR", label_o
131131
mod_plot_agg <- transformed_zscore(mod_plot_agg=mod_plot_agg, data_type = data_type, sr_method = sr_method)
132132

133133
# Winsorise or truncate depending on method
134-
mod_plot_agg <- winsorisation(mod_plot_agg = mod_plot_agg, data_type=data_type, sr_method = sr_method, winsorise_by=winsorise_by)
134+
if(data_type=="SR" & sr_method=="SHMI"){
135+
mod_plot_agg <- truncation(mod_plot_agg = mod_plot_agg, trim_by=trim_by)
136+
} else {
137+
mod_plot_agg <- winsorisation(mod_plot_agg = mod_plot_agg, trim_by=trim_by)
138+
}
135139

136140
n <- as.numeric(sum(!is.na(mod_plot_agg$Wuzscore)))
137141
# Calculate Phi (the overdispersion factor)

R/od_adjustment.R

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,44 +66,63 @@ transformed_zscore<-function(mod_plot_agg=mod_plot_agg, data_type = "SR", sr_met
6666

6767
#' Winsorisation function
6868
#'
69-
#' @description Internal function to perform the winsorisation or truncation.
69+
#' @description Internal function to perform the Winsorisation.
7070
#'
7171
#' @param mod_plot_agg Aggregated model input data
72-
#' @param data_type Type of data for adjustment and plotting: Indirectly Standardised ratio (\"SR\"), proportion (\"PR\"), or ratio of counts (\"RC\").
73-
#' @param sr_method Adjustment method for standardised ratios, can take the value \"SHMI\" or \"CQC\". \"SHMI\" is default. Not relevant to PR or RC data types
74-
#' @param winsorise_by The amount to winsorise\/truncate the distribution by, prior to transformation. 0.1 means 10\% (at each end).
72+
#' @param trim_by The amount to Winsorise the distribution by, prior to transformation. 0.1 means 10\% (at each end).
7573
#'
76-
#' @return A data.frame with winsorized z-scores returned added
74+
#' @return A data.frame with winsorised z-scores returned added
7775
#' @keywords internal
7876
#'
7977
#'
80-
winsorisation <- function(mod_plot_agg = mod_plot_agg, data_type=data_type, sr_method = "SHMI", winsorise_by = 0.1){
78+
winsorisation <- function(mod_plot_agg = mod_plot_agg, trim_by = 0.1){
8179

82-
lz <- quantile(x = mod_plot_agg$Uzscore, winsorise_by, na.rm = TRUE)
83-
uz <- quantile(x = mod_plot_agg$Uzscore, (1 - winsorise_by), na.rm = TRUE)
80+
lz <- quantile(x = mod_plot_agg$Uzscore, trim_by, na.rm = TRUE)
81+
uz <- quantile(x = mod_plot_agg$Uzscore, (1 - trim_by), na.rm = TRUE)
8482
mod_plot_agg$winsorised = ifelse(mod_plot_agg$Uzscore > lz & mod_plot_agg$Uzscore < uz, 0, 1)
8583

86-
if(data_type == "SR" & sr_method == "SHMI"){
87-
88-
#mod_plot_agg$Wuzscore[mod_plot_agg$winsorised == 1] <- mod_plot_agg$Uzscore
89-
mod_plot_agg$Wuzscore <-ifelse(mod_plot_agg$winsorised == 0, mod_plot_agg$Uzscore, NA)
90-
91-
} else {
92-
93-
#if(sr_method == "CQC"){
94-
95-
mod_plot_agg$Wuzscore = ifelse(mod_plot_agg$Uzscore < lz
84+
mod_plot_agg$Wuzscore = ifelse(mod_plot_agg$Uzscore < lz
9685
, lz
9786
, ifelse(mod_plot_agg$Uzscore > uz
9887
, uz
9988
, mod_plot_agg$Uzscore))
100-
}
89+
90+
return(mod_plot_agg)
91+
}
92+
93+
#' Truncation function for NHSD method
94+
#'
95+
#' @description Internal function to perform the truncation.
96+
#'
97+
#' @param mod_plot_agg Aggregated model input data
98+
#' @param trim_by The amount to truncate the distribution by, prior to transformation. 0.1 means 10\% (at each end).
99+
#'
100+
#' @return A data.frame with truncated z-scores added
101+
#' @keywords internal
102+
#'
103+
#'
104+
truncation <- function(mod_plot_agg = mod_plot_agg, trim_by = 0.1){
105+
106+
# How many groups for truncation
107+
k <- 1/trim_by
108+
maxk<- k-1
109+
mink<-min(k/k)-1
110+
111+
mod_plot_agg$rk <- rank(mod_plot_agg$Uzscore, ties.method = "average")
112+
mod_plot_agg$sp <- floor(mod_plot_agg$rk * k / (length(mod_plot_agg$rk) + 1))
113+
114+
mod_plot_agg$truncated = ifelse(mod_plot_agg$sp > mink & mod_plot_agg$Uzscore < maxk, 0, 1)
115+
116+
mod_plot_agg$Wuzscore = ifelse(mod_plot_agg$truncated == 1
117+
, NA
118+
, mod_plot_agg$Uzscore)
119+
101120
return(mod_plot_agg)
102121
}
103122

104123

105124

106-
#' Transformation function for z-scoring
125+
#' Calculate overdispersion ratio
107126
#'
108127
#' @description Internal function to perform the transformations for data types.
109128
#'

README.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ knitr::opts_chunk$set(
2121

2222

2323

24-
# Funnel plots for comparing institutional performance <img src="man/figures/logo.png" width="160px" align="right" />
24+
# Funnel Plots for Comparing Institutional Performance <img src="man/figures/logo.png" width="160px" align="right" />
2525

2626
<!-- badges: start -->
2727
[![Travis build status](https://travis-ci.org/chrismainey/FunnelPlotR.svg?branch=master)](https://travis-ci.org/chrismainey/FunnelPlotR)
@@ -49,7 +49,7 @@ Methods are based on those presented in Spiegelhalter's papers and the Care Qual
4949
This uses a log-transformation and truncation of the distribution for calculating overdispersion, whereas Spiegelhalter's methods use a square-root and Winsorisation.
5050

5151

52-
This package was originally developed for use in the author's PhD project, but published on github in case it's of use for others.
52+
This package was originally developed for use in the author's PhD project, but published on Github in case it's of use for others.
5353

5454
Contributions are welcome. Please note that the 'FunnelPlotR' project is released with a
5555
[Contributor Code of Conduct](https://chrismainey.github.io/FunnelPlotR/CODE_OF_CONDUCT.html).

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Funnel plots for comparing institutional performance <img src="man/figures/logo.png" width="160px" align="right" />
2+
# Funnel Plots for Comparing Institutional Performance <img src="man/figures/logo.png" width="160px" align="right" />
33

44
<!-- badges: start -->
55

@@ -55,7 +55,7 @@ calculating overdispersion, whereas Spiegelhalter’s methods use a
5555
square-root and Winsorisation.
5656

5757
This package was originally developed for use in the author’s PhD
58-
project, but published on github in case it’s of use for others.
58+
project, but published on Github in case it’s of use for others.
5959

6060
Contributions are welcome. Please note that the ‘FunnelPlotR’ project is
6161
released with a [Contributor Code of

cran-comments.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
## Release summary
2-
This is a minor bug-fix for the FunnelPlotR where some plot limits were incorrectly coloured3.
2+
This is a major release for the FunnelPlotR package, adding more methods, changes to uer interface, and removing dependencies.
33

44
## Test environments
5-
* local windows 7, R 3.6.1
65
* local windows 10, R 3.6.1
76
* Windows dev on Win-builder
87
* ubuntu 16.04.6 LTS (on travis-ci), R 3.6.1

dev/tests.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ library(COUNT)
33
library(ggplot2)
44

55
# Setup
6-
data_type = "PR"
6+
data_type = "SR"
77
label_outliers = 95
88
Poisson_limits = FALSE
99
OD_adjust = TRUE
1010
sr_method = "SHMI"
11-
Winsorise_by = 0.1
11+
trim_by = 0.1
1212
title="Untitled Funnel Plot"
1313
multiplier = 1
1414
x_label = "Expected"
@@ -31,7 +31,7 @@ medpar$prds<- predict(mod, type="response")
3131

3232
# Draw plot, returning just the plot object
3333
fp2<-funnel_plot(denominator=medpar$prds,numerator=medpar$los,
34-
group = medpar$provnum, label_outliers = 99,
34+
group = medpar$provnum, label_outliers = 95,
3535
Poisson_limits = TRUE)
3636

3737
fp2

man/figures/README-funnel2-1.png

-2.67 KB
Loading

man/funnel_plot.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)