Re: [R] A coding question involving variable assignments in ifelse() 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  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@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. ______________________________________________ R-help@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.