Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions R/app_helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Computations used by mispitools_app(), kept outside the app so they can
# be called and tested without starting Shiny. They used to be defined
# inside mispitools_app() itself. The bodies are unchanged from that
# version; only the names gained an app_ prefix so they don't get mixed
# up with the exported lr_*() functions, which use different models.

#' LR for sex evidence, as shown in the app
#'
#' @param MPs Sex of the missing person, `"F"` or `"M"`.
#' @param obsSex Observed sex of the unidentified person, `"F"` or `"M"`.
#' @param eps Probability that the recorded sex is wrong.
#' @param propF Proportion of females in the population.
#' @return The LR, a length-one numeric named after `obsSex`.
#' @noRd
app_lr_sex <- function(MPs, obsSex, eps, propF) {
propS <- c(F = propF, M = 1 - propF)
if (MPs == obsSex) {
LR <- (1 - eps) / propS[obsSex]
} else {
LR <- eps / propS[obsSex]
}
LR
}

#' LR for age evidence, as shown in the app
#'
#' Population ages are taken as uniform on 80 years. The interval
#' `MPa +/- MPr` is clamped to `[1, 80]`, and P(in range | H2) is kept
#' inside `[0.001, 0.999]`.
#'
#' @param MPa Missing person's age.
#' @param MPr Half-width of the age interval.
#' @param obsAge Observed (estimated) age of the unidentified person.
#' @param epa Probability that the age falls outside the interval under H1.
#' @return The LR as a single number.
#' @noRd
app_lr_age <- function(MPa, MPr, obsAge, epa) {
MPmin <- max(1, MPa - MPr)
MPmax <- min(80, MPa + MPr)
T1p <- (MPmax - MPmin) / 80
T1p <- max(0.001, min(0.999, T1p))
T0p <- 1 - T1p

if (obsAge >= MPmin && obsAge <= MPmax) {
LR <- (1 - epa) / T1p
} else {
LR <- epa / T0p
}
LR
}

#' LR for hair colour evidence, as shown in the app
#'
#' Uniform error model: the observed colour equals the missing person's
#' with probability `1 - epc`, otherwise it is any of the other colours
#' with equal probability.
#'
#' @param MPc Missing person's hair colour, an index into `propC`.
#' @param obsC Observed hair colour, an index into `propC`.
#' @param epc Probability that the observed colour is wrong.
#' @param propC Population frequency of each colour.
#' @return The LR, a length-one numeric (keeps the name of `propC[obsC]`).
#' @noRd
app_lr_hair <- function(MPc, obsC, epc, propC) {
# Uniform error model
n_colors <- length(propC)
if (MPc == obsC) {
p_obs_h1 <- 1 - epc
} else {
p_obs_h1 <- epc / (n_colors - 1)
}
LR <- p_obs_h1 / propC[obsC]
LR
}
52 changes: 6 additions & 46 deletions R/app_unified.R
Original file line number Diff line number Diff line change
Expand Up @@ -296,46 +296,6 @@ mispitools_app <- function() {
AUC = AUC_value)
}

# Calculate LR for sex
calc_lr_sex <- function(MPs, obsSex, eps, propF) {
propS <- c(F = propF, M = 1 - propF)
if (MPs == obsSex) {
LR <- (1 - eps) / propS[obsSex]
} else {
LR <- eps / propS[obsSex]
}
LR
}

# Calculate LR for age
calc_lr_age <- function(MPa, MPr, obsAge, epa) {
MPmin <- max(1, MPa - MPr)
MPmax <- min(80, MPa + MPr)
T1p <- (MPmax - MPmin) / 80
T1p <- max(0.001, min(0.999, T1p))
T0p <- 1 - T1p

if (obsAge >= MPmin && obsAge <= MPmax) {
LR <- (1 - epa) / T1p
} else {
LR <- epa / T0p
}
LR
}

