R/getDensity.R

getDensity <- function(records,
                       events,
                       datasetname,
                       filter = NULL,
                       daysback = 365,
                       h = c(.01, .01),
                       return_events = FALSE){
    
    require(data.table)
    require(MASS)
    
    i <- NULL
    longitude <- NULL
    latitude <- NULL
    point_date <- NULL
    i.longitude <- NULL
    i.latitude <- NULL
    
    ## Calculate event timeframe for data acquisition
    ## Use maximum of daysback to get maximum records needed to avoid future calls
    event_window <- records[ , c(min(date) - daysback, 
                                 max(date))]
    
    ## Download event data from plenario
    events <- plenarioDatadump(dataset = datasetname,
                               filter = filter, 
                               event_window = event_window)
    
    ## Create index values for pages
    N <- nrow(records)
    page_limit <- 150
    START_ROWS <- seq(1, N, page_limit)
    END_ROWS <- c(seq(1, N, page_limit)[-1] - 1, N)
    II <- mapply(`:`, START_ROWS, END_ROWS)
    
    ## Create an index
    records[ , i := .I]
    
    ## Within each processing block, for each Inspection_ID, 
    ## Calculate the KDE for that inspection based on citywide Lat / Lon values
    ## for whatever event. 
    ret <- rbindlist(lapply(II, function(ii) {
        foverlaps(    
            x = records[i = ii, 
                        j = list(i,
                                 longitude,
                                 latitude), 
                        keyby = list(start = date - daysback, 
                                     end = date)],
            y = events[i = TRUE, 
                       j = list(longitude, 
                                latitude),
                       keyby = list(start = point_date,  
                                    end = point_date)],
            type = "any")[ , kde(new = c(i.longitude[1], 
                                         i.latitude[1]), 
                                 x = longitude, 
                                 y = latitude, 
                                 h = h),
                           keyby = i]}))
    
    ## Set the names and return the object
    setnames(ret, "V1", "z")
    if(return_events){
        ret <- list(density = ret,
                    events = events)
    }
    return(ret)
}

