Skip to content

Commit

Permalink
create test problem for population means
Browse files Browse the repository at this point in the history
  • Loading branch information
Anish S. Shah committed May 6, 2024
1 parent ae3013b commit aecd57d
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 123 deletions.
7 changes: 3 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ Depends:
Imports:
utils,
stats,
sf,
generics,
methods,
ggplot2,
tidyr,
dplyr,
purrr,
tibble,
rlang,
broom,
hardhat,
parsnip,
generics,
methods,
hardhat,
Suggests:
kableExtra,
knitr,
Expand Down
32 changes: 18 additions & 14 deletions R/coding.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#' Center for Medicare and Medicaid Services (CMS) Procedure Codes
#'
#' @description
#' This is a generative function used to call CMS procedure codes. It is used to
#' create a dataset that can be generally used to map procedure codes to their
#' descriptions, allowing for understanding of interventions performed. The
#' currently supported codes are explained in the details.
#' @description This is a generative function used to call CMS procedure codes.
#' It is used to create a dataset that can be generally used to map procedure
#' codes to their descriptions, allowing for understanding of interventions
#' performed. The currently supported codes are explained in the details.
#'
#' The following procedure codes are currently supported:
#'
Expand All @@ -16,11 +15,10 @@
#'
#' * CPT procedure codes, most recently updated on `2023-11-29`
#'
#' @details
#' CMS will usually release updated version of these codes on an annual basis.
#' Each dataset that is supported below can be identified by the year it was
#' published (not the _go-live_ date, but the _publically-available_ date). The
#' previous versions that are included in the package are as below.
#' @details CMS will usually release updated version of these codes on an annual
#' basis. Each dataset that is supported below can be identified by the year it
#' was published (not the _go-live_ date, but the _publically-available_ date).
#' The previous versions that are included in the package are as below.
#'
#' * ICD9: 2014
#'
Expand All @@ -30,11 +28,17 @@
#'
#' * CPT: 2023
#'
#' @param format <character> The format of the procedure codes. Currently supported formats
#' are: `c("icd9", "icd10", "hcpcs", "cpt")` (case-insensitive).
#' @param format The format of the procedure codes, written as a `character`.
#' Currently supported formats are: `c("icd9", "icd10", "hcpcs", "cpt")`
#' (case-insensitive).
#'
#' @param version <character> The version of the procedure codes. Currently supported:
#' `c("2014", "2023")`
#' @param version The version of the procedure codes, which are written as a
#' `character` although the majority are listed as a year. Currently
#' supported: `c("2014", "2023")`
#'
#' @returns A `tbl_df` with two columns: `code` and `description`. The `code`
#' refers to the procedure code, while the `description` refers to the
#' description of the procedure.
#'
#' @name procedure_codes
#' @export
Expand Down
68 changes: 29 additions & 39 deletions R/cosinor-constructor.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@
#' @param t Represents the _ordered_ time indices that provide the positions for the
#' cosine wave. Depending on the context:
#'
#' - A __data frame__ of a time-based predictor/index.
#' - A `data frame` of a time-based predictor/index.
#'
#' - A __matrix__ of time-based predictor/index.
#' - A `matrix` of time-based predictor/index.
#'
#' - A __recipe__ specifying a set of preprocessing steps
#' - A `recipe` specifying a set of preprocessing steps
#' created from [recipes::recipe()].
#'
#' @param y When `t` is a __data frame__ or __matrix__, `y` is the outcome
#' @param y When __t__ is a `data frame` or `matrix`, __y__ is the outcome
#' specified as:
#'
#' - A __data frame__ with 1 numeric column.
#' - A `data frame` with 1 numeric column.
#'
#' - A __matrix__ with 1 numeric column.
#' - A `matrix` with 1 numeric column.
#'
#' - A numeric __vector__.
#' - A numeric `vector`.
#'
#' @param data When a __recipe__ or __formula__ is used, `data` is specified as:
#' @param data When a `recipe` or `formula` is used, __data__ is specified as:
#'
#' - A __data frame__ containing both the predictors and the outcome.
#' - A `data frame` containing both the predictors and the outcome.
#'
#' @param formula A formula specifying the outcome terms on the left-hand side,
#' and the predictor terms on the right-hand side.
Expand All @@ -37,23 +37,23 @@
#' number of elements in the vector determine the number of components (e.g.
#' single versus multiple cosinor).
#'
#' - A __vector__ with a single element = single-component cosinor, e.g.
#' - A `vector` with a single element = single-component cosinor, e.g.
#' period = c(24)
#' - A __vector__ with multiple elements = multiple-component
#' - A `vector` with multiple elements = multiple-component
#' cosinor, e.g. period = c(24, 12)
#'
#' @param population Represents the population to be analyzed with a
#' population-mean cosinor. Defaults to NULL, assuming individual cosinors are
#' being generated. When a __recipe__ or __formula__ is used, `population` is
#' being generated. When a `recipe` or `formula` is used, __population__ is
#' specified as:
#'
#' - A __character__ name of the column contained in `data` that contains
#' - A `character` name of the column contained in __data__ that contains
#' identifiers for each subject. Every row will have a subject name which
#' should be duplicated for each time index given.
#'
#' When a __data frame__ or __matrix__ is used, `population` is specified as:
#' When a `data frame` or `matrix` is used, __population__ is specified as:
#'
#' - A __vector__ of the same length as `t`, with values representing each
#' - A `vector` of the same length as __t__, with values representing each
#' subject at the correct indices.
#'
#' @param ... Not currently used, but required for extensibility.
Expand All @@ -73,7 +73,7 @@ cosinor <- function(t, ...) {
UseMethod("cosinor")
}

