stat645

Week 3

Complete code

Aesthetics

library(ggplot2)
library(lubridate)

two <- read.csv("pop-selected.csv")
two$date <- ymd(two$date)

Hanging rootogram

x <- c(rnorm(5000), rnorm(500, sd = 0.3))
qplot(x, binwidth = 0.2)

# 1. -------------------------------------------------------------------------
hist <- count(round_any(x, 0.2))
hist$dens <- hist$freq / sum(hist$freq) / 0.2
# Should equal one: sum(hist$dens * 0.2)

# 2. -------------------------------------------------------------------------
ggplot(hist, aes(x, xmin = x - 0.1, xmax = x + 0.1)) + 
  geom_rect(aes(ymin = 0, ymax = dens))

# 3. -------------------------------------------------------------------------
xgrid <- seq(-3, 3, length = 100)
norm <- data.frame(x = xgrid, dens = dnorm(xgrid))

ggplot(hist) + 
  geom_rect(aes(xmin = x - 0.1, xmax = x + 0.1, ymin = 0, ymax = dens)) + 
  geom_line(aes(x, dens), data = norm, colour = "red", size = 2)

# 4. -------------------------------------------------------------------------

hist$nfreq <- dnorm(hist$x, 0, 1) * 5500 * 0.2