Skip to content

Commit 825871e

Browse files
committed
bump doc and add new features
1 parent f61b9aa commit 825871e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+529
-143
lines changed

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export(mn2gamma)
147147
export(mn2norm)
148148
export(ms2beta)
149149
export(ms2gamma)
150+
export(msr2mvnorm)
150151
export(oc1S)
151152
export(oc1Sdecision)
152153
export(oc2S)
@@ -159,10 +160,12 @@ export(postmix)
159160
export(preddist)
160161
export(qmix)
161162
export(qmixdiff)
163+
export(read_mix_json)
162164
export(rmix)
163165
export(rmixdiff)
164166
export(robustify)
165167
export(sigma)
168+
export(write_mix_json)
166169
import(Formula)
167170
import(Rcpp)
168171
import(abind)
@@ -175,6 +178,9 @@ import(rstantools)
175178
import(stats)
176179
importFrom(RcppParallel,CxxFlags)
177180
importFrom(RcppParallel,RcppParallelLibs)
181+
importFrom(jsonlite,fromJSON)
182+
importFrom(jsonlite,toJSON)
183+
importFrom(lifecycle,deprecated)
178184
importFrom(matrixStats,colLogSumExps)
179185
importFrom(matrixStats,colSums2)
180186
importFrom(matrixStats,logSumExp)

R/mixjson.R

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#' Write and Read a Mixture Object with JSON
2+
#'
3+
#' @description
4+
#' `r lifecycle::badge("experimental")`
5+
#'
6+
#' These functions write and read a mixture object in the JSON format.
7+
#'
8+
#' @param mix A mixture object to be saved to JSON.
9+
#' @param con A connection specifying where the JSON will be written
10+
#' to or read.
11+
#' @param ... Additional arguments passed to the
12+
#' [jsonlite::toJSON()] and
13+
#' [jsonlite::fromJSON()] function for writing and
14+
#' reading, respectively.
15+
#'
16+
#' @details The mixture objects are written or read from the connection
17+
#' `con`, which can be a character string specifying a file
18+
#' path or a connection object as detailed in
19+
#' [base::connections()].
20+
#'
21+
#' When writing mixture objects as JSON it is strongly recommended to
22+
#' explicitly set the number of digits (argument `digits`) to be
23+
#' used for the numerical representation in order to control the
24+
#' accuracy of the JSON representation of the mixture object. If the
25+
#' mixture object inherits from the `"EM"` class (as is the
26+
#' case when the mixture is created using the
27+
#' [RBesT::mixfit()] function), then the mixture object
28+
#' will be cast to a simple mixture object such that diagnostics
29+
#' from the `"EM"` fitting procedure are dropped from the
30+
#' object. For easier readability the user is encouraged to set the
31+
#' argument `pretty=TRUE`, which is passed to the
32+
#' [jsonlite::toJSON()] function and makes the output more
33+
#' human readable.
34+
#'
35+
#' Note that when reading in mixture objects, then these are not
36+
#' necessarily equal to the mixtures passed to the `write_mix_json`
37+
#' function. This is a consequence of the limited precision of the
38+
#' textual representation as defined by the `digits` argument.
39+
#'
40+
#' @return The `write_mix_json` function does not return a value while
41+
#' the `read_mix_json` returns the mixture object stored in the
42+
#' connection specified.
43+
#'
44+
#' @family mixdist
45+
#'
46+
#' @examples
47+
#' \dontrun{
48+
#' nm <- mixnorm(rob = c(0.2, 0, 2), inf = c(0.8, 2, 2), sigma = 5)
49+
#'
50+
#' write_mix_json(nm, "normal_mixture.json", pretty=TRUE, digits=1)
51+
#'
52+
#' mix <- read_mix_json("normal_mixture.json")
53+
#' }
54+
#' @name mixjson
55+
NULL
56+
57+
#' @rdname mixjson
58+
#' @export
59+
write_mix_json <- function(mix, con, ...) {
60+
## strip off all attributes which are added by the EM outputs and
61+
## recast the class into a mixture only object
62+
if (inherits(mix, "EM")) {
63+
message("Dropping EM information from mixture object before serialization.")
64+
mix <- mixcombine(mix, weight = 1, rescale = FALSE)
65+
}
66+
dot_args <- list(...)
67+
if (!("digits" %in% names(dot_args))) {
68+
warning(
69+
"JSON serialization by default restricts number of digits to ",
70+
formals(jsonlite::toJSON)$digits,
71+
".\nIt is recommended to set this option explicitly."
72+
)
73+
}
74+
umix <- unclass(mix)
75+
amix <- attributes(mix)
76+
amix$link <- amix$link$name
77+
json <- jsonlite::toJSON(list(meta = amix, comp = umix), ...)
78+
writeLines(json, con, useBytes = TRUE)
79+
}
80+
81+
#' @rdname mixjson
82+
#' @param rescale A logical value indicating whether to rescale the
83+
#' mixture weights so that they sum to 1. Defaults to `TRUE`.
84+
#' @export
85+
read_mix_json <- function(con, ..., rescale = TRUE) {
86+
json <- readLines(con)
87+
mix_list <- jsonlite::fromJSON(json, ...)
88+
mix <- mix_list$comp
89+
attributes(mix) <- mix_list$meta
90+
attr(mix, "link") <- link_map[[mix_list$meta$link]]
91+
if (rescale) {
92+
mix[1, ] <- mix[1, ] / sum(mix[1, ])
93+
}
94+
mix
95+
}

man/AS.Rd

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

man/BinaryExactCI.Rd

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

man/RBesT-package.Rd

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

man/SimSum.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/automixfit.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/colitis.Rd

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

man/crohn.Rd

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

man/decision1S.Rd

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

0 commit comments

Comments
 (0)