# Cosinor Methods {{{ ----
# Cosinor Methods ----

## Default method

Expand Down Expand Up @@ -125,15 +125,13 @@ cosinor.recipe <- function(t, data, tau, population = NULL, ...) {
cosinor_bridge(processed, tau, population, data, ...)
}

# }}}

# Cosinor Construction {{{ ----
# Cosinor Construction ----

## Bridging Function

#' @description Bridging function takes user-facing call, after it is processed,
#' and moves it to `cosinor_bridge`, which then calls both `cosinor_impl`, the
#' fitting algorithm, and `new_cosinor`, the constructor for a new type of S3
#' and moves it to `cosinor_bridge()`, which then calls both `cosinor_impl()`, the
#' fitting algorithm, and `new_cosinor()`, the constructor for a new type of S3
#' class. This also bridges to population-mean cosinor implementation if
#' needed.
#' @noRd
Expand Down Expand Up @@ -254,9 +252,7 @@ new_cosinor <- function(
)
}

# }}}

# Cosinor Parsnip Methods {{{ ----
# Cosinor Parsnip Methods ----

# Wrapper function to load parsnip model
make_cosinor_reg <- function() {
Expand Down Expand Up @@ -327,8 +323,8 @@ make_cosinor_reg <- function() {
#' @examples
#' library(parsnip)
#' data(twins)
#' cosinor_reg(period = 24) %>%
#' set_engine("card") %>%
#' cosinor_reg(period = 24) |>
#' set_engine("card") |>
#' fit(rDYX ~ hour, data = twins)
#' @export
cosinor_reg <- function(mode = "regression", period = NULL) {
Expand Down Expand Up @@ -404,9 +400,7 @@ print.cosinor_reg <- function(x, ...) {
invisible(x)
}

# }}}

# Cosinor Generic S3 Methods {{{ ----
# Cosinor Generic S3 Methods ----

## Print Method

Expand Down Expand Up @@ -487,10 +481,7 @@ plot.cosinor <- function(x, ...) {
plot(model$t, model$yhat)
}


# }}}

# Cosinor Tidiers {{{ ----
# Cosinor Tidiers ----

## Tidy Method

Expand Down Expand Up @@ -525,17 +516,17 @@ tidy.cosinor <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {

# Tibble it
result <-
mat %>%
dplyr::as_tibble(rownames = "term") %>%
mat |>
dplyr::as_tibble(rownames = "term") |>
dplyr::rename(estimate = coefs, std.error = se)

if (conf.int) {

ci <- val$ci
colnames(ci) <- c("conf.low", "conf.high")
result <-
ci %>%
dplyr::as_tibble(rownames = "term") %>%
ci |>
dplyr::as_tibble(rownames = "term") |>
dplyr::left_join(result, ., by = "term")

}
Expand Down Expand Up @@ -571,5 +562,4 @@ augment.cosinor <- function(x, ...) {
# Return
return(result)

}

}
8 changes: 4 additions & 4 deletions R/cosinor-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Single Component Cosinor Implementation

#' @description Model fitting algorithm for cosinor. Results in output that define the new S3 class, as seen by the [hardhat::new_model], which generates the `new_cosinor` function.
#' @description Model fitting algorithm for cosinor. Results in output that define the new S3 class, as seen by the [hardhat::new_model], which generates the `new_cosinor()` function.
#' @noRd
cosinor_impl <- function(predictors, outcomes, tau) {

Expand Down Expand Up @@ -857,9 +857,9 @@ cosinor_features <- function(object, population = TRUE, ...) {

# Fits are based on individuals, already made from original pop-cosinor
results <-
aug %>%
tidyr::nest(models = -population) %>%
dplyr::mutate(purrr::map_df(models, features)) %>%
aug |>
tidyr::nest(models = -population) |>
dplyr::mutate(purrr::map_df(models, features)) |>
dplyr::select(-models)

}
Expand Down
2 changes: 1 addition & 1 deletion R/cosinor-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ ggmulticosinor <- function(object, labels, ...) {
# Merge together
aug <- dplyr::bind_rows(aug, .id = ".id")
features <-
dplyr::bind_rows(features, .id = ".id") %>%
dplyr::bind_rows(features, .id = ".id") |>
tibble::add_column(mesor = unlist(mesor))

# Plot
Expand Down
14 changes: 8 additions & 6 deletions R/cosinor-predict.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# Predict ----

#' Predict from a `cosinor`
#'
#' @param object A `cosinor` object.
#'
#' @param new_data A data frame or matrix of new predictors.
#' @param type A single character. The type of predictions to generate.
#' Valid options are:
#'
#' @param type A single character. The type of predictions to generate. Valid
#' options are:
#'
#' - `"numeric"` for numeric predictions.
#'
#' @param ... Not used, but required for extensibility.
#' @return
#' @param ... Additional arguments passed to the prediction function
#'
#' A tibble of predictions. The number of rows in the tibble is guaranteed
#' to be the same as the number of rows in `new_data`.
#' @return A tibble of predictions. The number of rows in the tibble is
#' guaranteed to be the same as the number of rows in `new_data`.
#'
#' @export
predict.cosinor <- function(object, new_data, type = "numeric", ...) {
Expand Down
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#' Clinical data is also available for visualization and comparison. Other HRV measures are used here for comparison and testing out functions.
#'
#' @docType data
#' @format An tibble data frame
#' @format A `tbl_df`
#' @keywords datasets
"triplets"

Expand Down
30 changes: 15 additions & 15 deletions man/cosinor.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/cosinor_reg.Rd

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

Loading

0 comments on commit aecd57d

Please sign in to comment.