Skip to content

Phase 2 of user interface: Type conversion functions #210

@kaitejohnson

Description

@kaitejohnson

We want to make the following helper functions:
as_reporting_triangle()
as_data_frame()

Questions remaining:

  • Do we need to create a reporting triangle class? I would think yes in order to save the matrices and associated metadata if we have different strata e.g. age groups, locations, etc.
  • Could also create a dataframe with metadata on strata remaining as columns?
  • Do we want a method to convert linelist data as well, and how do we detect this, given it is also a dataframe?
  • Does this require the user to specify a max delay or can the maximum delay be inferred from the dataframe (e.g. the max difference with a count greater than 0)?

Context

Type Conversion Functions

Add utility functions for data format conversion:

# Convert dataframe to reporting triangle (S3 generic)
as_reporting_triangle <- function(data, ...) UseMethod("as_reporting_triangle")

# S3 method for data.frame
as_reporting_triangle.data.frame <- function(
  data,
  reference_date,
  report_date,
  count,
  strata = NULL,
  max_delay = 30,  # Now here instead of main function
  ...
) {
  # Creates a reporting triangle with attributes:
  # - reference_dates: vector of dates matching triangle rows
  # - strata: strata values if applicable
  # - max_delay: maximum delay value used
}

# S3 method for epinowcast data
as_reporting_triangle.enw_data <- function(
  data,
  max_delay = 30,
  ...
) { ... }

# Convert reporting triangle/nowcast to dataframe with dates
as_data_frame <- function(
  triangle_or_nowcast,
  reference_dates = NULL,  # Vector of dates corresponding to triangle rows
  strata = NULL
) {
  # If reference_dates is NULL, try to get from attributes
  if (is.null(reference_dates)) {
    reference_dates <- attr(triangle_or_nowcast, "reference_dates")
    if (is.null(reference_dates)) {
      stop("Reference dates must be provided or available in attributes")
    }
  }
  
  # Map matrix rows back to dates and return data.frame
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions