Skip to content
Draft
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
6 changes: 6 additions & 0 deletions .lintr.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ exclusions <- list(
),
"tests/testthat.R" = list(
undesirable_function_linter = Inf
),
"R/t1.R" = list(
object_name_linter = Inf
),
"R/y1.R" = list(
object_name_linter = Inf
)

)
20 changes: 20 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# serocalculator (development version)

## New features

* Added interactive Shiny app for visualizing antigen-antibody kinetics models
- `curve_app()`: Launch interactive Shiny application
- `curve_app_ui()`: UI components for the kinetics visualization app
- `curve_app_server()`: Server logic for the kinetics visualization app
- App allows real-time parameter adjustments via sliders for exploring model behavior
* Added mathematical functions for modeling antibody and pathogen decay
- `antibody_decay_curve()`: Simulate and plot antibody decay over time
- `pathogen_decay_curve()`: Simulate and plot pathogen decay over time
- `t1f()`: Calculate time to end of active infection
- `y1f()`: Calculate peak antibody concentration
* Added `plot_decay_curve()` for easy plotting of decay functions using ggplot2
* Added app configuration utilities (`app_sys()`, `get_golem_config()`)

## Improvements

* Improved error messages in `autoplot.seroincidence()` and
`autoplot.seroincidence.by()` to use `cli::cli_abort()` for clearer errors

# serocalculator 1.4.0

