Skip to content

Commit

Permalink
adding importFrom stuff for stats,fertestr
Browse files Browse the repository at this point in the history
  • Loading branch information
timriffe committed Apr 4, 2021
1 parent dcb3a55 commit 31a8a73
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ importFrom(stats,optim)
importFrom(stats,optimize)
importFrom(stats,predict)
importFrom(stats,quantile)
importFrom(stats,reshape)
importFrom(stats,setNames)
importFrom(stats,splinefun)
importFrom(stats,uniroot)
importFrom(tibble,as.tibble)
Expand Down
3 changes: 3 additions & 0 deletions R/utils-pipe.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
#' @param lhs A value or the magrittr placeholder.
#' @param rhs A function call using the magrittr semantics.
#' @return The result of calling `rhs(lhs)`.
NULL
18 changes: 10 additions & 8 deletions R/utils_downloads.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#'
#' @return numeric matrix of `nLx` with `length(nLxDatesIn)` and abrdiged ages in rows.
#' @export
#' @importFrom stats setNames
#' @importFrom stats reshape
#' @examples
#' # life expectancy calculated from Lx downloaded from WPP19. Using names or codes.
#' Lxs_name <- downloadnLx(nLx=NULL, location = "Argentina",
Expand All @@ -35,8 +37,6 @@
#' }
downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") {

requireNamespace("fertestr", quietly = TRUE)
requireNamespace("magrittr", quietly = TRUE)
verbose <- getOption("basepop_verbose", TRUE)

if (!is.null(nLx)) {
Expand Down Expand Up @@ -81,7 +81,7 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") {
# filter and matrix shape
lt_ctry <- lt_wpp19[lt_wpp19$LocID %in% location_code &
lt_wpp19$Sex %in% sex_code,] %>% as.data.frame() %>%
reshape(data = .,
stats::reshape(data = .,
direction = "wide", idvar = c("LocID","AgeStart","Sex"),
timevar = "Year", v.names = "mx", drop = c("AgeSpan","lx"))

Expand All @@ -92,13 +92,13 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") {
seq(1953,2023,5), as.numeric(nLxDatesIn),
extrap = TRUE, method = method) %>%
as.data.frame() %>%
setNames(as.character(nLxDatesIn))
stats::setNames(as.character(nLxDatesIn))
) %>%
split(., list(lt_ctry$LocID, lt_ctry$Sex)) %>%
lapply(function(X){
Age <- X[["AgeStart"]]
apply(X[,-c(1:3)] %>%
as.data.frame()%>% setNames(as.character(nLxDatesIn)), 2,
as.data.frame()%>% stats::setNames(as.character(nLxDatesIn)), 2,
function(S){
MortalityLaws::LifeTable(x = Age,
mx = S,
Expand All @@ -125,6 +125,8 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") {
#'
#' @return numeric matrix interpolated asfr
#' @export
#' @importFrom fertestr get_location_code
#' @importFrom stats setNames
#' @examples
#' # Total fertility ratio calculated from ASFRx downloaded from WPP19.
#' # See `downloadnLx` for analogous examples on multiple countries or using codes instead of names.
Expand All @@ -133,7 +135,7 @@ downloadnLx <- function(nLx, location, gender, nLxDatesIn, method="linear") {
#' plot(1950:2025, as.numeric(colSums(ASFR_Arg))*5, xlab = "Year", ylab="TFR", ylim=c(1.5,4), t="l")
#' }
downloadAsfr <- function(Asfrmat, location = NULL, AsfrDatesIn, method="linear") {
requireNamespace("fertestr", quietly = TRUE)
#requireNamespace("fertestr", quietly = TRUE)
verbose <- getOption("basepop_verbose", TRUE)

if (!is.null(Asfrmat)) {
Expand Down Expand Up @@ -163,15 +165,15 @@ downloadAsfr <- function(Asfrmat, location = NULL, AsfrDatesIn, method="linear")
# spread format
asfr_ctry <- asfr_wpp19[asfr_wpp19$LocID %in% location_code,] %>%
as.data.frame() %>%
reshape(direction = "wide", idvar = c("LocID","AgeStart"),
stats::reshape(direction = "wide", idvar = c("LocID","AgeStart"),
timevar = "Year", v.names = "ASFR")

# interp/extrap
out <- interp(asfr_ctry[,-c(1:3)], seq(1953,2023,5),
as.numeric(AsfrDatesIn),
extrap = TRUE, method = method) %>%
as.data.frame() %>%
setNames(as.character(AsfrDatesIn)) %>%
stats::setNames(as.character(AsfrDatesIn)) %>%
as.matrix()

# combination as rowname
Expand Down
8 changes: 8 additions & 0 deletions man/pipe.Rd

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

0 comments on commit 31a8a73

Please sign in to comment.