Skip to content

Instantly share code, notes, and snippets.

View srinikumar's full-sized avatar

Srini Kumar srinikumar

  • San Francisco Bay Area, CA
View GitHub Profile
# Get Montgomery County geocode first to get the map for it
montgomery_county_gc <- geocode("Montgomery County, MD")
# Now, get the actual map
montgomery_county_map <- get_map(location = c(montgomery_county_gc$lon, montgomery_county_gc$lat))
# Showing shades of colors using an abs function
alldata$circadian <- abs(alldata$hour_of_day - 12)
# Using default colors
ggmap(montgomery_county_map) + geom_point(data=sampledata, aes(longitude, latitude, color=circadian))
# Get Montgomery County geocode first to get the map for it
45 montgomery_county_gc <- geocode("Montgomery County, MD")
46
47 # Now, get the actual map
48 montgomery_county_map <- get_map(location = c(montgomery_county_gc$lon, montgomery_county_gc$lat))
49
50
execute sp_execute_external_script
  @language = N'R',
  @script = N'
  OutputDataSet <- InputDataSet;
  inputData <- InputDataSet;
  names(inputData) <- tolower(names(inputData));
  resultTree <- violation_count_tree <- rxDTree(violations ~ commercial_vehicle + alcohol + work_zone + state +
                    vehicletype + year + make + model + color + race + gender +
                    driver_city + driver_state + drivers_license_state +
                    month_of_year + day_of_week + hour_of_day,
# The ctree from the party library did not finish at all!
cran_violation_count_tree <- ctree(data=rxViolations, formula=violations ~ commercial_vehicle + alcohol + work_zone + state +
vehicletype + year + make + model + color + race + gender +
driver_city + driver_state + drivers_license_state +
month_of_year + day_of_week + hour_of_day)
# I was pleasantly surprised that the rxDTree function finished in slightly less than 60 seconds
violation_count_tree <- rxDTree(violations ~ commercial_vehicle + alcohol + work_zone + state +
vehicletype + year + make + model + color + race + gender +
bymonth <- sqldf("select num_month_of_year, sum(frequency) as frequency from res group by num_month_of_year")
res <- sqlQuery(conn, "select [Date Of Stop] as date_of_stop, num_month_of_year, num_day_of_week, hour_of_day, count([Date Of Stop]) as frequency from Montgomery_County_MD_Traffic_Violations group by [Date Of Stop], num_month_of_year, num_day_of_week, hour_of_day order by [Date Of Stop]")
res <- sqlQuery(conn, "select [Date Of Stop] as date_of_stop, num_month_of_year, num_day_of_week, hour_of_day, count([Date Of Stop]) as frequency from Montgomery_County_MD_Traffic_Violations group by [Date Of Stop], num_month_of_year, num_day_of_week, hour_of_day order by [Date Of Stop]")