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

Prepare for cran #34

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions R/ext.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ a <- b <- NULL
#'
#' @return named list with entries `"a"`, the location parameter, and `"b"`, the
#' scale parameter
#' @noRd
calc_loc_scale_params <- function(ps, qs, dist) {
if (dist == "lnorm") {
if (any(qs <= 0.0)) {
Expand Down Expand Up @@ -48,6 +49,7 @@ calc_loc_scale_params <- function(ps, qs, dist) {
#' evaluate the density function (or its log) of the distribution in the
#' specified location-scale family that has quantiles matching those in `ps`
#' and `qs`
#' @noRd
d_ext_factory <- function(ps, qs, dist) {
c(a, b) %<-% calc_loc_scale_params(ps, qs, dist)

Expand Down Expand Up @@ -88,6 +90,7 @@ d_ext_factory <- function(ps, qs, dist) {
#' evaluate the cumulative distribution function (or its log) of the
#' distribution in the specified location-scale family that has quantiles
#' matching those in `ps` and `qs`
#' @noRd
p_ext_factory <- function(ps, qs, dist) {
c(a, b) %<-% calc_loc_scale_params(ps, qs, dist)

Expand Down Expand Up @@ -123,6 +126,7 @@ p_ext_factory <- function(ps, qs, dist) {
#' @return a function with parameter `p` that can be used to evaluate the
#' quantile function of the distribution in the specified location-scale
#' family that has quantiles matching those in `ps` and `qs`
#' @noRd
q_ext_factory <- function(ps, qs, dist) {
c(a, b) %<-% calc_loc_scale_params(ps, qs, dist)

Expand Down
39 changes: 19 additions & 20 deletions R/int.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
#' interpolating a given set of points.
#'
#' @param x vector giving the x coordinates of the points to be interpolated.
#' Alternatively a single plotting structure can be specified: see ‘xy.coords’.
#'
#' @param y vector giving the y coordinates of the points to be interpolated.
#' Must be increasing or decreasing for ‘method = "hyman"’. Alternatively a
#' single plotting structure can be specified: see ‘xy.coords’.
#' Must be increasing or decreasing for 'method = "hyman"'.
#'
#' @param m (for splinefunH()) vector of _slopes_ \eqn{m_i}{m[i]} at the
#' @param m (for 'splinefunH()') vector of _slopes_ \eqn{m_i}{m[i]} at the
#' points \eqn{(x_i,y_i)}{(x[i],y[i])}; these together determine the
#' *H*ermite “spline” which is piecewise cubic, (only) _once_ differentiable
#' continuously.
Expand Down Expand Up @@ -103,7 +101,7 @@ mono_Hermite_spline <- function(x, y, m) {
#' @param y numeric vector with the "vertical axis" coordinates of the points
#' to interpolate.
#' @param cont_dir at steps or discontinuities, the direction from which the
#' function is continuous. This will be "right" for a cdf or "left" for a qf.
#' function is continuous. This will be "right" for a CDF or "left" for a QF.
#' @param increasing boolean indicating whether the function is increasing or
#' decreasing. Only used in the degenerate case where there is only one unique
#' value of `x`.
Expand Down Expand Up @@ -202,41 +200,41 @@ step_interp_factory <- function(x, y, cont_dir = c("right", "left"),
}


#' Approximate density function, cdf, or quantile function on the interior of
#' Approximate density function, CDF, or quantile function on the interior of
#' provided quantiles by representing the distribution as a sum of a discrete
#' part at any duplicated `qs` and a continuous part for which the cdf is
#' part at any duplicated `qs` and a continuous part for which the CDF is
#' estimated using a monotonic Hermite spline. See details for more.
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#' @param tail_dist name of parametric distribution for the tails
#' @param fn_type the type of function that is requested: `"d"` for a pdf,
#' `"p"` for a cdf, or `"q"` for a quantile function.
#' @param fn_type the type of function that is requested: `"d"` for a PDF,
#' `"p"` for a CDF, or `"q"` for a quantile function.
#' @param n_grid grid size to use when augmenting the input `qs` to obtain a
#' finer grid of points along which we form a piecewise linear approximation
#' to the spline. `n_grid` evenly-spaced points are inserted between each
#' pair of consecutive values in `qs`. The default value is 20. This can
#' be set to `NULL`, in which case the piecewise linear approximation is not
#' used. This is not recommended if the `fn_type` is `"q"`.
#'
#' @details The cdf of the continuous part of the distribution is estimated
#' @details The CDF of the continuous part of the distribution is estimated
#' using a monotonic degree 3 Hermite spline that interpolates the quantiles
#' after subtracting the discrete distribution and renormalizing. In theory,
#' an estimate of the quantile function could be obtained by directly inverting
#' this spline. However, in practice, we have observed that this can suffer from
#' numerical problems. Therefore, the default behavior of this function is to
#' evaluate the "stage 1" cdf estimate corresponding to discrete point masses
#' plus monotonic spline at a fine grid of points, and use the "stage 2" cdf
#' evaluate the "stage 1" CDF estimate corresponding to discrete point masses
#' plus monotonic spline at a fine grid of points, and use the "stage 2" CDF
#' estimate that linearly interpolates these points with steps at any duplicated
#' q values. The quantile function estimate is obtained by inverting this
#' "stage 2" cdf estimate. When the distribution is continuous, we can obtain an
#' estimate of the pdf by differentiating the cdf estimate, resulting in a
#' "stage 2" CDF estimate. When the distribution is continuous, we can obtain an
#' estimate of the PDF by differentiating the CDF estimate, resulting in a
#' discontinuous "histogram density". The size of the grid can be specified with
#' the `n_grid` argument. In settings where it is desirable to obtain a
#' continuous density function, the "stage 1" cdf estimate can be used by
#' continuous density function, the "stage 1" CDF estimate can be used by
#' setting `n_grid = NULL`.
#'
#' @return a function to evaluate the pdf, cdf, or quantile function.
#' @return a function to evaluate the PDF, CDF, or quantile function.
#' @rdname spline_cdf
spline_cdf <- function(ps, qs, tail_dist,
fn_type = c("d", "p", "q"),
Expand All @@ -250,7 +248,7 @@ spline_cdf <- function(ps, qs, tail_dist,
}
}

#' @rdname spline_cdf
#' @noRd
#' @importFrom stats approx
spline_cdf_grid_interp <- function(ps, qs, tail_dist,
fn_type = c("d", "p", "q"),
Expand Down Expand Up @@ -291,6 +289,7 @@ spline_cdf_grid_interp <- function(ps, qs, tail_dist,
#' pair of consecutive values in qs, and the corresponding ps are filled in
#' by evaluating the spline output from `spline_cdf_direct` at the qs grid.
#' @keywords internal
#' @noRd
grid_augment_ps_qs <- function(ps, qs, tail_dist, n_grid) {
n_grid <- as.integer(n_grid)
if (n_grid < 0) {
Expand Down Expand Up @@ -328,14 +327,14 @@ grid_augment_ps_qs <- function(ps, qs, tail_dist, n_grid) {
#' Internal function that constructs a monotonic Hermite spline interpolating
#' ps and qs.
#' @importFrom stats predict
#' @rdname spline_cdf
#' @noRd
spline_cdf_direct <- function(ps, qs, tail_dist,
fn_type = c("d", "p", "q")) {
# fit a monotonic spline to the qs and ps for the continuous part of the
# distribution to approximate the cdf on the interior
# the vector m that we assemble here has the target slopes of the spline
# at each data point (qs[i], ps[i])
# on ends, slope of cdf approximation should match tail distribution pdf
# on ends, slope of cdf approximation should match tail distribution PDF
# on interior, slope is the mean of the slopes of the adjacent segments
# note: there is probably room for improvement for this interior behavior,
# but this seems to be the standard strategy for monotonic splines
Expand Down
25 changes: 13 additions & 12 deletions R/make_dist_fns.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ disc_weight <- disc_ps <- disc_qs <- cont_ps <- cont_qs <- disc_ps_range <- NULL
#' Clean up ps and qs provided by user: handle missing and unsorted values
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#'
#' @return named list with entries `ps` and `qs`
#' @noRd
clean_ps_and_qs <- function(ps, qs) {
checkmate::assert_numeric(ps, lower = 0, upper = 1)
checkmate::assert_numeric(qs)
Expand Down Expand Up @@ -40,7 +41,7 @@ clean_ps_and_qs <- function(ps, qs) {
#' from a set of quantiles of the distribution.
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#' @param interior_method method for interpolating the distribution on the
#' interior of the provided `qs`. This package provides one method for this,
#' `"spline_cdf"`. The user may also provide a custom function; see the
Expand All @@ -60,8 +61,8 @@ clean_ps_and_qs <- function(ps, qs) {
#' distribution as a sum of a discrete component at any points where there
#' are duplicated `qs` for multiple different `ps` and a continuous component
#' that is estimated by using a monotonic cubic spline that interpolates the
#' provided `(q, p)` pairs as an estimate of the cdf. The density function is
#' then obtained by differentiating this estimate of the cdf.
#' provided `(q, p)` pairs as an estimate of the CDF. The density function is
#' then obtained by differentiating this estimate of the CDF.
#'
#' Optionally, the user may provide another function that accepts arguments
#' `ps`, `qs`, `tail_dist`, and `fn_type` (which will be either `"d"`, `"p"`,
Expand Down Expand Up @@ -149,7 +150,7 @@ make_d_fn <- function(ps, qs,
#' from a set of quantiles of the distribution.
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#' @param interior_method method for interpolating the distribution on the
#' interior of the provided `qs`. This package provides one method for this,
#' `"spline_cdf"`. The user may also provide a custom function; see the
Expand All @@ -169,7 +170,7 @@ make_d_fn <- function(ps, qs,
#' distribution as a sum of a discrete component at any points where there
#' are duplicated `qs` for multiple different `ps` and a continuous component
#' that is estimated by using a monotonic cubic spline that interpolates the
#' provided `(q, p)` pairs as an estimate of the cdf.
#' provided `(q, p)` pairs as an estimate of the CDF.
#'
#' Optionally, the user may provide another function that accepts arguments
#' `ps`, `qs`, `tail_dist`, and `fn_type` (which will be either `"d"`, `"p"`,
Expand Down Expand Up @@ -294,7 +295,7 @@ make_p_fn <- function(ps, qs,
#' quantiles of the distribution.
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#' @param interior_method method for interpolating the distribution on the
#' interior of the provided `qs`. This package provides one method for this,
#' `"spline_cdf"`. The user may also provide a custom function; see the
Expand All @@ -314,8 +315,8 @@ make_p_fn <- function(ps, qs,
#' distribution as a sum of a discrete component at any points where there
#' are duplicated `qs` for multiple different `ps` and a continuous component
#' that is estimated by using a monotonic cubic spline that interpolates the
#' provided `(q, p)` pairs as an estimate of the cdf. The quantile function
#' is then obtained by inverting this estimate of the cdf.
#' provided `(q, p)` pairs as an estimate of the CDF. The quantile function
#' is then obtained by inverting this estimate of the CDF.
#'
#' Optionally, the user may provide another function that accepts arguments
#' `ps`, `qs`, `tail_dist`, and `fn_type` (which will be either `"d"`, `"p"`,
Expand Down Expand Up @@ -443,7 +444,7 @@ make_q_fn <- function(ps, qs,
#' quantiles of the distribution.
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#' @param interior_method method for interpolating the distribution on the
#' interior of the provided `qs`. This package provides one method for this,
#' `"spline_cdf"`. The user may also provide a custom function; see the
Expand All @@ -463,8 +464,8 @@ make_q_fn <- function(ps, qs,
#' distribution as a sum of a discrete component at any points where there
#' are duplicated `qs` for multiple different `ps` and a continuous component
#' that is estimated by using a monotonic cubic spline that interpolates the
#' provided `(q, p)` pairs as an estimate of the cdf. The quantile function
#' is then obtained by inverting this estimate of the cdf.
#' provided `(q, p)` pairs as an estimate of the CDF. The quantile function
#' is then obtained by inverting this estimate of the CDF.
#'
#' Optionally, the user may provide another function that accepts arguments
#' `ps`, `qs`, `tail_dist`, and `fn_type` (which will be either `"d"`, `"p"`,
Expand Down
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
#' parts of a distribution.
#'
#' @param ps vector of probability levels
#' @param qs vector of quantile values correponding to ps
#' @param qs vector of quantile values corresponding to ps
#' @param dup_tol numeric tolerance for identifying duplicated values indicating
#' a discrete component of the distribution. If there is a run of values where
#' each consecutive pair is closer together than the tolerance, all are
Expand All @@ -103,15 +103,15 @@
#' - `disc_qs`: locations of discrete components, corresponding to duplicated
#' values in the input `qs`. May be `numeric(0)` if there are no discrete
#' components.
#' - `cont_ps`: probability levels for the continous part of the distribution
#' - `cont_qs`: quantile values for the continous part of the distribution
#' - `cont_ps`: probability levels for the continuous part of the distribution
#' - `cont_qs`: quantile values for the continuous part of the distribution
#' - `disc_ps_range`: a list of length equal to the number of point masses in
#' the discrete distribution. Each entry is a numeric vector of length two
#' with the value of the cdf approaching the point mass from the left and
#' with the value of the CDF approaching the point mass from the left and
#' from the right.
#'
#' @export
split_disc_cont_ps_qs <- function(ps, qs, dup_tol = 1e-6, zero_tol = 1e-12,

Check warning on line 114 in R/utils.R

View workflow job for this annotation

GitHub Actions / lint

file=R/utils.R,line=114,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 19.
is_hurdle = FALSE) {
# if zero counts as discrete and any qs are (approximately) zero, augment
# with an additional zero so that logic below based on duplicate qs works
Expand Down
28 changes: 0 additions & 28 deletions man/calc_loc_scale_params.Rd

This file was deleted.

19 changes: 0 additions & 19 deletions man/clean_ps_and_qs.Rd

This file was deleted.

30 changes: 0 additions & 30 deletions man/d_ext_factory.Rd

This file was deleted.

18 changes: 0 additions & 18 deletions man/grid_augment_ps_qs.Rd

This file was deleted.

6 changes: 3 additions & 3 deletions man/make_d_fn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/make_p_fn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading