knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The goal of MIWilson is to implement the Wilson confidence interval for binomial proportions given multiple imputations of missing data.
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("hungf8342/MIWilson")
The functions which calculate confidence intervals (mi_wilson
and mi_wald
) take in a mids object (produced by the mice package), the binary response variable name (the response must be 0-1 valued), a summaries print option (default is TRUE), and a confidence level (default is 0.95).
As an example, let's work with the hypertension (hyp) variable in the nhanes toy dataset. The hyp variable refers to whether an individual has hypertension. It originally has no and yes encoded as 1 and 2, so we need to first re-value the hyp variable. Calculating Wilson and Wald intervals for hyp is straightfoward afterwards.
library(MIWilson) ## setting up correct response variable values and mice object nhanes = mice::nhanes %>% dplyr::mutate(hyp = hyp-1) imp = mice::mice(nhanes) ## MI-Wilson and MI-Wald 99% CIs of the proportion of patients with hypertension mi_wilson(imp,"hyp", 0.99) mi_wald(imp, "hyp", 0.99)
Helper functions (other than Qhats) do not take in mids objects directly since they can be calculated using Qhats (the mean proportions of each imputed dataset).
## Qhats calculates the mean proportion of hyp for each imputed dataset qhats = Qhats(imp,"hyp") ## Keeping track of number of imputed datasets (m) and number of observations in a dataset m = imp$m nrow = imp$data %>% nrow() ## Qbar (mean of Qhats) and Ubar (average response variance over imputed datasets) Qbar(qhats) Ubar(qhats, m, nrow)
If the user provides their own imputed datasets (rather than relying on the mice
package), MI-Wilson allows them to input the corresponding vector of observed binomial proportions instead of a mids object. mi_wilson_phat
and mi_wald_phat
take in a vector of observed binomial proportions (one proportion for each of $m$ imputed datasets), the number of total observations (must be constant across datasets), a summaries print option (default is TRUE), and a confidence level (default is 0.95).
mi_wilson_phat(phats = c(0.2,0.23,0.22), n = 200) mi_wald_phat(phats = c(0.2,0.23,0.22), n = 200)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.