|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
summarize_map <- function( |
|
data, |
|
x_cols, |
|
y_cols, |
|
n_examples = 4, |
|
verbose = FALSE) { |
|
|
|
|
|
x_cols <- tidyselect::eval_select(rlang::enquo(x_cols), data) |
|
y_cols <- tidyselect::eval_select(rlang::enquo(y_cols), data) |
|
xUy_cols <- union(x_cols, y_cols) |
|
names(xUy_cols) <- names(data[xUy_cols]) |
|
|
|
if(verbose) { |
|
cat("The following is a report of the relationship between two different ways of identifying instances\n") |
|
} |
|
|
|
|
|
problems <- list() |
|
|
|
count_xUy <- data |> |
|
dplyr::count(dplyr::across(tidyselect::all_of(xUy_cols))) |> |
|
dplyr::ungroup() |
|
count_x <- count_xUy |> |
|
dplyr::count(dplyr::across(tidyselect::all_of(names(x_cols))), name = "size") |> |
|
dplyr::ungroup() |
|
count_y <- count_xUy |> |
|
dplyr::count(dplyr::across(tidyselect::all_of(names(y_cols))), name = "size") |> |
|
dplyr::ungroup() |
|
|
|
if (verbose) { |
|
cat("\nProperties of X identifiers:\n") |
|
} |
|
cat("X<-[", paste(names(x_cols), collapse = ", "), "]:\n", sep = "") |
|
cat(" |X|: ", count_x |> stats::na.omit(method = "r") |> nrow(), sep = "") |
|
|
|
na_count <- data |> |
|
dplyr::select(tidyselect::all_of(x_cols)) |> |
|
stats::complete.cases() |> |
|
magrittr::not() |> |
|
sum() |
|
cat( |
|
ifelse( |
|
na_count == 0, |
|
"", |
|
paste0(" (", na_count, " NA)")), |
|
"\n", sep = "") |
|
|
|
size_dist <- count_x |> |
|
stats::na.omit(method = "r") |> |
|
dplyr::count(size) |> |
|
dplyr::ungroup() |
|
if (nrow(size_dist) < 12) { |
|
cat(" count*size: ", |
|
paste(size_dist$n, size_dist$size, sep = "*", collapse = ", "), |
|
"\n", sep = "") |
|
} else { |
|
top <- 1:6 |
|
bottom <- (nrow(size_dist) - 6+1):nrow(size_dist) |
|
cat(" count*size: ", |
|
paste( |
|
size_dist$n[top], |
|
size_dist$size[top], sep = "*", collapse = ", "), |
|
", ... ", |
|
paste( |
|
size_dist$n[bottom], |
|
size_dist$size[bottom], sep = "*", collapse = ", "), |
|
"\n", sep="") |
|
} |
|
|
|
if (verbose) { |
|
cat("\nProperties of the Y identifiers:\n") |
|
} |
|
cat("Y<-[", paste(names(y_cols), collapse = ", "), "]:\n", sep = "") |
|
cat(" |Y|: ", count_y |> stats::na.omit(method = "r") |> nrow(), sep = "") |
|
na_count <- data |> |
|
dplyr:::select(tidyselect::all_of(y_cols)) |> |
|
stats::complete.cases() |> |
|
magrittr::not() |> |
|
sum() |
|
cat(ifelse(na_count == 0, "", paste0(" (", na_count, " NA)")), "\n", sep = "") |
|
|
|
size_dist <- count_y |> |
|
stats::na.omit(method = "r") |> |
|
dplyr::count(size) |> |
|
dplyr::ungroup() |
|
if (nrow(size_dist) < 12) { |
|
cat(" count*size: ", |
|
paste(size_dist$n, size_dist$size, sep = "*", collapse = ", "), |
|
"\n", sep = "") |
|
} else { |
|
top <- 1:6 |
|
bottom <- (nrow(size_dist) - 6+1):nrow(size_dist) |
|
cat(" count*size: ", |
|
paste( |
|
size_dist$n[top], |
|
size_dist$size[top], sep = "*", collapse = ", "), |
|
", ... ", |
|
paste( |
|
size_dist$n[bottom], |
|
size_dist$size[bottom], sep = "*", collapse = ", "), |
|
"\n", sep="") |
|
} |
|
|
|
if (verbose) { |
|
cat("\nProperties of the intersection of union of the X and Y identifiers:\n") |
|
} |
|
cat("[X U Y]:\n") |
|
cat(" |X U Y|: ", count_xUy |> stats::na.omit(method = "r") |> nrow(), sep = "") |
|
na_count <- data |> |
|
dplyr:::select(!!!xUy_cols) |> |
|
stats::complete.cases() |> |
|
magrittr::not() |> |
|
sum() |
|
cat(ifelse(na_count == 0, "", paste0(" (", na_count, " NA)")), "\n", sep = "") |
|
|
|
size_dist <- count_xUy |> |
|
stats::na.omit(method = "r") |> |
|
dplyr::rename(size = n) |> |
|
dplyr::count(size) |> |
|
dplyr::ungroup() |
|
if (nrow(size_dist) < 12) { |
|
cat(" count*size: ", |
|
paste(size_dist$n, size_dist$size, sep = "*", collapse = ", "), |
|
"\n", sep="") |
|
} else { |
|
top <- 1:6 |
|
bottom <- (nrow(size_dist) - 6+1):nrow(size_dist) |
|
cat(" count*size: ", |
|
paste( |
|
size_dist$n[top], |
|
size_dist$size[top], sep = "*", collapse = ", "), |
|
", ... ", |
|
paste( |
|
size_dist$n[bottom], |
|
size_dist$size[bottom], sep = "*", collapse = ", "), |
|
"\n", sep = "") |
|
} |
|
|
|
|
|
count_xUy <- count_xUy |> stats::na.omit(method = "r") |
|
|
|
if (verbose) { |
|
cat("Properties of the intersection of the X and Y identifiers:\n") |
|
} |
|
cat("[X @ Y]:\n") |
|
if (verbose) { |
|
cat(" Number of X and Y identifiers that are 1 to 1:\n") |
|
} |
|
cat(" |X ~ Y|: ", |
|
count_xUy |> |
|
dplyr::semi_join( |
|
count_x |> dplyr::filter(size == 1), |
|
by = names(x_cols)) |> |
|
dplyr::semi_join( |
|
count_y |> dplyr::filter(size == 1), |
|
by = names(y_cols)) |> |
|
nrow(), |
|
"\n", sep = "") |
|
|
|
if (verbose) { |
|
cat(" Number of X and Y identifiers where an X identifier maps to multiple Y identifiers:\n") |
|
} |
|
cat( |
|
" |X:X < Y|, |Y:Y < X|: ", |
|
count_xUy |> |
|
dplyr::semi_join( |
|
count_x |> dplyr::filter(size > 1), |
|
by = names(x_cols)) |> |
|
nrow(), |
|
", ", |
|
count_xUy |> |
|
dplyr::count( |
|
dplyr::across(tidyselect::all_of(names(x_cols))), |
|
name = "size") |> |
|
dplyr::filter(size > 1) |> |
|
nrow(), |
|
"\n", sep = "") |
|
|
|
if (verbose) { |
|
cat( |
|
" Number of X and Y identifiers where a Y identifier maps to ", |
|
"multiple X identifiers:\n") |
|
} |
|
cat( |
|
" |X:X > Y|, |Y:Y > X|: ", |
|
count_xUy |> |
|
dplyr::semi_join( |
|
count_y |> |
|
dplyr::filter(size > 1), |
|
by = names(y_cols)) |> |
|
nrow(), |
|
", ", |
|
count_xUy |> |
|
dplyr::count( |
|
dplyr::across(tidyselect::all_of(names(y_cols))), |
|
name = "size") |> |
|
dplyr::filter(size > 1) |> |
|
nrow(), |
|
"\n", sep = "") |
|
|
|
|
|
ex_rows <- data |> |
|
dplyr:::select(tidyselect::all_of(x_cols)) |> |
|
stats::complete.cases() |> |
|
magrittr::not() |> |
|
which() |
|
if (length(ex_rows)) { |
|
if (!is.null(n_examples) && (n_examples < length(ex_rows))) { |
|
ex_rows <- ex_rows |> sample(n_examples, replace = FALSE) |
|
} |
|
problems$is.na.X <- data |> |
|
dplyr::slice(ex_rows) |> |
|
dplyr::arrange(dplyr::across(tidyselect::all_of(names(x_cols)))) |
|
} |
|
|
|
|
|
ex_rows <- data |> |
|
dplyr:::select(tidyselect::all_of(y_cols)) |> |
|
stats::complete.cases() |> |
|
magrittr::not() |> |
|
which() |
|
if (length(ex_rows)) { |
|
if (!is.null(n_examples) && (n_examples < length(ex_rows))) { |
|
ex_rows <- ex_rows |> sample(n_examples, replace = FALSE) |
|
} |
|
problems$is.na.Y <- data |> |
|
dplyr::slice(ex_rows) |> |
|
dplyr::arrange(dplyr::across(tidyselect::all_of(names(y_cols)))) |
|
} |
|
|
|
|
|
dup.X <- count_xUy |> |
|
dplyr::filter(n == 1) |> |
|
dplyr::count( |
|
dplyr::across(tidyselect::all_of(names(x_cols))), |
|
name = "size") |> |
|
dplyr::filter(size > 1) |> |
|
dplyr::ungroup() |> |
|
dplyr:::select(-size) |
|
if (nrow(dup.X) > 1) { |
|
if (!is.null(n_examples) && (n_examples < nrow(dup.X))) { |
|
dup.X <- dup.X |> dplyr::sample_n(n_examples, replace = FALSE) |
|
} |
|
problems$dup.X <- dup.X |> |
|
dplyr::left_join(data, by = names(x_cols)) |> |
|
dplyr::arrange(dplyr::across(tidyselect::all_of(names(x_cols)))) |
|
} |
|
|
|
|
|
dup.Y <- count_xUy |> |
|
dplyr::filter(n == 1) |> |
|
dplyr::count( |
|
dplyr::across(tidyselect::all_of(names(y_cols))), |
|
name = "size") |> |
|
dplyr::filter(size > 1) |> |
|
dplyr::ungroup() |> |
|
dplyr:::select(-size) |
|
if (nrow(dup.Y) > 1) { |
|
if (!is.null(n_examples) && (n_examples < nrow(dup.Y))) { |
|
dup.Y <- dup.Y |> dplyr::sample_n(n_examples, replace = FALSE) |
|
} |
|
problems$dup.Y <- dup.Y |> |
|
dplyr::left_join(data, by = names(ycols)) |> |
|
dplyr::arrange(dplyr::across(tidyselect::all_of(names(y_cols)))) |
|
} |
|
|
|
|
|
dup.XUY <- count_xUy |> |
|
dplyr::filter(n > 1) |> |
|
dplyr:::select(-n) |
|
if (nrow(dup.XUY) > 1) { |
|
if (!is.null(n_examples) && (n_examples < nrow(dup.XUY))) { |
|
dup.XUY <- dup.XUY |> dplyr::sample_n(n_examples, replace = FALSE) |
|
} |
|
problems$dup.XUY <- dup.XUY |> |
|
dplyr::left_join(data, by = names(xUy_cols)) |> |
|
dplyr::arrange(dplyr::across(tidyselect::all_of(names(xUy_cols)))) |
|
} |
|
if (verbose) { |
|
cat("Returned instances where:\n") |
|
cat("\tis.na.X: The X identifier is NA\n") |
|
cat("\tis.na.Y: The Y identifier is NA\n") |
|
cat("\tdup.X: The X identifier is not unique\n") |
|
cat("\tdup.Y: The Y identifier is not unique\n") |
|
cat("\tdup.XUY: The X and Y identifiers together are not unique\n") |
|
} |
|
problems |
|
} |
|
|