Skip to contents

We first need to retrieve the data from the fdmr example data store and unpack it and we’ll use retrieve_tutorial_data to do this.

fdmr::retrieve_tutorial_data(dataset = "priors")
## 
## Tutorial data extracted to  /home/runner/fdmr/tutorial_data/priors

Next we’ll use the load_tutorial_data function to load in the spatial data we want.

sp_data <- fdmr::load_tutorial_data(dataset = "priors", filename = "spatial_dataBris.rds")

Now we make a map of the study region. load

sp_data@data$mapp <- 0
domain <- sp_data@data$mapp
fdmr::plot_map(polygon_data = sp_data, domain = domain, add_scale_bar = TRUE, polygon_fill_opacity = 0.5, palette = "YlOrRd")

A map of the study region.

The COVID-19 data in Bristol are included in our tutorial data package. We’ll load in the data using same process we used above

covid19_data <- fdmr::load_tutorial_data(dataset = "priors", filename = "covid19_dataBris.rds")

Then the first 6 rows of the data set can be viewed using the following code

utils::head(covid19_data)
##           UtlaName       date week  MSOA11CD cases Population  prevalence
## 3 Bristol, City of 2021-12-25    1 E02003014    89       8292 0.010733237
## 4 Bristol, City of 2021-12-25    1 E02003015    99       6692 0.014793784
## 5 Bristol, City of 2021-12-25    1 E02003016    99       8169 0.012118986
## 6 Bristol, City of 2021-12-25    1 E02003017    74       6118 0.012095456
## 7 Bristol, City of 2021-12-25    1 E02003018    69       5870 0.011754685
## 9 Bristol, City of 2021-12-25    1 E02003020    55       6416 0.008572319
##       LONG      LAT
## 3 -2.66729 51.51507
## 4 -2.59093 51.49491
## 5 -2.57357 51.49471
## 6 -2.61209 51.48767
## 7 -2.65070 51.48954
## 9 -2.62725 51.49219

Create the mesh

We can have a look at the mesh and change it interactively using the fdmr::mesh_builder Shiny app. Using the app I came up with a mesh with the following parameters, we’ll use this and pass it into the “Setting Priors Shiny app” below.

initial_range <- diff(base::range(sp_data@data[, "LONG"])) / 3
max_edge <- initial_range / 2

mesh <- fmesher::fm_mesh_2d_inla(
  loc = sp_data@data[, c("LONG", "LAT")],
  max.edge = c(1, 2) * max_edge,
  offset = c(initial_range, initial_range),
  cutoff = max_edge / 7
)

In order to fit the model, we also need to define a temporal index (must be an integer starting at 1) and the number of discrete time points we want to model.

group_index <- covid19_data$week
n_groups <- length(unique(covid19_data$week))

Set coordinates on data

We will use the function inlabru::bru function to fit the model which expects the coordinates of the data, thus we transform covid19_data data set to a SpatialPointsDataFrame using the sp::coordinates function.

sp::coordinates(covid19_data) <- c("LONG", "LAT")

Use the Interactive Priors Shiny app

Now we have the filtered data we are ready to pass in the spatial

Selecting priors

First start by selecting the variable to model. In this example we’ll select cases, then select the features to add to the formula. At the bottom of the window you’ll see the formula being constructed. Once you’ve setup the formula to your specifications click on the Model tab and click Run. Once the model run has finished you’ll see a table of summarised model outputs.

Plotting model outputs

Once the model has run you view plots of model outputs by clicking on the Plot tab. Each time you run a model with different sets or priors the parsed model output saved and can be compared with previous runs.

Plotting model ouputs on a map

To plot model predictions on a map click on the Map tab. This will use the fdmr::create_prediction_field and fdmr::create_raster functions to first create a data.frame of model predictions and then create a raster image from this data. You can select between two different plot types, the predicted mean fields or the random effect fields. You can also select Gaussian or Poisson data type.

Saving model outputs

By default the app will write out parameter sets and logs to the fdmr/logs directory in your home folder. If you want these logs to be written to a different folder, you can pass in the folder path to the log_folder argument.

We’re now ready to run the app. To do this we’ll call the fdmr::model_builder function, passing in

  • spatial_data = sp_data - the spatial data covering areas across the city of Bristol
  • measurement_data = covid19_data - the COVID-19 data we want to model
  • mesh = mesh - the mesh we created above
  • time_variable = week - the name of the time variable in the data
fdmr::model_builder(spatial_data = sp_data, measurement_data = covid19_data, mesh = mesh, time_variable = "week")

Viewing model outputs and parameter sets

The outputs of the model run by the app can be viewed in the fdmr/logs folder in your home directory. There you’ll find three log files

  • priors_exploration_applog_timestamp.txt - holds general logging information and errors
  • priors_exploration_parameters_timestamp.json - holds the priors used in each model run
  • priors_exploration_modelout_timestamp.rds - holds the model output for each run