Define the table set you want to use
There are a lot of different possible combinations of parameters that you might want to use, such as rates that vary by sex, risk class, table set (VBT/CSO/etc), smoking status, relative risk, ALB/ANB, etc.
It’s easy to define the parameters applicable to your assumption set. Here, we’ll use a dictionary to define the relationship:
rate_map = Dict(
"Male" => Dict(
"Smoker" => MortalityTables.table("2001 VBT Residual Standard Select and Ultimate - Male Smoker, ANB"),
"Nonsmoker" => MortalityTables.table("2001 VBT Residual Standard Select and Ultimate - Male Nonsmoker, ANB"),
),
"Female" => Dict(
"Smoker" => MortalityTables.table("2001 VBT Residual Standard Select and Ultimate - Female Smoker, ANB"),
"Nonsmoker" => MortalityTables.table("2001 VBT Residual Standard Select and Ultimate - Female Nonsmoker, ANB"),
)
);
And then we’ll define a function to look up the relevant rate. Note how the function matches the levels we defined for the assumption set dictionary above.
function rate_lookup(assumption_map,sex,smoke,issue_age,attained_age)
table = assumption_map[sex][smoke]
if issue_age in eachindex(table.select)
table.select[issue_age][attained_age]
else
table.ultimate[attained_age]
end
end
rate_lookup (generic function with 1 method)
Lining up with dataframe
By mapping each row’s data to the lookup function, we get a vector of rates for our data:
rates = map(eachrow(sample_data)) do row
rate_lookup(rate_map, row.sex, row.smoke, row.issue_age, row.attained_age)
end
10000-element Vector{Float64}:
0.00128
0.01019
0.00872
0.00055
0.00361
0.00642
0.00064
0.00035
0.00061
0.00071
⋮
0.00209
0.01047
0.00064
0.01746
0.00125
0.00741
0.00047
0.00212
0.00995
And finally, we can just add this to the dataframe:
sample_data.expectation = rates
sample_data
10000×5 DataFrame
9975 rows omitted
1 |
Male |
Smoker |
36 |
38 |
0.00128 |
2 |
Male |
Nonsmoker |
62 |
67 |
0.01019 |
3 |
Female |
Smoker |
58 |
62 |
0.00872 |
4 |
Female |
Smoker |
35 |
36 |
0.00055 |
5 |
Male |
Nonsmoker |
47 |
54 |
0.00361 |
6 |
Female |
Smoker |
63 |
64 |
0.00642 |
7 |
Male |
Nonsmoker |
32 |
35 |
0.00064 |
8 |
Female |
Nonsmoker |
25 |
29 |
0.00035 |
9 |
Female |
Smoker |
37 |
38 |
0.00061 |
10 |
Male |
Nonsmoker |
27 |
33 |
0.00071 |
11 |
Male |
Smoker |
57 |
59 |
0.00729 |
12 |
Female |
Smoker |
37 |
47 |
0.00334 |
13 |
Male |
Smoker |
53 |
62 |
0.01684 |
⋮ |
⋮ |
⋮ |
⋮ |
⋮ |
⋮ |
9989 |
Male |
Nonsmoker |
27 |
35 |
0.0008 |
9990 |
Female |
Nonsmoker |
50 |
57 |
0.00375 |
9991 |
Female |
Nonsmoker |
63 |
70 |
0.01 |
9992 |
Male |
Smoker |
36 |
41 |
0.00209 |
9993 |
Male |
Smoker |
47 |
56 |
0.01047 |
9994 |
Female |
Nonsmoker |
25 |
34 |
0.00064 |
9995 |
Male |
Smoker |
58 |
64 |
0.01746 |
9996 |
Male |
Smoker |
32 |
35 |
0.00125 |
9997 |
Female |
Smoker |
46 |
55 |
0.00741 |
9998 |
Female |
Nonsmoker |
39 |
41 |
0.00047 |
9999 |
Female |
Smoker |
47 |
48 |
0.00212 |
10000 |
Female |
Smoker |
48 |
58 |
0.00995 |