Skip to content

Commit

Permalink
arguments show.mean and traj.index in e0.tajectories.plot
Browse files Browse the repository at this point in the history
  • Loading branch information
hanase committed Jun 6, 2024
1 parent e0868bb commit 3215f4c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
Expand Down
18 changes: 18 additions & 0 deletions R/plot_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
3 changes: 3 additions & 0 deletions man/e0.trajectories.plot.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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')}.}
Expand Down

0 comments on commit 3215f4c

Please sign in to comment.