# Calculate LR for hair color
calc_lr_hair <- function(MPc, obsC, epc, propC) {
# Uniform error model
n_colors <- length(propC)
if (MPc == obsC) {
p_obs_h1 <- 1 - epc
} else {
p_obs_h1 <- epc / (n_colors - 1)
}
LR <- p_obs_h1 / propC[obsC]
LR
}

# ============================================================================
# UI DEFINITION
# ============================================================================
Expand Down Expand Up @@ -1051,14 +1011,14 @@ mispitools_app <- function() {
)
}
if (input$indiv_type == "sex") {
calc_lr_sex(input$indiv_MPs, input$indiv_obsSex,
app_lr_sex(input$indiv_MPs, input$indiv_obsSex,
input$indiv_eps_sex, input$indiv_propF)
} else if (input$indiv_type == "age") {
calc_lr_age(input$indiv_MPa, input$indiv_MPr,
app_lr_age(input$indiv_MPa, input$indiv_MPr,
input$indiv_obsAge, input$indiv_epa)
} else if (input$indiv_type == "hair") {
propC <- DEFAULT_HAIR_FREQ
calc_lr_hair(as.numeric(input$indiv_MPc), as.numeric(input$indiv_obsC),
app_lr_hair(as.numeric(input$indiv_MPc), as.numeric(input$indiv_obsC),
input$indiv_epc, propC)
} else {
1
Expand Down Expand Up @@ -1205,23 +1165,23 @@ mispitools_app <- function() {

comb_lr_sex_val <- shiny::reactive({
if (input$comb_use_sex) {
calc_lr_sex(input$comb_MPs, input$comb_obsSex, input$comb_eps, 0.51)
app_lr_sex(input$comb_MPs, input$comb_obsSex, input$comb_eps, 0.51)
} else {
1
}
})

comb_lr_age_val <- shiny::reactive({
if (input$comb_use_age) {
calc_lr_age(input$comb_MPa, input$comb_MPr, input$comb_obsAge, input$comb_epa)
app_lr_age(input$comb_MPa, input$comb_MPr, input$comb_obsAge, input$comb_epa)
} else {
1
}
})

comb_lr_hair_val <- shiny::reactive({
if (input$comb_use_hair) {
calc_lr_hair(as.numeric(input$comb_MPc), as.numeric(input$comb_obsC),
app_lr_hair(as.numeric(input$comb_MPc), as.numeric(input$comb_obsC),
input$comb_epc, DEFAULT_HAIR_FREQ)
} else {
1
Expand Down
63 changes: 63 additions & 0 deletions Toys/Ibrahim/app_snapshot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Records what the Shiny app displays over a grid of inputs, so a refactor
# of R/app_unified.R can be checked against the version before it.
#
# Rscript Toys/Ibrahim/app_snapshot.R before.rds # on main
# Rscript Toys/Ibrahim/app_snapshot.R after.rds # on the branch
# Rscript -e 'all.equal(readRDS("before.rds"), readRDS("after.rds"))'
#
# Uses shiny::testServer, so no browser is needed.

out_file <- commandArgs(TRUE)[1]
if (is.na(out_file)) out_file <- "app_snapshot.rds"

suppressMessages(devtools::load_all(".", quiet = TRUE))
app <- mispitools_app()
snap <- list()

shiny::testServer(app, {
# individual tab, sex
for (mps in c("F", "M")) for (obs in c("F", "M"))
for (eps in c(0, 0.01, 0.05, 0.3)) for (pf in c(0.3, 0.5, 0.51)) {
session$setInputs(indiv_type = "sex", indiv_MPs = mps, indiv_obsSex = obs,
indiv_eps_sex = eps, indiv_propF = pf)
key <- paste("sex", mps, obs, eps, pf)
snap[[key]] <<- c(output$indiv_lr_value, output$indiv_lr_log,
output$indiv_formula)
}

# individual tab, age (includes ranges that hit the 1 and 80 limits)
for (mpa in c(3, 25, 40, 78)) for (mpr in c(1, 5, 10))
for (obs in c(1, 20, 35, 40, 50, 79, 85)) for (epa in c(0.01, 0.05)) {
session$setInputs(indiv_type = "age", indiv_MPa = mpa, indiv_MPr = mpr,
indiv_obsAge = obs, indiv_epa = epa)
key <- paste("age", mpa, mpr, obs, epa)
snap[[key]] <<- c(output$indiv_lr_value, output$indiv_lr_log,
output$indiv_formula)
}

# individual tab, hair
for (mpc in 1:5) for (obs in 1:5) for (epc in c(0.01, 0.1, 0.4)) {
session$setInputs(indiv_type = "hair", indiv_MPc = as.character(mpc),
indiv_obsC = as.character(obs), indiv_epc = epc)
key <- paste("hair", mpc, obs, epc)
snap[[key]] <<- c(output$indiv_lr_value, output$indiv_lr_log,
output$indiv_formula)
}

# combine tab
for (us in c(TRUE, FALSE)) for (ua in c(TRUE, FALSE)) for (uh in c(TRUE, FALSE))
for (obs_sex in c("F", "M")) for (obs_age in c(30, 60)) for (obs_c in c("2", "4")) {
session$setInputs(comb_use_sex = us, comb_use_age = ua, comb_use_hair = uh,
comb_MPs = "F", comb_obsSex = obs_sex, comb_eps = 0.05,
comb_MPa = 35, comb_MPr = 5, comb_obsAge = obs_age,
comb_epa = 0.05, comb_MPc = "2", comb_obsC = obs_c,
comb_epc = 0.1)
key <- paste("comb", us, ua, uh, obs_sex, obs_age, obs_c)
snap[[key]] <<- c(output$comb_lr_sex, output$comb_lr_age,
output$comb_lr_hair, output$comb_lr_total,
output$comb_lr_log, output$comb_formula)
}
})

saveRDS(snap, out_file)
cat(length(snap), "input combinations recorded in", out_file, "\n")
57 changes: 57 additions & 0 deletions tests/testthat/test-app-helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# The LR calculators behind mispitools_app(), checked against values
# worked out by hand.

test_that("app_lr_sex matches the hand calculation", {
# match: (1 - eps) / P(sex); mismatch: eps / P(sex)
expect_equal(unname(app_lr_sex("F", "F", 0.05, 0.5)), 0.95 / 0.5)
expect_equal(unname(app_lr_sex("F", "M", 0.05, 0.5)), 0.05 / 0.5)
expect_equal(unname(app_lr_sex("M", "M", 0.01, 0.51)), 0.99 / 0.49)
expect_equal(unname(app_lr_sex("M", "F", 0.01, 0.51)), 0.01 / 0.51)
# no error at all: a mismatch excludes
expect_equal(unname(app_lr_sex("F", "M", 0, 0.5)), 0)
})

test_that("app_lr_age matches the hand calculation", {
# 40 +/- 5: P(in range | H2) = 10 / 80
expect_equal(app_lr_age(40, 5, 42, 0.05), 0.95 / (10 / 80))
expect_equal(app_lr_age(40, 5, 60, 0.05), 0.05 / (70 / 80))
# interval ends count as in range
expect_equal(app_lr_age(40, 5, 35, 0.05), app_lr_age(40, 5, 45, 0.05))
expect_equal(app_lr_age(40, 5, 45, 0.05), 0.95 / (10 / 80))
})

test_that("app_lr_age clamps the interval to ages 1 to 80", {
# 78 +/- 6 becomes [72, 80]: P(in range | H2) = 8 / 80
expect_equal(app_lr_age(78, 6, 79, 0.05), 0.95 / (8 / 80))
# 82 is inside 78 +/- 6 but above 80, so it counts as out of range
expect_equal(app_lr_age(78, 6, 82, 0.05), 0.05 / (72 / 80))
# 3 +/- 5 becomes [1, 8]
expect_equal(app_lr_age(3, 5, 2, 0.05), 0.95 / (7 / 80))
})

test_that("app_lr_age handles zero-width and full-width intervals", {
# zero width: P(in range | H2) is floored at 0.001
expect_equal(app_lr_age(40, 0, 40, 0.05), 0.95 / 0.001)
# wider than the population: clamped to [1, 80], so 79 / 80. The 0.999
# cap in the function is never reached, because of the clamping.
expect_equal(app_lr_age(40, 60, 10, 0.05), 0.95 / (79 / 80))
})

test_that("app_lr_hair matches the hand calculation", {
freq <- c(Black = 0.08, Brown = 0.45, Blonde = 0.30, Red = 0.07, Gray = 0.10)
# match: (1 - epc) / freq
expect_equal(unname(app_lr_hair(2, 2, 0.1, freq)), 0.9 / 0.45)
expect_equal(unname(app_lr_hair(4, 4, 0.1, freq)), 0.9 / 0.07)
# mismatch: the error mass is spread evenly over the other 4 colours
expect_equal(unname(app_lr_hair(2, 4, 0.1, freq)), (0.1 / 4) / 0.07)
expect_equal(unname(app_lr_hair(1, 5, 0.4, freq)), (0.4 / 4) / 0.10)
})

test_that("app_lr_hair LRs average to 1 under H2", {
# sum over observed colours of P(obs | H2) * LR = sum of P(obs | H1) = 1
freq <- c(0.08, 0.45, 0.30, 0.07, 0.10)
for (mpc in 1:5) {
lr <- vapply(1:5, function(o) app_lr_hair(mpc, o, 0.1, freq), numeric(1))
expect_equal(sum(freq * lr), 1)
}
})
45 changes: 45 additions & 0 deletions tests/testthat/test-app-server.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Drives the server of mispitools_app() with shiny::testServer, so the
# displayed values are checked without opening a browser.

test_that("individual tab shows the LR from the helper functions", {
skip_if_not_installed("shiny")
shiny::testServer(mispitools_app(), {
session$setInputs(indiv_type = "sex", indiv_MPs = "F",
indiv_obsSex = "F", indiv_eps_sex = 0.05,
indiv_propF = 0.5)
expect_equal(output$indiv_lr_value, "LR = 1.900")
expect_equal(output$indiv_lr_log, "log10(LR) = 0.279")

session$setInputs(indiv_type = "age", indiv_MPa = 40, indiv_MPr = 5,
indiv_obsAge = 42, indiv_epa = 0.05)
expect_equal(output$indiv_lr_value, sprintf("LR = %.3f", 0.95 / (10 / 80)))

session$setInputs(indiv_type = "hair", indiv_MPc = "2", indiv_obsC = "4",
indiv_epc = 0.1)
expect_equal(output$indiv_lr_value, sprintf("LR = %.3f", (0.1 / 4) / 0.07))
})
})

test_that("combine tab multiplies the selected LRs", {
skip_if_not_installed("shiny")
shiny::testServer(mispitools_app(), {
session$setInputs(comb_use_sex = TRUE, comb_use_age = TRUE,
comb_use_hair = TRUE,
comb_MPs = "F", comb_obsSex = "F", comb_eps = 0.05,
comb_MPa = 35, comb_MPr = 5, comb_obsAge = 30,
comb_epa = 0.05,
comb_MPc = "2", comb_obsC = "2", comb_epc = 0.1)
# the combine tab uses 51% female, not the 50% of the individual tab
lr_sex <- 0.95 / 0.51
lr_age <- 0.95 / (10 / 80)
lr_hair <- 0.9 / 0.45
expect_equal(output$comb_lr_sex, sprintf("LR = %.2f", lr_sex))
expect_equal(output$comb_lr_total,
sprintf("LR = %.2f", lr_sex * lr_age * lr_hair))

# leaving age out drops it from the product
session$setInputs(comb_use_age = FALSE)
expect_equal(output$comb_lr_age, "Not included")
expect_equal(output$comb_lr_total, sprintf("LR = %.2f", lr_sex * lr_hair))
})
})
Loading