Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebnyberg/28e6a23ba7e1149e15b30b60fd2cc9d6 to your computer and use it in GitHub Desktop.
Save sebnyberg/28e6a23ba7e1149e15b30b60fd2cc9d6 to your computer and use it in GitHub Desktop.
project3.R
library(ggplot2)
source('helpers.R')
load('data.Rda')
head(data)
####### Setup data
data$crm <- (data$crimes / data$popul) * 1000
data$crm <- ceiling(data$crm)
data$region <- factor(data$region,
labels=c("Northeast","Midwest","South","West"),
levels=c(1,2,3,4))
####### 1
p1 <- ggplot(data, aes(crm, fill=region)) + theme_classic() +
geom_histogram(binwidth = 20, position = "dodge") + scale_fill_brewer(palette="Set2") +
xlim(0, 200)
p
# plot crm as a function of region
data$n <- 1
agg<-aggregate(data[,c("crm", "n")],by=list(region=data$region),FUN=sum)
agg
p2 <- ggplot(agg, aes(region, crm, fill=region)) +
theme_classic() +
geom_col() +
scale_fill_brewer(palette="Set2")
p
# calculate region
agg$mu <- agg$crm / agg$n
agg
#### 2
# lets plot the regular distribution against the poisson distribution
p <- list()
i = 1
for (region in levels(data$region)){
# cut out the subsection which matches the region name
df <- data[(data$region == region), ]
# calculate the mean and response of the poisson for the different
# crime levels in our data
mu <- mean(df$crm)
df$po <- dpois(df$crm, mu)
# add the plot to the list of plots
p[[i]] <- ggplot(df, aes(crm)) + theme_classic() + theme(plot.title = element_text(hjust = 0.5)) +
geom_histogram(aes(y = ..density.., fill="Red"), binwidth = 5) +
geom_ribbon(aes(ymin = 0, ymax = po, fill="Blue", alpha = 0.5)) +
xlim(0,130) + ggtitle(region) + theme(legend.position = "none")
i = i + 1
}
multiplot(p[[1]],p[[2]],p[[3]],p[[4]],cols=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment