Skip to content

Commit

Permalink
feat: Inital setup of MACB function
Browse files Browse the repository at this point in the history
This defines the API and core calculation for calculating MACB
from an input `data.table`.

References #60, #19
  • Loading branch information
spencerpease committed Sep 1, 2020
1 parent 13c7979 commit bf76151
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(gen_u5_ax)
export(iterate_ax)
export(leslie_matrix)
export(lifetable)
export(macb_from_nfx)
export(mx_ax_to_qx)
export(mx_qx_to_ax)
export(mx_to_ax)
Expand Down
52 changes: 52 additions & 0 deletions R/mcab.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' @title Calculate Mean Age of Childbearing from Fertility Rate (MACB)
#'
#' @description
#' The mean age at childbearing is the mean age of mothers at the birth of their
#' children if women were subject throughout their lives to the age-specific
#' fertility rates observed in a given year.
#'
#' See [UNPOP page][1] for more deatils.
#'
#' [1]: https://www.un.org/en/development/desa/population/publications/dataset/fertility/age-childbearing.asp
#'
#' @param dt \[`data.table()`\]\cr A data.table with columns 'age_start',
#' 'age_end', and a column of fertility rates named after `nfx_col`.
#' @param nfx_col \[`character(1)`\]\cr Name of fertility rate column in `dt`.
#' Defualts to "nfx".
#' @inheritParams gen_lifetable_parameters
#' @param value_col \[`character(1)`\]\cr Name of output 'MACB' column. Defaults
#' to "mcab"
#'
#' @return \[`data.table()`\] A data.table with calculated 'MACB' for each
#' unique grouping in `id_cols`, stored in `value_col`.
#'
#' @export
#'
#' @examples
macb_from_nfx <- function(dt, id_cols, nfx_col = "nfx", value_col = "macb") {


# Validate parameters -----------------------------------------------------

assertthat::assert_that(
assertthat::is.string(nfx_col),
assertthat::is.string(value_col)
)


# Prep id columns ---------------------------------------------------------

id_cols_no_age <- id_cols[!id_cols %in% c("age_start", "age_end")]


# Calculate MACB ----------------------------------------------------------

macb_dt <- dt[
,
get(value_col) = weighted.mean((age_end - age_start) / 2, get(nfx_col)),
by = id_cols_no_age
]

return(macb_dt)

}

0 comments on commit bf76151

Please sign in to comment.