Skip to content

App: move the LR calculators out of mispitools_app() - #40

Merged
MarsicoFL merged 1 commit into
MarsicoFL:mainfrom
ibrhmyi:app-extract-lr-calcs
Nov 28, 2026
Merged

MarsicoFL merged 1 commit into
MarsicoFL:mainfrom
ibrhmyi:app-extract-lr-calcs

Conversation

@ibrhmyi

@ibrhmyi ibrhmyi commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

First step of making the Shiny app testable. calc_lr_sex, calc_lr_age and calc_lr_hair were defined inside mispitools_app(), so there was no way to call them without launching the app. They are now in R/app_helpers.R as internal functions (app_lr_sex, app_lr_age, app_lr_hair). The bodies are copied as they were, I only renamed them so they don't get confused with the exported lr_*() functions.

Tests:

  • test-app-helpers.R: hand-computed LRs, including the clamping at 1 and 80, and that the hair LRs average to 1 under H2
  • test-app-server.R: drives the server with shiny::testServer(), so no browser is needed, and checks what the individual and combine tabs display

To check the displayed numbers stay the same I wrote Toys/Ibrahim/app_snapshot.R. It records every output of the individual and combine tabs over 355 input combinations. The script is only on this branch, so copy it out of the repo first and run it from the repo root on each side:

git checkout app-extract-lr-calcs && cp Toys/Ibrahim/app_snapshot.R /tmp/
git checkout main && Rscript /tmp/app_snapshot.R before.rds
git checkout app-extract-lr-calcs && Rscript /tmp/app_snapshot.R after.rds
Rscript -e 'all.equal(readRDS("before.rds"), readRDS("after.rds"))'
[1] TRUE

Something that looks wrong in the age panel, R/app_unified.R:1091-1097 (the indiv_formula output). The LR is computed with the interval clamped to [1, 80], but the formula text uses the unclamped interval. With MP age 78 +/- 6, error 0.05:

  • observed 82: the panel says "in range", P(in range | H1) = 0.95, P(in range | H2) = 0.15, but then LR = 0.0556, which is the out-of-range value 0.05 / 0.9
  • observed 79: shows P(in range | H2) = 0.15 but the LR 9.5 uses 0.1

So the numbers on the screen don't give the LR shown under them. I left it as it is in this PR so the refactor stays a pure move, happy to fix it next.

Also a small one: in calc_lr_age the cap at 0.999 is never reached, because after clamping the widest interval is 79/80.

Next steps would be build_cpt_pop/build_cpt_mp, then the distribution, metrics and ROC functions.

calc_lr_sex, calc_lr_age and calc_lr_hair were defined inside
mispitools_app(), so they could only be reached by launching the app.
They now live in R/app_helpers.R as internal functions (app_lr_sex,
app_lr_age, app_lr_hair) with the same bodies, and have unit tests.

Also adds a testServer test for the individual and combine tabs, and
Toys/Ibrahim/app_snapshot.R, which records the displayed values over a
grid of 355 inputs. The snapshot is identical before and after this
change.
@MarsicoFL

Copy link
Copy Markdown
Owner

Cool!

@MarsicoFL
MarsicoFL merged commit c31cb02 into MarsicoFL:main Nov 28, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants