Skip to content

Commit eb830d4

Browse files
authored
Merge pull request #332 from 4DModeller/328-joss-review-documentation
Functionality documentation
2 parents 1c1397b + 4a1c279 commit eb830d4

32 files changed

+157
-154
lines changed

R/meshing.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# This function taken from https://groups.google.com/g/r-inla-discussion-group/c/z1n1exlZrKM/m/8vYNr2D8BwAJ
22
#' Convert an INLA mesh to a SpatialPolygonsDataFrame
33
#'
4-
#' @param mesh Mesh
5-
#' @param crs Coordinate Reference System as proj4string
4+
#' @param mesh Mesh object from INLA or fmesher (only R2 manifolds are valid)
5+
#' @param crs Coordinate Reference System as proj4string, does not support geocentric coordinates
66
#'
7-
#' @return SpatialPolygonsDataFrame
7+
#' @return SpatialPolygonsDataFrame with mesh triangles
88
#' @export
99
mesh_to_spatial <- function(mesh, crs) {
1010
if (!is.character(crs)) {

R/meshing_checks.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#' 2. A greater number of mesh triangles than observations
44
#' 3. Isolated triangles in mesh
55
#'
6-
#' @param mesh INLA mesh
7-
#' @param observations Observations data to be used woith model
6+
#' @param mesh INLA/fmesher mesh
7+
#' @param observations Observations data to be used with model (can be data.frame, SpatialPoints/SpatialPointsDataFrame, or simple feature collection)
88
#'
99
#' @return list: Named list containing errors
1010
#' @export

R/model_parse.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#' Parses inlabru::bru model output to create a list of model parameters
22
#'
33
#' @param model_output Output from running inlabru::bru
4-
#' @param measurement_data Measurement data
4+
#' @param measurement_data Measurement data (can be data.frame, SpatialPoints/SpatialPointsDataFrame, or simple feature collection)
55
#'
6-
#' @return list
6+
#' @return Creates a list of model outputs for the first timestep, including fitted mean and sd, mean of fixed effects, mean and sd of random effects, posterior hyperparameters, and DIC
77
#' @keywords internal
88
parse_model_output_bru <- function(model_output, measurement_data) {
99
fitted_mean_post <- model_output$summary.fitted.values$mean[seq_len(nrow(measurement_data))]
@@ -33,11 +33,11 @@ parse_model_output_bru <- function(model_output, measurement_data) {
3333

3434
#' Parse model output to create a list of model parameters
3535
#'
36-
#' @param model_output Data returned by model
37-
#' @param measurement_data Measurement data
38-
#' @param model_type Type of model, we currently support inlabru
36+
#' @param model_output Output returned by model (currently only from running inlabru::bru)
37+
#' @param measurement_data Measurement data (can be data.frame, SpatialPoints/SpatialPointsDataFrame, or simple feature collection)
38+
#' @param model_type Type of model, we currently only support inlabru
3939
#'
40-
#' @return list
40+
#' @return Creates a list of model outputs, including fitted mean and sd, mean of fixed effects, mean and sd of random effects, posterior hyperparameters, and DIC
4141
#' @export
4242
parse_model_output <- function(model_output, measurement_data, model_type = "inlabru") {
4343
if (model_type == "inlabru") {
@@ -48,13 +48,13 @@ parse_model_output <- function(model_output, measurement_data, model_type = "inl
4848

4949
#' Create a prediction field from the parsed model output and the mesh
5050
#'
51-
#' @param mesh INLA mesh
52-
#' @param plot_type Type of plot to create, "predicted_mean_fields" etc
53-
#' @param data_dist Type of data, "poisson" etc
54-
#' @param var_a Data for variable a, required for "predicted_mean_fields" and "random_effect_fields"
55-
#' @param var_b Data for variable b, required for "predicted_mean_fields"
51+
#' @param mesh INLA/fmesher mesh
52+
#' @param plot_type Type of plot to create, "predicted_mean_fields" or "random_mean_fields"
53+
#' @param data_dist Type of data, "poisson" or "gaussian"
54+
#' @param var_a Data for variable a (numeric), required for "predicted_mean_fields" and "random_effect_fields"; normally, random effect field from "summary.random$f"
55+
#' @param var_b Data for variable b (numeric), required for "predicted_mean_fields"; normally, fixed effect from "summary.fixed"
5656
#'
57-
#' @return data.frame
57+
#' @return data.frame with mesh node latlong coordinates (x, y) and the selected field values at the mesh nodes (z)
5858
#' @export
5959
create_prediction_field <- function(mesh,
6060
plot_type = "predicted_mean_fields",

R/plot_graph.R

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#' Plot a bar chart using ggplot2
22
#'
3-
#' @param data Data to plot
4-
#' @param x x-axis data
3+
#' @param data Data to plot (all data types recognised by ggplot2::geom_bar() like data.frame)
4+
#' @param x x-axis data, must be a Date object
55
#' @param y y-axis data
6-
#' @param breaks Break points
6+
#' @param breaks Break points, must be a sequence of dates
77
#' @param x_label x-axis label
88
#' @param y_label y-axis label
99
#' @param fill Fill colour
@@ -47,10 +47,10 @@ plot_barchart <- function(data, x, y, breaks, x_label, y_label, fill = "pink", c
4747

4848
#' Plot a boxplot using ggplot2
4949
#'
50-
#' @param data Data to plot
51-
#' @param x x-axis data
50+
#' @param data Data to plot (all data types recognised by ggplot2::geom_boxplot() like data.frame)
51+
#' @param x x-axis data, must be a Date object
5252
#' @param y y-axis data
53-
#' @param breaks Break points
53+
#' @param breaks Break points, must be a sequence of dates
5454
#' @param x_label x-axis label
5555
#' @param y_label y-axis label
5656
#'
@@ -82,19 +82,19 @@ plot_boxplot <- function(data, x, y, breaks, x_label, y_label) {
8282

8383
#' Plot timeseries data
8484
#'
85-
#' @param data Data to plot
86-
#' @param x Name of column to plot on x-axis, should be datetime, will be converted to Dates using as.Date
85+
#' @param data Data to plot (data.frame with x-axis column a character convertible to Date)
86+
#' @param x Name of column to plot on x-axis
8787
#' @param y Name of column to plot on y-axis
88-
#' @param breaks Date break points
88+
#' @param breaks Date break points, must be a sequence of dates
8989
#' @param x_label x-axis label
9090
#' @param y_label y-axis label
9191
#' @param title Figure title
9292
#' @param line_width Line width
9393
#' @param line_colour Line colour
9494
#' @param horizontal_y y-intercept for horizontal line
9595
#' @param vertical_x x-intercept for vertical line
96-
#' @param x_lim Limits for x-axis continous scale, vector passed to scale_x_continuous
97-
#' @param y_lim Limits for y-axis continuous scale, vector passed to scale_y_continous
96+
#' @param x_lim Limits for x-axis continuous scale, vector passed to scale_x_continuous
97+
#' @param y_lim Limits for y-axis continuous scale, vector passed to scale_y_continuous
9898
#'
9999
#' @return ggplot
100100
#' @export
@@ -172,12 +172,12 @@ plot_timeseries <- function(data,
172172

173173
#' Create a line plot with a confidence interval.
174174
#'
175-
#' @param data Data to plot
176-
#' @param x x-axis data
175+
#' @param data Data to plot (all data types recognised by ggplot2::geom_line() like data.frame)
176+
#' @param x x-axis data, must be a Date object
177177
#' @param y1 y1 line data - solid
178178
#' @param y2 y2 line data - dashed
179179
#' @param y3 y3 line data - dashed
180-
#' @param breaks Breaks vector
180+
#' @param breaks Breaks vector, must be a sequence of dates
181181
#' @param x_label x-axis label
182182
#' @param y_label y-axis label
183183
#' @param y1_colour Colour for y1

R/plot_mapping.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#' The leaflet backend will work for most use cases and is recommended.
77
#' For plotting of maps with UTM coordinates, the mapview backend is recommended.
88
#'
9-
#' @param polygon_data Polygon data
10-
#' @param raster_data Raster datas
11-
#' @param domain Domain data to be passed to leaflet::colorNumeric and leaflet::addLegend
12-
#' @param markers Markers to display on map. A named list with latitude, longitude and label names must be given.
9+
#' @param polygon_data Polygon data (SpatialPolygon or SpatialPolygonDataFrame)
10+
#' @param raster_data Raster data (RasterLayer or SpatialRaster)
11+
#' @param domain Domain data (numeric or factor) to be passed to leaflet::colorNumeric and leaflet::addLegend
12+
#' @param markers Markers to display on map. A named list or data.frame with latitude, longitude and label names must be given.
1313
#' @param palette Palette to be used for colours, defaults to viridis
1414
#' @param legend_title Title for legend
1515
#' @param add_scale_bar Add scale bar if TRUE
@@ -71,10 +71,10 @@ plot_map <- function(polygon_data = NULL,
7171

7272
#' Create a simple Leaflet map from data
7373
#'
74-
#' @param polygon_data Polygon data
75-
#' @param raster_data Raster data
76-
#' @param domain Domain data to be passed to leaflet::colorNumeric and leaflet::addLegend
77-
#' @param markers Markers to display on map. A named list with latitude, longitude and label names must be given.
74+
#' @param polygon_data Polygon data (SpatialPolygon or SpatialPolygonDataFrame)
75+
#' @param raster_data Raster data (RasterLayer or SpatialRaster)
76+
#' @param domain Domain data (numeric or factor) to be passed to leaflet::colorNumeric and leaflet::addLegend
77+
#' @param markers Markers to display on map. A named list or data.frame with latitude, longitude and label names must be given.
7878
#' @param palette Palette to be used for colours, defaults to viridis
7979
#' @param legend_title Title for legend
8080
#' @param add_scale_bar Add scale bar if TRUE

R/plot_mesh.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#' Plot a mesh on a Leaflet map
22
#'
3-
#' @param mesh Mesh data
3+
#' @param mesh Mesh object from INLA or fmesher
44
#' @param spatial_data Spatial data, either a SpatialPolygonsDataFrame, SpatialPointsDataFrame or an object
55
#' that can be converted to a data.frame with longitude and latitude columns
6-
#' @param longitude_column Longitude column in spatial_data
7-
#' @param latitude_column Latitude column in spatial_data name
6+
#' @param longitude_column Longitude column name in spatial_data
7+
#' @param latitude_column Latitude column name in spatial_data
88
#' @param markers Markers to display on top of mesh. A named list with latitude, longitude and label names must be given.
99
#' Expects longitude name to be longitude, latitude name to be latitude, label name to be label.
1010
#'
11-
#' @return leaflet::leaflet
11+
#' @return leaflet::leaflet with mesh and markers
1212
#' @export
1313
plot_mesh <- function(mesh, spatial_data = NULL, longitude_column = "LONG", latitude_column = "LAT", markers = NULL) {
1414
expected_crs <- "+proj=longlat +datum=WGS84"

R/plot_priors.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#' Plot line comparison for stdev etc
22
#'
3-
#' @param data Parsed model output
3+
#' @param data List of parsed model outputs from "parse_model_output" (a list of lists)
44
#' @param to_plot Type of data to plot, "Range for f" etc
5+
#' @param title Title of the plot, defaults to the value of "to_plot"
56
#'
6-
#' @return ggplot2::ggplot
7-
plot_line_comparison <- function(data, to_plot, title) {
7+
#' @return ggplot2::ggplot of the selected hyperparameter posterior distribution
8+
plot_line_comparison <- function(data, to_plot, title = to_plot) {
89
parsed_data <- purrr::map(data, function(x) as.data.frame(x$pars[[to_plot]]))
910
single_df <- dplyr::bind_rows(parsed_data, .id = "Run")
1011
if (nrow(single_df) == 0) {
@@ -19,9 +20,9 @@ plot_line_comparison <- function(data, to_plot, title) {
1920

2021
#' Create boxplots from priors run data
2122
#'
22-
#' @param data
23+
#' @param data A list of parsed model output from "parse_model_output" (a list of lists)
2324
#'
24-
#' @return graphics::boxplot
25+
#' @return graphics::boxplot of fitted values
2526
plot_priors_boxplot <- function(data) {
2627
# TODO - I'm sure this can be done in a nicer functional way
2728
fitted_mean_post <- purrr::map(data, function(x) x$fitted_mean_post)
@@ -34,10 +35,10 @@ plot_priors_boxplot <- function(data) {
3435
#' Plot density function
3536
#'
3637
#'
37-
#' @param data Parsed model outputs
38-
#' @param measurement_data Measurement data
38+
#' @param data List of parsed model outputs from "parse_model_output" (a list of lists)
39+
#' @param measurement_data Measurement data (can be data.frame, SpatialPoints/SpatialPointsDataFrame, or simple feature collection)
3940
#'
40-
#' @return ggplot2::ggplot
41+
#' @return ggplot2::ggplot of measurement density by value
4142
plot_priors_density <- function(data, measurement_data) {
4243
# Can this be done in a cleaner way? Just create a dataframe from the lists?
4344
fitted_values <- unlist(purrr::map(data, function(x) x$fitted_mean_post))
@@ -56,9 +57,9 @@ plot_priors_density <- function(data, measurement_data) {
5657

5758
#' Plot Deviance Information Criterion (DIC) values
5859
#'
59-
#' @param data
60+
#' @param data List of parsed model outputs from "parse_model_output" (a list of lists)
6061
#'
61-
#' @return ggplot2::ggplot
62+
#' @return ggplot2::ggplot with DIC values shown as points
6263
plot_dic <- function(data) {
6364
infocri <- base::cbind.data.frame(
6465
priors = unlist(purrr::map(seq(1, length(data)), function(x) paste("Run", x))),

R/shiny_meshbuilder.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#' Mesh building shiny app
22
#'
3-
#' @param spatial_data Spatial data
3+
#' @param spatial_data Spatial data (must be a data.frame, SpatialPointsDataFrame or SpatialPolygonsDataFrame)
44
#' @param obs_data Measurement data
55
#' @param crs CRS as a proj4string
66
#' @param offset Specifies the size of the inner and outer extensions around data locations, passed to fmesher::fm_mesh_2d_inla
@@ -11,7 +11,7 @@
1111
#'
1212
#' @importFrom magrittr %>%
1313
#'
14-
#' @return shiny::app
14+
#' @return shiny::app with meshbuilder functionality
1515
#' @keywords internal
1616
meshbuilder_shiny <- function(
1717
spatial_data,
@@ -270,7 +270,7 @@ meshbuilder_shiny <- function(
270270

271271
#' Mesh building shiny app. Creates and visualises a mesh from some spatial data.
272272
#'
273-
#' @param spatial_data Spatial data
273+
#' @param spatial_data Spatial data (must be a data.frame, SpatialPointsDataFrame or SpatialPolygonsDataFrame)
274274
#' @param obs_data Measurement data
275275
#' @param crs CRS as a proj4string
276276
#' @param offset Specifies the size of the inner and outer extensions around data locations, passed to fmesher::fm_mesh_2d_inla
@@ -279,7 +279,7 @@ meshbuilder_shiny <- function(
279279
#' @param y_coord Name of the latitude column in the spatial data
280280
#' @param x_coord Name of the longitude column in the spatial data
281281
#'
282-
#' @return shiny::app
282+
#' @return shiny::app with meshbuilder functionality
283283
#' @export
284284
mesh_builder <- function(spatial_data, obs_data = NULL, crs = NULL, max_edge = NULL, offset = NULL, cutoff = NULL, y_coord = "LAT", x_coord = "LONG") {
285285
shiny::runApp(meshbuilder_shiny(

R/shiny_modelbuilder.R

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#' Interactively set and see the result of different priors
22
#'
3-
#' @param spatial_data Spatial data
4-
#' @param measurement_data Measurement data
5-
#' @param time_variable Time variable in measurement_data
6-
#' @param mesh INLA mesh
3+
#' @param spatial_data Spatial data (must be SpatialPoints or SpatialPointsDataFrame)
4+
#' @param measurement_data Measurement data (data.frame, SpatialPointsDataFrame, SpatialPolygonsDataFrame with a named time column and at least 1 named feature column with measurements)
5+
#' @param time_variable Time variable name in measurement_data
6+
#' @param mesh INLA/fmesher mesh
77
#' @param data_distribution Data distribution, Poisson or Gaussian
8-
#' @param log_folder Folder to write out logs
8+
#' @param log_folder Folder to write out logs (character)
99
#'
1010
#' @importFrom INLA f
1111
#'
12-
#' @return shiny::app
12+
#' @return shiny::app with model builder functionality
1313
#' @keywords internal
1414
model_builder_shiny <- function(spatial_data,
1515
measurement_data,
@@ -697,12 +697,12 @@ model_builder_shiny <- function(spatial_data,
697697
#' Interactively set and see the result of different priors
698698
#'
699699
#' @param spatial_data Spatial data
700-
#' @param measurement_data Measurement data
701-
#' @param time_variable Time variable in measurement_data
702-
#' @param mesh INLA mesh
703-
#' @param log_folder Folder to write logs to
700+
#' @param measurement_data Measurement data (data.frame, SpatialPointsDataFrame, SpatialPolygonsDataFrame with a named time column and at least 1 named feature column with measurements)
701+
#' @param time_variable Time variable name in measurement_data
702+
#' @param mesh INLA/fmesher mesh
703+
#' @param log_folder Folder to write logs to (character)
704704
#'
705-
#' @return shiny::app
705+
#' @return shiny::app with model builder functionality
706706
#' @export
707707
model_builder <- function(spatial_data, measurement_data, time_variable, mesh, data_distribution = "Poisson", log_folder = NULL) {
708708
shiny::runApp(model_builder_shiny(spatial_data = spatial_data, measurement_data = measurement_data, time_variable = time_variable, mesh = mesh, data_distribution = data_distribution, log_folder = log_folder))

R/shiny_modelviewer.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#' Parse inlabru model output
22
#'
3-
#' @param model_output INLA model output
4-
#' @param mesh INLA mesh
5-
#' @param measurement_data Measurement data
3+
#' @param model_output INLA/inlabru model output
4+
#' @param mesh INLA/fmesher mesh
5+
#' @param measurement_data Measurement data (can be data.frame, SpatialPoints/SpatialPointsDataFrame, or simple feature collection)
66
#' @param data_distribution Type of data, Poisson or Gaussian
77
#'
88
#' @importFrom magrittr %>%
99
#'
10-
#' @return shiny::app
10+
#' @return shiny::app with model viewer functionality
1111
#' @keywords internal
1212
model_viewer_shiny <- function(model_output, mesh, measurement_data, data_distribution) {
1313
busy_spinner <- get_busy_spinner()
@@ -297,12 +297,12 @@ model_viewer_shiny <- function(model_output, mesh, measurement_data, data_distri
297297

298298
#' Model viewing shiny app. Visualises INLA model output, plots the posterior distributions of model parameters and prediction statistics.
299299
#'
300-
#' @param model_output INLA model output
301-
#' @param mesh INLA mesh
302-
#' @param measurement_data Measurement data
303-
#' @param data_distribution Type of data, Poisson or Gaussian
300+
#' @param model_output INLA/inlabru model output
301+
#' @param mesh INLA/fmesher mesh used for the run
302+
#' @param measurement_data Measurement data used for the run (can be data.frame, SpatialPoints/SpatialPointsDataFrame, or simple feature collection)
303+
#' @param data_distribution Type of data, Poisson or Gaussian (character)
304304
#'
305-
#' @return shiny::app
305+
#' @return shiny::app with model viewer functionality
306306
#' @export
307307
model_viewer <- function(model_output, mesh, measurement_data, data_distribution = "Poisson") {
308308
shiny::runApp(model_viewer_shiny(model_output = model_output, mesh = mesh, measurement_data = measurement_data, data_distribution = data_distribution))

0 commit comments

Comments
 (0)