Tuesday, March 31, 2015

Code for FBI UCR, WASPC, OFM WA SAC data


"For years, my native city of Oakland, CA has poured millions of dollars of our general fund and other monies into our possible department and city jail; fighting what is often a pitched war against narcotics trafficking and the social results of narcotics trafficking. The battle never seems to have  a winner, just brief respites.  A brief respite in Oakland ( a city of 400K ) means 60 - 80 homicides a year. Full pitched casualties in a year of pitched battle means 150 - 160 homicides.  To give you some example of what that war feels like to civilians: In 2013, the state of WA (pop then 6.8M) had 129 homicides.  When you grow up inside that war, you hear gunfire on a weekly basis. You know what it is like to be shot at with automatic gunfire.  You see guns, drugs, and gang presences frequently enough to where to walk at what risk level when." from http://www.bellinghampoliticsandeconomics.com/2015/03/thought-about-new-jail-selected-violent.html  .  Code below the break.


### FBI UCR data http://www.ucrdatatool.gov/
## download format in spreadsheet CSV. Looks like this:
as.matrix(names(Redmond))
cat('                             
 [1,] "Year"                                
 [2,] "Months"                              
 [3,] "Population"                          
 [4,] "Violent.crime.total"                 
 [5,] "Murder.and.nonnegligent.Manslaughter"
 [6,] "Forcible.rape"                       
 [7,] "Robbery"                             
 [8,] "Aggravated.assault" 
 ')

Oakland <- read.csv("C:/Crime/Oakland.csv")
Seattle <- read.csv("C:/Crime/Seattle.csv")
Bellingham <- read.csv("C:/Crime/Bellingham.csv")
Yakima <- read.csv("C:/Crime/Yakima.csv")
Bellevue <- read.csv("C:/Crime/Bellevue.csv")
Redmond <- read.csv("C:/Crime/Redmond.csv")

# Oakland vs. Seattle
with(Oakland,plot(Violent.crime.total ~ Year,type="l",col="red",ylim=c(2500,10000),xlab="UCR Data: Oakland,CA (red) vs. Seattle, WA (blue)"))
with(Seattle,lines(Violent.crime.total ~ Year,col="blue"))
with(Oakland,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.026),xlab="UCR Data: Oakland,CA (red) vs. Seattle, WA (blue)"))
with(Seattle,lines((Violent.crime.total/Population) ~ Year,col="blue"))
# As percentage of population
with(Oakland,plot((Violent.crime.total/Population) ~ Year, type="l",col="red",xlab="Violent Crime in Oakland as a percentage of population 1985 - 2012. Lowess smoothing in blue."))
with(Oakland,lines(lowess((Violent.crime.total/Population) ~ Year),type="l",col="blue"))
with(Seattle,plot((Violent.crime.total/Population) ~ Year, type="l",col="red",xlab="Violent Crime in Seattle as a percentage of population 1985 - 2012. Lowess smoothing in blue."))
with(Seattle,lines(lowess((Violent.crime.total/Population) ~ Year),type="l",col="blue"))

# Yakima vs. Bellingham
with(Yakima,plot(Violent.crime.total ~ Year,type="l",col="red"))
mtext("UCR Data: Violent Crime in Yakima, WA")
with(Bellingham,plot(Violent.crime.total ~ Year,type="l",col="blue"))
mtext("UCR Data: Violent Crime in Bellingham, WA")
# As percentage of population
with(Yakima,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.020),xlab="UCR Data: Yakima (red) vs. Bellingham (blue)"))
with(Bellingham,lines((Violent.crime.total/Population) ~ Year,col="blue"))

# Redmond vs. Bellingham
with(Bellingham,plot(Violent.crime.total ~ Year,type="l",col="blue"))
mtext("UCR Data: Violent Crime in Bellingham, WA")
with(Redmond,plot(Violent.crime.total ~ Year,type="l",col="red"))
mtext("UCR Data: Violent Crime in Redmond, WA")
# As percentage of population
with(Bellingham,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.010),xlab="UCR Data: Bellingham(red) vs. Redmond (blue)"))
with(Redmond,lines((Violent.crime.total/Population) ~ Year,col="blue"))

with(Yakima,plot((Violent.crime.total) ~ Year,type="l",col="red",ylim=c(0,1000),xlab="UCR Data: Yakima (red),Bellingham (blue), Bellevue (black), Redmond (green)"))
with(Bellingham,lines((Violent.crime.total) ~ Year,col="blue"))
with(Bellevue,lines((Violent.crime.total) ~ Year,col="black"))
with(Redmond,lines((Violent.crime.total) ~ Year,col="green"))

# As percentage of population
with(Yakima,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.020),xlab="UCR Data: Yakima (red),Bellingham (blue), Bellevue (black), Redmond (green)"))
with(Bellingham,lines((Violent.crime.total/Population) ~ Year,col="blue"))
with(Bellevue,lines((Violent.crime.total/Population) ~ Year,col="black"))
with(Redmond,lines((Violent.crime.total/Population) ~ Year,col="green"))

# OFM WA SAC (WASPC) data http://www.ofm.wa.gov/sac/data.asp
# Plot Arrest Records
KingArrests <- read.csv("C:/Crime/KingArrestTotal.csv")
WhatcomArrests <- read.csv("C:/Crime/WhatcomArrestTotal.csv")
YakimaArrests <- read.csv("C:/Crime/YakimaArrestTotal.csv")
SnohomishArrests <- read.csv("C:/Crime/SnohomishArrestTotal.csv")
AllWaArrests <- read.csv("C:/Crime/AllWA.csv")

# All Arrests
with(AllWaArrests,plot((Number) ~ Year,type="l",col="red",xlab="UCR Arrest Data: All WA"))
#Yakima vs Whatcom Arrests
with(YakimaArrests,plot((Number) ~ Year,type="l",ylim=c(4500,12000),col="red",xlab="UCR Arrest Data: Yakima (red),Whatcom (blue)"))
with(WhatcomArrests,lines((Number) ~ Year,col="blue"))
#King vs Snohomish Arrests
with(KingArrests,plot((Number) ~ Year,type="l",col="red",ylim=c(12000,50000),xlab="UCR Arrest Data: King (red), Snohomish (blue)"))
with(SnohomishArrests,lines((Number) ~ Year,col="blue"))

JailMatrix <- read.csv("2014_jail_statistics.csv",stringsAsFactors = FALSE)
print("This is the OFM WA SAC data derived from WASPC. I reformat in a spreadsheet to as more R friendly form(at):")

cat('     
CountyJail             "character"
DesignCapacity         "integer"  
AverageDailyBedRate    "numeric"  
AverageDailyPopulation "numeric"  
PercentageofUse        "numeric"  
AverageLengthStay      "numeric"  
MalesADP               "integer"  
FemalesADP             "integer"  
UnGenderADP            "integer"  
WhiteADP               "integer"  
BlackADP               "integer"  
HispanicADP            "integer"  
NativeAmericanADP      "integer"  
AsianOtherADP          "integer"  
UnRaceADP              "integer"
')  

as.matrix(sapply(read.csv("2014_jail_statistics.csv",stringsAsFactors = FALSE),class))
with(JailMatrix,barplot(AverageDailyPopulation,names.arg=CountyJail,cex.names=.70,las=2))
x1 <- with(JailMatrix,(DesignCapacity - AverageDailyPopulation))
with(JailMatrix,barplot(x1,names.arg=CountyJail,cex.names=.70,las=2))
x2 <- with(JailMatrix,sum(x1[x1 > 0],na.rm=TRUE))
x3 <- with(JailMatrix,sum(x1[x1 < 0],na.rm=TRUE))
x4 <- x2 + x3 # Additional daily capacity in all WA jails (??)

### Graphs sent to file with jpeg_create()
### graphics.off() to write files to disk at end of run. 
### Time stamped filenames only so in cmd.exe : for /f %i in ('more list') do ren %i %i.jpg 
### where 'list' is the time stamped names produced by jpeg_create()

jpeg_create <- function() {
 systime <- as.numeric(Sys.time())
 # dev.new()
 jpeg(filename = systime,
          width = 1220, height = 824, units = "px", pointsize = 18,
          quality = 500, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
 Sys.sleep(2)
   }
setwd("C:/Crime")

# Oakland vs. Seattle
jpeg_create()
with(Oakland,plot(Violent.crime.total ~ Year,type="l",col="red",ylim=c(2500,10000),xlab="UCR Data: Oakland,CA (red) vs. Seattle, WA (blue)"))
with(Seattle,lines(Violent.crime.total ~ Year,col="blue"))
jpeg_create()
with(Oakland,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.026),xlab="UCR Data: Oakland,CA (red) vs. Seattle, WA (blue)"))
with(Seattle,lines((Violent.crime.total/Population) ~ Year,col="blue"))
# As percentage of population
jpeg_create()
with(Oakland,plot((Violent.crime.total/Population) ~ Year, type="l",col="red",xlab="Violent Crime in Oakland as a percentage of population 1985 - 2012. Lowess smoothing in blue."))
with(Oakland,lines(lowess((Violent.crime.total/Population) ~ Year),type="l",col="blue"))
jpeg_create()
with(Seattle,plot((Violent.crime.total/Population) ~ Year, type="l",col="red",xlab="Violent Crime in Seattle as a percentage of population 1985 - 2012. Lowess smoothing in blue."))
with(Seattle,lines(lowess((Violent.crime.total/Population) ~ Year),type="l",col="blue"))

