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

Refactor #20

Merged
merged 69 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
964caaa
Updating draft typhoid vignettte
kristinawlai Sep 5, 2023
965c789
Delete README.html
d-morrison Oct 5, 2023
eec2030
I think I fixed the error in compiling this vignette
d-morrison Oct 5, 2023
982a533
prelim seroincidence function (KA + ezra)
kaiemjoy Oct 5, 2023
d6e8b67
ifelse
kaiemjoy Oct 5, 2023
ce17f0c
data
kaiemjoy Oct 5, 2023
09ad16a
for loop implementation for multiple antigens
d-morrison Oct 5, 2023
3d678bf
importing functions from dplyr, etc, using the `use_import_from()` fu…
d-morrison Oct 6, 2023
ab0ea0a
Update README.md
kristinawlai Oct 9, 2023
b030c07
Merge remote-tracking branch 'upstream/main' into typhoid_vignette
kristinawlai Oct 10, 2023
82da590
Add text and annoattion to code chunks. Still have a few questions to…
kristinawlai Oct 18, 2023
ae084af
Merge branch 'revise-functions' into patch
d-morrison Oct 24, 2023
037e184
Merge pull request #17 from UCD-SERG/patch
d-morrison Oct 24, 2023
4bdbad7
in progress
d-morrison Oct 24, 2023
b9a1ad6
Merge branch 'revise-functions' of https://github.com/UCD-SERG/seroca…
d-morrison Oct 24, 2023
9b1f84b
Merge remote-tracking branch 'origin/main' into revise-functions
d-morrison Oct 25, 2023
91dc2f0
in progress
d-morrison Oct 25, 2023
af383b5
formatting
d-morrison Oct 25, 2023
5b25cad
more
d-morrison Oct 25, 2023
649de94
Merge branch 'revise-functions' into typhoid_vignette
d-morrison Oct 25, 2023
3cc52ba
Merge pull request #19 from UCD-SERG/typhoid_vignette
d-morrison Oct 25, 2023
91d552c
setting up test of `estIncidence()` function
d-morrison Oct 25, 2023
2451fbd
fix note on test
d-morrison Oct 25, 2023
4b22cfe
in progress
d-morrison Oct 25, 2023
9ca585e
more cleanup
d-morrison Oct 27, 2023
2b1e617
in progress
d-morrison Oct 27, 2023
8dabb13
in progress
d-morrison Oct 27, 2023
cca1b67
update docs
d-morrison Oct 27, 2023
8b31bab
tutorial wont run until inputs are updated to match new method
d-morrison Oct 27, 2023
35f471f
import rename
d-morrison Oct 27, 2023
71e210d
use dplyr
d-morrison Oct 27, 2023
23ed739
cleanup
d-morrison Nov 1, 2023
1ed5b52
formatting
d-morrison Nov 2, 2023
4b6dd30
getting close to functional
d-morrison Nov 7, 2023
f5b3d7f
more
d-morrison Nov 7, 2023
4c5cf40
more
d-morrison Nov 7, 2023
7c8512d
cleaning up
d-morrison Nov 7, 2023
a1326a7
more
d-morrison Nov 7, 2023
24f6d3f
inconsistent results
d-morrison Nov 7, 2023
099ac46
seems to work!
d-morrison Nov 7, 2023
9dba648
dealing with load_all()
d-morrison Nov 7, 2023
06f7e5f
correct typo
d-morrison Nov 7, 2023
627c6cf
cleanup
d-morrison Nov 7, 2023
b29ec08
rename
d-morrison Nov 7, 2023
10c9b28
more
d-morrison Nov 7, 2023
d4ebee0
more fixes
d-morrison Nov 7, 2023
1983192
fix typo in documentation
d-morrison Nov 7, 2023
5bce3c6
lots of stuff
d-morrison Nov 7, 2023
e964fff
more polishing
d-morrison Nov 7, 2023
166f07e
small change to output
kaiemjoy Nov 8, 2023
08239d1
Merge pull request #22 from UCD-SERG/ka_mini
d-morrison Nov 8, 2023
c760d75
clearing up some check errors
d-morrison Nov 8, 2023
a78f4db
Merge branch 'refactor' of https://github.com/UCD-SERG/serocalculator…
d-morrison Nov 8, 2023
5eead5a
fix data
d-morrison Nov 8, 2023
1131478
cleanup
d-morrison Nov 8, 2023
26df97a
more
d-morrison Nov 9, 2023
82c19cb
more cleanup
d-morrison Nov 9, 2023
0651933
posit-cloud edits
d-morrison Nov 9, 2023
777f9dc
ignore temp files
d-morrison Nov 9, 2023
233359a
ignore some large files when building
d-morrison Nov 9, 2023
7aebf50
need bookdown for bookdown::html_document2
d-morrison Nov 9, 2023
eee8e76
skipping test
d-morrison Nov 9, 2023
56e658a
turning off evaluation of sees-analysis for now.
d-morrison Nov 9, 2023
f899236
I think this might work
d-morrison Nov 9, 2023
ccc681e
polished
d-morrison Nov 9, 2023
af7f906
Merge pull request #23 from UCD-SERG/redo-stratified-analysis
d-morrison Nov 9, 2023
c8b91de
more cleanup
d-morrison Nov 9, 2023
6eebe84
correcting one more dplyr "global variable" note.
d-morrison Nov 9, 2023
48ce79d
dont need documentation for sees data yet
d-morrison Nov 9, 2023
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
11 changes: 11 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@
^\.github$
^cran-comments\.md$
^CODE_OF_CONDUCT.md$
^README\.Rmd$
^README.html$
^data-raw$
^vignettes/articles$
^CODE_OF_CONDUCT.md$
sees_dmcmc_09.30.2021.rds$
SEES_2022-10-24_redacted_2023-10-12.csv$
allpopsamples_hlye.csv$
^serocalculator\.Rcheck$
^serocalculator.*\.tar\.gz$
^serocalculator.*\.tgz$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ docs
src-i386
src-x64
..Rcheck
serocalculator.Rcheck/
serocalculator*.tar.gz
serocalculator*.tgz
24 changes: 16 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,40 @@ Authors@R: c(
person(given = "Kristina", family = "Lai", role = c("aut")),
person(given = "Kristen", family = "Aiemjoy", email = "kaiemjoy@ucdavis.edu", role = c("aut")),
person(given = "Douglas Ezra", family = "Morrison", email = "demorrison@ucdavis.edu", role = c("aut", "cre")))
Description: Translates antibody levels measured in a cross-sectional population
sample into an estimate of the frequency with which seroconversions (infections)
occur in the sampled population. Forked from the "seroincidence" package v2.0.0 on CRAN.
Depends: R (>= 2.10)
Description: Translates antibody levels measured in cross-sectional population
samples into estimates of the frequency with which seroconversions (infections)
occur in the sampled populations. Replaces the previous `seroincidence` package.
Depends: R (>= 3.5.0)
License: GPL-3
Imports:
bookdown,
dplyr,
magrittr,
parallel,
Rcpp,
rlang,
stats,
tibble,
tidyr,
utils
Suggests:
knitr,
rmarkdown,
parallel,
pander,
Hmisc,
tidyverse,
fs
fs,
testthat (>= 3.0.0),
readr,
ggplot2,
bookdown
VignetteBuilder: knitr
LazyData: true
Encoding: UTF-8
URL: https://github.com/UCD-SERG/serocalculator, https://ucd-serg.github.io/serocalculator/
RoxygenNote: 7.2.3
RoxygenNote: 7.2.3.9000
NeedsCompilation: no
LinkingTo:
Rcpp
Language: en-US
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
4 changes: 2 additions & 2 deletions MD5
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ce096f7d73a14d083004dc4cd4827088 *NEWS
2f0d05fa159c0cbaf611b95b9fb8198c *R/data-help.R
a1522b6b5522ee3108f4c18369b9cd4d *R/deltaFunc.R
2459fe699d9f2dee2d4096a8459a69c3 *R/densFunc.R
59400925562a9721e63e7991ff154bcb *R/estimateSeroincidence.R
59400925562a9721e63e7991ff154bcb *R/est.incidence.by.R
97fa0b804650de611a334024916baaca *R/getAdditionalData.R
f0d9baae9d10e02db385f555555931fe *R/nll.R
3ef0eb791a21c2221dafe07457466876 *R/nllByType.R
Expand Down Expand Up @@ -38,7 +38,7 @@ a23cfd5e4e5e88a96e2774853f48aa65 *man/campylobacterDelftParams4.Rd
e6415e1ea335b82a24cc3ef9aa5478bc *man/campylobacterSSIParams2.Rd
4d2797bc3c01ab24effb7a6900ae879a *man/campylobacterSSIParams4.Rd
f35091d97beb8e70dd2fa2f655c4321b *man/campylobacterSimLowData.Rd
c0e69fa2dec067fa5db26cdaeab47998 *man/estimateSeroincidence.Rd
c0e69fa2dec067fa5db26cdaeab47998 *man/est.incidence.by.Rd
a351cb2497a6e921c2eced22d0d95c1a *man/getAdditionalData.Rd
01c20490f1e8ed5ec32473649160c8ad *man/pertussisIgGPTParams1.Rd
02a459fb98436acfdfb292a74947c09c *man/pertussisIgGPTParams2.Rd
Expand Down
39 changes: 35 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
# Generated by roxygen2: do not edit by hand

S3method(print,seroincidence)
S3method(print,summary.seroincidence)
S3method(summary,seroincidence)
export(estimateSeroincidence)
S3method(print,seroincidence.ests)
S3method(print,summary.seroincidence.ests)
S3method(summary,seroincidence.ests)
export(.optNll)
export(est.incidence)
export(est.incidence.by)
export(fdev)
export(getAdditionalData)
export(postprocess_fit)
importFrom(Rcpp,sourceCpp)
importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,anti_join)
importFrom(dplyr,any_of)
importFrom(dplyr,bind_rows)
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,inner_join)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,relocate)
importFrom(dplyr,rename)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
importFrom(dplyr,semi_join)
importFrom(dplyr,tibble)
importFrom(magrittr,"%>%")
importFrom(parallel,clusterEvalQ)
importFrom(parallel,clusterExport)
importFrom(parallel,parLapplyLB)
importFrom(rlang,.data)
importFrom(rlang,.env)
importFrom(stats,dlnorm)
importFrom(stats,nlm)
importFrom(stats,optim)
importFrom(stats,pgamma)
importFrom(stats,plnorm)
importFrom(stats,qnorm)
importFrom(tibble,as_tibble)
importFrom(tibble,column_to_rownames)
importFrom(tibble,tibble)
importFrom(tidyr,drop_na)
importFrom(utils,download.file)
importFrom(utils,unzip)
useDynLib(serocalculator, .registration = TRUE)
29 changes: 29 additions & 0 deletions R/build_likelihood_function.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
build_likelihood_function = function(
cross_sectional_data,
longitudinal_parameter_samples,
noise_params,
antigen_isos = names(cross_sectional_data))
{

likelihood_function = function(llam)
{

res = 0

# add terms, e.g. for other antibodies
for (cur_antigen in antigen_isos)
{
res = res +
fdev(
llam,
cross_sectional_data[[cur_antigen]],
longitudinal_parameter_samples[[cur_antigen]],
noise_params[[cur_antigen]])
}

return(res)

}

return(likelihood_function)
}
121 changes: 121 additions & 0 deletions R/est.incidence.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