if(FALSE){
    
    library(data.table)
    
    records <- data.table(
        longitude = c(-87.731235078, -87.693059543, -87.693651826, -87.740277214, -87.761542757, 
                      -87.66652414, -87.669419596, -87.654610973, -87.56818989, -87.688135036, 
                      -87.624629465, -87.689589095, -87.724955159, -87.732645515, -87.603824783, 
                      -87.654299927, -87.626793222, -87.69150611, -87.687521795, -87.644451656, 
                      -87.682996722, -87.703578041, -87.702663979, -87.624308522, -87.624308522, 
                      -87.624308522, -87.699341069, -87.65747936, -87.625172221, -87.681474804, 
                      -87.714251663, -87.736628651, -87.718210254, -87.70264255, -87.717980003, 
                      -87.726984399, -87.774462374, -87.724467782, -87.724467782, -87.62638079, 
                      -87.601856872, -87.601856872, -87.655650217, -87.697358123, -87.688215844, 
                      -87.631490891, -87.577299189, -87.696689974, -87.696689974, -87.625918972, 
                      -87.668458159, -87.663841654, -87.765187156, -87.703511284, -87.719800113, 
                      -87.728315296, -87.662148696, -87.707870449, -87.673563217, -87.694781495, 
                      -87.675980446, -87.666617078, -87.673924067, -87.732605444, -87.801058993, 
                      -87.728798601, -87.710662129, -87.710662129, -87.639293174, -87.674500719, 
                      -87.687208836, -87.703437404, -87.702976058, -87.65631439, -87.645922694, 
                      -87.702051287, -87.671947714, -87.671610797, -87.703218092, -87.702890933, 
                      -87.652873754, -87.609773874, -87.691898226, -87.62008844, -87.702950176, 
                      -87.6546978, -87.658921856, -87.713854071, -87.713854071, -87.651450825, 
                      -87.651450825, -87.651450825, -87.694753106, -87.739185418, -87.739185418, 
                      -87.663284759, -87.745532526, -87.703704392, -87.701753666, -87.662533658, 
                      -87.771193968, -87.703096673, -87.633117174, -87.746038713, -87.627312749, 
                      -87.627312749, -87.659072208, -87.624384671, -87.684133697, -87.684133697, 
                      -87.678412243, -87.745661762, -87.560035604, -87.688308108, -87.688308108, 
                      -87.627764549, -87.703224386, -87.702825432, -87.675772566, -87.713210503, 
                      -87.663877258, -87.703158746, -87.70310189, -87.771666516, -87.702385685, 
                      -87.663712101, -87.644556952, -87.781786942, -87.704450278, -87.559086946, 
                      -87.58584066, -87.681926113, -87.66868422, -87.701676888, -87.692557524, 
                      -87.667805662, -87.667805662, -87.592488565, -87.764617436, -87.721805662, 
                      -87.725552367, -87.667242678, -87.643904593, -87.627396122, -87.718843692, 
                      -87.716652328, -87.716652328, -87.693450119, -87.663858045, -87.694464363, 
                      -87.733025764, -87.638017712, -87.693456441, -87.672884037, -87.623585923, 
                      -87.6393041, -87.718632013, -87.609263741, -87.659139366, -87.563167175, 
                      -87.755057995, -87.695806048, -87.727199005, -87.723550636, -87.560274242, 
                      -87.722924191, -87.686157599, -87.600563843, -87.600563843, -87.557705515, 
                      -87.665082619, -87.611228488, -87.68256824, -87.648643426, -87.648643426, 
                      -87.696897253, -87.622096001, -87.622096001, -87.630891572, -87.629289514, 
                      -87.667088055, -87.68364762, -87.702662869, -87.708446646, -87.703511606, 
                      -87.777301232, -87.708157535, -87.764903935, -87.708285293, -87.78608407, 
                      -87.687364482, -87.60140141, -87.707384333, -87.713901331, -87.746350068, 
                      -87.727327519, -87.708188233, -87.714887231, -87.603904047, -87.663978055, 
                      -87.560610044, -87.684437422, -87.666342428, -87.694953595, -87.658021978, 
                      -87.736645893, -87.702479662, -87.687788426, -87.624739965, -87.707172399, 
                      -87.68099467, -87.606636416, -87.552358447, -87.63134009, -87.7953281, 
                      -87.680658959, -87.658965172, -87.689177655, -87.705945783, -87.716444876, 
                      -87.599751114, -87.657744972, -87.707489169, -87.698405198, -87.735029364, 
                      -87.735029364, -87.643451823),
        latitude = c(41.898826269, 41.793593809, 41.786296014, 41.778563244, 41.777268673, 
                     41.866254176, 41.857856568, 41.96257634, 41.751817802, 41.824112854, 
                     41.885109501, 41.779062858, 41.853589475, 41.793008028, 41.751412847, 
                     41.75038047, 41.88196533, 41.997814396, 41.924791907, 41.772520371, 
                     41.750191728, 41.799370483, 41.764298991, 41.884540111, 41.884540111, 
                     41.884540111, 41.917358545, 41.85711226, 41.677886324, 41.697238209, 
                     41.778684716, 41.924254896, 41.917155379, 41.75335698, 41.778842458, 
                     41.917050893, 41.777915319, 41.846039823, 41.846039823, 41.836371827, 
                     41.751214349, 41.751214349, 41.881534355, 41.778947962, 41.950427126, 
                     41.910117945, 41.758973539, 41.90115303, 41.90115303, 41.885837688, 
                     41.854219374, 41.768662296, 41.887109173, 41.73527409, 41.953854139, 
                     41.866017149, 41.892599705, 41.946520533, 41.853935514, 41.73542821, 
                     41.89603921, 41.925282314, 41.830515003, 41.9169782, 41.792056479, 
                     41.898886005, 41.866237769, 41.866237769, 41.86714316, 42.012704371, 
                     41.89926516, 41.794383423, 41.778421336, 41.925443779, 41.735930625, 
                     41.742474883, 41.896105731, 41.896111467, 41.786904716, 41.735283742, 
                     41.958459512, 41.751314577, 41.801134224, 41.758208445, 41.777191576, 
                     41.857872895, 41.768715735, 41.749454364, 41.749454364, 41.858133216, 
                     41.858133216, 41.858133216, 41.778986629, 41.931520901, 41.931520901, 
                     41.750212217, 41.885851559, 41.793615387, 41.691664924, 41.721326987, 
                     41.777757539, 41.78256739, 41.911272068, 41.898729831, 41.856163133, 
                     41.856163133, 41.965489801, 41.886567371, 41.791884715, 41.791884715, 
                     41.852025692, 41.895053985, 41.751704509, 41.953610907, 41.953610907, 
                     41.884524362, 41.777935762, 41.77184388, 41.854911319, 41.691483413, 
                     41.808509095, 41.773543946, 41.78275257, 41.902163615, 41.744230973, 
                     41.857945167, 41.765109432, 41.777778359, 41.997366867, 41.751937934, 
                     41.739027185, 41.997975012, 41.85786568, 41.779102728, 41.997793234, 
                     41.830365901, 41.830365901, 41.751529418, 41.777886798, 41.741544634, 
                     41.873017269, 41.899606263, 41.750989852, 41.872391868, 41.902625299, 
                     41.953912901, 41.953912901, 41.779005046, 41.869193691, 41.812001745, 
                     41.793006478, 41.911013149, 41.779224532, 42.019494002, 41.707273582, 
                     41.864521194, 41.931781642, 41.751104138, 41.837978928, 41.702656739, 
                     41.876547875, 41.88119124, 41.934420356, 41.778538629, 41.744429259, 
                     41.779253606, 41.866501191, 41.751226288, 41.751226288, 41.761305471, 
                     41.922259676, 41.751070974, 41.706267847, 41.7365436, 41.7365436, 
                     41.778973883, 41.765680738, 41.765680738, 41.721517344, 41.883824213, 
                     41.885434986, 41.764801894, 41.939253922, 41.962960557, 41.797123381, 
                     41.777641173, 41.96310062, 41.960619553, 41.946094812, 41.938206965, 
                     41.691836286, 41.751217934, 41.935791573, 41.741281599, 41.916821119, 
                     41.778492907, 41.961296921, 41.8424589, 41.751192374, 41.95096537, 
                     41.744425096, 41.772071935, 41.750211231, 41.793560993, 41.841484039, 
                     41.953654349, 41.747615838, 41.779092685, 41.764573633, 41.79357135, 
                     41.855732733, 41.80598255, 41.744535937, 41.816394534, 41.930995823, 
                     41.853825083, 41.856135964, 41.800961821, 41.793589179, 41.953915877, 
                     41.77330897, 41.856202221, 41.931920016, 41.808321987, 41.778624993, 
                     41.778624993, 41.81618611),
        date = structure(c(16617L, 16624L, 16623L, 16629L, 16622L, 16617L, 16618L, 
                           16636L, 16639L, 16630L, 16631L, 16628L, 16626L, 16631L, 
                           16623L, 16618L, 16630L, 16631L, 16630L, 16622L, 16631L, 
                           16628L, 16628L, 16625L, 16631L, 16632L, 16630L, 16623L, 
                           16617L, 16624L, 16622L, 16638L, 16626L, 16628L, 16617L, 
                           16626L, 16625L, 16625L, 16626L, 16617L, 16624L, 16624L, 
                           16629L, 16629L, 16631L, 16631L, 16633L, 16618L, 16626L, 
                           16618L, 16624L, 16632L, 16630L, 16631L, 16623L, 16623L, 
                           16633L, 16618L, 16618L, 16631L, 16631L, 16631L, 16625L, 
                           16626L, 16629L, 16617L, 16630L, 16631L, 16629L, 16631L, 
                           16630L, 16631L, 16623L, 16631L, 16622L, 16617L, 16629L, 
                           16629L, 16628L, 16623L, 16622L, 16624L, 16623L, 16630L, 
                           16628L, 16623L, 16632L, 16623L, 16631L, 16624L, 16630L, 
                           16631L, 16623L, 16626L, 16630L, 16632L, 16633L, 16624L, 
                           16624L, 16632L, 16629L, 16625L, 16630L, 16630L, 16630L, 
                           16631L, 16631L, 16631L, 16618L, 16628L, 16623L, 16617L, 
                           16617L, 16631L, 16636L, 16630L, 16629L, 16629L, 16622L, 
                           16624L, 16622L, 16630L, 16625L, 16625L, 16628L, 16624L, 
                           16632L, 16625L, 16631L, 16617L, 16639L, 16636L, 16618L, 
                           16629L, 16636L, 16617L, 16630L, 16618L, 16623L, 16631L, 
                           16617L, 16622L, 16618L, 16630L, 16625L, 16623L, 16638L, 
                           16622L, 16630L, 16630L, 16630L, 16630L, 16617L, 16638L, 
                           16637L, 16629L, 16618L, 16631L, 16624L, 16639L, 16630L, 
                           16633L, 16633L, 16622L, 16631L, 16617L, 16631L, 16624L, 
                           16633L, 16624L, 16631L, 16631L, 16630L, 16622L, 16637L, 
                           16623L, 16624L, 16632L, 16632L, 16630L, 16630L, 16637L, 
                           16631L, 16630L, 16625L, 16623L, 16638L, 16630L, 16618L, 
                           16630L, 16624L, 16618L, 16623L, 16631L, 16626L, 16622L, 
                           16631L, 16630L, 16623L, 16631L, 16631L, 16630L, 16632L, 
                           16631L, 16624L, 16626L, 16617L, 16623L, 16624L, 16630L, 
                           16617L, 16633L, 16633L, 16625L, 16630L, 16618L, 16623L, 
                           16623L, 16630L, 16618L, 16633L, 16623L, 16618L, 16630L, 
                           16629L, 16631L, 16625L),
                         class = c('IDate', 'Date')))
    
    
    
    dens <- sdpFeatureFetcher::getDensity(records = records, 
                                          datasetname = "crimes_2001_to_present", 
                                          filter = "primary_type=BATTERY",
                                          daysback = 365,
                                          h = .01,
                                          return_events = TRUE)
    records
    dens
    records[['dens']] <- dens[['density']][['z']]
}
Chicago/sdp-feature-fetcher documentation built on May 6, 2019, 10:54 a.m.