forked from 4DModeller/fdmr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding modelbuilder vignette that got deleted
- Loading branch information
Showing
1 changed file
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
title: "Model builder" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{Model builder} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
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. | ||
|
||
```{r} | ||
fdmr::retrieve_tutorial_data(dataset = "priors") | ||
``` | ||
|
||
Next we'll use the `load_tutorial_data` function to load in the spatial data we want. | ||
|
||
```{r loadshape, error=TRUE} | ||
sp_data <- fdmr::load_tutorial_data(dataset = "priors", filename = "spatial_dataBris.rds") | ||
``` | ||
|
||
Now we make a map of the study region. | ||
load | ||
```{r rmap, error=TRUE,fig.cap="A map of the study region.", fig.width=8, fig.height=4, fig.align='center'} | ||
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") | ||
``` | ||
|
||
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 | ||
|
||
```{r loadCOVIDat, error=TRUE} | ||
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 | ||
|
||
```{r , error=TRUE} | ||
utils::head(covid19_data) | ||
``` | ||
|
||
# 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. | ||
|
||
```{r createmesh, error=TRUE} | ||
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. | ||
|
||
```{r, error=TRUE, eval=FALSE} | ||
group_index <- covid19_data$week | ||
n_groups <- length(unique(covid19_data$week)) | ||
``` | ||
|
||
# Set coordinates on data | ||
|
||
We will use the function [`inlabru::bru`](https://inlabru-org.github.io/inlabru/reference/bru.html) function to fit the model which expects the coordinates of the data, thus we transform `covid19_data` data set to a [`SpatialPointsDataFrame`](https://www.rdocumentation.org/packages/sp/versions/2.0-0/topics/SpatialPointsDataFrame-class) using the [`sp::coordinates`](https://www.rdocumentation.org/packages/sp/versions/2.0-0/topics/coordinates) function. | ||
|
||
```{r, error=TRUE, eval=FALSE} | ||
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 | ||
|
||
```{r eval=FALSE} | ||
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 |