# ---
# title: MICE predicted values and confidence intervals
# author: Michelle María Early Capistrán
# email: earlycapistran@comunidad.unam.mx
# date: March 2021
# Script and data info:
# - This script calculates pooled predicted values from multiply imputed
# repeated analyses from MICE and calculateds 95% confidence intervals
# for pooled predicted values by pooling according to Rubin's Rules.
# - Multiply imputed data is generated by "~R/cons_mice_analysis.R" and
# "~R/cf_mice_analysis.R" and processed with "~/R/imputed_data_wrangling.R"
# - - -
# Load libraries and packages
library("tidyverse")
library("broom")
library("here")
library("devtools")
devtools::load_all("consLettersUtils")
# Load 'mira' objects to estimate confidence intervals
cf_mira <- readRDS("results/cf_mira_final.rds")
cons_mira <- readRDS("results/cons_mira_final.rds")
# Calculate pooled standard error according to Rubin's rules
consSe <- getPooledSE(cons_mira)
cfSe <- getPooledSE(cf_mira)
# Get a dataframe of pooled predicted values across m datasets
consPreds <- getPooledPreds(cons_mira)
cfPreds <- getPooledPreds(cf_mira)
# Calculate upper and lower confidence intervals for pooled predicted values.
# Use 1.96 approximation of margin of error given the normal distribution
# of residuals.
consPreds$upr <- consPreds$means + 1.96 * consSe
consPreds$lwr <- consPreds$means - 1.96 * consSe
cfPreds$upr <- cfPreds$means + 1.96 * cfSe
cfPreds$lwr <- cfPreds$means - 1.96 * cfSe
# Plot
ggplot(data = consPreds, aes(x = yearSerial)) +
geom_line(aes(y = means)) +
geom_line(aes(y = upr), linetype = 2) +
geom_line(aes(y = lwr), linetype = 2)
ggplot(data = cfPreds, aes(x = yearSerial)) +
geom_line(aes(y = means)) +
geom_line(aes(y = upr), linetype = 2) +
geom_line(aes(y = lwr), linetype = 2)
# Save as RDS
cons_pred_ci <- saveRDS(consPreds, "results/cons_pred_ci")
cf_pred_ci <- saveRDS(cfPreds, "results/cf_pred_ci")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.