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.00971
0.00056
0.00091
0.001
0.01237
0.00292
0.00081
0.00066
0.00527
0.00483
0.00977
0.00373
0.00064
⋮
0.00362
0.00263
0.00127
0.00981
0.02741
0.02602
0.00087
0.00615
0.00744
0.00145
0.00059
0.01198
And finally, we can just add this to the dataframe:
sample_data.expectation = rates
sample_data
10000×5 DataFrame
9975 rows omitted
1 |
Female |
Nonsmoker |
65 |
71 |
0.00971 |
2 |
Male |
Nonsmoker |
33 |
35 |
0.00056 |
3 |
Male |
Nonsmoker |
44 |
45 |
0.00091 |
4 |
Male |
Smoker |
36 |
37 |
0.001 |
5 |
Female |
Smoker |
57 |
64 |
0.01237 |
6 |
Female |
Smoker |
40 |
47 |
0.00292 |
7 |
Female |
Nonsmoker |
31 |
40 |
0.00081 |
8 |
Female |
Nonsmoker |
44 |
45 |
0.00066 |
9 |
Male |
Smoker |
40 |
49 |
0.00527 |
10 |
Male |
Nonsmoker |
47 |
56 |
0.00483 |
11 |
Male |
Smoker |
48 |
56 |
0.00977 |
12 |
Male |
Nonsmoker |
54 |
58 |
0.00373 |
13 |
Female |
Nonsmoker |
25 |
34 |
0.00064 |
⋮ |
⋮ |
⋮ |
⋮ |
⋮ |
⋮ |
9989 |
Male |
Smoker |
36 |
45 |
0.00362 |
9990 |
Male |
Smoker |
39 |
44 |
0.00263 |
9991 |
Male |
Nonsmoker |
49 |
50 |
0.00127 |
9992 |
Male |
Smoker |
45 |
55 |
0.00981 |
9993 |
Male |
Smoker |
65 |
70 |
0.02741 |
9994 |
Male |
Smoker |
60 |
68 |
0.02602 |
9995 |
Male |
Nonsmoker |
27 |
36 |
0.00087 |
9996 |
Male |
Nonsmoker |
52 |
60 |
0.00615 |
9997 |
Female |
Nonsmoker |
56 |
65 |
0.00744 |
9998 |
Female |
Nonsmoker |
40 |
47 |
0.00145 |
9999 |
Female |
Nonsmoker |
34 |
39 |
0.00059 |
10000 |
Female |
Nonsmoker |
63 |
71 |
0.01198 |