########################################################################### # Number of vehicles stopped at stoplight # # # ########################################################################### ######################################################################## # Read in data stoplight <- read.csv(file = "stoplight.csv") head(stoplight) ######################################################################## # Compare to Poisson # Numerical summaries mean(stoplight$vehicles) var(stoplight$vehicles) # Frequencies table(stoplight$vehicles) #Note that y = 0, 1, ..., 8 all have positive counts rel.freq <- table(stoplight$vehicles)/length(stoplight$vehicles) rel.freq2 <- c(rel.freq, rep(0, times = 7)) # Poisson calculations y <- 0:15 prob <- round(dpois(x = y, lambda = mean(stoplight$vehicles)), 4) # Examine lambda used # Observed and Poisson data.frame(y, prob, rel.freq = rel.freq2) # Plot plot(x = y - 0.1, y = prob, type = "h", ylab = "Probability", xlab = "Number of vehicles", lwd = 2, xaxt = "n") axis(side = 1, at = 0:15) lines(x = y + 0.1, y = rel.freq2, type = "h", lwd = 2, lty = "solid", col = "red") abline(h = 0) legend(x = 9, y = 0.15, legend = c("Poisson", "Observed"), lty = c("solid", "solid"), lwd = c(2,2), col = c("black", "red"), bty = "n") #