Skip to content

Instantly share code, notes, and snippets.

@mick001
Last active January 17, 2021 03:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mick001/4a2d899cf58aa86864ae to your computer and use it in GitHub Desktop.
Save mick001/4a2d899cf58aa86864ae to your computer and use it in GitHub Desktop.
Estimating arrival times of people in a shop using R. Part 1. Full article at: http://www.firsttimeprogrammer.blogspot.com/2015/07/estimating-arrival-times-of-people-in.html
#-------------------------------------------------------------------------------
# Loading data
#-------------------------------------------------------------------------------
# Observed data
data <- read.csv('data.txt',header=T)
# Take a look of the data
View(data)
# Total minutes ~ 60 minutes (~ 1 hour of observations)
total_minutes <- sum(data$min)+sum(data$sec)/60+sum(data$cent)/100/60
print(total_minutes)
# Compute the inter-arrival times in minutes
interarrivals <- data$min + data$sec/60 + data$cent/100/60
# Range of the bins for the histogram
bins.range <- 0.3
# Intervals of the histograms
breaks.points1 <- seq(0,max(interarrivals)+1,bins.range)
# Histogram of observed data
x.lab <- 'Minutes'
y.lab <- 'Frequencies'
main.lab <- 'Interarrival times'
hist(interarrivals,breaks=breaks.points1,xlab=x.lab,ylab=y.lab,main=main.lab,
col='cyan',border='blue',density=30)
# Boxplot of observed data
boxplot(interarrivals,col='cyan',border='blue',horizontal=T,xlab='Minutes',
main='Interarrival times')
# Customers arrive alone or in groups according to the observed probabilities
# Probability list -> probability vector
break.points2 <- seq(0,max(data$pers),1)
hist(data$pers,xlab='No. of people',breaks=break.points2,
main='Number of people per arrival',col='red',border='red4',density=30)
# Calculating relative frequencies
p <- table(data$pers)/sum(data$pers)
p.vec <- c(p[[1]],p[[2]],p[[3]],p[[4]])
names(p.vec) <- c('p(1)','p(2)','p(3)','p(4)')
# Vector of observed number of people per arrival
n.of.people <- min(data$pers):max(data$pers)
print(p.vec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment