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

possible fix for typhoid vignette #26

Merged
merged 62 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
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
513c231
Merge pull request #20 from UCD-SERG/refactor
d-morrison Nov 9, 2023
3313afd
add n, fixed antigen_isos bug
kaiemjoy Nov 9, 2023
b71947f
Merge pull request #24 from UCD-SERG/scrub-vignette
kaiemjoy Nov 9, 2023
10d31f5
require dplyr version 1.1.1 or higher, to use the `relationship` argu…
d-morrison Nov 9, 2023
883e54b
Merge branch 'typhoid_vignette' into typhoid_vignette_patch_11-11
d-morrison Nov 12, 2023
d08b75f
add stepmax, and a little documentation
d-morrison Nov 13, 2023
6347aaa
cleanup of documentation
d-morrison Nov 13, 2023
7726563
Merge pull request #25 from UCD-SERG/dplyr-version-req
d-morrison Nov 13, 2023
6757bba
more fixes to documentation
d-morrison Nov 13, 2023
77735b6
Merge pull request #27 from UCD-SERG/set-stepmax
d-morrison Nov 13, 2023
9dfab6d
Merge remote-tracking branch 'origin/main' into typhoid_vignette_patc…
d-morrison Nov 13, 2023
e1442bb
rename to remove spaces
d-morrison Nov 14, 2023
2b398a5
purge one more filename space
d-morrison Nov 14, 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
9 changes: 9 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@
^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
21 changes: 12 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,37 @@ 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,
dplyr (>= 1.1.1),
magrittr,
parallel,
Rcpp,
rlang,
stats,
tibble,
tidyr,
utils
Suggests:
knitr,
rmarkdown,
parallel,
pander,
Hmisc,
tidyverse,
fs,
testthat (>= 3.0.0)
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
Expand Down
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
33 changes: 28 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
# 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(incidence.age)
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,count)
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)
Expand All @@ -20,6 +40,9 @@ 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)
Expand Down
3 changes: 3 additions & 0 deletions R/build_likelihood_function.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ build_likelihood_function = function(
longitudinal_parameter_samples[[cur_antigen]],
noise_params[[cur_antigen]])
}

return(res)

}

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

#
#' Age specific seroincidence function
#' This function models seroincidence using maximum likelihood estimation; that is, it finds the value of the seroincidence parameter which maximizes the likelihood (i.e., joint probability) of the data.
#'
#' @param dpop cross-sectional population data
#' @param c.age age category
#' @param start starting value for incidence rate
#' @param antigen_isos antigen isotypes: a [character()] vector of one or more antigen isotype names, which should match the values of the `antigen_iso` column in the `dpop` input argument
#' @param noise_params a [data.frame()] containing columns `nu`, etc. specifying conditional noise parameters
#' @param dmcmc mcmc samples from distribution of longitudinal decay curve parameters
#' @param verbose logical: if TRUE, print verbose log information to console
#' @param iterlim an [integer()], which provides an upper limit on the number of computational iterations used to search for the maximum likelihood estimate of incidence (passed to [stats::nlm()]).
#' @param stepmax a [numeric()], which limits how aggressively the [stats::nlm()] algorithm searches for the maximum likelihood estimate of incidence. If this function output an infinite standard error estimate, consider reducing this parameter.
#' @inheritParams postprocess_fit
#' @inheritParams stats::nlm
#' @inheritDotParams stats::nlm -f -p -hessian
#'
#' @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,
stepmax = 1,
...)
{

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,
stepmax = stepmax,
...)
} |> 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