diff --git a/DESCRIPTION b/DESCRIPTION index a6424d87f..0cb6491bd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: DemoTools Type: Package Title: Standardize, Evaluate, and Adjust Demographic Data -Version: 01.10.05 +Version: 01.11.000 Date: 2021-01-03 Authors@R: c( person("Tim", "Riffe", role = c("aut", "cre"), @@ -31,8 +31,7 @@ Suggests: knitr, rmarkdown, DT, - ggplot2, - bibtex (>= 0.4.2) + ggplot2 RdMacros: Rdpack Imports: demogR, diff --git a/NAMESPACE b/NAMESPACE index 65b810e14..5975afed5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(ADM) export(AGEN) export(ID) export(IRD) +export(OPAG) export(OPAG_fit_stable_standard) export(OPAG_nLx_warp_r) export(OPAG_r_min) @@ -35,6 +36,7 @@ export(check_heaping_spoorenberg) export(check_heaping_whipple) export(dec.date) export(downloadSRB) +export(downloadnLx) export(getModelLifeTable) export(graduate) export(graduate_beers) @@ -132,6 +134,7 @@ import(Rcpp) import(Rdpack) import(demogR) importFrom(MortalityLaws,MortalityLaw) +importFrom(Rdpack,reprompt) importFrom(demogR,cdmltw) importFrom(dplyr,group_by) importFrom(dplyr,mutate) diff --git a/R/DemoTools-package.R b/R/DemoTools-package.R index 0fe9ce5c9..cc1995855 100644 --- a/R/DemoTools-package.R +++ b/R/DemoTools-package.R @@ -6,6 +6,7 @@ #' @import Rcpp #' @importFrom MortalityLaws MortalityLaw #' @importFrom stats coef predict +#' @importFrom Rdpack reprompt #' @name DemoTools-package #' @docType package "_PACKAGE" diff --git a/R/OPAG.R b/R/OPAG.R index 0f17ab8ab..1d1afb151 100644 --- a/R/OPAG.R +++ b/R/OPAG.R @@ -118,7 +118,7 @@ OPAG_simple <- #' @param Age interger vector of lower bounds of age groups of `nLx` #' @param r stable growth rate #' @param AgeInt optional integer vector of widths of age groups, inferred if not given. -#' @param continous logical. If `TRUE` we use the growth adjustment. `e^(-age*r)`. If `FALSE` we assume `r` is geometric growth, and we use `(1+r)^age` for the growth adjustment. +#' @param continuous logical. If `TRUE` we use the growth adjustment. `e^(-age*r)`. If `FALSE` we assume `r` is geometric growth, and we use `(1+r)^age` for the growth adjustment. #' @param method character, graduation method used for intermediate graduation. Default `"uniform"`. Other reasonable choices include `"mono"` or `"pclm"`. #' @return numeric vector of the transformed `nLx`. Note, this vector sums to `1`. #' @export @@ -173,13 +173,13 @@ OPAG_nLx_warp_r <- function(nLx, #' @param nLx numeric vector of stable population standard #' @param Age_nLx integer vector of lower bounds for age groups of `nLx` #' @param AgeInt_nLx optional integer vector of widths of age groups of `nLx`, inferred if not given. -#' @param continous logical. If `TRUE` we use the growth adjustment. `e^(-age*r)`. If `FALSE` we assume `r` is geometric growth, and we use `(1+r)^age` for the growth adjustment. +#' @param continuous logical. If `TRUE` we use the growth adjustment. `e^(-age*r)`. If `FALSE` we assume `r` is geometric growth, and we use `(1+r)^age` for the growth adjustment. #' @param method character. Graduation method, default `"uniform"`. `"mono"` or `"pclm"` would also be good choices. #' @return numeric. A residual that you're presumably trying to minimize. #' @export #' @examples -#' Make up some population data to fit to: +#' # Make up some population data to fit to: #' Pop_fit <- c(85000,37000) #' Age_fit <- c(70,80) #' AgeInt_fit <- c(10,10) @@ -386,8 +386,11 @@ OPAG_fit_stable_standard <- function(Pop_fit, #' @param Pop numeric vector of population counts #' @param Age_Pop integer vector of the lower bounds of the population age groups #' @param AgeInt_Pop integer vector of the population age group interval widths, using `Inf` for the open age group. - -#'India Males, 1991 +#' @param Redistribute_from integer lower age bound that forms the cutoff, above which we redistribute counts using the stable standard. +#' @param OAnew integer. Desired open age group in the output (must being element of `Age_nLx`) +#' @export +#' @examples +#' # India Males, 1991 #' Pop <- smooth_age_5(pop1m_ind, #' Age = 0:100, #' method = "Arriaga") diff --git a/R/basepop.R b/R/basepop.R index 6b6a006a1..664cf2c2e 100644 --- a/R/basepop.R +++ b/R/basepop.R @@ -850,8 +850,17 @@ lt_infer_radix_from_1L0 <- function(L0){ the_radix } -# TR: radix removed, as it seems lx was 1 but nLx was based on 1e5... -# will use indirect inference. +#' Extract Lx estimates from WPP2019 +#' @description We use the `FetchLifeTableWpp2019` function of the `fertestr` to extract `Lx` from `wpp2019`, interpolated to an exact date. +#' @param nLx either `NULL` or a numeric vector of lifetable exposure. If it's the second then we just pass it back. +#' @param country character country name available UN Pop Div `LocName` set +#' @param gender `"male"`, `"female"`, or `"both"` +#' @param nLxDatesIn numeric vector of three decimal dates produced by (or passed through) `basepop_ive()` +#' +#' @return numeric matrix of `nLx` with `length(nLxDatesIn)` and abrdiged ages in rows. +#' @export +#' +#' @importFrom rlang .data downloadnLx <- function(nLx, country, gender, nLxDatesIn) { requireNamespace("fertestr", quietly = TRUE) requireNamespace("magrittr", quietly = TRUE) diff --git a/README.md b/README.md index 762fcd41f..808bb0681 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![R CMD Check via {tic}](https://github.com/timriffe/DemoTools/workflows/R%20CMD%20Check%20via%20%7Btic%7D/badge.svg) [![codecov](https://codecov.io/gh/timriffe/DemoTools/branch/master/graph/badge.svg)](https://codecov.io/gh/timriffe/DemoTools) -[![](https://img.shields.io/badge/devel%20version-01.10.05-yellow.svg)](https://github.com/timriffe/DemoTools) +[![](https://img.shields.io/badge/devel%20version-01.11.000-yellow.svg)](https://github.com/timriffe/DemoTools) [![issues](https://img.shields.io/github/issues-raw/timriffe/DemoTools.svg)](https://github.com/timriffe/DemoTools/issues) [![lifecycle](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental) diff --git a/man/OPAG.Rd b/man/OPAG.Rd index c95c6aa5b..8066e73ac 100644 --- a/man/OPAG.Rd +++ b/man/OPAG.Rd @@ -24,38 +24,7 @@ OPAG( \item{Age_Pop}{integer vector of the lower bounds of the population age groups} -\item{AgeInt_Pop}{integer vector of the population age group interval widths, using \code{Inf} for the open age group. -India Males, 1991 -Pop <- smooth_age_5(pop1m_ind, -Age = 0:100, -method = "Arriaga") -Age_Pop <- names2age(Pop) -AgeInt_Pop <- age2int(Age_Pop, OAvalue = 1) - -nLx <- downloadnLx(NULL, "India","male",1991) -Age_nLx <- names2age(nLx) -AgeInt_nLx <- age2int(Age_nLx, OAvalue = 1) - -Pop_fit <- OPAG(Pop, -Age_Pop = Age_Pop, -AgeInt_Pop = AgeInt_Pop, -nLx = nLx, -Age_nLx = Age_nLx, -AgeInt_nLx, -Age_fit = c(60,70), -AgeInt_fit = c(10,10), -Redistribute_from = 80) - -\dontrun{ -# look at 75+ -ind <- Age_Pop >= 75 -plot(Age_Pop[ind], Pop[ind]) -lines(Age_Pop[ind], Pop_fit$Pop_out[ind], col = "blue") - -# relative differences in ages 80+ -ind <- Age_Pop >= 80 -plot(Age_Pop[ind], (Pop_fit$Pop_out[ind] - Pop[ind]) / Pop[ind]) -}} +\item{AgeInt_Pop}{integer vector of the population age group interval widths, using \code{Inf} for the open age group.} \item{nLx}{numeric vector of stable population standard} @@ -67,7 +36,13 @@ plot(Age_Pop[ind], (Pop_fit$Pop_out[ind] - Pop[ind]) / Pop[ind]) \item{AgeInt_fit}{integer vector of widths of age groups of \code{Pop_fit}} +\item{Redistribute_from}{integer lower age bound that forms the cutoff, above which we redistribute counts using the stable standard.} + +\item{OAnew}{integer. Desired open age group in the output (must being element of \code{Age_nLx})} + \item{method}{character. Graduation method, default \code{"uniform"}. \code{"mono"} or \code{"pclm"} would also be good choices.} + +\item{continuous}{logical. If \code{TRUE} we use the growth adjustment. \code{e^(-age*r)}. If \code{FALSE} we assume \code{r} is geometric growth, and we use \code{(1+r)^age} for the growth adjustment.} } \description{ This can be used as an external check of population counts @@ -96,3 +71,36 @@ it ahead of time. For this, you'd want the \code{nMx} the underly it, and you can use \code{lt_abridged()}, specifying a higher open age, and then extracting \code{nLx} again from it. } +\examples{ +# India Males, 1991 +Pop <- smooth_age_5(pop1m_ind, + Age = 0:100, + method = "Arriaga") +Age_Pop <- names2age(Pop) +AgeInt_Pop <- age2int(Age_Pop, OAvalue = 1) + +nLx <- downloadnLx(NULL, "India","male",1991) +Age_nLx <- names2age(nLx) +AgeInt_nLx <- age2int(Age_nLx, OAvalue = 1) + +Pop_fit <- OPAG(Pop, + Age_Pop = Age_Pop, + AgeInt_Pop = AgeInt_Pop, + nLx = nLx, + Age_nLx = Age_nLx, + AgeInt_nLx, + Age_fit = c(60,70), + AgeInt_fit = c(10,10), + Redistribute_from = 80) + +\dontrun{ +# look at 75+ +ind <- Age_Pop >= 75 +plot(Age_Pop[ind], Pop[ind]) +lines(Age_Pop[ind], Pop_fit$Pop_out[ind], col = "blue") + +# relative differences in ages 80+ +ind <- Age_Pop >= 80 +plot(Age_Pop[ind], (Pop_fit$Pop_out[ind] - Pop[ind]) / Pop[ind]) +} +} diff --git a/man/OPAG_fit_stable_standard.Rd b/man/OPAG_fit_stable_standard.Rd index 0850762a5..187847e82 100644 --- a/man/OPAG_fit_stable_standard.Rd +++ b/man/OPAG_fit_stable_standard.Rd @@ -29,6 +29,8 @@ OPAG_fit_stable_standard( \item{AgeInt_nLx}{optional integer vector of widths of age groups of \code{nLx}, inferred if not given.} \item{method}{character. Graduation method, default \code{"uniform"}. \code{"mono"} or \code{"pclm"} would also be good choices.} + +\item{continuous}{logical. If \code{TRUE} we use the growth adjustment. \code{e^(-age*r)}. If \code{FALSE} we assume \code{r} is geometric growth, and we use \code{(1+r)^age} for the growth adjustment.} } \value{ list constaining diff --git a/man/OPAG_nLx_warp_r.Rd b/man/OPAG_nLx_warp_r.Rd index 064b9349d..f12537911 100644 --- a/man/OPAG_nLx_warp_r.Rd +++ b/man/OPAG_nLx_warp_r.Rd @@ -22,9 +22,9 @@ OPAG_nLx_warp_r( \item{AgeInt}{optional integer vector of widths of age groups, inferred if not given.} -\item{method}{character, graduation method used for intermediate graduation. Default \code{"uniform"}. Other reasonable choices include \code{"mono"} or \code{"pclm"}.} +\item{continuous}{logical. If \code{TRUE} we use the growth adjustment. \code{e^(-age*r)}. If \code{FALSE} we assume \code{r} is geometric growth, and we use \code{(1+r)^age} for the growth adjustment.} -\item{continous}{logical. If \code{TRUE} we use the growth adjustment. \code{e^(-age*r)}. If \code{FALSE} we assume \code{r} is geometric growth, and we use \code{(1+r)^age} for the growth adjustment.} +\item{method}{character, graduation method used for intermediate graduation. Default \code{"uniform"}. Other reasonable choices include \code{"mono"} or \code{"pclm"}.} } \value{ numeric vector of the transformed \code{nLx}. Note, this vector sums to \code{1}. diff --git a/man/OPAG_r_min.Rd b/man/OPAG_r_min.Rd index 0bf35e74a..615f48306 100644 --- a/man/OPAG_r_min.Rd +++ b/man/OPAG_r_min.Rd @@ -31,9 +31,9 @@ OPAG_r_min( \item{AgeInt_nLx}{optional integer vector of widths of age groups of \code{nLx}, inferred if not given.} -\item{method}{character. Graduation method, default \code{"uniform"}. \code{"mono"} or \code{"pclm"} would also be good choices.} +\item{continuous}{logical. If \code{TRUE} we use the growth adjustment. \code{e^(-age*r)}. If \code{FALSE} we assume \code{r} is geometric growth, and we use \code{(1+r)^age} for the growth adjustment.} -\item{continous}{logical. If \code{TRUE} we use the growth adjustment. \code{e^(-age*r)}. If \code{FALSE} we assume \code{r} is geometric growth, and we use \code{(1+r)^age} for the growth adjustment.} +\item{method}{character. Graduation method, default \code{"uniform"}. \code{"mono"} or \code{"pclm"} would also be good choices.} } \value{ numeric. A residual that you're presumably trying to minimize. @@ -47,7 +47,7 @@ This is a utiltiy function for \code{OPAG()}, which needs to optimize $r$ for a given population vector and stationary standard. } \examples{ -Make up some population data to fit to: +# Make up some population data to fit to: Pop_fit <- c(85000,37000) Age_fit <- c(70,80) AgeInt_fit <- c(10,10) diff --git a/man/downloadnLx.Rd b/man/downloadnLx.Rd new file mode 100644 index 000000000..d1f455696 --- /dev/null +++ b/man/downloadnLx.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/basepop.R +\name{downloadnLx} +\alias{downloadnLx} +\title{Extract Lx estimates from WPP2019} +\usage{ +downloadnLx(nLx, country, gender, nLxDatesIn) +} +\arguments{ +\item{nLx}{either \code{NULL} or a numeric vector of lifetable exposure. If it's the second then we just pass it back.} + +\item{country}{character country name available UN Pop Div \code{LocName} set} + +\item{gender}{\code{"male"}, \code{"female"}, or \code{"both"}} + +\item{nLxDatesIn}{numeric vector of three decimal dates produced by (or passed through) \code{basepop_ive()}} +} +\value{ +numeric matrix of \code{nLx} with \code{length(nLxDatesIn)} and abrdiged ages in rows. +} +\description{ +We use the \code{FetchLifeTableWpp2019} function of the \code{fertestr} to extract \code{Lx} from \code{wpp2019}, interpolated to an exact date. +}