Learn R Programming

directadjusting (version 0.6.1)

confidence_intervals: Confidence Intervals

Description

Functions to compute confidence intervals.

Usage

delta_method_confidence_intervals(
  statistics,
  variances,
  conf_lvl = 0.95,
  conf_method = "identity"
)

Value

directadjusting::delta_method_confidence_intervals

Returns a data.table with columns c("statistic", "variance", "ci_lo", "ci_hi").

Arguments

statistics

[numeric] (no default)

Statistics for which to calculate confidence intervals.

variances

[numeric] (no default)

Variance estimates of statistics used to compute confidence intervals.

conf_lvl

[numeric] (default 0.95)

Confidence level of confidence intervals in ]0, 1[.

conf_method

[character, call, list] (default "identity")

Delta method transformation to be applied.

  • character: Use one of the pre-defined transformations. Table of options with the corresponding expressions:

namegg_invg_gradient
identitythetag1
loglog(theta)exp(g)1/theta
log-loglog(-log(theta))exp(-exp(g))1/(theta * log(theta))
logitlog(theta) - log(1 - theta)1/(1 + exp(-g))1/(theta - theta^2)

  • call: A quoted R expression which produces the lower / upper limit when evaluated. E.g. quote(theta * exp(z * theta_standard_error / theta)).

  • list: Contains both the transformation and its inverse. E.g. list(g = quote(log(theta)), g_inv = quote(exp(g))).

Functions

directadjusting::delta_method_confidence_intervals

directadjusting::delta_method_confidence_intervals can be used to compute confidence intervals using the delta method. The following steps are performed:

  • Compute confidence intervals based on conf_method, statistics, variances, and conf_lvl.

    • If conf_method is a string, a pre-defined set of mathematical expressions are used to compute the confidence intervals.

    • If conf_method is a call, it is evaluated with the variables theta, theta_variance, theta_standard_error, and z. This is done once for the lower and once for the upper bound of the confidence interval, so for the lower bound and conf_level = 0.95 we use z = stats::qnorm(p = (1 - conf_lvl) / 2).

    • If conf_method is a list, it must contain elements g and g_inv, e.g. list(g = quote(log(theta)), g_inv = quote(exp(g))).

      • g is passed to [stats::deriv]. If that fails, a numerical derivative is computed.

      • With the derivative known the variance after the transformation is variance * g_gradient ^ 2.

      • With the transformed variance known the transform confidence interval is calculated simply via g(theta) + g_standard_error * z.

      • These transformation-scale confidence intervals are then converted back to the original scale using g_inv.

  • Collect a data.table with the confidence intervals and with also the columns statistics = statistics and variance = variances.

  • Add attribute named ci_meta to the data.table. This attribute is a list which contains elements conf_lvl and conf_method.

  • Return data.table with columns c("statistic", "variance", "ci_lo", "ci_hi").

Examples

Run this code

# directadjusting::delta_method_confidence_intervals
dt_1 <- directadjusting::delta_method_confidence_intervals(
  statistics = 0.9,
  variances = 0.1,
  conf_lvl = 0.95,
  conf_method = "log"
)

# you can also supply your own math for computing the confidence intervals
dt_2 <- directadjusting::delta_method_confidence_intervals(
  statistics = 0.9,
  variances = 0.1,
  conf_lvl = 0.95,
  conf_method = quote(theta * exp(z * theta_standard_error / theta))
)

dt_3 <- directadjusting::delta_method_confidence_intervals(
  statistics = 0.9,
  variances = 0.1,
  conf_lvl = 0.95,
  conf_method = list(
    g = quote(log(theta)),
    g_inv = quote(exp(g))
  )
)

dt_4 <- directadjusting::delta_method_confidence_intervals(
  statistics = 0.9,
  variances = 0.1,
  conf_lvl = 0.95,
  conf_method = list(
    g = quote(stats::qnorm(theta)),
    g_inv = quote(stats::pnorm(g))
  )
)
stopifnot(
  all.equal(dt_1, dt_2, check.attributes = FALSE),
  all.equal(dt_1, dt_3, check.attributes = FALSE)
)

Run the code above in your browser using DataLab