https://github.com/robjhyndman/vital
  
  
    Tidy Analysis Tools for Mortality, Fertility, Migration and Population Data 
    https://github.com/robjhyndman/vital
  
        Last synced: 7 months ago 
        JSON representation
    
Tidy Analysis Tools for Mortality, Fertility, Migration and Population Data
- Host: GitHub
- URL: https://github.com/robjhyndman/vital
- Owner: robjhyndman
- Created: 2019-08-21T09:28:39.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-03-27T22:15:45.000Z (7 months ago)
- Last Synced: 2025-04-13T16:08:21.273Z (7 months ago)
- Language: R
- Homepage: https://pkg.robjhyndman.com/vital
- Size: 66.3 MB
- Stars: 28
- Watchers: 2
- Forks: 2
- Open Issues: 6
- 
            Metadata Files:
            - Readme: README.Rmd
- Changelog: NEWS.md
 
Awesome Lists containing this project
- jimsghstars - robjhyndman/vital - Tidy Analysis Tools for Mortality, Fertility, Migration and Population Data (R)
README
          ---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
# Okabe-Ito colours for discrete scales
options(
  ggplot2.discrete.colour = c("#D55E00", "#0072B2", "#009E73", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442"),
  ggplot2.discrete.fill = c("#D55E00", "#0072B2", "#009E73", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442")
)
# Fira Sans font for graphics
ggplot2::theme_set(
  ggplot2::theme_get() +
    ggplot2::theme(text = ggplot2::element_text(family = "Fira Sans"))
)
```
# vital  
[](https://github.com/robjhyndman/vital/actions/workflows/R-CMD-check.yaml)
The goal of vital is to allow analysis of demographic data using tidy tools.
## Installation
You can install the **stable** version from
[CRAN](https://cran.r-project.org/package=vital):
```r
pak::pak("vital")
```
You can install the **development** version from
[Github](https://github.com/robjhyndman/vital):
``` r
pak::pak("robjhyndman/vital")
```
## Examples
First load the necessary packages.
```{r packages, message=FALSE}
library(vital)
library(tsibble)
library(dplyr)
library(ggplot2)
```
### vital objects
The basic data object is a `vital`, which is time-indexed tibble that contains vital statistics such as births, deaths, population counts, and mortality and fertility rates. 
Here is an example of a `vital` object containing mortality data for Australia.
```{r aus}
aus_mortality
```
We can use functions to see which variables are index, key or vital:
```{r vars}
index_var(aus_mortality)
key_vars(aus_mortality)
vital_vars(aus_mortality)
```
### Plots
```{r autoplot, warning = FALSE}
aus_mortality |>
  filter(State == "Victoria", Sex != "total", Year < 1980, Age < 90) |>
  autoplot(Mortality) + scale_y_log10()
```
### Life tables and life expectancy
```{r lifetable}
# Life table for Victorian males in 2000
aus_mortality |>
  filter(State == "Victoria", Sex == "male", Year == 2000) |>
  life_table()
```
```{r e0}
# Life expectancy
aus_mortality |>
  filter(State == "Victoria", Sex != "total") |>
  life_expectancy() |>
  ggplot(aes(x = Year, y = ex, color = Sex)) +
  geom_line()
```
### Smoothing 
Several smoothing functions are provided: `smooth_spline()`, `smooth_mortality()`, `smooth_fertility()`, and `smooth_loess()`, each smoothing across the age variable for each year.
```{r smoothing}
# Smoothed data
aus_mortality |>
  filter(State == "Victoria", Sex != "total", Year == 1967) |>
  smooth_mortality(Mortality) |>
  autoplot(Mortality) +
  geom_line(aes(y = .smooth), col = "#0072B2") +
  ylab("Mortality rate") +
  scale_y_log10()
```
### Mortality models
Several mortality models are available including variations on Lee-Carter models (Lee & Carter, JASA, 1992), and functional data models (Hyndman & Ullah, CSDA, 2007).  
```{r lc}
fit <- aus_mortality |>
  filter(State == "Victoria", Sex != "total") |>
  model(
    lee_carter = LC(log(Mortality)),
    fdm = FDM(log(Mortality))
  )
fit
```
Models are fitted for all combinations of key variables excluding age. 
```{r lc2}
fit |>
  select(lee_carter) |>
  filter(Sex == "female") |>
  report()
```
```{r lc3}
fit |>
  select(lee_carter) |>
  autoplot()
```
```{r}
fit |>
  select(lee_carter) |>
  age_components()
fit |>
  select(lee_carter) |>
  time_components()
```
```{r lc5}
fit |> forecast(h = 20)
```
The forecasts are returned as a distribution column (here transformed normal because of the log transformation used in the model). The `.mean` column gives the point forecasts equal to the mean of the distribution column.