r - Dealing with conditionals in a better manner than deeply nested ifelse blocks -
i'm trying write code analyze company's insurance plan offerings... they're complicated! ppo plan straightforward, high deductible health plans complicated, introduced "split" deductible and out of pocket maximum (individual , total) family plans. works this:
- once individual meets individual deductible, he/she covered @ 90%
- once remaining 1+ individuals on plan meet total deductible, entire family covered @ 90%
- the individual cannot satisfy family deductible medical expenses
i want feed in vector of expenses family members (there 4 of them) , output total cost each plan. below table of possible scenarios, following column codes:
ded_ind
: did 1 individual meet individual deductible?ded_tot
: total deductible reached?oop_ind
: individual out of pocket max reachedoop_tot
: total out of pocket max reached?exp_ind
= expenses of highest spenderexp_rem
= expenses of remaining /other/ family members (not highest spender)oop_max_ind
= level of expenses @ individual has paid out of pocket maximum (whended_ind + 0.1 * exp_ind =
out of pocket max individualoop_max_fam
= same individual, 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
, , 0 1 0 1
not present, oop_ind
, oop_rem
not have been met if ded_ind
, ded_rem
, respectively, have not been met.
my current code massive ifelse
loop (not code, does):
check if plan 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 not met cost = exp_ind + exp_rem else exp_ind > oop_ind_max # ded_ind met, 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 else, total greater family deductible. check see if contributed more 2 people , continue on that.
my question, i've given background problem: there better way manage conditional situations ifelse
loops filter them down bit @ time?
the code ends seeming redundant, 1 checks higher level conditions (consider table ded_rem
met or not met... 1 still has check ded_ind
, oop_max_ind
in both cases, , code same... positioned @ 2 different places in ifelse
structure).
could done sort of matrix operation? there other examples online of more clever ways deal filtering of conditions?
many suggestions.
p.s. i'm using r , creating interactive shiny
other employees can input best , worst case scenarios each of family members , see plan comes out ahead via dot or bar chart.
the suggestion convert binary value based on result gave me idea, helped me learn 1 can vectorized true
/ false
checks (i guess obvious many).
here's current idea:
expenses
vector of individual forecast medical expenses year (example of 3 people):
expenses <- c(1500, 100, 400)
we set exp_ind
max value, , sum rest exp_rem
exp_ind <- max(expenses) # [1] index of which() cases multiple max values exp_rem <- sum(expenses[-which(expenses == exp_ind)[1]])
for given plan, can set vector cutoffs, example:
- individual deductible = 1000
- individual out of pocket max = 2000 (need incur 11k of expenses there)
- family deductible = 2000
- family out of pocket max = 4000 (need incur 22k of expenses there)
set values:
ded_ind <- 1000 oop_max_ind <- 11000 ded_tot <- 2000 oop_max_tot <- 22000 cutoffs <- c(ded_ind, oop_max_ind, ded_tot, oop_max_tot)
now can check input expense against cutoffs:
result <- as.numeric(rep(c(exp_ind, exp_rem), each = 2) > cutoffs)
last, convert binary:
result_bin <- sum(2^(seq_along(result) - 1) * result)
now can set functions possible outcomes based on value in result_bin
:
if(result_bin == 1) {cost <- ded_ind + 0.1 * (exp_ind - ded_ind) + exp_rem } cost [1] 1550
we can check this...
- high spender have paid 1000 , 10% of remaining 500 = 1050
- other members did not reach family deductible , paid full 400 + 100 = 500
- total: 1550
i still need create mapping of results_bin
values corresponding functions, doing vectorized check , converting unique binary value much, better, in opinion, ifelse
nested mess.
i @ this: i'd have had set variables , write functions anyway; saves me 1) explicitly writing conditions, 2) redundancy issue talking in 1 ends writing identical "sibling" branches of parent splits in ifelse
structure, , lastly, 3) code far, far, far more followed.
Comments
Post a Comment