Make a new vital from products and ratios of a measured variable by a key variable. The most common use case of this function is for computing mortality rates by sex, from the sex ratios and geometric mean of the rates.
Arguments
- .data
A vital object
- .var
A bare variable name of the measured variable to use.
- key
A bare variable name specifying the key variable to use. This key variable must include the value
geometric_mean
.- times
When the variable is a distribution, the product must be computed by simulation. This argument specifies the number of simulations to use.
Details
Note that when a measured variable takes value 0, the geometric mean is set to 10^-6 to avoid infinite values in the ratio. Therefore, when the transformation is undone, the results will not be identical to the original in the case that the original data was 0.
References
Hyndman, R.J., Booth, H., & Yasmeen, F. (2013). Coherent mortality forecasting: the product-ratio method with functional time series models. Demography, 50(1), 261-283.
Examples
# Make products and ratios
orig_data <- aus_mortality |>
dplyr::filter(Year > 2015, Sex != "total", Code == "NSW")
pr <- orig_data |>
make_pr(Mortality)
# Compare original data with product/ratio version
orig_data
#> # A vital: 1,010 x 8 [1Y]
#> # Key: Age x (Sex, State, Code) [101 x 2]
#> Year Age Sex State Code Mortality Exposure Deaths
#> <int> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 2016 0 female New South Wales NSW 0.00252 48454. 122
#> 2 2016 1 female New South Wales NSW 0.000269 48291. 13
#> 3 2016 2 female New South Wales NSW 0.000104 48255. 5
#> 4 2016 3 female New South Wales NSW 0.000123 48915. 6
#> 5 2016 4 female New South Wales NSW 0.000143 49099. 7
#> 6 2016 5 female New South Wales NSW 0.0000613 48951. 3
#> 7 2016 6 female New South Wales NSW 0.0000204 49122. 1
#> 8 2016 7 female New South Wales NSW 0.0000824 48515. 4
#> 9 2016 8 female New South Wales NSW 0.0000207 48423. 1
#> 10 2016 9 female New South Wales NSW 0.000104 47980. 5
#> # ℹ 1,000 more rows
pr
#> # A vital: 1,515 x 8 [1Y]
#> # Key: Age x (Sex, State, Code) [101 x 3]
#> Year Age Sex State Code Mortality Exposure Deaths
#> <int> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 2016 0 female New South Wales NSW 0.966 48454. 122
#> 2 2016 1 female New South Wales NSW 0.926 48291. 13
#> 3 2016 2 female New South Wales NSW 0.867 48255. 5
#> 4 2016 3 female New South Wales NSW 0.760 48915. 6
#> 5 2016 4 female New South Wales NSW 1.11 49099. 7
#> 6 2016 5 female New South Wales NSW 1.78 48951. 3
#> 7 2016 6 female New South Wales NSW 0.458 49122. 1
#> 8 2016 7 female New South Wales NSW 1.45 48515. 4
#> 9 2016 8 female New South Wales NSW 1.03 48423. 1
#> 10 2016 9 female New South Wales NSW 2.30 47980. 5
#> # ℹ 1,505 more rows
# Undo products and ratios
pr |> undo_pr(Mortality)
#> # A vital: 1,010 x 8 [1Y]
#> # Key: Age x (Sex, State, Code) [101 x 2]
#> Year Age Sex State Code Mortality Exposure Deaths
#> <int> <int> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 2016 0 female New South Wales NSW 0.00252 48454. 122
#> 2 2016 1 female New South Wales NSW 0.000269 48291. 13
#> 3 2016 2 female New South Wales NSW 0.000104 48255. 5
#> 4 2016 3 female New South Wales NSW 0.000123 48915. 6
#> 5 2016 4 female New South Wales NSW 0.000143 49099. 7
#> 6 2016 5 female New South Wales NSW 0.0000613 48951. 3
#> 7 2016 6 female New South Wales NSW 0.0000204 49122. 1
#> 8 2016 7 female New South Wales NSW 0.0000824 48515. 4
#> 9 2016 8 female New South Wales NSW 0.0000207 48423. 1
#> 10 2016 9 female New South Wales NSW 0.000104 47980. 5
#> # ℹ 1,000 more rows