# directadjusting::directly_adjusted_estimates
library("data.table")
set.seed(1337)
offsets <- rnorm(8, mean = 1000, sd = 100)
baseline <- 100
hrs_by_sex <- rep(1:2, each = 4)
hrs_by_ag <- rep(c(0.75, 0.90, 1.10, 1.25), times = 2)
counts <- rpois(8, baseline * hrs_by_sex * hrs_by_ag)
# raw estimates
my_stats <- data.table::data.table(
sex = rep(1:2, each = 4),
ag = rep(1:4, times = 2),
e = counts / offsets,
v = counts / (offsets ** 2)
)
# adjusted by age group
my_adj_stats <- directly_adjusted_estimates(
stats_dt = my_stats,
stat_col_nms = "e",
var_col_nms = "v",
conf_lvls = 0.95,
conf_methods = "log",
stratum_col_nms = "sex",
adjust_col_nms = "ag",
weights = c(200, 300, 400, 100)
)
# adjusted by smaller age groups, stratified by larger age groups
my_stats[, "ag2" := c(1,1, 2,2, 1,1, 2,2)]
my_adj_stats <- directly_adjusted_estimates(
stats_dt = my_stats,
stat_col_nms = "e",
var_col_nms = "v",
conf_lvls = 0.95,
conf_methods = "log",
stratum_col_nms = c("sex", "ag2"),
adjust_col_nms = "ag",
weights = c(200, 300, 400, 100)
)
# with no adjusting columns defined you get the same table as input
# but with confidence intervals. this for the sake of
# convenience for programming cases where sometimes you want to adjust,
# sometimes not.
stats_dt_2 <- data.table::data.table(
sex = 0:1,
e = 0.0,
v = 0.1
)
dt_2 <- directadjusting::directly_adjusted_estimates(
stats_dt = stats_dt_2,
stat_col_nms = "e",
var_col_nms = "v",
conf_lvls = 0.95,
conf_methods = "identity",
stratum_col_nms = "sex"
)
stopifnot(
dt_2[["e"]] == stats_dt_2[["e"]],
dt_2[["v"]] == stats_dt_2[["v"]],
dt_2[["sex"]] == stats_dt_2[["sex"]]
)
# sometimes when adjusting rates or counts, there can be strata where the
# statistic is zero. these should be included in your statistics dataset
# if you still want the weighted average be influenced by the zero.
# otherwise you will get the wrong result. sometimes when naively tabulating
# a dataset with e.g. dt[, .N, keyby = "stratum"] one does not get a result
# row for a stratum that does not appear in the dataset even if we know that
# the stratum exists, for instance only the age groups 1-17 are present in
# the dataset.
stats_dt_3 <- data.table::data.table(
age_group = 1:18,
count = 17:0,
var = 17:0
)
# this goes as intended
dt_3 <- directadjusting::directly_adjusted_estimates(
stats_dt = stats_dt_3,
stat_col_nms = "count",
var_col_nms = "var",
stratum_col_nms = NULL,
adjust_col_nms = "age_group",
weights = data.table::data.table(
age_group = 1:18,
weight = 18:1
)
)
# this does not
dt_4 <- directadjusting::directly_adjusted_estimates(
stats_dt = stats_dt_3[1:17, ],
stat_col_nms = "count",
var_col_nms = "var",
stratum_col_nms = NULL,
adjust_col_nms = "age_group",
weights = data.table::data.table(
age_group = 1:18,
weight = 18:1
)
)
# the weighted average that included the zero is smaller
stopifnot(
dt_3[["count"]] < dt_4[["count"]]
)
# NAs are allowed and produce in turn NAs silently.
stats_dt_5 <- data.table::data.table(
age_group = 1:18,
count = c(NA, 16:0),
var = c(NA, 16:0)
)
dt_5 <- directadjusting::directly_adjusted_estimates(
stats_dt = stats_dt_5,
stat_col_nms = "count",
var_col_nms = "var",
adjust_col_nms = "age_group",
weights = data.table::data.table(
age_group = 1:18,
weight = 18:1
)
)
stopifnot(
is.na(dt_5)
)
stats_dt_6 <- data.table::data.table(
age_group = 1:4,
survival = c(0.20, 0.40, 0.60, 0.80),
var = 0.05 ^ 2
)
# you can use conf_method to pass whatever to
# `delta_method_confidence_intervals`.
dt_6 <- directadjusting::directly_adjusted_estimates(
stats_dt = stats_dt_6,
stat_col_nms = "survival",
var_col_nms = "var",
adjust_col_nms = "age_group",
weights = data.table::data.table(
age_group = 1:4,
weight = 1:4
),
conf_methods = list(
list(
g = quote(stats::qnorm(theta)),
g_inv = quote(stats::pnorm(g))
)
)
)
Run the code above in your browser using DataLab