#
#' Age specific seroincidence function
#' add some details here
#'
#' @param dpop cross-sectional population data
#' @param c.age age category
#' @param start starting value for incidence rate
#' @param antigen_isos antigen-isotype(s) (a [character()] vector of one or more antigen names)
#' @param noise_params a [data.frame()] containing columns `nu`, etc. specifying conditional noise parameters
#' @param iterlim a positive integer specifying the maximum number of iterations to be performed before the program is terminated.
#' @param dmcmc mcmc samples from distribution of longitudinal decay curve parameters
#' @param verbose logical: if TRUE, print verbose log information to console
#' @inheritParams postprocess_fit
#' @inheritDotParams stats::nlm -f -p -hessian -iterlim
#'
#' @return A [data.frame()] containing the following:
#' * `est.start`: the starting guess for incidence rate
#' * `ageCat`: the age category we are analyzing
#' * `incidence.rate`: the estimated incidence rate, per person year
#' * `CI.lwr`: lower limit of confidence interval for incidence rate
#' * `CI.upr`: upper limit of confidence interval for incidence rate
#' * `coverage`: coverage probability
#' * `neg.llik`: negative log-likelihood
#' * `iterations`: the number of iterations used
#'
#' @export

est.incidence <- function(
dpop,
dmcmc,
noise_params,
c.age = NULL,
antigen_isos = dpop$antigen_iso |> unique(),
start = 0.1,
iterlim = 100,
coverage = .95,
verbose = FALSE,
...)
{

lambda = start # initial estimate: starting value
log.lambda = log(lambda)

if(!is.null(c.age))
{
dpop = dpop %>% dplyr::filter(.data[["ageCat"]] == c.age)
dmcmc = dmcmc %>% dplyr::filter(.data[["ageCat"]] == c.age)

if("ageCat" %in% names(noise_params))
{
noise_params =
noise_params %>%
dplyr::filter(.data[["ageCat"]] == c.age)
}
}

ps = list()
cs = list()
conds = list()

for (cur_antigen in antigen_isos)
{
ps[[cur_antigen]] = get_xspd_one_antigen(
dpop = dpop,
antigen = cur_antigen)

cs[[cur_antigen]] = get_curve_params_one_antigen(
params = dmcmc,
antigen = cur_antigen)

conds[[cur_antigen]] =
noise_params %>%
dplyr::filter(.data[["antigen_iso"]] == cur_antigen)

}

# noise parameters
# cond.hlye.IgG

objfunc = build_likelihood_function(
cross_sectional_data = ps,
longitudinal_parameter_samples = cs,
noise_params = conds)

# seroincidence estimation
{
fit = nlm(
f = objfunc,
p = log.lambda,
hessian = TRUE,
iterlim = iterlim,
...)
} |> system.time() -> time

if(verbose)
{
message('elapsed time: ')
print(time)
}

if(fit$iterations >= iterlim)
{
warning(
"Maximum `nlm()` iterations reached; consider increasing `iterlim` argument.")
}

log.lambda.est =
fit |>
postprocess_fit(
coverage = coverage,
start = start
) |>
mutate(
ageCat = c.age,
antigen.iso = antigen_isos |> paste(collapse = "+")) %>%
structure(
noise.parameters = noise_params)

return(log.lambda.est)
}
Loading