Dealing with conditionals in a better manner than deeply nested ifelse blocks
I'm trying to write some code to analyze my company's insurance plan
offerings... but they're complicated! The PPO plan is straightforward, but
the high deductible health plans are complicated, as they introduced a
"split" deductible and out of pocket maximum (individual and total) for
the family plans. It works like this:
Once the individual meets the individual deductible, he/she is covered at 90%
Once the remaining 1+ individuals on the plan meet the total deductible,
the entire family is covered at 90%
The individual cannot satisfy the family deductible with only their
medical expenses
I want to feed in a vector of expenses for my family members (there are
four of them) and output the total cost for each plan. Below is a table of
possible scenarios, with the following column codes:
ded_ind: did one individual meet the individual deductible?
ded_tot: was the total deductible reached?
oop_ind: was the individual out of pocket max reached
oop_tot: was the total out of pocket max reached?
exp_ind = the expenses of the highest spender
exp_rem = the expenses of the remaining /other/ family members (not the
highest spender)
oop_max_ind = the level of expenses at which the individual has paid their
out of pocket maximum (when ded_ind + 0.1 * exp_ind = out of pocket max
for the individual
oop_max_fam = same as for individual, but for remaining family members
The table:
| ded_ind | oop_ind | ded_rem | oop_rem | formula
|---------+---------+---------+---------+---------------------------------------------------------------------------|
| 0 | 0 | 0 | 0 | exp_ind + exp_rem
|
| 1 | 0 | 0 | 0 | ded_ind + 0.1 * (exp_ind -
ded_ind) + exp_rem |
| 0 | 0 | 1 | 0 | exp_ind + ded_rem + 0.1 *
(exp_rem - ded_rem) |
| 1 | 1 | 0 | 0 | oop_max_ind + exp_fam
|
| 1 | 0 | 1 | 0 | ded_ind + 0.1 * (exp_ind -
ded_ind) + ded_rem + 0.1 * (exp_rem - ded_rem) |
| 0 | 0 | 1 | 1 | oop_max_rem + exp_ind
|
| 1 | 0 | 1 | 1 | ded_ind + 0.1 * (exp_ind -
ded_ind) + oop_max_rem |
| 1 | 1 | 1 | 0 | oop_ind_max + ded_rem + 0.1 *
(exp_rem - ded_rem) |
| 1 | 1 | 1 | 1 | oop_ind_max + oop_rem_max
|
Omitted: 0 1 0 0, 0 0 0 1, 0 1 1 0, and 0 1 0 1 are not present, as
oop_ind and oop_rem could not have been met if ded_ind and ded_rem,
respectively, have not been met.
My current code is a somewhat massive ifelse loop like so (not the code,
but what it does):
check if plan is ppo or hsa if hsa plan if exp_ind + exp_rem < ded_rem #
didn't meet family deductible if exp_ind < ded_ind # individual deductible
also not met cost = exp_ind + exp_rem else is exp_ind > oop_ind_max #
ded_ind met, is oop_ind? ded_ind + 0.1 * (exp_ind - ded_ind) + exp_fam #
didn't reach oop_max_ind else oop_max_ind + exp_fam # reached oop_max_ind
else ...
After the else, the total is greater than the family deductible. I check
to see if it was contributed by more than two people and just continue on
like that.
My question, now that I've given some background to the problem: Is there
a better way to manage conditional situations like this than ifelse loops
to filter them down a bit at a time?
The code ends up seeming redundant, as one checks for some higher level
conditions (consider the table where ded_rem is met or not met... one
still has to check for ded_ind and oop_max_ind in both cases, and the code
is the same... just positioned at two different places in the ifelse
structure).
Could this be done with some sort of matrix operation? Are there other
examples online of more clever ways to deal with filtering of conditions?
Many thanks for any suggestions.
P.S. I'm using R and will be creating an interactive with shiny so that
other employees can input best and worst case scenarios for each of their
family members and see which plan comes out ahead via a dot or bar chart.
No comments:
Post a Comment