knitr::opts_chunk$set( collapse = TRUE, fig.width=6, fig.height=4, comment = "#>" )
The simulator software is a framework to run mobile network data micro-simulations [1]. In this vignette, we describe how the results can be plotted with the R package mobvis. The package can be loaded in the standard way.
library(mobvis)
In this vignette, we will also use tmap [2] to explore spatial objects in general. In the following code chunk, the tmap package is loaded, and via a global tmap option, warnings raised by tmap are suppress for plotting fictional spatial objects, i.e. that do not have a real geographic location. For this vignette, the github development version is required. This can be installed via remotes::install_github("mtenneks/tmap")
.
library(tmap) tmap_options(show.warnings = FALSE)
Example data for the simulator, both the input and the output files, are contained in this package as a single zip file. It can be retrieved and extracted as follows.
# specify folder for the simulator files dir <- "simdata" if (!dir.exists(dir)) dir.create(dir, recursive = TRUE) else unlink(dir, recursive = TRUE) # unzip the zip file into that folder zipfile <- system.file("sim", "simdata.zip", package = "mobvis") unzip(zipfile, exdir = dir)
In this case, the files will be located in the "simdata"
subfolder of the working directory. The input files are stored in that folder, and the output files in the subfolder "simdata/output"
.
In order to use this simulator data in mobvis, we have to create a list object that contains a few settings:
sim <- list(input_dir = "simdata", output_dir = "simdata/output", mno = "MNO1", crs = sf::st_crs(NA))
Besides the input and output directories, it also contains the Mobile Network Operator (MNO) name, in this case "MNO1"
, and a CRS (coordinate reference system). The latter is needed to project the spatial data on the surface of the earth. If the CRS is missing (such as in this example), the coordinate units are still considered to be meters. However, since there is no information about the projection itself, it is not possible to use basemaps in interactive maps.
The following code is used to load the simulator data into R. Note that all the functions in the mobvis package that do this have a sim_get_
prefix.
rst <- sim_get_raster(sim) cp <- sim_get_cellplan(sim) region <- sim_get_region(sim)
The object rst
is a raster object that contains the tile id numbers. In correspondence with [1], the numbering goes from left to right and then bottom to top, with 0 being the number for the bottom right tile.
The object cp
is the cellplan, which is an sf
object that contains the locations of the cells, and physical properties such as the power.
The object region
is an sf
object that contains the borders of the region of interest.
In mobvis the visualization settings can be retrieved, and modified via mobvis_settings()
. It a function that generates a list of visualization settings. An overview of these settings can be seen by looking at the structure with str()
:
str(mobvis_settings())
All these settings can easily be changed, as we will demonstrate in the next examples. The visualization settings are described in the package documentation. The default settings are aimed for static maps. There are also default settings for interactive maps and for animations, which can be called via the functions mobvis_settings_interactive()
and mobvis_settings_animation()
respectively.
The following function reads the signal strength and signal dominance data into R:
strength <- sim_get_signal_strength(sim, rst, cp) print(strength)
The columns of strength
are: cell id number cell
, tile number rid
, signal strength dBm
, and signal dominance s
.
The maps are created in the same way as with mobloc data as described in the other vignette.
map_sig_strength(rst, dt = strength, cp = cp, region = region, cells = 3, type = "dBm", interactive = FALSE, settings = mobvis_settings(cell_size = 1, cell_labels = TRUE)) map_sig_strength(rst, dt = strength, cp = cp, region = region, cells = 3, type = "s", interactive = FALSE, settings = mobvis_settings(cell_colors = "gold", cell_size = 1, cell_labels = TRUE, cell_label_color = "white")) map_best_server(rst = rst, dt = strength, cp = cp, region = region, cells = 3, type = "bsm", interactive = FALSE, settings = mobvis_settings(cell_size = 1, cell_labels = TRUE))
The last map, the Best Server Map shows the cell with the best signal strength per tile. Often, this is the nearest cell, but not necessarily.
The function map_pag
visualizes the event location. The event location (likelihood) probabilities are calculated with mobloc (see other vignette).
library(mobloc) param <- mobloc_param() strength[, dist:= NA] strength_llh <- create_strength_llh(strength, param = param) print(strength_llh)
The mobloc requires the signal strength object strength with an additional column dist
, which stands for distance between tile center and cell location. However, since we do not use dist
for viusalization purposes, we set it to NA
. The data.table
object strength_llh
has four columns cell id number cell
, tile number rid
, distance dist
(not used), and event location (likelihood) probability pag
.
In the next code chunk, we set the mobvis parameters, and create the map in the same way as before.
# Retrieve and change the visualization settings settings = mobvis_settings(cell_size = 2, dev_size = 2) settings$titles["pag"] <- "Event location" # Create the plot map_pag(rst, dt = strength_llh, cp = cp, region = region, cells = 3, interactive = FALSE, settings = settings)
The following function visualizes the probability location. The probabilities are also calculated with mobloc. The network prior is used.
# Calculate the event location (likelihood) probabilities using mobloc network_prior <- create_network_prior(strength, rst) posterior <- calculate_posterior(prior = network_prior, llh = strength_llh, raster = rst) print(posterior)
The data.table
object posterior
has three columns, cell id cell, tile number
rid, and posterior probability
pga`.
The map is created as follows:
# Edit settings settings$titles["pga"] <- "Posterior probabilities" # Create the plot map_pga(rst, dt = posterior, cp = cp, region = region, cells = 3, interactive = FALSE, settings = settings)
The simulator simulates trajectories of devices. The following code retrieves the routes and calculates the traveled distance per device.
# get route and distance per device (routes <- sim_get_trajectory_routes(sim)) # plot the routes with tmap (run tmap_mode("view") to show it interactively) qtm(region, fill= NULL) + qtm(routes)
Interactive exploration of this map allows us to focus on particular devices that are interesting for further analysis. For instance, the route of device 250 is interesting, since it moved at a steady pace and direction within the south-west part of the region, which has a high density of cells, including one antenna that contains three directional cells.
The following code chunk creates an animation of the event locations of the cells to which device 250 is connected. Both gif and mp4 animations can be created. The former generally creates sharper images, but is much larger in file size.
traj <- sim_get_trajectory_data(sim, device = 250) settings = mobvis_settings_animation(cell_size = 2, dev_size = 2) animate_pag(rst = rst, dt = strength_llh, cp = cp, traj = traj, region = region, settings = settings, title = "Event location at time %s", filename = "event_locations_dev_%s.mp4")
The %s
in the title and filename are placements for the time and device id respectively. In this case event_locations_dev_250.mp4
will be generated.
In a similar way, an animation for the posterior probabilities is created as follows.
animate_pga(rst = rst, dt = posterior, cp = cp, traj = traj, region = region, settings = settings, title = "Posterior distribution at time %s", filename = "posterior_dev_%s.mp4")
One of the output files of the simulator is called \code{"probabilities_network_MNO1.csv"}. Since this file is very large, 539 MB, only the probabilities for three devices, including 250, have been included in this package.
prob <- sim_get_prob(sim, device = 250) animate_p(rst = rst, dt = prob, cp = cp, traj = traj, region = region, settings = settings, title = "HMM probability distribution at time %s", filename = "HMM_probabilities_dev_%s.mp4")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.