Comments:"crime-doesnt-climb/README.md at master · gwintrob/crime-doesnt-climb · GitHub"
URL:https://github.com/gwintrob/crime-doesnt-climb/blob/master/README.md
Crime Doesn't Climb
Among San Francisco's diverse neighborhoods and varied micro-climates, we've heard the phrase "Crime Doesn't Climb," meaning that the city's loftier areas are often associated with less crime. San Francisco, sometimes refered to as the "homeless capital of the United States", ranks in the bottom 10% of safest cities in the country (New York City is nearly three times safer). Although certain neighborhoods (e.g. the Tenderloin) have particularly high crime rates, we wondered if there was more granular data that could answer the question: does crime climb?
We used the DataSF repository, part of the Mayor's 2.0 City initiative to tap into local innovation. The site includes a CSV file of all reported SFPD incidents since 2003. Of the 83,991 incidents in 2013 (we pulled the data on 9/7 at 2:00pm), we selected 15,000 from January 1 to February 25. These crimes ranged from 7,691 "Grand Theft from Locked Auto" incidents to 610 cases of drunkenness. The incidents are tagged with latitude/longitude information and we used the Google Elevation API to translate to height in meters. Here's the script that reads the incident CSV file:
varfs=require('fs');varcsv=require('csv');var_=require('underscore');varrequest=require('request');varasync=require('async');varbatchSize=50;csv().from.path(__dirname+'/sfpd_incident_2013-partial.csv',{delimiter:',',escape:'"'}).to.array(function(data){varlocations=[];varelevationFunctions=_.chain(data.slice(1)).groupBy(function(row,index){returnMath.floor(index/batchSize);}).map(function(dataGroup,groupIndex){varlocations=_.map(_.values(dataGroup),function(row){return[row[10],row[9]];});returnfunction(callback){callElevationApiWithLocations(locations,function(elevations){varstartIndex=groupIndex*batchSize+1;for(vari=0;i<elevations.length;i++){data[startIndex+i].push(elevations[i]);}setTimeout(callback,200);});};}).value();async.series(elevationFunctions,function(err,results){csv().from.array(data).to.stream(fs.createWriteStream(__dirname+'/incidentsWithElevation.csv'));});});
Here's the code snippet that queries the Google Elevation API (careful--Google rate limits agressively):
varcallElevationApiWithLocations=function(locations,callback){varbaseUrl='http://maps.googleapis.com/maps/api/elevation/json?sensor=false&locations=';varlocationsParam=_.reduce(locations,function(memo,loc){returnmemo+'|'+loc[0]+','+loc[1];},'');varurl=baseUrl+locationsParam.substring(1);request.get({url:url,json:true},function(error,response,body){if(!error&&response.statusCode==200){varelevations=_.map(body.results,function(result){returnresult.elevation;});callback(elevations);}});};
CartoDB lets you quickly slice-and-dice geospatial data and easily display it on a map. We segmented the data into incidents at 25 meter intervals (see the charts folder for more details) and found that crime levels drop off sharply at higher elevations:
One flaw in this analysis is that it could be a byproduct of the fact that there is less land mass in the city at these higher altitudes. In other words, if 90% of the city is at an elevation less than 25m, then 90% of the crime would occur at lower altitudes, assuming an even distribution of incidents. To correct for this problem, we took a distributed sample of 10,000 locations in San Francisco and divided the number of incidents in each elevation range by the number of locations in that bucket. We manually selected latitude and longitude points to define San Francisco as a grid and created a CSV of latitude, longitude, elevation triplets:
varinitialLat=37.735121;varinitialLong=-122.469749;varfinalLat=37.804596;varfinalLong=-122.405891;varlatStep=(finalLat-initialLat)/100.0;varlongStep=(finalLong-initialLong)/100.0;varlatArray=_.range(initialLat,finalLat,latStep);varlongArray=_.range(initialLong,finalLong,latStep);vardata=[];_.each(latArray,function(latValue){_.each(longArray,function(longValue){data.push([latValue,longValue]);});});varelevationFunctions=_.chain(data).groupBy(function(row,index){returnMath.floor(index/batchSize);}).map(function(dataGroup,groupIndex){varlocations=_.map(_.values(dataGroup),function(row){returnrow;});returnfunction(callback){callElevationApiWithLocations(locations,function(elevations){varstartIndex=groupIndex*batchSize+1;for(vari=0;i<elevations.length;i++){data[startIndex+i].push(elevations[i]);}setTimeout(callback,200);});};}).value();async.series(elevationFunctions,function(err,results){csv().from.array(data).to.stream(fs.createWriteStream(__dirname+'/elevationSample.csv'));});
When normalizing for land mass at different elevations, we found that the trend of lower crime at higher elevations was equally drastic: