# Mortality/Lapse Rate Calculations" In expstudies: Calculate Exposures, Assign Records to Intervals

```library(expstudies)
library(dplyr)
library(magrittr)
library(pander)

exposures_mod <- exposures %>%
inner_join(select(records, key, issue_age, gender), by = "key") %>%
mutate(attained_age = issue_age + duration - 1)
```

```head(exposures_mod)
```
```pander::pandoc.table(head(exposures_mod), split.table = Inf)
```

Add a death indicator and use a full exposure in the year of death when performing a mortality study.

```exposures_mort <- exposures_mod %>%
group_by(key) %>%
mutate(exposure_mod = if_else(duration == max(duration), 1, exposure),
death_cnt = if_else(duration == max(duration), 1, 0))

tail(exposures_mort, 4)
```
```exposures_mort <- exposures_mort %>% ungroup() %>% select(-exposure, -issue_age)
pander::pandoc.table(tail(exposures_mort, 4), split.table = Inf)
```

We then aggregate by duration to calculate mortality rates.

```duration_rate <- exposures_mort %>%
group_by(duration) %>%
summarise(q = sum(death_cnt)/sum(exposure_mod))

duration_rate
```
```pander::pandoc.table(duration_rate)
```

Or by attained age and gender.

```attained_age_gender_rates <- exposures_mort %>%
group_by(attained_age, gender) %>%
summarise(q = sum(death_cnt)/sum(exposure_mod))

tail(attained_age_gender_rates)
```
```pander::pandoc.table(tail(attained_age_gender_rates))
```

## Expected mortality

Some mortality tables have been loaded in an easy to join format so that users can conduct A/E analysis.

```summary(expstudies::mortality_tables)
```

The "table" column includes the table identifier from the SOA website's collection of mortality tables.

```head(mortality_tables\$AM92\$AM92_Ultimate)
```
```pander::pandoc.table(head(mortality_tables\$AM92\$AM92_Ultimate))
```

We join the mortality table to the data frame using the attained age.

```head(left_join(exposures_mort, mortality_tables\$AM92\$AM92_Ultimate, by = "attained_age"))
```
```pander::pandoc.table(head(left_join(exposures_mort, select(mortality_tables\$AM92\$AM92_Ultimate, -table), by = "attained_age")), split.table = Inf)
```

## Try the expstudies package in your browser

Any scripts or data that you put into this service are public.

expstudies documentation built on June 14, 2019, 5:03 p.m.