The goal of this vignette is to present the differences between the three Network Kernel Density Estimation methods implemented in the package **spNetwork**. They are presented with 3D rendering to get a better understanding of their differences.

For a more detailed description, the reader should read the provided references.

Let us start with the definition of a simple situation with one event and a small network. The spatial coordinates are given in metres.

```
options(rgl.useNULL=TRUE)
library(spNetwork)
library(rgeos)
library(sp)
library(maptools)
library(rgl)
library(FNN)
# we define a set of lines
<- c(
wkt_lines "LINESTRING (0 0, 1 0)", "LINESTRING (1 0, 2 0)", "LINESTRING (2 0, 3 0)",
"LINESTRING (0 1, 1 1)", "LINESTRING (1 1, 2 1)", "LINESTRING (2 1, 3 1)",
"LINESTRING (0 2, 1 2)", "LINESTRING (1 2, 2 2)", "LINESTRING (2 2, 3 2)",
"LINESTRING (0 3, 1 3)", "LINESTRING (1 3, 2 3)", "LINESTRING (2 3, 3 3)",
"LINESTRING (0 0, 0 1)", "LINESTRING (0 1, 0 2)", "LINESTRING (0 2, 0 3)",
"LINESTRING (1 0, 1 1)", "LINESTRING (1 1, 1 2)", "LINESTRING (1 2, 1 3)",
"LINESTRING (2 0, 2 1)", "LINESTRING (2 1, 2 2)", "LINESTRING (2 2, 2 3)",
"LINESTRING (3 0, 3 1)", "LINESTRING (3 1, 3 2)", "LINESTRING (3 2, 3 3)"
)
# and create a spatial lines dataframe
<- data.frame(wkt = wkt_lines,
linesdf id = paste("l",1:length(wkt_lines),sep=""))
<- do.call(rbind,lapply(1:nrow(linesdf),function(i){
geoms <- as.character(linesdf[i,]$wkt)
txt <- rgeos::readWKT(txt,id=i)
geom return(geom)
}))
<- sp::SpatialLinesDataFrame(geoms, linesdf,match.ID = F)
all_lines
# we define now one event
<- data.frame(x=c(1),
event y=c(1.5))
::coordinates(event) <- cbind(event$x,event$y)
sp
# and map the situation
par(mar=c(0.1,0.1,0.1,0.1))
::plot(all_lines)
sp::plot(event,add=T,col="red",pch = 19) sp
```

The first method was proposed by Xie and Yan (2008). The differences with a classical KDE are:

- the events are snapped on a network
- the distances between sampling points and events are network distances (i.e.Â not Euclidean distance)

To visualize the result, we calculate here the density values and plot them in 3D. We sample the kernel values at each one centimetres on the lines.

```
<- lines_points_along(all_lines,0.01)
samples_pts
<- nkde(all_lines, event, w = 1,
simple_kernel samples = samples_pts, kernel_name = "quartic",
bw = 2, method = "simple", div = "bw",
digits = 3, tol = 0.001, grid_shape = c(1,1),
check = FALSE,
verbose = FALSE)
```

Let us see this in 3D !

`d3_plot_situation(all_lines, event, samples_pts, simple_kernel, scales = c(1,1,3))`

(The function used to display the 3d plot is available in the source code of the vignette)

As one can see, NKDE is only determined by the distance between the sampling points and the event. This is a problem, because at intersections, the eventâ€™s mass is multiplied by the number of edges at that intersection. In more mathematical words, this is not a true kernel because it does not integrate to 1 on its domain.

This method remains useful for two reasons:

- With big datasets, it might be useful to use this simple method to do a primary investigation (quick calculation).
- In a purely geographical view, this method is intuitive. In the case of crime analysis for example, one could argue that the
*strength*of the event should not be affected by intersections on the network. In that case, the kernel function can be seen as a distance decaying function.

**What to retain about the simple method**

Pros | Cons |
---|---|

quick to calculate | biased (not a true kernel) |

intuitive | overestimate the densities |

continuous |

To overcome the limits of the previous method, Okabe and Sugihara (2012) have proposed the discontinuous NKDE, which has been extended by Sugihara, Satoh, and Okabe (2010) for cases where the network has cycles shorter than the bandwidth.

```
<- nkde(all_lines, event, w = 1,
discontinuous_kernel samples = samples_pts, kernel_name = "quartic",
bw = 2, method = "discontinuous", div = "bw",
digits = 3, tol = 0.001, grid_shape = c(1,1),
check = FALSE,
verbose = FALSE)
```

`d3_plot_situation(all_lines, event, samples_pts, discontinuous_kernel, scales = c(1,1,3))`

As one can see, the values of the NKDE are split at intersections to avoid the multiplication of the mass observed in the simple version. However, this NKDE is discontinuous, which is counter-intuitive, and leads to sharp differences between density values in the network, and could be problematic in networks with numerous and close intersections.

**What to retain about the discontinuous method**

Pros | Cons |
---|---|

quick to calculate | counter-intuitive |

unbiased | discontinuous |

contrasted results |

Finally, the continuous method takes the best of the two worlds: it adjusts the values of the NKDE at intersections to ensure that it integrates to one on its domain, and applies a backward correction to force the density values to be continuous. This process is accomplished by a recursive function, described in the book *Spatial Analysis Along Networks* (Okabe and Sugihara 2012). This function is very time consuming, so it might be necessary to stop it when the recursion is too deep. Considering that the kernel density is divided at each intersection, stopping the function at deep level 15 produces results that are almost equal to the true values.

```
<- nkde(all_lines, event, w = 1,
continuous_kernel samples = samples_pts, kernel_name = "quartic",
bw = 2, method = "continuous", div = "bw",
digits = 3, tol = 0.001, grid_shape = c(1,1),
check = FALSE,
verbose = FALSE)
```

`d3_plot_situation(all_lines, event, samples_pts, continuous_kernel, scales = c(1,1,3))`

As one can see, the values of the NKDE are continuous, and the density values close to the events have been adjusted. This method produces smoother results than the discontinuous method.

**what to retain about the discontinuous method**

Pros | Cons |
---|---|

unbiased | long calculation time |

continuous | smoother values |

If we add a second event, their mass will add up when they share some range.

```
# we define now two events
<- data.frame(x=c(1,2),
events y=c(1.5,1.5))
::coordinates(events) <- cbind(events$x,events$y)
sp
<- nkde(all_lines, events, w = 1,
continuous_kernel samples = samples_pts, kernel_name = "quartic",
bw = 1.3, method = "continuous", div = "bw",
digits = 3, tol = 0.001, grid_shape = c(1,1),
check = FALSE,
verbose = FALSE)
```

`d3_plot_situation(all_lines, events, samples_pts, continuous_kernel, scales = c(1,1,3))`

The larger the bandwidth, the more common links the events share in their range.

```
<- nkde(all_lines, events, w = 1,
continuous_kernel samples = samples_pts, kernel_name = "quartic",
bw = 1.6, method = "continuous", div = "bw",
digits = 3, tol = 0.001, grid_shape = c(1,1),
check = FALSE,
verbose = FALSE)
d3_plot_situation(all_lines, events, samples_pts, continuous_kernel, scales = c(1,1,3))
```