Analyzing Nausea/Vomiting


Vomiting Read More…
Comments

Analyzing Train Crashes, Shootings..


The data can be analyzed using the R project software.



# Load some standard libraries
suppressMessages(library(dplyr))
library(ggplot2)
library(tidyr)
library(ggmap)
library(readr)
library(lubridate)
library(reshape2)
library(animation)
library(scales)

# Grab the most recent data
urlLink=
"https://storage.googleapis.com/montco-stats/tzr.csv"
d=read.csv(url(urlLink),header=TRUE,stringsAsFactors=FALSE)
wd<-function(x)
as.POSIXct(strptime(x, '%Y-%m-%d %H:%M:%S',tz='EST'))


# Some functions used
epoch<-function(x) strftime(x, format =
"%s", tz = 'EST')
epocht<- function(x)
as.integer(strtoi(x)/600)*600
epoch10<- function(x)
as.POSIXct(strptime(x, '%s',tz='EST'))
wdhr<-function(x)
as.POSIXct(strptime(x, '%Y-%m-%d %H:%M:%S',tz='EST'))

# Let's add a few fields
d$mdate=wd(d$timeStamp)
d$epoch=epoch(d$timeStamp)
d$epoch10=epoch10(epocht(epoch(d$timeStamp)))

d$hr=format(d$mdate, format=
"%Y-%m-%d %H:00:00")
d$day=format(d$mdate, format=
"%Y-%m-%d 00:00:00")

# We only want data from this year
d=d[d$mdate >=
'2016-01-01 00:00:00',]

# Let's get some counts. This is everything. We'll
# narrow it down in a minute
counts <- summarise(group_by(d, title), Counts=length(title))
counts <- counts[order(-counts$Counts),]
counts

# Get a subset of the data and store it as dd
tt=c(
'EMS: POISONING','EMS: SHOOTING','EMS: AMPUTATION','EMS: STABBING','EMS: TRAIN CRASH','Fire: TRAIN CRASH')

dd=d[d$title %
in% tt,]

# Now get our subset of data
counts <- summarise(group_by(dd, title), Counts=length(title))
counts <- counts[order(-counts$Counts),]
counts

# This will give you the following output

title Counts
1 EMS: POISONING 6
2 EMS: SHOOTING 6
3 EMS: STABBING 6
4 EMS: AMPUTATION 4
5 EMS: TRAIN CRASH 3
6 Fire: TRAIN CRASH 3

# Convert all data to csv, to view in Excel
write.csv(dd, file =
"TrainShootingEtc.csv")





Screenshot 2016-02-12 10.00.21



# Looking to map the data
map <- qmap(c(lon =
-75.222372,lat = 40.156019), color="bw", fill = FALSE, add = TRUE,zoom = 11,legend = 'top')
map
# Redo the selection - You may want to add or delete
tt=c(
'EMS: POISONING','EMS: SHOOTING','EMS: AMPUTATION','EMS: STABBING','EMS: TRAIN CRASH')

# Resetting
dd=d[d$title %
in% tt,]

# You might want to verify
length(tt)
tt[which(tt==
"EMS: POISONING")]


map +
geom_point(data=d[d$title==tt[
1],], aes(x=lng, y=lat)
, alpha=
1, size=4.3,col='red',
position=position_jitter(w =
0.0009, h = 0.0009)
) +
geom_point(data=d[d$title==tt[
2],], aes(x=lng, y=lat)
, alpha=
1, size=4.3,col='blue',
position=position_jitter(w =
0.0009, h = 0.0009)
) +
geom_point(data=d[d$title==tt[
3],], aes(x=lng, y=lat)
, alpha=
1, size=4.3,col='green',
position=position_jitter(w =
0.0009, h = 0.0009)
) +
geom_point(data=d[d$title==tt[
4],], aes(x=lng, y=lat)
, alpha=
1, size=4.3,col='yellow',
position=position_jitter(w =
0.0009, h = 0.0009)
) +
geom_point(data=d[d$title==tt[
5],], aes(x=lng, y=lat)
, alpha=
1, size=4.3,col='black',
position=position_jitter(w =
0.0009, h = 0.0009)
)



Screenshot 2016-02-12 10.54.29


# Look at the data by township/boro
# Just shooting and stabbing
tt=c(
'EMS: SHOOTING','EMS: STABBING')

# Resetting
dd=d[d$title %
in% tt,]

counts <- summarise(group_by(dd,twp, title), Counts=length(title))
counts <- counts[order(-counts$Counts),]
counts

> counts
Source: local data frame [
9 x 3]
Groups: twp

twp title Counts
1 NORRISTOWN EMS: SHOOTING 3
2 POTTSTOWN EMS: STABBING 2
3 CHELTENHAM EMS: STABBING 1
4 LANSDALE EMS: STABBING 1
5 LOWER SALFORD EMS: SHOOTING 1
6 NORRISTOWN EMS: STABBING 1
7 POTTSTOWN EMS: SHOOTING 1
8 UPPER DUBLIN EMS: STABBING 1
9 UPPER MORELAND EMS: SHOOTING 1
>

# Add another category, and ask the question: What's the most
# dangerous township to be in, and what day of the week are
# most of the crimes?

# Break it down by wday 1=Sun,2=Mon..7=Sat
d$wday = wday(d$mdate)

tt=c(
'EMS: SHOOTING','EMS: STABBING','EMS: ASSAULT VICTIM')
#tt=c('EMS: STABBING')

# Resetting
dd=d[d$title %
in% tt,]
dd

counts <- summarise(group_by(dd,wday,twp, title), Counts=length(title))
counts <- counts[order(-counts$Counts),]
colnames(counts) <- c(
"tunit","twp","title","Counts")
counts



# Playing around with displays
ggplot(data=counts, aes(x=tunit, y=Counts, group = twp, colour = title)) +
geom_line() +
geom_point( size=
9, shape=18, fill="white") +
facet_wrap(
"twp" ) +
ggtitle(
"Shootings,Stabbings,Assults\n(since beg 2016)") +
labs(x=
"Weekday 1=Sun,2=Mon...4=Wed..6=Fri.",y="Counts",colour="Category")


Screenshot 2016-02-12 12.58.21







Comments

Vehicle Accidents - Weather Related


Vehicle Accidents Read More…
Comments