A casino onwer wants to estimate the revenue for an upcoming friday night. Below we have the sample of revenues of the past 15 fridays. We can produce a oint estimate of the next friday revenue as well as an interval estimate, using bootstrapping. In this example, we basically estimate the next revenue by the sample mean from the past data.
library(mosaic)
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 3.5.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: lattice
## Loading required package: ggformula
## Loading required package: ggplot2
## Loading required package: ggstance
##
## Attaching package: 'ggstance'
## The following objects are masked from 'package:ggplot2':
##
## geom_errorbarh, GeomErrorbarh
##
## New to ggformula? Try the tutorials:
## learnr::run_tutorial("introduction", package = "ggformula")
## learnr::run_tutorial("refining", package = "ggformula")
## Loading required package: mosaicData
## Loading required package: Matrix
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Note: If you use the Matrix package, be sure to load it BEFORE loading mosaic.
##
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median,
## prop.test, quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
donut <- c(105,97,117,130,100,95,89,101,112,140,122,93,131,111,100)
mean(donut)# the point estimate
## [1] 109.5333
min(donut)
## [1] 89
max(donut)
## [1] 140
sd(donut)
## [1] 15.50515
The function boo will be used to resample the data with replacement, 500 times. Each time, we will compute a bootsrapped mean. For the 500 computed means, we will compute the standard deviation which we call the standard error of the estimate. The estimate is the mean calculated from the original sample.
boo <- function(x){
a <- sample(x,length(x), replace = TRUE)
return(mean(a))
}
set.seed(123)
my.boo <- do(500) * boo(donut)
head(my.boo$boo)
## [1] 114.8000 106.1333 107.9333 110.4667 107.7333 106.3333
sd(my.boo$boo)
## [1] 3.874494
se <- sd(my.boo$boo)
upper.lim <- mean(donut) + 2 * se
lower.lim <- mean(donut) - 2 * se
lower.lim # the lower limit of the interval
## [1] 101.7843
upper.lim # the upper limit of the interval
## [1] 117.2823
Note that the lower limit is the original mean minus twice the standard error. And the upper limit is the original mean plus twice the standard error. So the interval is (101, 117). We can be reasonably confident that the revenue for the next friday will be between 101k and 117k, based on the sample data.