Nothing
## ----setup, include = FALSE---------------------------------------------------
library(ggplot2); library(GPAbin); library(ggrepel)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 6,
fig.height = 5,
fig.align = "center"
)
## ----install-cran, eval = FALSE-----------------------------------------------
# install.packages("GPAbin")
## ----install-git, eval = FALSE------------------------------------------------
# devtools::install_github("jnienk/GPAbin")
## ----workflow-overview, eval = FALSE------------------------------------------
# missbp <- missmi(implist) |>
# DRT() |>
# GPAbin() |>
# evaluation(compdat = compdat) |>
# plot()
## ----data---------------------------------------------------------------------
data("wg93", package = "ca")
compdat <- wg93[, c("A", "B", "C", "D", "age")]
str(compdat)
## ----missing------------------------------------------------------------------
set.seed(2024)
p_miss <- c(0.10, 0.15, 0.20, 0.30, 0.40, 0.50)[as.integer(compdat$age)]
missdat <- compdat
for (v in c("A", "B", "C", "D")) {
missdat[runif(nrow(missdat)) < p_miss, v] <- NA
}
round(colMeans(is.na(missdat)) * 100, 1)
## ----missmi-------------------------------------------------------------------
missbp <- missmi(missdat)
missbp
round(missbp$miss_pct, 1)
## ----impute, results = "hide", message = FALSE--------------------------------
set.seed(927)
missbp <- impute(missbp, imp.method = "miceB", m = 5)
## ----impute-check-------------------------------------------------------------
missbp$m
anyNA(missbp$dataimp[[1]])
## ----drt----------------------------------------------------------------------
missbp <- DRT(missbp)
length(missbp$Z)
dim(missbp$Z[[1]])
head(missbp$lvls[[1]], 6)
## ----gpabin-------------------------------------------------------------------
missbp <- missbp |> GPAbin()
## ----before-after-plot, fig.height = 4.5, echo=FALSE--------------------------
gpabin_cols <- c("#4E386E", "#DCAA5F", "#4F9DA6", "#D9728A", "#9686AA")
stack_clps <- function(clp_list, stage) {
do.call(rbind, lapply(seq_along(clp_list), function(i) {
data.frame(Dim1 = clp_list[[i]][, 1],
Dim2 = clp_list[[i]][, 2],
level = missbp$lvls[[i]],
imputation = factor(i),
stage = stage)
}))
}
clps <- rbind(
stack_clps(missbp$CLP, "Before GPA"),
stack_clps(missbp$CLP.GPA.list, "After GPA")
)
clps$stage <- factor(clps$stage, levels = c("Before GPA", "After GPA"))
ggplot(clps, aes(Dim1, Dim2, colour = imputation)) +
geom_point(size = 1.8) +
facet_wrap(~ stage) +
coord_fixed() +
scale_colour_manual(values = gpabin_cols) +
labs(colour = "Imputation") +
theme_void()
## ----plot, fig.height = 6.5---------------------------------------------------
missbp <- plot(missbp,
title = "GPAbin biplot",
CLP.col = "#DCAA5F",
Z.col = "#9686AA")
## ----biplfig, fig.show = "hold", out.width = "75%"----------------------------
missbp |> plot(engine = "base",
title = "GPAbin biplot",
CLP.col = "#DCAA5F",
Z.col = "#9686AA")
## ----plot-custom--------------------------------------------------------------
missbp$plot +
labs(subtitle = "Five imputations, aligned with GPA") +
theme_minimal()
## ----eval---------------------------------------------------------------------
missbp <- evaluation(missbp, compdat = compdat)
missbp$eval.tab
## ----overlay, echo=FALSE------------------------------------------------------
align_to <- function(testee, target) {
testee <- scale(testee, scale = FALSE)
target <- scale(target, scale = FALSE)
s <- svd(crossprod(target, testee))
rho <- sum(s$d) / sum(testee^2)
rho * testee %*% (s$v %*% t(s$u))
}
target <- rbind(missbp$compZs[, 1:2], missbp$compCLPs[, 1:2])
testee <- rbind(missbp$Z.GPAbin[, 1:2], missbp$CLP.GPAbin[, 1:2])
aligned <- align_to(testee, target)
target <- scale(target, scale = FALSE)
clp_rows <- missbp$n + seq_len(nrow(missbp$CLP.GPAbin))
overlay <- data.frame(
level = missbp$complvls,
cx = target[clp_rows, 1], cy = target[clp_rows, 2], # complete data
gx = aligned[clp_rows, 1], gy = aligned[clp_rows, 2] # GPAbin
)
ggplot(overlay) +
geom_segment(aes(x = cx, y = cy, xend = gx, yend = gy),
colour = "grey60") +
geom_point(aes(cx, cy, shape = "Complete data"),
colour = "#4F9DA6", size = 2.5) +
geom_point(aes(gx, gy, shape = "GPAbin"),
colour = "#9686AA", size = 3) +
ggrepel::geom_text_repel(aes(cx, cy, label = level), size = 3) +
scale_shape_manual(name = NULL,
values = c("Complete data" = 16, "GPAbin" = 15)) +
coord_fixed() +
theme_void() +
theme(legend.position = "bottom")
## ----own-imputations----------------------------------------------------------
imp <- mice::mice(missdat, m = 5, method = "polyreg", maxit = 5,
printFlag = FALSE, seed = 1)
implist_own <- mice::complete(imp, "all") # list of 5 completed data frames
own <- missmi(implist_own) |> DRT() |> GPAbin()
own
## ----quick, eval = FALSE------------------------------------------------------
# data(implist)
# missmi(implist) |> DRT() |> GPAbin() |> plot() |> evaluation(compdat = compdat)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.