Skip to content

Commit a90beca

Browse files
authored
Merge pull request #101 from UCD-SERG/residuals_fits
Adding fitted and residual values
2 parents de3b806 + 2562c5a commit a90beca

13 files changed

+2356
-22
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: serodynamics
22
Title: What the Package Does (One Line, Title Case)
3-
Version: 0.0.0.9037
3+
Version: 0.0.0.9038
44
Authors@R: c(
55
person("Peter", "Teunis", , "p.teunis@emory.edu", role = c("aut", "cph"),
66
comment = "Author of the method and original code."),

NEWS.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# serodynamics (development version)
22

3-
* Added support for faceting by multiple IDs in `plot_predicted_curve()`:
4-
- New `facet_by_id` argument allows faceted plots by participant ID.
5-
* Added `plot_predicted_curve()` (#68)
3+
* Initial CRAN submission.
4+
5+
## New features
6+
7+
* Including fitted and residual values as data frame in run_mod output. (#101)
8+
* Added `plot_predicted_curve()` with support for faceting by multiple IDs (#68)
69
* Replacing old data object with new run_mod output (#102)
710
* Adding class assignment to run_mod output (#76)
811
* Making prep_priors modifiable (#78)

R/Run_Mod.R

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@
4848
#' - `nParameters`: The amount of parameters estimated in the model.
4949
#' - `nIterations`: Number of iteration specified.
5050
#' - `nBurnin`: Number of burn ins.
51-
#' - `nThin`: Thinning number (niter/nmc)
51+
#' - `nThin`: Thinning number (niter/nmc).
5252
#' - `priors`: A [list] that summarizes the input priors, including:
5353
#' - `mu_hyp_param`
5454
#' - `prec_hyp_param`
5555
#' - `omega_param`
5656
#' - `wishdf`
5757
#' - `prec_logy_hyp_param`
58-
#' - An optional `"jags.post"` attribute, included when argument
58+
#' - `fitted_residuals`: A [data.frame] containing fitted and residual values
59+
#' for all observations.
60+
#' - An optional `"jags.post"` attribute, included when argument
5961
#' `with_post` = TRUE.
6062
#' @inheritDotParams prep_priors
6163
#' @export
@@ -192,11 +194,18 @@ run_mod <- function(data,
192194
jags_out <- jags_out |>
193195
structure("priors" = attributes(priorspec)$used_priors)
194196

197+
# Calculating fitted and residuals
198+
# Renaming columns using attributes from as_case_data
199+
fit_res <- calc_fit_mod(modeled_dat = jags_out,
200+
original_data = dl_sub)
201+
jags_out <- jags_out |>
202+
structure(fitted_residuals = fit_res)
203+
195204
# Conditionally adding jags.post
196205
if (with_post) {
197206
jags_out <- jags_out |>
198207
structure(jags.post = jags_post_final)
199-
}
208+
}
200209
jags_out <- jags_out |>
201210
structure(class = union("sr_model", class(jags_out)))
202211
jags_out

R/calc_fit_mod.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#' @title Calculates fitted and residual values for modeled outputs
2+
#' @description
3+
#' `calc_fit_mod()` takes antibody kinetic parameter estimates and calculates
4+
#' fitted and residual values. Fitted values correspond to the estimated assay
5+
#' value (ex. ELISA units etc.) at time since infection (TSI). Residual values
6+
#' are calculated as the difference between fitted and observed values.
7+
#' @param modeled_dat A [data.frame] of modeled antibody kinetic parameter
8+
#' values.
9+
#' @param original_data A [data.frame] of the original input dataset.
10+
#' @returns A [data.frame] attached as an [attributes] with the following
11+
#' values:
12+
#' - Subject = ID number specifying an individual
13+
#' - Iso_type = The modeled antigen_isotype
14+
#' - t = Time since infection
15+
#' - fitted = The fitted value calculated using model output parameters for a
16+
#' given `t`
17+
#' - residual = The residual value calculated as the difference between
18+
#' observed and fitted values for a given `t`
19+
#' @keywords internal
20+
calc_fit_mod <- function(modeled_dat,
21+
original_data) {
22+
original_data <- original_data |>
23+
use_att_names() |>
24+
select(.data$Subject, .data$Iso_type, .data$t, .data$result)
25+
26+
# Preparing modeled data
27+
modeled_dat <- modeled_dat |>
28+
dplyr::summarize(.by = c(.data$Parameter, .data$Iso_type,
29+
.data$Stratification,
30+
.data$Subject),
31+
med_value = stats::median(.data$value)) |>
32+
tidyr::pivot_wider(names_from = .data$Parameter,
33+
values_from = .data$med_value)
34+
35+
# Matching input data with modeled data
36+
matched_dat <- merge(modeled_dat, original_data,
37+
by = c("Subject", "Iso_type"),
38+
all.y = TRUE)
39+
40+
# Calculating fitted and residual
41+
fitted_dat <- matched_dat |>
42+
mutate(fitted = ab(.data$t, .data$y0, .data$y1, .data$t1,
43+
.data$alpha, .data$shape),
44+
residual = .data$result - .data$fitted) |>
45+
select(.data$Subject, .data$Iso_type, .data$t, .data$fitted, .data$residual)
46+
fitted_dat
47+
}

R/use_att_names.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#' @title Assigns column names to conform with [calc_fit_mod()] using attributes
2+
#' @description
3+
#' `use_att_names` takes prepared longitudinal data for antibody kinetic
4+
#' modeling and names columns using attribute values to allow merging
5+
#' with a modeled [run_mod()] output [dplyr::tbl_df]. The column names include
6+
#' `Subject`, `Iso_type`, `t`, and `result`.
7+
#' @param data A [data.frame] raw longitudinal data that has been
8+
#' prepared for antibody kinetic modeling using [as_case_data()].
9+
#' @returns The input [data.frame] with columns named after attributes.
10+
#' @keywords internal
11+
use_att_names <- function(data) {
12+
data <- data |>
13+
dplyr::rename(
14+
Subject = data |> serocalculator::ids_varname(),
15+
Iso_type = data |> serocalculator::get_biomarker_names_var(),
16+
t = data |> get_timeindays_var(),
17+
result = data |> serocalculator::get_values_var()
18+
)
19+
return(data)
20+
}

inst/WORDLIST

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
1+
Biomarker
12
CMD
23
CodeFactor
34
Codecov
5+
HlyE
6+
Hyperpriors
7+
IgA
8+
IgG
9+
Iso
410
JAGs
511
Lifecycle
612
Postprocess
713
Rhat
14+
TSI
815
Traceplots
916
behaviour
1017
biomarker
1118
biomarkers
19+
bmatrix
20+
cdot
1221
dobson
1322
dotplots
1423
geoms
1524
ggproto
1625
hyp
1726
hyperprior
1827
hyperpriors
28+
ij
1929
iso
2030
isotype
2131
isotypes
32+
mathcal
2233
mcmc
34+
mergine
2335
nmc
36+
npl
37+
overfit
2438
params
2539
prec
40+
pred
2641
rhat
2742
sd
2843
seroconversion
@@ -32,13 +47,7 @@ seroresponses
3247
strat
3348
stratifications
3449
tbl
50+
tibble
3551
unstratified
52+
vectorized
3653
wishdf
37-
dayssincefeveronset
38-
ggplot
39-
tibble
40-
HlyE
41-
IgA
42-
IgG
43-
newpage
44-
npl

man/calc_fit_mod.Rd

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/run_mod.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/use_att_names.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)