Skip to content

Commit

Permalink
Updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
gowerc committed Mar 28, 2024
1 parent ef87806 commit b954ffc
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 145 deletions.
109 changes: 107 additions & 2 deletions R/Grid.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,114 @@

# TODO - Docs
#' Quantity Grid Specification
#'
#' @param subjects (`character` or `NULL`)\cr vector of subjects to extract quantities for.
#' If `NULL` will default to all subjects within the dataset.
#' @param times (`numeric` or `NULL`)\cr vector of time points to extract quantities at.
#' If `NULL` will default to 201 evenly spaced timepoints between 0 and either the max
#' observation time (for [`LongitudinalQuantities`]) or max event time (for [`SurvivalQuantities`]).
#' @param groups (`list`)\cr named list of subjects to extract quantities for. See Group Specification.
#'
#' @description
#' These functions are used to specify which subjects and timepoints should be generated
#' when calculating quantities via [`SurvivalQuantities`] and [`LongitudinalQuantities`].
#'
#' @section Group Specification:
#' For `GridGrouped()`, `groups` must be a named list of character vectors. Each element of the list
#' must be a character vector of the subjects that will form the group where the element name
#' is the corresponding name of the group. For example if the goal was to create two groups
#' named `Group-1` and `Group-2` which are composed of the subjects `pt-1`, `pt-2` and
#' `pt-3`, `pt-4` respectively then this would be specified as:
#' ```
#' GridGrouped(
#' groups = list(
#' "Group-1" = c("pt-1", "pt-2"),
#' "Group-2" = c("pt-3", "pt-4")
#' )
#' )
#' ```
#' @seealso [`SurvivalQuantities`], [`LongitudinalQuantities`]
#' @name Grid-Functions
NULL






#' Grid Developer Notes
#'
#' @description
#' Developer details for implementing / extending `Grid` objects for defining
#' generated quantities for [`SurvivalQuantities`] and [`LongitudinalQuantities`].
#'
#' @slot subjects (`character` or `NULL`)\cr vector of subjects to extract quantities for.
#' If `NULL` will default to all subjects within the dataset.
#' @slot times (`numeric` or `NULL`)\cr vector of time points to extract quantities at.
#' If `NULL` will default to 201 evenly spaced timepoints between 0 and either the max
#' @slot groups (`list`)\cr named list of subjects to extract quantities for. See details.
#'
#' @details
#' All grid classes must inherit from the abstract `Grid` class.
#' All grid classes must provide `as.QuantityGenerator(object, data)` and
#' `as.QuantityCollapser(object, data)` methods where `data` is a [`DataJoint`] object.
#' These methods must return a `QuantityGenerator` and `QuantityCollapser` object respectively.
#' The `QuantityGenerator` object specifies unique subject/timepoint combinations that samples
#' should be generated at.
#' The `QuantityCollapser` object specifies how to combine these generated samples
#' to form the desired quantities.
#' As an example say we want to generate grouped samples for the groups `Group-1` and `Group-2`
#' which consist of the subjects `pt-1`, `pt-2` and `pt-3`, `pt-4` respectively at two time points
#' `10` and `20`. We can achieve this as follows:
#' ```
#' .QuantityGenerator(
#' times = c(10, 10, 10, 10, 20, 20, 20, 20),
#' subjects = c("pt-1" "pt-2", "pt-3", "pt-4", "pt-1" "pt-2", "pt-3", "pt-4")
#' )
#' .QuantityCollapser(
#' times = c(10, 20, 10 , 20),
#' groups = c("Group-1", "Group-1", "Group-2", "Group-2"),
#' indexes = list(c(1, 2), c(5, 6), c(3, 4), c(7, 8))
#' )
#' ```
#'
#' @inheritSection Grid-Functions Group Specification
#'
#' @keywords internal
#' @seealso Quant-Dev
#' @name Grid-Dev
NULL