## New features
Expand Down
33 changes: 16 additions & 17 deletions R/antibody_decay_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,35 @@
#' @examples
#' library(ggplot2)
#' ggplot() + geom_function(fun = antibody_decay_curve) + xlim(1,100)
antibody_decay_curve = function(
t,
y0 = 0.74916052, # taken from simpar run from vignette
b0 = 1,
mu_b = 0.18432798,
mu_y = 0.36853621,
gamma = 0.0013040664,
alpha = 0.00002192627,
rho = 2
# rho = 2 # exponential decay?
)
{
antibody_decay_curve <- function(
t,
y0 = 0.74916052, # taken from simpar run from vignette
b0 = 1,
mu_b = 0.18432798,
mu_y = 0.36853621,
gamma = 0.0013040664,
alpha = 0.00002192627,
rho = 2
) {

t1 = t1f(
t1 <- t1f(
b0 = b0,
y0 = y0,
mu_y = mu_y,
mu_b = mu_b,
gamma = gamma
)

y1 = y1f(
y1 <- y1f(
y0 = y0,
mu_y = mu_y,
t1 = t1)
t1 = t1
)

phase2_rho1 <- y1 * exp(-alpha * (t - t1))
phase2_default <-
y1 * (1+ (rho-1) * (y1 ^ (rho - 1)) * alpha * (t - t1))^(-1/(rho - 1))
yt = ifelse(
y1 * (1 + (rho - 1) * (y1^(rho - 1)) * alpha * (t - t1))^(-1 / (rho - 1))
yt <- ifelse(
t < t1,
y0 * exp(mu_y * t),
ifelse(
Expand Down
22 changes: 11 additions & 11 deletions R/antigen_decay_curve.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
#' mu_b = 0.18432798,
#' mu_y = 0.36853621,
#' gamma = 0.0013040664)) + xlim(0, 100)
pathogen_decay_curve = function(
t,
y0 = 0.74916052, # taken from simpar run from vignette
b0 = 1,
mu_b = 0.18432798,
mu_y = 0.36853621,
gamma = 0.0013040664)
{
pathogen_decay_curve <- function(
t,
y0 = 0.74916052, # taken from simpar run from vignette
b0 = 1,
mu_b = 0.18432798,
mu_y = 0.36853621,
gamma = 0.0013040664
) {

fraction = gamma * y0 * (exp(mu_y * t) - exp(mu_b*t)) / (mu_y - mu_b)
bt_active = (b0 * exp(mu_b * t)) - fraction
fraction <- gamma * y0 * (exp(mu_y * t) - exp(mu_b * t)) / (mu_y - mu_b)
bt_active <- (b0 * exp(mu_b * t)) - fraction

bt = pmax(0, bt_active)
bt <- pmax(0, bt_active)
return(bt)
}
3 changes: 2 additions & 1 deletion R/app_config.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#'
#' NOTE: If you manually change your package name in the DESCRIPTION,
#' don't forget to change it here too, and in the config file.
#' For a safer name change mechanism, use the `golem::set_golem_name()` function.
#' For a safer name change mechanism, use the
#' `golem::set_golem_name()` function.
#'
#' @param ... character vectors, specifying subdirectory and file(s)
#' within your package. The default, none, returns the root of the app.
Expand Down
3 changes: 1 addition & 2 deletions R/curve_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#' \dontrun{
#' curve_app()
#' }
curve_app = function()
{
curve_app <- function() {
shiny::shinyApp(
ui = curve_app_ui,
server = curve_app_server
Expand Down
75 changes: 37 additions & 38 deletions R/curve_app_server.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
curve_app_server = function(input, output, session)
{
exp10 = function(x) 10^x
derived_params =
{
tibble(
mu_y = input$mu_y |> exp10(),
mu_b = input$mu_b |> exp10(),
gamma = input$gamma |> exp10(),
y0 = input$y0 |> exp10(),
b0 = input$b0 |> exp10(),
alpha = input$alpha |> exp10(),
rho = input$rho,
t1 = t1f(
mu_y = .data$mu_y,
mu_b = .data$mu_b,
gamma = .data$gamma,
y0 = .data$y0,
b0 = .data$b0),
y1 = y1f(
y0 = .data$y0,
mu_y = .data$mu_y,
t1 = .data$t1)
)
}|> reactive()
curve_app_server <- function(input, output, session) {
exp10 <- function(x) {
10^x
}
derived_params <- tibble(
mu_y = input$mu_y |> exp10(),
mu_b = input$mu_b |> exp10(),
gamma = input$gamma |> exp10(),
y0 = input$y0 |> exp10(),
b0 = input$b0 |> exp10(),
alpha = input$alpha |> exp10(),
rho = input$rho,
t1 = t1f(
mu_y = .data$mu_y,
mu_b = .data$mu_b,
gamma = .data$gamma,
y0 = .data$y0,
b0 = .data$b0
),
y1 = y1f(
y0 = .data$y0,
mu_y = .data$mu_y,
t1 = .data$t1
)
) |>
reactive()


output$tab1 =
output$tab1 <-
derived_params() |> shiny::renderTable(digits = 4)

plot1 =
plot1 <-
shiny::eventReactive(
eventExpr = derived_params() | input$ymax1 | input$alpha | input$rho,
{
Expand All @@ -43,9 +44,11 @@ curve_app_server = function(input, output, session)
rho = input$rho,
ymax = input$ymax1 |> exp10()

)})
)
}
)

plot2 =
plot2 <-
shiny::eventReactive(
eventExpr = derived_params() | input$ymax2,
{
Expand All @@ -57,19 +60,15 @@ curve_app_server = function(input, output, session)
y0 = input$y0 |> exp10(),
b0 = input$b0 |> exp10(),
ymax = input$ymax2 |> exp10()
)})
)
}
)

output$plot1 =
output$plot1 <-
plot1() |>
renderPlot()
# plotly::ggplotly() |>
# plotly::layout(legend = list(orientation = 'h')) |>
# plotly::renderPlotly()

output$plot2 =
output$plot2 <-
plot2() |>
renderPlot()
# plotly::ggplotly() |>
# plotly::layout(legend = list(orientation = 'h')) |>
# plotly::renderPlotly()
}
38 changes: 21 additions & 17 deletions R/curve_app_ui.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
curve_app_ui = function(request)
{
curve_app_ui <- function(request) {

sidebar = shiny::sidebarPanel(
sidebar <- shiny::sidebarPanel(
width = 3,

sliderInput(
Expand All @@ -10,7 +9,8 @@ curve_app_ui = function(request)
min = -2,
max = 2,
step = .1,
value = 0),
value = 0
),


sliderInput(
Expand All @@ -19,45 +19,51 @@ curve_app_ui = function(request)
min = -2,
max = 2,
step = .1,
value = 0),
value = 0
),

sliderInput(
inputId = "mu_b",
label = "log10(mu_b)",
min = -2,
max = 2,
step = .1,
value = log10(0.18432798)),
value = log10(0.18432798)
),
sliderInput(
inputId = "mu_y",
label = "log10(mu_y)",
min = -2,
max = 2,
step = .1,
value = log10(0.36853621)),
value = log10(0.36853621)
),
sliderInput(
inputId = "gamma",
label = "log10(gamma)",
min = -10,
max = 10,
step = .1,
value = log10(0.0013040664)),
value = log10(0.0013040664)
),

sliderInput(
inputId = "rho",
label = "rho",
min = 1,
max = 3,
step = .1,
value = 2),
value = 2
),

sliderInput(
inputId = "alpha",
label = "log10(alpha)",
min = -8,
max = -1,
step = .1,
value = log10(0.00002192627))
value = log10(0.00002192627)
)
)


Expand All @@ -73,28 +79,26 @@ curve_app_ui = function(request)
shiny::column(
width = 12,
h2("pathogens"),
# plotly::plotlyOutput("plot2")),
shiny::plotOutput(height = "300px", "plot2"),
shiny::sliderInput(
inputId = "ymax2",
label = "log10(ymax)",
min = 0,
max = 5,
step = .1,
value = 1),
# shiny::column(
# width = 6,
value = 1
),
h2("antibodies"),
# plotly::plotlyOutput("plot1"))
shiny::plotOutput(height = "300px", 'plot1'),
shiny::plotOutput(height = "300px", "plot1"),
shiny::sliderInput(
inputId = "ymax1",
label = "log10(ymax)",
min = 2,
max = 10,
step = .1,
value = 4.5)
value = 4.5
)
)


)
Expand Down
Loading
Loading