Frage

I have a data frame of policies like the one below

df<-data.frame(start=as.Date(c("2012-1-1","2012-3-1","2012-3-15")),end=as.Date(c("2012-12-31","2012-8-31","2012-12-31")),
           premium=c(500,200,300))
df
       start        end premium
1 2012-01-01 2012-12-31     500
2 2012-03-01 2012-08-31     200
3 2012-03-15 2012-12-31     300

I would like to plot the total earned premium on a daily basis starting on 2012-01-01 and 2012-03-15 using ggplot.

To understand earned premium, consider the first day of 2012. Only one policy was in effect. This policy had a total premium of 500 and it spanned 365 days, so the premium earned on 1/1/12 would be 500/365. Similarly, the premium earned on day 3/1/12 would be 500/365+200/183 since policies 1 and 2 were in effect.

So, how do I graph earned premium on daily basis for all of 2012?

War es hilfreich?

Lösung

    df$numdays <- as.numeric(df$end - df$start)
    df$daily_premium <- df$premium / df$numdays

    days_2012 <- seq.Date(from=as.Date('2012-01-01'), to=as.Date('2012-12-31'),by=1)

    check_range <- function(day_i) apply(df, 1, function(x) ifelse(day_i >= x['start'] && day_i <= x['end'], x['daily_premium'], 0))

    day_tally <- sapply(days_2012, check_range)

    day_sums <- colSums(apply(day_tally,2,as.numeric))

    qplot(days_2012,day_sums)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top