1
1
# ' @title Run Jags Model
2
2
# ' @author Sam Schildhauer
3
3
# ' @description
4
- # ' run_mod() takes a data frame and adjustable mcmc inputs to
5
- # ' [runjags::run.jags()] as an mcmc
6
- # ' bayesian model to estimate antibody dynamic curve parameters.
4
+ # ' ` run_mod()` takes a data frame and adjustable MCMC inputs to
5
+ # ' [runjags::run.jags()] as an MCMC
6
+ # ' Bayesian model to estimate antibody dynamic curve parameters.
7
7
# ' The [rjags::jags.model()] models seroresponse dynamics to an
8
8
# ' infection. The antibody dynamic curve includes the following parameters:
9
9
# ' - y0 = baseline antibody concentration
10
10
# ' - y1 = peak antibody concentration
11
11
# ' - t1 = time to peak
12
- # ' - r = shape parameter
12
+ # ' - shape = shape parameter
13
13
# ' - alpha = decay rate
14
14
# ' @param data A [base::data.frame()] with the following columns.
15
15
# ' @param file_mod The name of the file that contains model structure.
16
16
# ' @param nchain An [integer] between 1 and 4 that specifies
17
- # ' the number of mcmc chains to be run per jags model.
17
+ # ' the number of MCMC chains to be run per jags model.
18
18
# ' @param nadapt An [integer] specifying the number of adaptations per chain.
19
19
# ' @param nburn An [integer] specifying the number of burn ins before sampling.
20
20
# ' @param nmc An [integer] specifying the number of samples in posterior chains.
26
26
# ' should be included as an element of the [list] object returned by `run_mod()`
27
27
# ' (see `Value` section below for details).
28
28
# ' Note: These objects can be large.
29
- # ' @param include_subs A [logical] value specifying whether posterior
30
- # ' distributions should be included for all subjects. A value of [FALSE] will
31
- # ' only include the predictive distribution.
32
- # ' @returns A [list] containing the following elements:
33
- # ' - `"jags.post"`: a [list] containing one or more [runjags::runjags-class]
34
- # ' objects (one per stratum).
35
- # ' - A [base::data.frame()] titled `curve_params` that contains the posterior
36
- # ' distribution will be exported with the following attributes:
37
- # ' - `iteration` = number of sampling iterations
38
- # ' - `chain` = number of mcmc chains run; between 1 and 4
39
- # ' - `indexid` = "newperson", indicating posterior distribution
40
- # ' - `antigen_iso` = antibody/antigen type combination being evaluated
41
- # ' - `alpha` = posterior estimate of decay rate
42
- # ' - `r` = posterior estimate of shape parameter
43
- # ' - `t1` = posterior estimate of time to peak
44
- # ' - `y0` = posterior estimate of baseline antibody concentration
45
- # ' - `y1` = posterior estimate of peak antibody concentration
46
- # ' - `stratified variable` = the variable used to stratify jags model
47
- # ' - A [list] of `attributes` that summarize the jags inputs, including:
29
+ # ' @returns An `sr_model` class object: a subclass of [dplyr::tbl_df] that
30
+ # ' contains MCMC samples from the joint posterior distribution of the model
31
+ # ' parameters, conditional on the provided input `data`,
32
+ # ' including the following:
33
+ # ' - `iteration` = Number of sampling iterations
34
+ # ' - `chain` = Number of MCMC chains run; between 1 and 4
35
+ # ' - `Parameter` = Parameter being estimated. Includes the following:
36
+ # ' - `y0` = Posterior estimate of baseline antibody concentration
37
+ # ' - `y1` = Posterior estimate of peak antibody concentration
38
+ # ' - `t1` = Posterior estimate of time to peak
39
+ # ' - `shape` = Posterior estimate of shape parameter
40
+ # ' - `alpha` = Posterior estimate of decay rate
41
+ # ' - `Iso_type` = Antibody/antigen type combination being evaluated
42
+ # ' - `Stratification` = The variable used to stratify jags model
43
+ # ' - `Subject` = ID of subject being evaluated
44
+ # ' - `value` = Estimated value of the parameter
45
+ # ' - The following [attributes] are included in the output:
48
46
# ' - `class`: Class of the output object.
49
47
# ' - `nChain`: Number of chains run.
50
48
# ' - `nParameters`: The amount of parameters estimated in the model.
51
49
# ' - `nIterations`: Number of iteration specified.
52
50
# ' - `nBurnin`: Number of burn ins.
53
- # ' - `nThin`: Thinning number (niter/nmc).
51
+ # ' - `nThin`: Thinning number (niter/nmc)
52
+ # ' - `priors`: A [list] that summarizes the input priors, including:
53
+ # ' - `mu_hyp_param`
54
+ # ' - `prec_hyp_param`
55
+ # ' - `omega_param`
56
+ # ' - `wishdf`
57
+ # ' - `prec_logy_hyp_param`
58
+ # ' - An optional `"jags.post"` attribute, included when argument
59
+ # ' `with_post` = TRUE.
60
+ # ' @inheritDotParams prep_priors
54
61
# ' @export
55
62
# ' @example inst/examples/run_mod-examples.R
56
63
run_mod <- function (data ,
57
- file_mod ,
64
+ file_mod = serodynamics_example( " model.jags " ) ,
58
65
nchain = 4 ,
59
66
nadapt = 0 ,
60
67
nburn = 0 ,
61
68
nmc = 100 ,
62
69
niter = 100 ,
63
70
strat = NA ,
64
71
with_post = FALSE ,
65
- include_subs = FALSE ) {
72
+ ... ) {
66
73
# # Conditionally creating a stratification list to loop through
67
74
if (is.na(strat )) {
68
75
strat_list <- " None"
@@ -97,7 +104,8 @@ run_mod <- function(data,
97
104
98
105
# prepare data for modeline
99
106
longdata <- prep_data(dl_sub )
100
- priors <- prep_priors(max_antigens = longdata $ n_antigen_isos )
107
+ priorspec <- prep_priors(max_antigens = longdata $ n_antigen_isos ,
108
+ ... )
101
109
102
110
# inputs for jags model
103
111
nchains <- nchain # nr of MC chains to run simultaneously
@@ -111,7 +119,7 @@ run_mod <- function(data,
111
119
112
120
jags_post <- runjags :: run.jags(
113
121
model = file_mod ,
114
- data = c(longdata , priors ),
122
+ data = c(longdata , priorspec ),
115
123
inits = initsfunction ,
116
124
method = " parallel" ,
117
125
adapt = nadapt ,
@@ -127,13 +135,13 @@ run_mod <- function(data,
127
135
# stratification and will only be included if specified.
128
136
jags_post_final [[i ]] <- jags_post
129
137
130
- # Unpacking and cleaning mcmc output.
138
+ # Unpacking and cleaning MCMC output.
131
139
jags_unpack <- ggmcmc :: ggs(jags_post [[" mcmc" ]])
132
140
133
141
# Adding attributes
134
142
mod_atts <- attributes(jags_unpack )
135
- # Only keeping necesarry attributes
136
- mod_atts <- mod_atts [3 : 8 ]
143
+ # Only keeping necessary attributes
144
+ mod_atts <- mod_atts [4 : 8 ]
137
145
138
146
# extracting antigen-iso combinations to correctly number
139
147
# then by the order they are estimated by the program.
@@ -169,25 +177,27 @@ run_mod <- function(data,
169
177
jags_out <- jags_out [complete.cases(jags_out ), ]
170
178
# Outputting the finalized jags output as a data frame with the
171
179
# jags output results for each stratification rbinded.
172
- # Logical argument to include posterior of all subjects or just the
173
- # predictive distribution (new person).
174
- if (! include_subs ) {
175
- jags_out <- jags_out | >
176
- filter(.data $ Subject == " newperson" )
177
- }
178
180
179
- # Logical argument to keep the raw jags post or not.
181
+ # Making output a tibble and restructing.
182
+ jags_out <- dplyr :: as_tibble(jags_out ) | >
183
+ select(! c(" Parameter" )) | >
184
+ rename(" Parameter" = " Parameter_sub" )
185
+ jags_out <- jags_out [, c(" Iteration" , " Chain" , " Parameter" , " Iso_type" ,
186
+ " Stratification" , " Subject" , " value" )]
187
+ current_atts <- attributes(jags_out )
188
+ current_atts <- c(current_atts , mod_atts )
189
+ attributes(jags_out ) <- current_atts
190
+
191
+ # Adding priors
192
+ jags_out <- jags_out | >
193
+ structure(" priors" = attributes(priorspec )$ used_priors )
194
+
195
+ # Conditionally adding jags.post
180
196
if (with_post ) {
181
- jags_out <- list (
182
- " curve_params" = jags_out ,
183
- " jags.post" = jags_post_final ,
184
- " attributes" = mod_atts
185
- )
186
- } else {
187
- jags_out <- list (
188
- " curve_params" = jags_out ,
189
- " attributes" = mod_atts
190
- )
191
- }
197
+ jags_out <- jags_out | >
198
+ structure(jags.post = jags_post_final )
199
+ }
200
+ jags_out <- jags_out | >
201
+ structure(class = union(" sr_model" , class(jags_out )))
192
202
jags_out
193
203
}
0 commit comments