Suppose, we have 100 observations of variable X. X is the day of the week (Sunday – Saturday) when a specific event occurs on regular basis. E.g. when a postman delivers a package every week for 100 weeks. Note: the event typically should happen on the same day of the week, unless there are special cases (holiday, etc). Let’s take the following distribution:
Sunday: 78
Monday: 5
Wednesday: 9
Saturday: 8
Sum = 100
Based on the above data, we need to predict the day of the week the postman delivers a package next week and it's certainty in %.
Possible Solution
78% of the time the delivery day is Sunday. Educated guess tells us that since we expect the event to happen on same day every week (except holidays, etc), the expected package day is Sunday.
Binomial uncertainty of that is: 0.78*(1-0.78)/sqrt(100) = 0.017
Using bootstrap
in R
:
library(boot)
x = c(rep(1,78),rep(2,5),rep(3,0),rep(4,9),rep(5,0),rep(6,0),rep(7,8))
h = hist(x+0.01,breaks=c(1,2,3,4,5,6,7,8))
plot(h)
GetMaxBinFractionBoot = function(x,i){
.sample = x[i]
.hist = hist(.sample+0.01,breaks=c(1,2,3,4,5,6,7,8),plot=FALSE)
.max = max(.hist$counts)
.frac = .max / length(.sample)
return(.frac)
}
GetMaxBinPositionBoot = function(x,i){
.sample = x[i]
.hist = hist(.sample+0.01,breaks=c(1,2,3,4,5,6,7,8),plot=FALSE)
.pos = which.max(.hist$counts)
return(.pos)
}
myBoot = boot(data=x,
statistic=GetMaxBinFractionBoot,
R=100)
print(myBoot)
hist(myBoot$t)
myBoot2 = boot(data=x,
statistic=GetMaxBinPositionBoot,
R=1000)
print(myBoot2)
hist(myBoot2$t)
Results from R
:
Call:
boot(data = x, statistic = GetMaxBinFractionBoot, R = 100)
Bootstrap Statistics :
original bias std. error
t1* 0.78 0.004 0.0432
Call:
boot(data = x, statistic = GetMaxBinPositionBoot, R = 1000)
Bootstrap Statistics :
original bias std. error
t1* 1 0 0
And then I am stuck with the second part of the problem - how to quantify how certain we are that the day is Sunday in this case. Any help greatly appreciated! Thank you!