#' Quantity Developer Notes
#'
#' @description
#' Developer details for `QuantityX` objects/methods. This page just outlines the arguements
#' and slots of these objects/methods. For the full implementation details please see [Grid-Dev]
#'
#' @slot times (`numeric`)\cr vector of time points to extract quantities at.
#' @slot subjects (`character`)\cr vector of subjects to extract quantities for.
#' @slot groups (`character`)\cr vector of labels to apply to the generated quantities.
#' @slot indexes (`list`)\cr list of indexes that specify which observations from a
#' `QuantityGenerator` should be combined to form the desired quantities.
#'
#' @param object (`Grid`)\cr object to convert to a `QuantityGenerator` or `QuantityCollapser`.
#' @param data (`DataJoint`)\cr Survival and Longitudinal Data.
#' @param ... Not currently used.
#'
#' @details
#' The `as.QuantityGenerator` must return a `QuantityGenerator` object.
#' The `as.QuantityCollapser` must return a `QuantityCollapser` object.
#' @keywords internal
#' @name Quant-Dev
NULL


#' @rdname Grid-Dev
.Grid <- setClass("Grid")


#' @rdname Quant-Dev
.QuantityGenerator <- setClass(
"QuantityGenerator",
slots = c(
Expand All @@ -12,7 +117,7 @@
)
)