# Yakima vs. Bellingham
jpeg_create()
with(Yakima,plot(Violent.crime.total ~ Year,type="l",col="red"))
mtext("UCR Data: Violent Crime in Yakima, WA")
jpeg_create()
with(Bellingham,plot(Violent.crime.total ~ Year,type="l",col="blue"))
mtext("UCR Data: Violent Crime in Bellingham, WA")
# As percentage of population
jpeg_create()
with(Yakima,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.015),xlab="UCR Data: Yakima (red) vs. Bellingham (blue)"))
with(Bellingham,lines((Violent.crime.total/Population) ~ Year,col="blue"))

# Redmond vs. Bellingham
jpeg_create()
with(Bellingham,plot(Violent.crime.total ~ Year,type="l",col="blue"))
mtext("UCR Data: Violent Crime in Bellingham, WA")
jpeg_create()
with(Redmond,plot(Violent.crime.total ~ Year,type="l",col="red"))
mtext("UCR Data: Violent Crime in Redmond, WA")
# As percentage of population
jpeg_create()
with(Bellingham,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.010),xlab="UCR Data: Bellingham(red) vs. Redmond (blue)"))
with(Redmond,lines((Violent.crime.total/Population) ~ Year,col="blue"))

# All the actors
jpeg_create()
with(Yakima,plot((Violent.crime.total) ~ Year,type="l",col="red",ylim=c(0,1000),xlab="UCR Data: Yakima (red),Bellingham (blue), Bellevue (black), Redmond (green)"))
with(Bellingham,lines((Violent.crime.total) ~ Year,col="blue"))
with(Bellevue,lines((Violent.crime.total) ~ Year,col="black"))
with(Redmond,lines((Violent.crime.total) ~ Year,col="green"))

# As percentage of population
jpeg_create()
with(Yakima,plot((Violent.crime.total/Population) ~ Year,type="l",col="red",ylim=c(.000,.020),xlab="UCR Data: Yakima (red),Bellingham (blue), Bellevue (black), Redmond (green)"))
with(Bellingham,lines((Violent.crime.total/Population) ~ Year,col="blue"))
with(Bellevue,lines((Violent.crime.total/Population) ~ Year,col="black"))
with(Redmond,lines((Violent.crime.total/Population) ~ Year,col="green"))

# All Arrests
jpeg_create()
with(AllWaArrests,plot((Number) ~ Year,type="l",col="red",xlab="UCR Arrest Data: All WA"))
#Yakima vs Whatcom Arrests
jpeg_create()
with(YakimaArrests,plot((Number) ~ Year,type="l",ylim=c(4500,12000),col="red",xlab="UCR Arrest Data: Yakima (red),Whatcom (blue)"))
with(WhatcomArrests,lines((Number) ~ Year,col="blue"))
#King vs Snohomish Arrests
jpeg_create()
with(KingArrests,plot((Number) ~ Year,type="l",col="red",ylim=c(12000,50000),xlab="UCR Arrest Data: King (red), Snohomish (blue)"))
with(SnohomishArrests,lines((Number) ~ Year,col="blue"))

JailMatrix <- read.csv("2014_jail_statistics.csv",stringsAsFactors = FALSE)
as.matrix(sapply(read.csv("2014_jail_statistics.csv",stringsAsFactors = FALSE),class))
jpeg_create()
with(JailMatrix,barplot(AverageDailyPopulation,names.arg=CountyJail,cex.names=.70,las=2,))
mtext("2014 Average Daily Jail Population (from WASPC)")
x1 <- with(JailMatrix,(DesignCapacity - AverageDailyPopulation))
jpeg_create()
with(JailMatrix,barplot(x1,names.arg=CountyJail,cex.names=.70,las=2))
mtext("2014 DesignCapacity - AverageDailyPopulation (from WASPC)")

jpeg_create()
with(AllWaArrests,plot((Number) ~ Year,col="red",xlab="OFM WA SAC Arrest Data: All WA"))
with(AllWaArrests,lines(lowess(Number ~ Year),col="blue"))
jpeg_create()
with(WhatcomArrests,plot(Number ~ Year,col="red",,xlab="OFM WA SAC Arrest Data: Whatcom County. Lowess smoothing in blue"))
with(WhatcomArrests,lines(lowess(Number ~ Year),col="blue"))
jpeg_create()
with(YakimaArrests,plot((Number) ~ Year,col="red",xlab="OFM WA SAC Arrest Data: Yakima County. Lowess smoothing in blue."))
with(YakimaArrests,lines(lowess(Number ~ Year),col="blue"))
jpeg_create()
with(SnohomishArrests,plot((Number) ~ Year,col="red",xlab="OFM WA SAC Arrest Data: Snohomish County. Lowess smoothing in blue."))
with(SnohomishArrests,lines(lowess(Number ~ Year),col="blue"))
jpeg_create()
with(KingArrests,plot((Number) ~ Year,col="red",xlab="OFM WA SAC Arrest Data: King County. Lowess smoothing in blue."))
with(KingArrests,lines(lowess(Number ~ Year),col="blue"))

No comments:

Post a Comment