From 3215f4cba69b997fbfcd6dbe10d082aabe80c456 Mon Sep 17 00:00:00 2001 From: Hana Sevcikova Date: Wed, 5 Jun 2024 21:04:02 -0700 Subject: [PATCH] arguments show.mean and traj.index in e0.tajectories.plot --- ChangeLog | 3 +++ DESCRIPTION | 2 +- R/plot_functions.R | 18 ++++++++++++++++++ man/e0.trajectories.plot.Rd | 3 +++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a0fb566..c341169 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,9 @@ Added argument use.wpp.data to run.e0.mcmc(). Added the mean variant into sumary output. +e0.trajectories.plot() got arguments to plot the means (show.mean), +as well as selected trajectories (traj.index). + 5.2-0 (09/15/2023) ----- Annual subnational projections are now possible, via the argument "annual" diff --git a/DESCRIPTION b/DESCRIPTION index 2cf5b5c..a556361 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bayesLife Type: Package Title: Bayesian Projection of Life Expectancy -Version: 5.2-0.9008 +Version: 5.2-0.9009 Date: 2024-06-05 Author: Hana Sevcikova, Adrian Raftery, Jennifer Chunn Maintainer: Hana Sevcikova diff --git a/R/plot_functions.R b/R/plot_functions.R index 57fd3aa..ce72767 100644 --- a/R/plot_functions.R +++ b/R/plot_functions.R @@ -200,6 +200,7 @@ e0.trajectories.plot.all <- function(e0.pred, e0.trajectories.plot <- function(e0.pred, country, pi=c(80, 95), both.sexes=FALSE, nr.traj=NULL, adjusted.only = TRUE, typical.trajectory=FALSE, + traj.index = NULL, show.mean = FALSE, xlim=NULL, ylim=NULL, type='b', xlab='Year', ylab='Life expectancy at birth', main=NULL, lwd=c(2,2,2,2,1), col=c('black', 'green', 'red', 'red', '#00000020'), @@ -325,13 +326,20 @@ e0.trajectories.plot <- function(e0.pred, country, pi=c(80, 95), both.sexes=FALS e0pred <- pred[[ipred]] this.col <- plotcols[[ipred]] meta <- e0pred$mcmc.set$meta + if(!is.null(traj.index)) nr.traj <- length(traj.index) if(do.average) { trajectories <- get.e0.trajectories.object(pred, country$code, nr.traj=nr.traj, typical.trajectory=typical.trajectory, pi=pi) e0.median <- trajectories$median + if(show.mean) + e0.mean <- apply(trajectories$trajectories, 1, mean, na.rm=TRUE) } else { trajectories <- get.e0.trajectories.object(e0pred, country$code, nr.traj=nr.traj, typical.trajectory=typical.trajectory) e0.median <- bayesTFR::get.median.from.prediction(e0pred, country$index, country$code) + if(show.mean) + e0.mean <- bayesTFR::get.mean.from.prediction(e0pred, country$index, country$code) } + if(!is.null(traj.index) && !is.null(trajectories$trajectories)) trajectories$index <- traj.index + cqp <- list() if(ipred > 1) add <- TRUE if(!add) @@ -418,6 +426,16 @@ e0.trajectories.plot <- function(e0.pred, country, pi=c(80, 95), both.sexes=FALS lwds <- c(lwds, lwd[3]) lty <- c(lty, max(lty)+1) } + if(show.mean){ + # plot mean + lines(plot.data[[ipred]]$pred.x, e0.mean, type='l', col=this.col[3], lwd=1, lty=max(lty)+1) + mean.leg <- 'mean' + if(do.both.sexes) mean.leg <- paste(lowerize(get.sex.label(meta)), mean.leg) + legend <- c(legend, mean.leg) + cols <- c(cols, this.col[3]) + lwds <- c(lwds, 1) + lty <- c(lty, max(lty)+1) + } if(lpart2 > 0) { legend <- c(legend, paste('imputed', if(do.both.sexes) paste(lowerize(get.sex.label(meta)), 'e0') else 'e0')) cols <- c(cols, this.col[2]) diff --git a/man/e0.trajectories.plot.Rd b/man/e0.trajectories.plot.Rd index f4eb46e..9ba0020 100644 --- a/man/e0.trajectories.plot.Rd +++ b/man/e0.trajectories.plot.Rd @@ -12,6 +12,7 @@ The functions plot/tabulate the posterior distribution of trajectories of the li \usage{ e0.trajectories.plot(e0.pred, country, pi = c(80, 95), both.sexes = FALSE, nr.traj = NULL, adjusted.only = TRUE, typical.trajectory = FALSE, + traj.index = NULL, show.mean = FALSE, xlim = NULL, ylim = NULL, type = "b", xlab = "Year", ylab = "Life expectancy at birth", main = NULL, lwd = c(2, 2, 2, 2, 1), col = c('black', 'green', 'red', 'red', '#00000020'), @@ -34,6 +35,8 @@ e0.trajectories.table(e0.pred, country, pi = c(80, 95), \item{nr.traj}{Number of trajectories to be plotted. If \code{NULL}, all trajectories are plotted, otherwise they are thinned evenly. If \code{both.sexes} is \code{TRUE} the default is zero.} \item{adjusted.only}{Logical. By default, if the projection median is adjusted using e.g. \code{\link{e0.median.set}}, the function plots the adjusted median. If \code{adjusted.only=FALSE} the original (non-adjusted) median is plotted as well.} \item{typical.trajectory}{Logical. If \code{TRUE} one trajectory is shown for which the median absolute deviation from the median e0 projection is the median among all the trajectories.} + \item{traj.index}{Vector of trajectory indices to show. If not given, the trajectories are selected using equidistant spacing.} + \item{show.mean}{Logical indicating if the mean of the distribution should be shown.} \item{xlim, ylim, type, xlab, ylab, main}{Graphical parameters passed to the \code{plot} function.} \item{lwd, col, col2}{Vector of five elements giving the line width and color for: 1. observed data, 2. imputed missing data, 3. median, 4. quantiles, 5. trajectories. \code{col2} is only used if \code{both.sexes} is \code{TRUE}. In such a case, \code{col2} is used for female lines and \code{col} is used for male lines, which in this case defaults to \code{c('black', 'green', 'darkgreen', 'darkgreen', 'gray')}.}