#' @rdname Quant-Dev
.QuantityCollapser <- setClass(
"QuantityCollapser",
slots = c(
Expand Down
11 changes: 8 additions & 3 deletions R/GridFixed.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#' @include generics.R
NULL


#' @rdname Grid-Dev
.GridFixed <- setClass(
"GridFixed",
contains = "Grid",
Expand All @@ -11,6 +13,7 @@ NULL
)
)

#' @rdname Grid-Functions
#' @export
GridFixed <- function(subjects = NULL, times = NULL) {
.GridFixed(
Expand All @@ -19,8 +22,9 @@ GridFixed <- function(subjects = NULL, times = NULL) {
)
}

#' @rdname Quant-Dev
#' @export
as.QuantityGenerator.GridFixed <- function(object, data) {
as.QuantityGenerator.GridFixed <- function(object, data, ...) {
assert_class(data, "DataJoint")
data_list <- as.list(data)
subjects <- unlist(as.list(object, data = data), use.names = FALSE)
Expand All @@ -38,8 +42,9 @@ as.QuantityGenerator.GridFixed <- function(object, data) {
)
}

#' @rdname Quant-Dev
#' @export
as.QuantityCollapser.GridFixed <- function(object, data) {
as.QuantityCollapser.GridFixed <- function(object, data, ...) {
generator <- as.QuantityGenerator(object, data)
.QuantityCollapser(
times = generator@times,
Expand All @@ -50,7 +55,7 @@ as.QuantityCollapser.GridFixed <- function(object, data) {


#' @export
as.list.GridFixed <- function(x, data) {
as.list.GridFixed <- function(x, data, ...) {
data_list <- as.list(data)
subjects <- if (is.null(x@subjects)) {
subs <- as.list(names(data_list$pt_to_ind))
Expand Down
9 changes: 6 additions & 3 deletions R/GridGrouped.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' @include generics.R
NULL

#' @rdname Grid-Dev
.GridGrouped <- setClass(
"GridGrouped",
contains = "Grid",
Expand All @@ -12,6 +13,7 @@ NULL
)
)

#' @rdname Grid-Functions
#' @export
GridGrouped <- function(groups, times = NULL) {
.GridGrouped(
Expand All @@ -37,9 +39,9 @@ setValidity(
)



#' @rdname Quant-Dev
#' @export
as.QuantityGenerator.GridGrouped <- function(object, data) {
as.QuantityGenerator.GridGrouped <- function(object, data, ...) {
assert_class(data, "DataJoint")
data_list <- as.list(data)
patients_unique <- unique(unlist(object@groups))
Expand All @@ -56,8 +58,9 @@ as.QuantityGenerator.GridGrouped <- function(object, data) {
}


#' @rdname Quant-Dev
#' @export
as.QuantityCollapser.GridGrouped <- function(object, data) {
as.QuantityCollapser.GridGrouped <- function(object, data, ...) {
assert_class(data, "DataJoint")
data_list <- as.list(data)
assert_that(
Expand Down
12 changes: 8 additions & 4 deletions R/GridObserved.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#' @include generics.R
NULL

#' @rdname Grid-Dev
.GridObserved <- setClass(
"GridObserved",
contains = "Grid",
Expand All @@ -10,15 +11,18 @@ NULL
)
)


#' @rdname Grid-Functions
#' @export
GridObserved <- function(subjects = NULL) {
.GridObserved(
subjects = subjects
)
}

#' @rdname Quant-Dev
#' @export
as.QuantityGenerator.GridObserved <- function(object, data) {
as.QuantityGenerator.GridObserved <- function(object, data, ...) {
assert_class(data, "DataJoint")
data_list <- as.list(data)
subjects <- unlist(as.list(object, data = data), use.names = FALSE)
Expand All @@ -31,9 +35,9 @@ as.QuantityGenerator.GridObserved <- function(object, data) {
)
}


#' @rdname Quant-Dev
#' @export
as.QuantityCollapser.GridObserved <- function(object, data) {
as.QuantityCollapser.GridObserved <- function(object, data, ...) {
generator <- as.QuantityGenerator(object, data)

.QuantityCollapser(
Expand All @@ -44,7 +48,7 @@ as.QuantityCollapser.GridObserved <- function(object, data) {
}

#' @export
as.list.GridObserved <- function(x, data) {
as.list.GridObserved <- function(x, data, ...) {
data_list <- as.list(data)
subjects <- if (is.null(x@subjects)) {
subs <- as.list(names(data_list$pt_to_ind))
Expand Down
7 changes: 3 additions & 4 deletions R/JointModelSamples.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ setOldClass("CmdStanMCMC")


#' @rdname generateQuantities
#' @param patients (`character`)\cr explicit vector of patient IDs for whom the
#' generated quantities should be extracted
#' @param type (`character`)\cr type of quantities to be generated, must be either "survial" or
#' @param generator ([`QuantityGenerator`])\cr object that specifies which subjects and time points
#' to calculate the quantities at
#' @param type (`character`)\cr type of quantities to be generated, must be either "survival" or
#' "longitudinal".
#' @param times TODO
#' @export
generateQuantities.JointModelSamples <- function(object, generator, type, ...) {

Expand Down
9 changes: 3 additions & 6 deletions R/LongitudinalQuantities.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ NULL
NULL


# TODO - Update docs
#' `LongitudinalQuantities` Object & Constructor Function
#'
#' Constructor function to generate a `LongitudinalQuantities` object.
Expand All @@ -27,6 +26,7 @@ NULL
#' @slot quantities (`Quantities`)\cr The sampled quantities. Should contain 1 element per
#' element of `group`
#'
#' @slot data (`DataJoint`)\cr Survival and Longitudinal Data.
#'
#' @family LongitudinalQuantities
#' @name LongitudinalQuantities-class
Expand All @@ -41,11 +41,8 @@ NULL

#' @param object ([`JointModelSamples`]) \cr samples as drawn from a Joint Model.
#'
#' @param groups (`character`, `NULL`)\cr which patients to calculate the desired
#' quantities for.
#'
#' @param time_grid (`numeric` or `NULL`)\cr a vector of time points to calculate the desired
#' quantity at. If `NULL` will be set to `seq(0, max_longitudinal_time, length = 201)`.
#' @param grid (`Grid`) \cr object that specifies which subjects and time points to calculate the
#' quantities for. See [Grid-Functions].
#' @rdname LongitudinalQuantities-class
LongitudinalQuantities <- function(
object,
Expand Down
Loading

0 comments on commit b954ffc

Please sign in to comment.