R/life.years.lived.R

Defines functions life.years.lived

Documented in life.years.lived

#' A function that returns the total number of life-years lived between two points in simulation
#' time for a particular age group and gender
#'
#' @param datalist The datalist that is produced by \code{\link{readthedata}}
#' @param agegroup alive people within this agegroup.
#' @param timewindow alive people within this simulation time e.g timewindow = c(15, 30).
#' @param site Select only the particular site from the study, if all ignore site/use all sites.
#' @return the total number of life-years lived aggregated by gender.
#' @examples
#' data(datalist)
#' life.years.lived <- life.years.lived(datalist = datalist, agegroup=c(15, 30),
#' timewindow=c(15, 30), site="All")
#' life.years.lived
#'
#' @importFrom magrittr %>%
#' @import dplyr
#' @export

life.years.lived <- function(datalist = datalist,
                             agegroup = c(15, 30),
                             timewindow = c(15, 30), site="All"){

  person.alive.timewindow <- age.group.time.window(datalist = datalist,
                                                   agegroup = agegroup,
                                                   timewindow = timewindow, site="All")


  person.alive.timewindow <- person.alive.timewindow %>%
    dplyr::mutate(exposure.time.Alive = exposure.end - exposure.start)
  person.alive.timewindow <- subset(person.alive.timewindow, exposure.time.Alive > 0)

  ## Sum all people's contibution years

  lifeyears.lived <- data.frame(dplyr::summarise(dplyr::group_by(person.alive.timewindow, Gender),
                                                   TotalExposed = n(),
                                                   LifeYearsLived = sum(exposure.time.Alive) ))

  return(lifeyears.lived)

}
wdelva/RSimpactHelp documentation built on Dec. 26, 2019, 3:42 a.m.