[R] A coding question involving variable assignments in ifelse()

Duncan Murdoch murdoch at stats.uwo.ca
Thu Apr 26 23:30:57 CEST 2007


On 4/26/2007 5:21 PM, xpRt.wannabe wrote:
> I made a few slight modifications to the original model in an effort
> to see the inner workings of the code:
> 
> deductible <- 1
> coverage.limit <- 2
> insurance.threshold <- deductible + coverage.limit
> 
> <snip>
> 
> set.seed(123)
> loss <- abs(rnorm(rpois(1, 5), 1, 3))
> n <- length(loss)
> accept <- runif(n) < 0.8
> payout <- runif(n) < 0.999
> sum(ifelse(accept & payout, ifelse(loss > insurance.threshold,
> loss - coverage.limit, pmin(loss, deductible)), 0))
> 
> [1] 6.188817
> 
> <snip>
> 
> To tease out the data as well as to see the effect of 'accept &
> payout', I did the following:
> 
>> loss
> [1] 3.401663 4.570620 4.068667 4.718488
>> accept
> [1]  TRUE FALSE  TRUE  TRUE  # The second loss claim is NOT accepted
> by the insurance company.
>> payout
> [1] TRUE TRUE TRUE TRUE
>> accept & payout
> [1]  TRUE FALSE  TRUE  TRUE  # The second entry is FALSE because of
> the second entry in 'accept.'
> 
> Based on the inner ifelse() expression, the original loss numbers
> become : 1.401663, 2.570620, 2.068667, 2.718488, respectively (which
> is fine and what I wanted).
> 
> Because the second entry in 'accept & payout' is FALSE, the second
> altered loss number (2.570620) becomes 0, making sum(...) equal
> 6.188817.  Unfortunately this is _not_ what I want, and I apologize
> for not being clear in the first place.  What I want is: for any FALSE
> entry, the original loss number is unaltered, as opposed to become 0.
> So in the example above, the four numbers that should have been added
> are: 1.401663, 4.570620, 2.068667, 2.718488, yielding 10.759438
> instead of 6.188817.
> 
> Any further suggestions would be greatly appreciated.

I'm sorry, but from an anonymous poster that's not a reasonable request. 
  Just work it out yourself.

Duncan Murdoch

> 
> On 4/26/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 4/26/2007 2:31 PM, xpRt.wannabe wrote:
>>> Just to be sure, is what I have below the right intepretation of your
>>> suggestion:
>> Yes, that's what I suggested.
>>
>> Duncan Murdoch
>>
>>> deductible <- 15
>>> coverage.limit <- 75
>>> insurance.threshold <- deductible + coverage.limit
>>>
>>> tmpf <- function() {
>>> loss <- rlnorm(rpois(1, 3), 2, 5)
>>> n <- length(loss)
>>> accept <- runif(n) < 0.8
>>> payout <- runif(n) < 0.999
>>> sum(ifelse(accept & payout, ifelse(loss > insurance.threshold, loss -
>>> coverage.limit, pmin(loss, deductible)), 0))
>>> }
>>> net <- replicate(1000000, tmpf())
>>>
>>> On 4/26/07, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>>>> On 4/26/2007 12:48 PM, xpRt.wannabe wrote:
>>>>> Dear List,
>>>>>
>>>>> Below is a simple, standard loss model that takes into account the
>>>>> terms of an insurance policy:
>>>>>
>>>>> deductible <- 15
>>>>> coverage.limit <- 75
>>>>> insurance.threshold <- deductible + coverage.limit
>>>>>
>>>>> tmpf <- function() {
>>>>> loss <- rlnorm(rpois(1, 3), 2, 5)
>>>>> sum(ifelse(loss > insurance.threshold, loss - coverage.limit,
>>>>> pmin(loss, deductible)))
>>>>> }
>>>>> net <- replicate(1000000, tmpf())
>>>>>
>>>>> Now, I would like to enhance the model by incorporating the following
>>>>> two probabilities:
>>>>>
>>>>> 1. Probability of claim being accepted by the insurance company, say, 0.8
>>>>> 2. Probability of payout by the insurance company, say, 0.999
>>>>>
>>>>> Could anyone suggest how one might do this?
>>>> A general way to generate events with probability p is runif(n) < p.  So
>>>> I'd add
>>>>
>>>> n <- length(loss)
>>>> accept <- runif(n) < 0.8
>>>> payout <- runif(n) < 0.999
>>>>
>>>> and then require "accept & payout"  before any payment at all, e.g.
>>>>
>>>> sum(ifelse(accept & payout, [ your old ifelse expression ], 0))
>>>>
>>>> There are a lot of implicit independence assumptions here; they may not
>>>> be very realistic.
>>>>
>>>> Duncan Murdoch
>>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>



More information about the R-help mailing list