Skip to content
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

feat (WIP): Mean age of childbearing #61

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
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
64 changes: 64 additions & 0 deletions R/mcab.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#' @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".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we should use standard notation nfx in other functions instead of asfr.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error

#' @inheritParams gen_lifetable_parameters
#' @param value_col \[`character(1)`\]\cr Name of output 'MACB' column. Defaults
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would you not just want this to be mcab? We don't have this option in other functions.

#' to "mcab"
#'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add @seealso for where to find more information on MACB

#' @return \[`data.table()`\] A data.table with calculated 'MACB' for each
#' unique grouping in `id_cols`, stored in `value_col`.
#'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @details section with equation?

#' @export
#'
#' @examples
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add example(s)

#' asfr_data <- data.table(
#' asfr = c(.051, .196, .208, .147, .075, .024, .004),
#' age_start = seq(15, 45, 5),
#' age_end = seq(20, 50, 5)
#' )
#'
#' id_cols <- c("age_start", "age_end")
#' macb_dt <- macb_from_nfx(asfr_data, id_cols = id_cols, nfx_col = "asfr")
#'
#'
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)
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add more checks for dt and id_cols.


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

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


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

macb_dt <- dt[
,
.(macb = weighted.mean((age_end + age_start) / 2, get(nfx_col))),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to use list instead of . in the packages. Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the weighted.mean function come from?

by = id_cols_no_age
]

setnames(macb_dt, "macb", value_col)

return(macb_dt)

}
44 changes: 44 additions & 0 deletions man/macb_from_nfx.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-mcab.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("MACB works", {

asfr_data <- data.table(
asfr = c(.051, .196, .208, .147, .075, .024, .004),
age_start = seq(15, 45, 5),
age_end = seq(20, 50, 5)
)

asfr_data2 <- data.table(
nfx = c(.2, .2),
age_start = c(20, 25),
age_end = c(25, 30)
)

id_cols <- c("age_start", "age_end")

macb_dt <- macb_from_nfx(asfr_data, id_cols = id_cols, nfx_col = "asfr")
macb_dt2 <- macb_from_nfx(asfr_data2, id_cols = id_cols)

testthat::expect_equal(macb_dt$macb, 28.11702, tolerance = 1e-5)
testthat::expect_equal(macb_dt2$macb, 25)

})