Skip to content

Commit

Permalink
finished changed related to new subdir argument
Browse files Browse the repository at this point in the history
  • Loading branch information
hanase committed Nov 1, 2024
1 parent b8e9094 commit bc4827f
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
^README\.md$
^.github$
^data-raw
^tests/last.dump.rda
^tests/.clustersize
^tests/.proc
7 changes: 5 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
5.2-0.9xxx (09/26/2024)
5.2-0.9xxx (10/31/2024)
-----
Added dataset include_2024.

Expand All @@ -10,11 +10,14 @@ Fixed bug in setting time index when imputation is present in e0.predict.subnat(

Added argument use.wpp.data to run.e0.mcmc().

Added the mean variant into sumary output.
Added the mean variant into summary output.

e0.trajectories.plot() got arguments to plot the means (show.mean), the medians (show.median),
as well as selected trajectories (traj.index).

Added option of keeping multiple predictions directories in one simulation directory
(argument "subdir" added to various functions).

5.2-0 (09/15/2023)
-----
Annual subnational projections are now possible, via the argument "annual"
Expand Down
16 changes: 14 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
Package: bayesLife
Type: Package
Title: Bayesian Projection of Life Expectancy
Version: 5.2-0.9011
Date: 2024-09-26
Version: 5.2-0.9012
Date: 2024-10-31
Author: Hana Sevcikova, Adrian Raftery, Jennifer Chunn
Maintainer: Hana Sevcikova <[email protected]>
Authors@R: c(person(given = "Hana",
family = "Sevcikova",
role = c("cre", "aut"),
email = "[email protected]"),
person(given = "Adrian",
family = "Raftery",
role = "aut",
email = "[email protected]"),
person(given = "Jennifer",
family = "Chunn",
role = "aut")
)
Description: Making probabilistic projections of life expectancy for all countries of the world, using a Bayesian hierarchical model <doi:10.1007/s13524-012-0193-x>. Subnational projections are also supported.
Depends:
bayesTFR (>= 7.3-0),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export(
create.thinned.e0.mcmc,
get.e0.prediction,
has.e0.prediction,
available.e0.predictions,
get.e0.jmale.prediction,
has.e0.jmale.prediction,
get.rege0.prediction,
Expand Down
6 changes: 3 additions & 3 deletions R/project_subnat.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
e0.predict.subnat <- function(countries, my.e0.file, sim.dir=file.path(getwd(), 'bayesLife.output'),
method = c("ar1", "shift", "scale"), predict.jmale = FALSE, my.e0M.file = NULL,
end.year=2100, start.year=NULL, output.dir = NULL, annual = NULL,
end.year=2100, start.year=NULL, subdir = "predictions", output.dir = NULL, annual = NULL,
nr.traj=NULL, seed = NULL, ar.pars = NULL,
save.as.ascii = 0, verbose = TRUE, jmale.estimates = NULL, ...) {
# Run subnational projections, using the Scale AR(1) model applied to a national bayesLife simulation
Expand Down Expand Up @@ -30,7 +30,7 @@ e0.predict.subnat <- function(countries, my.e0.file, sim.dir=file.path(getwd(),
return(compute.alpha.ar1(...))

method <- match.arg(method)
wpred <- get.e0.prediction(sim.dir) # contains national projections
wpred <- get.e0.prediction(sim.dir, subdir = subdir) # contains national projections
wdata <- wpred$e0.matrix.reconstructed
wmeta <- wpred$mcmc.set$meta
if(!is.null(seed)) set.seed(seed)
Expand Down Expand Up @@ -153,7 +153,7 @@ e0.predict.subnat <- function(countries, my.e0.file, sim.dir=file.path(getwd(),
PIs_cqp <- array(NA, c(nr.reg, length(quantiles.to.keep), nrow(wtrajs)),
dimnames=list(meta$regions$country_code, dimnames(wpred$quantiles)[[2]], dimnames(wtrajs)[[1]]))
mean_sd <- array(NA, c(nr.reg, 2, nrow(wtrajs)))
meta$Tc.index <- .get.Tcindex(meta$e0.matrix, cnames = meta$regions$country_name)
meta$Tc.index <- .get.Tcindex(meta$e0.matrix, cnames = meta$regions$country_name, stop.if.less.than2 = FALSE) # allow just one data point
country.char <- as.character(country.obj$code)
e0reconstructed <- meta$e0.matrix

Expand Down
58 changes: 30 additions & 28 deletions R/projection_fcns.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ e0.predict <- function(mcmc.set = NULL, end.year = 2100,
replace.output = FALSE, predict.jmale = TRUE,
nr.traj = NULL, thin = NULL, burnin = 10000,
use.diagnostics = FALSE, save.as.ascii = 0, start.year = NULL,
output.dir = NULL, low.memory = TRUE, ignore.last.observed = FALSE,
output.dir = NULL, subdir = "predictions", low.memory = TRUE,
ignore.last.observed = FALSE,
seed = NULL, verbose = TRUE, ...){
if(!is.null(mcmc.set)) {
if (!inherits(mcmc.set, 'bayesLife.mcmc.set')) {
Expand All @@ -41,15 +42,15 @@ e0.predict <- function(mcmc.set = NULL, end.year = 2100,
replace.output = replace.output,
nr.traj = nr.traj, thin = thin, burnin = burnin,
save.as.ascii = save.as.ascii, start.year = start.year,
output.dir = output.dir, ignore.last.observed = ignore.last.observed,
output.dir = output.dir, subdir = subdir, ignore.last.observed = ignore.last.observed,
verbose = verbose)
if(predict.jmale && mcmc.set$meta$sex == 'F')
pred <- e0.jmale.predict(pred, ..., save.as.ascii = save.as.ascii, verbose = verbose)
invisible(pred)
}

e0.predict.extra <- function(sim.dir = file.path(getwd(), 'bayesLife.output'),
prediction.dir = sim.dir,
prediction.dir = sim.dir, subdir = "predictions",
countries = NULL, save.as.ascii = 1000, verbose = TRUE, ...) {
# Run prediction for given countries/regions (as codes). If they are not given it will be set to countries
# for which there are MCMC results but no prediction.
Expand All @@ -58,9 +59,9 @@ e0.predict.extra <- function(sim.dir = file.path(getwd(), 'bayesLife.output'),
mcmc.set <- get.e0.mcmc(sim.dir)
if(is.null(mcmc.set))
stop('Error in "sim.dir" argument.')
pred <- get.e0.prediction(sim.dir=prediction.dir)
pred <- get.e0.prediction(sim.dir=prediction.dir, subdir = subdir)
if(is.null(pred))
stop('Error in "prediction.dir" argument.')
stop('Error in "sim.dir", "prediction.dir" or/and "subdir" argument. Use available.e0.predictions() to check on valid predictions directories.')
if(length(setdiff(pred$mcmc.set$meta$regions$country_code, mcmc.set$meta$regions$country_code)) > 0)
stop('Prediction is inconsistent with the mcmc results. Use e0.predict.')
if(is.null(countries)) {
Expand All @@ -77,7 +78,7 @@ e0.predict.extra <- function(sim.dir = file.path(getwd(), 'bayesLife.output'),
new.pred <- make.e0.prediction(mcmc.set, start.year=pred$start.year, end.year=pred$end.year, replace.output=FALSE,
nr.traj=pred$nr.traj, burnin=pred$burnin,
countries=countries.idx, save.as.ascii=0, output.dir=prediction.dir,
force.creating.thinned.mcmc=TRUE,
subdir = subdir, force.creating.thinned.mcmc=TRUE,
write.summary.files=FALSE, ignore.last.observed = pred$ignore.last.observed,
verbose=verbose)

Expand Down Expand Up @@ -121,7 +122,7 @@ e0.predict.extra <- function(sim.dir = file.path(getwd(), 'bayesLife.output'),

e0.prediction.setup <- function(...) {
setup <- list(...)
mcmc.set <- start.year <- end.year <- burnin <- replace.output <- verbose <- countries <- NULL # to avoid R check note "no visible binding ..."
mcmc.set <- start.year <- end.year <- burnin <- replace.output <- verbose <- countries <- subdir <- NULL # to avoid R check note "no visible binding ..."
if(is.null(setup$thin)) setup$thin <- NA # this is because thin is a method in coda and the naming clashes within the setup expression
setup <- within(setup, {
meta <- mcmc.set$meta
Expand Down Expand Up @@ -151,10 +152,10 @@ e0.prediction.setup <- function(...) {

#setup output directory
if (!exists("output.dir") || is.null(output.dir)) output.dir <- meta$output.dir
outdir <- file.path(output.dir, 'predictions')
outdir <- file.path(output.dir, basename(subdir))

if(is.null(get0("countries"))) {
if(!replace.output && has.e0.prediction(sim.dir = output.dir))
if(!replace.output && has.e0.prediction(sim.dir = output.dir, subdir = subdir))
stop('Prediction in ', outdir,
' already exists.\nSet replace.output=TRUE if you want to overwrite existing projections.')
unlink(outdir, recursive=TRUE)
Expand Down Expand Up @@ -359,11 +360,11 @@ get.friendly.variant.names.bayesLife.prediction <- function(pred, ...)
return(c('median', 'lower 80', 'upper 80', 'lower 95', 'upper 95', 'mean', 'constant'))

convert.e0.trajectories <- function(dir=file.path(getwd(), 'bayesLife.output'),
n=1000, output.dir=NULL,
n=1000, subdir = "predictions", output.dir=NULL,
verbose=FALSE) {
# Converts all trajectory rda files into UN ascii, selecting n trajectories by equal spacing.
if(n <= 0) return()
pred <- get.e0.prediction(sim.dir=dir)
pred <- get.e0.prediction(sim.dir=dir, subdir = subdir)
predsex <- pred$mcmc.set$meta$sex
preds <- list()
preds[[predsex]] <- pred
Expand All @@ -376,15 +377,15 @@ convert.e0.trajectories <- function(dir=file.path(getwd(), 'bayesLife.output'),
else outdir <- output.dir
}
if(!file.exists(outdir)) dir.create(outdir, recursive=TRUE)
cat('Converting ', list(M='Male', F='Female')[[sex]], ' trajectories from', dir, '\n')
cat('Converting ', list(M='Male', F='Female')[[sex]], ' trajectories from', file.path(dir, subdir), '\n')
bayesTFR:::do.convert.trajectories(pred=preds[[sex]], n=n, output.dir=outdir, verbose=verbose)
}
}

write.e0.projection.summary <- function(dir=file.path(getwd(), 'bayesLife.output'),
output.dir=NULL, revision=NULL, adjusted=FALSE) {
subdir = "predictions", output.dir=NULL, revision=NULL, adjusted=FALSE) {
# Writes four prediction summary files, one in a user-friendly format, one in a UN-format, one for each sex.
pred <- get.e0.prediction(sim.dir=dir)
pred <- get.e0.prediction(sim.dir=dir, subdir = subdir)
predsex <- pred$mcmc.set$meta$sex
preds <- list()
preds[[predsex]] <- pred
Expand Down Expand Up @@ -429,40 +430,41 @@ get.e0.reconstructed <- function(data, meta) {
} else store.bayesLife.prediction(new.pred)
}

e0.median.reset <- function(sim.dir, countries = NULL, joint.male=FALSE) {
e0.median.reset <- function(sim.dir, countries = NULL, joint.male=FALSE, ...) {
if(is.null(countries)) {
pred <- get.e0.prediction(sim.dir, joint.male = joint.male)
pred <- get.e0.prediction(sim.dir, joint.male = joint.male, ...)
pred$median.shift <- NULL
.e0.store.adjustment(pred, sim.dir, joint.male)
cat('\nMedians for all countries reset.\n')
} else
for(country in countries) pred <- e0.median.shift(sim.dir, country, reset=TRUE, joint.male=joint.male)
for(country in countries) pred <- e0.median.shift(sim.dir, country, reset=TRUE, joint.male=joint.male, ...)
invisible(pred)
}

get.e0.shift <- function(country.code, pred) return(bayesTFR::get.tfr.shift(country.code, pred))

e0.median.shift <- function(sim.dir, country, reset=FALSE, shift=0, from=NULL, to=NULL, joint.male=FALSE) {
pred <- get.e0.prediction(sim.dir, joint.male=joint.male)
e0.median.shift <- function(sim.dir, country, reset=FALSE, shift=0, from=NULL, to=NULL, joint.male=FALSE, ...) {
pred <- get.e0.prediction(sim.dir, joint.male=joint.male, ...)
new.pred <- bayesTFR:::.bdem.median.shift(pred, type='e0', country=country, reset=reset,
shift=shift, from=from, to=to)
.e0.store.adjustment(new.pred, sim.dir, joint.male)
invisible(new.pred)
}

e0.median.set <- function(sim.dir, country, values, years=NULL, joint.male=FALSE) {
pred <- get.e0.prediction(sim.dir, joint.male=joint.male)
e0.median.set <- function(sim.dir, country, values, years=NULL, joint.male=FALSE, ...) {
pred <- get.e0.prediction(sim.dir, joint.male=joint.male, ...)
new.pred <- bayesTFR:::.bdem.median.set(pred, type='e0', country=country,
values=values, years=years)
.e0.store.adjustment(new.pred, sim.dir, joint.male)
invisible(new.pred)
}

e0.median.adjust.jmale <- function(sim.dir, countries, factors = c(1.2, 1.1)) {
pred <- get.e0.prediction(sim.dir)
if (is.null(pred)) stop('No valid prediction in ', sim.dir)
e0.median.adjust.jmale <- function(sim.dir, countries, factors = c(1.2, 1.1), subdir = "predictions") {
pred <- get.e0.prediction(sim.dir, subdir = subdir)
if (is.null(pred)) stop('Prediction not found in ', file.path(sim.dir, subdir),
'. Check available.e0.predictions() and use the subdir argument to set non-standard prediction subdirectory.')
joint.male <- get.e0.jmale.prediction(pred)
if (is.null(joint.male)) stop('No valid male prediction in ', sim.dir)
if (is.null(joint.male)) stop('No valid male prediction in ', file.path(sim.dir, subdir))
mcmc.set <- pred$mcmc.set
if(is.null(countries)) {
cat('\nNo countries given. Nothing to be done.\n')
Expand All @@ -485,11 +487,11 @@ e0.median.adjust.jmale <- function(sim.dir, countries, factors = c(1.2, 1.1)) {
new.meds[get.country.object(countries[icountry], mcmc.set$meta)$index,], joint.male = TRUE)
}
# reload adjusted prediction
invisible(get.e0.prediction(sim.dir, joint.male = TRUE))
invisible(get.e0.prediction(sim.dir, joint.male = TRUE, subdir = subdir))
}

e0.shift.prediction.to.wpp <- function(sim.dir, joint.male = FALSE, ...){
pred <- get.e0.prediction(sim.dir, joint.male = joint.male)
e0.shift.prediction.to.wpp <- function(sim.dir, joint.male = FALSE, subdir = "predictions", ...){
pred <- get.e0.prediction(sim.dir, joint.male = joint.male, subdir = subdir)
new.pred <- bayesTFR:::.do.shift.prediction.to.wpp(pred,
wpp.dataset = if(joint.male) "e0Mproj" else "e0Fproj", ...)
.e0.store.adjustment(new.pred, sim.dir, joint.male)
Expand Down
3 changes: 2 additions & 1 deletion man/convert.trajectories.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ Converts trajectories of the life expectancy stored in a binary format into two
}
\usage{
convert.e0.trajectories(dir = file.path(getwd(), "bayesLife.output"),
n = 1000, output.dir = NULL, verbose = FALSE)
n = 1000, subdir = "predictions", output.dir = NULL, verbose = FALSE)
}
\arguments{
\item{dir}{Directory containing the prediction object. It should correspond to the \code{output.dir} argument of the \code{\link{e0.predict}} function.}
\item{n}{Number of trajectories to be stored. It can be either a single number or the word \dQuote{all} in which case all available trajectories are converted.}
\item{subdir}{Name of subdirectory of \code{dir} containing the prediction.}
\item{output.dir}{Directory in which the resulting files will be stored. If \code{NULL} the same directory is used as for the prediction. Otherwise, if the directory contains joint predictions for both sexes, the ouptuts are stored into subdirectories \file{F} and \file{M}.}
\item{verbose}{Logical switching log messages on and off.}
}
Expand Down
17 changes: 10 additions & 7 deletions man/e0.median.set.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ Editing the Projection Medians
These functions are to be used by expert analysts. They allow to change the projection medians either to specific values, including the WPP values, or shift the medians by a given constant or a factor.
}
\usage{
e0.median.set(sim.dir, country, values, years = NULL, joint.male = FALSE)
e0.median.set(sim.dir, country, values, years = NULL, joint.male = FALSE, \dots)

e0.median.shift(sim.dir, country, reset = FALSE, shift = 0,
from = NULL, to = NULL, joint.male = FALSE)
from = NULL, to = NULL, joint.male = FALSE, \dots)

e0.median.adjust.jmale(sim.dir, countries, factors = c(1.2, 1.1))
e0.median.adjust.jmale(sim.dir, countries, factors = c(1.2, 1.1),
subdir = "predictions")

e0.median.reset(sim.dir, countries = NULL, joint.male = FALSE)
e0.median.reset(sim.dir, countries = NULL, joint.male = FALSE, \dots)

e0.shift.prediction.to.wpp(sim.dir, joint.male = FALSE, ...)
e0.shift.prediction.to.wpp(sim.dir, joint.male = FALSE,
subdir = "predictions", \dots)
}
\arguments{
\item{sim.dir}{Directory containing the prediction object.}
Expand All @@ -36,12 +38,13 @@ e0.shift.prediction.to.wpp(sim.dir, joint.male = FALSE, ...)
\item{from}{Year from which the offset/reset should start. By default, it starts at the first prediction period.}
\item{to}{Year until which the offset/reset should be done. By default, it is set to the last prediction period.}
\item{factors}{It should be a vector where each element corresponds to one time period. The adjustment of male medians is done as \code{e0m(t) = e0f(t) - gap(t)*factor(t)}.}
\item{\dots}{Additional arguments passed to the underlying adjustment function. It can be \code{verbose} to show/hide the progress of the adjustment and \code{wpp.year} to adjust it to if it differs from the wpp year of the simulation.}
\item{subdir}{Subdirectory of \code{sim.dir} containing the predictions.}
\item{\dots}{Additional arguments passed to the underlying adjustment function. For \code{e0.shift.prediction.to.wpp} it can be \code{stat} with values \dQuote{median} (default) or \dQuote{mean} to specify which statistics should be adjusted; \code{verbose} to show/hide the progress of the adjustment and \code{wpp.year} to adjust it to if it differs from the wpp year of the simulation. For the other functions it can be \code{subdir} to specify the location of the prediction.}
}
\details{
The function \code{e0.median.set} can be used to set the medians of the given country to specific values. Function \code{e0.median.shift} can be used to offset the medians by a specific constant, or to reset the medians to their original BHM values. Function \code{e0.median.adjust.jmale} adjusts male medians using factors that can expand or shrink the female-male gap.

Function\code{e0.shift.prediction.to.wpp} shifts the projected medians so that they correspond to the values found in the \code{e0Fproj} (\code{joint.male = FALSE}) or \code{e0Mproj} (\code{joint.male = TRUE}) datasets of the \pkg{wpp} package that either corresponds to the package used for the simulation itself or is given by the \code{wpp.year} argument. If using \pkg{wpp2022}, the dataset name is automatically adjusted depending if it is an annual or a 5-year simulation.
Function\code{e0.shift.prediction.to.wpp} shifts the projected medians or means (if \code{stat} is \dQuote{mean}) so that they correspond to the values found in the \code{e0Fproj} (\code{joint.male = FALSE}) or \code{e0Mproj} (\code{joint.male = TRUE}) datasets of the \pkg{wpp} package that either corresponds to the package used for the simulation itself or is given by the \code{wpp.year} argument. If using \pkg{wpp2022} or higher, the dataset name is automatically adjusted depending if it is an annual or a 5-year simulation. Note that regardless if it is an adjustment of the median or mean, the corresponding offset is always converted to a shift of the median.

Function \code{e0.median.reset} resets medians of the given countries to the original values. By default it deletes adjustments for all countries.

Expand Down
Loading

0 comments on commit bc4827f

Please sign in to comment.