Skip to content

Rename things #29

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

Merged
merged 6 commits into from
Jan 10, 2022
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Package: epitools
Package: epiprocess
Type: Package
Title: Tools for basic signal processing in epidemiology
Version: 1.0.0
Expand Down
20 changes: 10 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by roxygen2: do not edit by hand

S3method(as.epi_tibble,data.frame)
S3method(as.epi_tibble,epi_tibble)
S3method(as.epi_tibble,tibble)
S3method(group_by,epi_tibble)
S3method(head,epi_tibble)
S3method(print,epi_tibble)
S3method(summary,epi_tibble)
S3method(ungroup,epi_tibble)
S3method(as.epi_df,data.frame)
S3method(as.epi_df,epi_df)
S3method(as.epi_df,tibble)
S3method(group_by,epi_df)
S3method(head,epi_df)
S3method(print,epi_df)
S3method(summary,epi_df)
S3method(ungroup,epi_df)
export("%>%")
export(End)
export(Max)
Expand All @@ -16,11 +16,11 @@ export(Median)
export(Min)
export(Start)
export(Sum)
export(as.epi_tibble)
export(cor_lagged)
export(as.epi_df)
export(detect_outliers)
export(detect_outliers_rm)
export(detect_outliers_stl)
export(epi_cor)
export(epi_slide)
export(estimate_deriv)
export(pct_change)
Expand Down
20 changes: 10 additions & 10 deletions R/correlation.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' Compute lagged correlations between variables in an `epi_tibble` object
#' Compute lagged correlations between variables in an `epi_df` object
#'
#' Computes lagged correlations between variables in an `epi_tibble` object,
#' Computes lagged correlations between variables in an `epi_df` object,
#' allowing for grouping by geo value, time value, or any other variables. See
#' the [correlations
#' vignette](https://cmu-delphi.github.io/epitools/articles/correlations.html)
#' the [correlation
#' vignette](https://cmu-delphi.github.io/epitools/articles/correlation.html)
#' for examples.
#'
#' @param x The `epi_tibble` object under consideration.
#' @param x The `epi_df` object under consideration.
#' @param var1,var2 The variables in `x` to correlate.
#' @param dt1,dt2 Time shifts to consider for the two variables, respectively,
#' before computing correlations. Negative shifts translate into in a lag
Expand Down Expand Up @@ -40,11 +40,11 @@
#' @importFrom stats cor
#' @importFrom rlang .data enquo
#' @export
cor_lagged = function(x, var1, var2, dt1 = 0, dt2 = 0,
by = geo_value, use = "na.or.complete",
method = c("pearson", "kendall", "spearman")) {
# Check we have an `epi_tibble` object
if (!inherits(x, "epi_tibble")) abort("`x` must be of class `epi_tibble`.")
epi_cor = function(x, var1, var2, dt1 = 0, dt2 = 0, by = geo_value,
use = "na.or.complete",
method = c("pearson", "kendall", "spearman")) {
# Check we have an `epi_df` object
if (!inherits(x, "epi_df")) abort("`x` must be of class `epi_df`.")

# Check that we have variables to do computations on
if (missing(var1)) abort("`var1` must be specified.")
Expand Down
18 changes: 9 additions & 9 deletions R/derivative.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' Estimate derivatives of a variable in an `epi_tibble` object
#' Estimate derivatives of a variable in an `epi_df` object
#'
#' Estimates derivatives of a variable in an `epi_tibble` object, using a local
#' (in time) linear regression or alternative smoother. See the [estimating
#' derivatives
#' vignette](https://cmu-delphi.github.io/epitools/articles/derivatives.html)
#' for examples.
#' Estimates derivatives of a variable in an `epi_df` object, using a local (in
#' time) linear regression or alternative smoothing method. See the [estimating
#' derivatives
#' vignette](https://cmu-delphi.github.io/epitools/articles/derivative.html) for
#' examples.
#'
#' @param x The `epi_tibble` object under consideration.
#' @param x The `epi_df` object under consideration.
#' @param var The variable in `x` whose derivatives are to be estimated.
#' @param method One of "lin", "ss", or "tf" indicating the method to use for
#' the derivative calculation. To estimate the derivative at any time point,
Expand Down Expand Up @@ -49,7 +49,7 @@
#' is `TRUE`.
#' @param ... Additional arguments to pass to the function that estimates
#' derivatives. See details below.
#' @return An `epi_tibble` object given by appending a new column to `x`, named
#' @return An `epi_df` object given by appending a new column to `x`, named
#' according to the `new_col_name` argument, containing the derivative values.
#'
#' @details Derivatives are estimated using:
Expand Down Expand Up @@ -141,7 +141,7 @@ estimate_deriv = function(x, var, method = c("lin", "ss", "tf"), n = 14,
x = select(x, -temp) %>% ungroup()

# Attach the class and metadata and return
class(x) = c("epi_tibble", class(x))
class(x) = c("epi_df", class(x))
attributes(x)$metadata = metadata
return(x)
}
Expand Down
111 changes: 55 additions & 56 deletions R/epi_tibble.R → R/epi_df.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#' Convert data to `epi_tibble` format
#' Convert data to `epi_df` format
#'
#' Converts a data frame or tibble into a format that is consistent with the
#' `epi_tibble` class, ensuring that it has a certain minimal set of columns,
#' and that it has certain minimal metadata.
#' `epi_df` class, ensuring that it has a certain minimal set of columns, and
#' that it has certain minimal metadata.
#'
#' @param x The object to be converted. See the methods section below for
#' details on formatting of each input type.
Expand All @@ -16,40 +16,39 @@
#' attempt to infer it from the passed object `x`; if this fails, then the
#' current day-time will be used.
#' @param additional_metadata List of additional metadata to attach to the
#' `epi_tibble` object. All objects will have `time_type`, `geo_type`, and
#' `issue` fields; named entries from the passed list or will be included as
#' well.
#' `epi_df` object. All objects will have `time_type`, `geo_type`, and `issue`
#' fields; named entries from the passed list or will be included as well.
#' @param ... Additional arguments passed to methods.
#' @return An `epi_tibble` object.
#' @return An `epi_df` object.
#'
#' @details An `epi_tibble` is a tibble with (at least) the following columns:
#' @details An `epi_df` is a tibble with (at least) the following columns:
#'
#' * `geo_value`: the geographic value associated with each measurement.
#' * `time_value`: the time value associated with each measurement.
#'
#' Other columns can be considered as measured variables, which we also broadly
#' refer to as signal variables. An `epi_tibble` object also has metadata with
#' (at least) the following fields:
#' refer to as signal variables. An `epi_df` object also has metadata with (at
#' least) the following fields:
#'
#' * `geo_type`: the type for the geo values.
#' * `time_type`: the type for the time values.
#' * `issue`: the time value at which the given data set was issued.
#'
#' The first two fields above, `geo_type` and `time_type`, can usually be
#' inferred from the `geo_value` and `time_value` columns, respectively. The
#' last field above, `issue`, is the most unique to the `epi_tibble` format.
#' In a typical case, this represents the maximum of the issues of individual
#' last field above, `issue`, is the most unique to the `epi_df` format. In a
#' typical case, this represents the maximum of the issues of individual
#' signal values measured in the data set; hence we would also say that the
#' data set is comprised of all signal values observed "as of" the given issue
#' in the metadata.
#'
#' Metadata for an `epi_tibble` object `x` can be accessed (and altered) via
#' Metadata for an `epi_df` object `x` can be accessed (and altered) via
#' `attributes(x)$metadata`. More information on geo types, time types, and
#' issues is given below.
#'
#' @section Geo types:
#' The following geo types are supported in an `epi_tibble`. Their geo coding
#' (specification of geo values for each geo type) is also described below.
#' The following geo types are supported in an `epi_df`. Their geo coding
#' (specification of geo values for each geo type) is also described below.
#'
#' * `"county"`: each observation corresponds to a U.S. county; coded by 5-digit
#' FIPS code.
Expand All @@ -69,7 +68,7 @@
#' geo type is labeled as "custom".
#'
#' @section Time types:
#' The following time types are supported in an `epi_tibble`. Their time coding
#' The following time types are supported in an `epi_df`. Their time coding
#' (specification of time values for each time type) is also described below.
#'
#' * `"day-time"`: each observation corresponds to a time on a given day (measured
Expand All @@ -88,28 +87,28 @@
#' todo
#'
#' @export
as.epi_tibble = function(x, ...) {
UseMethod("as.epi_tibble")
as.epi_df = function(x, ...) {
UseMethod("as.epi_df")
}

#' @method as.epi_tibble epi_tibble
#' @describeIn as.epi_tibble Simply returns the `epi_tibble` object unchanged.
#' @method as.epi_df epi_df
#' @describeIn as.epi_df Simply returns the `epi_df` object unchanged.
#' @export
as.epi_tibble.epi_tibble = function(x, ...) {
as.epi_df.epi_df = function(x, ...) {
return(x)
}

#' @method as.epi_tibble tibble
#' @describeIn as.epi_tibble The input tibble `x` must contain the columns
#' @method as.epi_df tibble
#' @describeIn as.epi_df The input tibble `x` must contain the columns
#' `geo_value` and `time_value`. All other columns will be preserved as is,
#' and treated as measured variables. If `issue` is missing, then the function
#' will look for `issue` as a column of `x`, or as a field in its metadata
#' (stored in its attributes), to infer the issue; if this fails, then the
#' current day-time will be used.
#' @importFrom rlang .data abort
#' @export
as.epi_tibble.tibble = function(x, geo_type, time_type, issue,
additional_metadata = list(), ...) {
as.epi_df.tibble = function(x, geo_type, time_type, issue,
additional_metadata = list(), ...) {
# Check that we have geo_value and time_value columns
if (!("geo_value" %in% names(x))) {
abort("`x` must contain a `geo_value` column.")
Expand Down Expand Up @@ -198,69 +197,69 @@ as.epi_tibble.tibble = function(x, geo_type, time_type, issue,
metadata$issue = issue
metadata = c(metadata, additional_metadata)

# Convert to a tibble, apply epi_tibble class, attach metadata
# Convert to a tibble, apply epi_df class, attach metadata
if (!inherits(x, "tibble")) x = tibble::as_tibble(x)
class(x) = c("epi_tibble", class(x))
class(x) = c("epi_df", class(x))
attributes(x)$metadata = metadata

# Reorder columns (geo_value, time_value, ...) and return
x = dplyr::relocate(x, .data$geo_value, .data$time_value)
return(x)
}

#' @method as.epi_tibble data.frame
#' @describeIn as.epi_tibble The input data frame `x` must contain the columns
#' @method as.epi_df data.frame
#' @describeIn as.epi_df The input data frame `x` must contain the columns
#' `geo_value` and `time_value`. All other columns will be preserved as is,
#' and treated as measured variables. If `issue` is missing, then the function
#' will look for `issue` as a column of `x`, or as a field in its metadata
#' (stored in its attributes), to infer the issue; if this fails, then the
#' current day-time will be used.
#' current day-time will be used.
#' @export
as.epi_tibble.data.frame = as.epi_tibble.tibble
as.epi_df.data.frame = as.epi_df.tibble

#' Print `epi_tibble` object
#' Print `epi_df` object
#'
#' Prints a brief summary of the `epi_tibble` object, then prints the underlying
#' tibble.
#' Prints a brief summary of the `epi_df` object, then prints the underlying
#' tibble.
#'
#' @param x The `epi_tibble` object.
#' @param x The `epi_df` object.
#' @param ... Additional arguments passed to `print.tibble()` to print the
#' data.
#' @return The `epi_tibble` object, unchanged.
#' @return The `epi_df` object, unchanged.
#'
#' @method print epi_tibble
#' @method print epi_df
#' @export
print.epi_tibble = function(x, ...) {
cat("An `epi_tibble` object, with metadata:\n")
print.epi_df = function(x, ...) {
cat("An `epi_df` object, with metadata:\n")
cat(sprintf("* %-10s= %s\n", "geo_type", attributes(x)$metadata$geo_type))
cat(sprintf("* %-10s= %s\n", "time_type", attributes(x)$metadata$time_type))
cat(sprintf("* %-10s= %s\n", "issue", attributes(x)$metadata$issue))
cat("\n")
NextMethod()
}

#' @method head epi_tibble
#' @method head epi_df
#' @importFrom utils head
#' @export
head.epi_tibble = function(x, ...) {
head.epi_df = function(x, ...) {
head(tibble::as_tibble(x), ...)
}

#' Summarize `epi_tibble` object
#' Summarize `epi_df` object
#'
#' Prints a variety of summary statistics about the `epi_tibble` object, such as
#' Prints a variety of summary statistics about the `epi_df` object, such as
#' the time range included and geographic coverage.
#'
#' @param object The `epi_tibble` object.
#' @param object The `epi_df` object.
#' @param ... Additional arguments, for compatibility with `summary()`.
#' Currently unused.
#' @return No return value; called only to print summary statistics.
#'
#' @method summary epi_tibble
#' @method summary epi_df
#' @importFrom stats median
#' @export
summary.epi_tibble = function(object, ...) {
cat("An `epi_tibble` object, with metadata:\n")
summary.epi_df = function(object, ...) {
cat("An `epi_df` object, with metadata:\n")
cat(sprintf("* %-10s= %s\n", "geo_type", attributes(x)$metadata$geo_type))
cat(sprintf("* %-10s= %s\n", "time_type", attributes(x)$metadata$time_type))
cat(sprintf("* %-10s= %s\n", "issue", attributes(x)$metadata$issue))
Expand All @@ -273,29 +272,29 @@ summary.epi_tibble = function(object, ...) {
dplyr::summarize(median(.data$num)))))
}

#' Group or ungroup `epi_tibble` object
#' Group or ungroup `epi_df` object
#'
#' Groups or ungroups an `epi_tibble`, preserving class and attributes.
#' Groups or ungroups an `epi_df`, preserving class and attributes.
#'
#' @method group_by epi_tibble
#' @method group_by epi_df
#' @importFrom dplyr group_by
#' @export
group_by.epi_tibble = function(x, ...) {
group_by.epi_df = function(x, ...) {
metadata = attributes(x)$metadata
x = NextMethod()
class(x) = c("epi_tibble", class(x))
class(x) = c("epi_df", class(x))
attributes(x)$metadata = metadata
return(x)
}

#' @method ungroup epi_tibble
#' @rdname group_by.epi_tibble
#' @method ungroup epi_df
#' @rdname group_by.epi_df
#' @importFrom dplyr ungroup
#' @export
ungroup.epi_tibble = function(x, ...) {
ungroup.epi_df = function(x, ...) {
metadata = attributes(x)$metadata
x = NextMethod()
class(x) = c("epi_tibble", class(x))
class(x) = c("epi_df", class(x))
attributes(x)$metadata = metadata
return(x)
}
4 changes: 2 additions & 2 deletions R/epitools.R → R/epiprocess.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' epitools: Tools for basic signal processing in epidemiology
#' epiprocess: Tools for basic signal processing in epidemiology
#'
#' This package introduces a common data structure for epidemiological data sets
#' measured over space and time, and offers associated utilities to perform
#' basic signal processing tasks.
#'
#' @docType package
#' @name epitools
#' @name epiprocess
NULL
Loading