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:

- Calculating the NKDE density of accidents at each accident point
- Snapping the accidents to the network
- Cutting the lines of the network at the snapped accidentsâ€™ locations
- Building a graph with the cut lines
- Calculating for each node of the graph its centrality (betweenness)
- Calculating the correlation between the NKDE density and the centrality measure.

Let us start by loading some data.

```
# first load data and packages
library(sf)
library(spNetwork)
library(tmap)
library(dbscan)
data(mtl_network)
data(bike_accidents)
# then plotting the data
tm_shape(mtl_network) +
tm_lines("black") +
tm_shape(bike_accidents) +
tm_dots("red", size = 0.2)
```

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
<- nkde(mtl_network,
densities 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)
$density <- densities * 1000 bike_accidents
```

```
# mapping the density values
tm_shape(mtl_network) +
tm_lines(col = "black") +
tm_shape(bike_accidents) +
tm_dots(col = "density", style = "kmeans",
n = 6, size = 0.1, palette = "-RdYlBu")+
tm_layout(legend.outside = TRUE)
```

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.

```
$weight <- 1
bike_accidents<- aggregate_points(bike_accidents, maxdist = 5)
agg_points
$OID <- 1:nrow(agg_points)
agg_points$LineID <- 1:nrow(mtl_network)
mtl_network
<- snapPointsToLines2(agg_points,
snapped_accidents
mtl_network,"LineID")
```

The next step is to use the new points to cut the lines of the network.

```
<- split_lines_at_vertex(mtl_network,
new_lines
snapped_accidents,$nearest_line_id,
snapped_accidentsmindist = 0.1)
```

We can now build the graph from the cut lines.

```
$OID <- 1:nrow(new_lines)
new_lines$length <- as.numeric(st_length(new_lines))
new_lines
<- build_graph(new_lines, 2, "length", attrs = TRUE) graph_result
```

The graph can be used with the library **igraph**. We
will calculate here the betweenness centrality of each node in the
graph.

```
<- igraph::betweenness(graph_result$graph, directed = FALSE,
btws normalized = TRUE)
<- graph_result$spvertices
vertices $btws <- btws vertices
```

```
# mapping the betweenness
tm_shape(vertices) +
tm_dots(col = "btws", style = "kmeans",
n = 6, size = 0.1, palette = "-RdYlBu")+
tm_layout(legend.outside = TRUE)
```

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
<- st_coordinates(snapped_accidents)
xy1 <- st_coordinates(vertices)
xy2 <- dbscan::kNN(x = xy2, query = xy1, k=1)$id
corr_nodes
$btws <- vertices$btws[corr_nodes]
snapped_accidents
# second: nn merging between original points and snapped points
<- st_coordinates(bike_accidents)
xy1 <- st_coordinates(snapped_accidents)
xy2
<- dbscan::kNN(x = xy2, query = xy1, k=1)$id
corr_nodes $btws <- snapped_accidents$btws[corr_nodes] bike_accidents
```

```
# mapping the results
tm_shape(bike_accidents) +
tm_dots(col = "btws", style = "kmeans",
n = 6, size = 0.1, palette = "-RdYlBu")+
tm_layout(legend.outside = TRUE)
```

```
tm_shape(bike_accidents) +
tm_dots(col = "density", style = "kmeans",
n = 6, size = 0.1, palette = "-RdYlBu")+
tm_layout(legend.outside = TRUE)
```

And finally, we can calculate the correlation between the two variables !

`cor.test(bike_accidents$density, bike_accidents$btws)`

```
##
## 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.