Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug curve plot #323

Merged
merged 12 commits into from
Dec 20, 2024
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: serocalculator
Title: Estimating Infection Rates from Serological Data
Version: 1.2.0.9022
Version: 1.2.0.9023
Authors@R: c(
person("Peter", "Teunis", , "p.teunis@emory.edu", role = c("aut", "cph"),
comment = "Author of the method and original code."),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## New features

* Fixed a bug in computing the antibody response curve when $r=1$ (#323)

* Added example datasets with documentation for examples and testing (#314)

* Improved error messaging for `autoplot.pop_data()` (#234).
Expand Down
17 changes: 12 additions & 5 deletions R/ab0.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# uses r > 1 scale for shape
ab0 <- function(
t,
curve_params) {
ab0 <- function(t, curve_params) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint

y0 <- curve_params[["y0"]]
y1 <- curve_params[["y1"]]
t1 <- curve_params[["t1"]]
alpha <- curve_params[["alpha"]]
shape <- curve_params[["r"]]
r <- curve_params[["r"]]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shortening varname to make lines below fit in less than 80 characters


beta <- bt(y0, y1, t1)

yt <- 0

yt_phase_1 <- y0 * exp(beta * t)
yt_phase_2 <- (y1^(1 - shape) - (1 - shape) * alpha * (t - t1))^(1 / (1 - shape))
if (r == 1) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if r == 1, the usual expression for this function reduces to 0^Inf = 0, which isn't correct; as r -> 1, the function should reduce to the exponential.

yt_phase_2 <- y1 * exp(-alpha * (t - t1))
# see wolfram alpha result: https://bit.ly/3ZB69Yn
# this is a version the product-limit characterization of the
# exponential function
} else {
yt_phase_2 <-
(y1^(1 - r) - (1 - r) * alpha * (t - t1))^(1 / (1 - r))
}

yt <- dplyr::if_else(t <= t1, yt_phase_1, yt_phase_2)
return(yt)
}
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/ab0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `ab0()` produces consistent results

9298.67465260527

---

9990.00499833375

---

9298.67465260527

---

909.090909090912

82 changes: 82 additions & 0 deletions tests/testthat/_snaps/plot_curve_params_one_ab/curve-r1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions tests/testthat/test-ab0.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test_that("`ab0()` produces consistent results", {
params1 <-
data.frame(
y0 = 10,
y1 = 10^4,
t1 = 9.5,
alpha = 0.01,
r = 1,
antigen_iso = "test"
) |>
as_curve_params()

calc1 <-
ab0(curve_params = params1, t = 9.4)

expect_snapshot_value(calc1, style = "deparse")

calc2 <-
ab0(curve_params = params1, t = 9.6)

expect_snapshot_value(calc2, style = "deparse")

params2 <-
data.frame(
y0 = 10,
y1 = 10^4,
t1 = 9.5,
alpha = 0.01,
r = 2,
antigen_iso = "test"
) |>
as_curve_params()

calc3 <-
ab0(curve_params = params2, t = 9.4)

expect_snapshot_value(calc3, style = "deparse")

calc4 <-
ab0(curve_params = params2, t = 9.6)

expect_snapshot_value(calc4, style = "deparse")
})
23 changes: 23 additions & 0 deletions tests/testthat/test-plot_curve_params_one_ab.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("`plot_curve_params_one_ab()` produces consistent results", {
params <-
data.frame(
y0 = 10,
y1 = 10 ^ 4,
t1 = 9.5,
alpha = 0.01,
r = 1,
antigen_iso = "test"
) |>
as_curve_params()


fig1 <-
params |>
plot_curve_params_one_ab(n_points = 10 ^ 5,
xlim = c(0, 25),
log_y = FALSE)

fig1 |>
vdiffr::expect_doppelganger(title = "curve_r1")

})
Loading