Parameter Calculation Comparison Summary
parameter_summary.Rd
Implements several approaches to computing partition-aggregated parameters, then tables them up for convenient plotting.
Arguments
- f_param
a function,
f(x)
which transforms the feature (e.g. age), and yields the parameter value. Alternatively, adata.frame
where the first column is the feature (x) and the second is the parameter (y); seexy.coords()
for details. If the latter, combined withpars_interp_opts
, and defaulting to spline interpolation.- f_dense
like
f_param
, either a density function (though it does not have to integrate to 1 like a pdf) or adata.frame
of values. If the latter, combined withdens_interp_opts
and defaulting to constant density from each x to the next.- model_partition
a numeric vector of cut points, which define the partitioning that will be used in the model
- resolution
the number of points to calculate for the underlying
f_param
function.
Value
a data.table
, columns:
model_category
, a integer corresponding to which of the intervals ofmodel_partition
thex
value is inx
, a numeric series from the first to last elements ofmodel_partition
with lengthresolution
method
, a factor with levels:f_val
:f_param(x)
f_mid
:f_param(x_mid)
, wherex_mid
is the midpoint x of themodel_category
f_mean
:f_param(weighted.mean(x, w))
, wherew
defined bydensities
andmodel_category
mean_f
:weighted.mean(f_param(x), w)
, same as previouswm_f
: the result as if having usedparamix::blend()
; this should be very similar tomean_f
, though will be slightly different sinceblend
usesintegrate()
Examples
# COVID IFR from Levin et al 2020 https://doi.org/10.1007/s10654-020-00698-1
f_param <- function(age_in_years) {
(10^(-3.27 + 0.0524 * age_in_years))/100
}
densities <- data.frame(
from = 0:100,
weight = c(rep(1, 66), exp(-0.075 * 1:35))
)
model_partition <- c(0, 5, 20, 65, 101)
ps_dt <- parameter_summary(f_param, densities, model_partition)
ps_dt
#> model_category x method value
#> <int> <num> <fctr> <num>
#> 1: 1 0 f_val 5.370318e-06
#> 2: 1 1 f_val 6.058987e-06
#> 3: 1 2 f_val 6.835968e-06
#> 4: 1 3 f_val 7.712586e-06
#> 5: 1 4 f_val 8.701618e-06
#> ---
#> 501: 4 96 wm_f 9.557924e-02
#> 502: 4 97 wm_f 9.557924e-02
#> 503: 4 98 wm_f 9.557924e-02
#> 504: 4 99 wm_f 9.557924e-02
#> 505: 4 100 wm_f 9.557924e-02
ggplot(ps_dt) + aes(x, y = value, color = method) +
geom_line(data = \(dt) subset(dt, method == "f_val")) +
geom_step(data = \(dt) subset(dt, method != "f_val")) +
theme_bw() + theme(
legend.position = "inside", legend.position.inside = c(0.05, 0.95),
legend.justification = c(0, 1)
) + scale_color_discrete(
"Method", labels = c(
f_val = "f(x)", f_mid = "f(mid(x))", f_mean = "f(E[x])",
mean_f = "discrete E[f(x)]", wm_f = "integrated E[f(x)]"
)
) +
scale_x_continuous("Age", breaks = seq(0, 100, by = 10)) +
scale_y_log10("IFR", breaks = 10^c(-6, -4, -2, 0), limits = 10^c(-6, 0))