library(dplyr)
df <- data.frame(
indicatorid = rep(c(1234, 5678, 91011, 121314), each = 19 * 2 * 5),
year = rep(2006:2010, each = 19 * 2),
sex = rep(rep(c("Male", "Female"), each = 19), 5),
ageband = rep(c(0,5,10,15,20,25,30,35,40,45,
50,55,60,65,70,75,80,85,90), times = 10),
obs = sample(200, 19 * 2 * 5 * 4, replace = TRUE),
pop = sample(10000:20000, 19 * 2 * 5 * 4, replace = TRUE),
esp2013 = rep(esp2013, 40)
)
## Example 1 - Default execution
df %>%
group_by(indicatorid, year, sex) %>%
calculate_dsr(obs, pop, stdpop = esp2013)
## Example 2 - Calculate both 95% and 99.8% CIs in single execution
df %>%
group_by(indicatorid, year, sex) %>%
calculate_dsr(obs, pop, stdpop = esp2013, confidence = c(0.95, 0.998))
## Example 3 - Drop metadata columns from the output
df %>%
group_by(indicatorid, year, sex) %>%
calculate_dsr(obs, pop, stdpop = esp2013, type = "standard")
## Example 4 - Calculate DSRs for non-independent events
library(tidyr)
# For non-independent events the input data frame must breakdown events into
# counts of unique individuals by event frequency. The code chunk below
# creates a dummy data frame in this required format. Note that assignment of
# 10%, 20% and 70% of events to each event frequency is purely to create a
# data frame in the required format whilst retaining the same total event and
# population distributions by group and age band as example 1 to allow
# comparison of the outputs.
df_freq <- df %>%
mutate(
f3 = floor((obs * 0.1)/3), # 10 % of events in individuals with 3 events
f2 = floor((obs * 0.2)/2), # 20 % of events in individuals with 2 events
f1 = (obs - (3 * f3) - (2 * f2)) # 70% of events in individuals with 1 event
) %>%
select(!"obs") %>%
pivot_longer(
cols = c("f1", "f2", "f3"),
names_to = "eventfrequency",
values_to = "uniqueindividuals",
names_prefix = "f"
) %>%
mutate(eventfrequency = as.integer(eventfrequency))
# Calculate the dsrs - notice that output DSR values match those in
# example 1 but the confidence intervals are wider
df_freq %>%
group_by(indicatorid, year, sex) %>%
calculate_dsr(
x = uniqueindividuals,
n = pop,
stdpop = esp2013,
independent_events = FALSE,
eventfreq = eventfrequency,
ageband = ageband
)
Run the code above in your browser using DataLab