The goal of this vignette is to present how several functions in spNetwork can be used to build graphs and to analyze it.
To illustrate the process, we give a full example with the calculation of the correlation between the centrality of bike accidents in the Montreal network and the NKDE density calculated at each accident point. Here are the steps we will follow to perform this analysis:
Let us start by loading some data.
This step is easy to perform with the basic functions of spNetwork. For the sake of simplicity, we select an arbitrary bandwidth of 300m and use the discontinuous kernel.
# calculating the density values densities <- nkde(mtl_network, events = bike_accidents, w = rep(1,nrow(bike_accidents)), samples = bike_accidents, kernel_name = "quartic", bw = 300, div= "bw", method = "discontinuous", digits = 2, tol = 0.5, grid_shape = c(1,1), max_depth = 8, agg = 5, sparse = TRUE, verbose = FALSE) bike_accidents$density <- densities * 1000
For this step, we will use the function
snapPointsToLines2. It is mainly based on the function
snapPointsToLines from maptools but can be used for bigger datasets. Note that we create two index columns:
OID for the accidents’ location and
LineID for the network lines.
We will also start by aggregating the points that are too close to each other. We will aggregate all the points that are within a 5 metres radius.
The next step is to use the new points to cut the lines of the network.
new_lines <- split_lines_at_vertex(mtl_network, snapped_accidents, snapped_accidents$nearest_line_id, mindist = 0.1)
We can now build the graph from the cut lines.
The graph can be used with the library igraph. We will calculate here the betweenness centrality of each node in the graph.
btws <- igraph::betweenness(graph_result$graph, directed = FALSE, normalized = TRUE) vertices <- graph_result$spvertices vertices$btws <- btws
The last step is to find for each of the original points its corresponding node. We will do it by using the k nearest neighbours approach with the package FNN.
# first: nn merging between snapped points and nodes xy1 <- st_coordinates(snapped_accidents) xy2 <- st_coordinates(vertices) corr_nodes <- dbscan::kNN(x = xy2, query = xy1, k=1)$id snapped_accidents$btws <- vertices$btws[corr_nodes] # second: nn merging between original points and snapped points xy1 <- st_coordinates(bike_accidents) xy2 <- st_coordinates(snapped_accidents) corr_nodes <- dbscan::kNN(x = xy2, query = xy1, k=1)$id bike_accidents$btws <- snapped_accidents$btws[corr_nodes]
And finally, we can calculate the correlation between the two variables !
## ## Pearson's product-moment correlation ## ## data: bike_accidents$density and bike_accidents$btws ## t = NA, df = 345, p-value = NA ## alternative hypothesis: true correlation is not equal to 0 ## 95 percent confidence interval: ## NA NA ## sample estimates: ## cor ## NA
We can see that there is no correlation between the two variables. There is no association between the degree of centrality of an accident location in the network and the density of accidents in a radius of 300m at that location.