prepare_data {disaggregation} | R Documentation |

## Prepare data for disaggregation modelling

### Description

*prepare_data* function is used to extract all the data required for fitting a disaggregation model.
Designed to be used in the *disaggregation::fit_model* function.

### Usage

```
prepare_data(
polygon_shapefile,
covariate_rasters,
aggregation_raster = NULL,
id_var = "area_id",
response_var = "response",
sample_size_var = NULL,
mesh.args = NULL,
na.action = FALSE,
makeMesh = TRUE,
ncores = NULL
)
```

### Arguments

`polygon_shapefile` |
sf object containing at least three columns: one with the geometried, one with the id for the polygons ( |

`covariate_rasters` |
SpatRaster of covariate rasters to be used in the model. |

`aggregation_raster` |
SpatRaster to aggregate pixel level predictions to polygon level e.g. population to aggregate prevalence. If this is not supplied a uniform raster will be used. |

`id_var` |
Name of column in sf object with the polygon id. |

`response_var` |
Name of column in sf object with the response data. |

`sample_size_var` |
For survey data, name of column in sf object (if it exists) with the sample size data. |

`mesh.args` |
list of parameters that control the mesh structure with the same names as used by INLA. |

`na.action` |
logical. If TRUE, NAs in response will be removed, covariate NAs will be given the median value, aggregation NAs will be set to zero. Default FALSE (NAs in response or covariate data within the polygons will give errors). |

`makeMesh` |
logical. If TRUE, build INLA mesh, takes some time. Default TRUE. |

`ncores` |
Deprecated. |

### Details

Takes a sf object with the response data and a SpatRaster of covariates.

Extract the values of the covariates (as well as the aggregation raster, if given) at each pixel within the polygons
(*parallelExtract* function). This is done in parallel and *n.cores* argument is used to set the number of cores
to use for covariate extraction. This can be the number of covariates used in the model.

The aggregation raster defines how the pixels within each polygon are aggregated. The disaggregation model performs a weighted sum of the pixel prediction, weighted by the pixel values in the aggregation raster. For disease incidence rate you use the population raster to aggregate pixel incidence rate by summing the number of cases (rate weighted by population). If no aggregation raster is provided a uniform distribution is assumed, i.e. the pixel predictions are aggregated to polygon level by summing the pixel values.

Makes a matrix that contains the start and end pixel index for each polygon. Builds an INLA mesh to use for the spatial field
(*getStartendindex* function).

The *mesh.args* argument allows you to supply a list of INLA mesh parameters to control the mesh used for the spatial field
(*build_mesh* function).

The *na.action* flag is automatically off. If there are any NAs in the response or covariate data within the polygons the
*prepare_data* method will error. Ideally the NAs in the data would be dealt with beforehand, however, setting na.action = TRUE
will automatically deal with NAs. It removes any polygons that have NAs as a response, sets any aggregation pixels with NA to zero
and sets covariate NAs pixels to the median value for the that covariate.

### Value

A list is returned of class `disag_data`

.
The functions *summary*, *print* and *plot* can be used on `disag_data`

.
The list of class `disag_data`

contains:

`x` |
The sf object used as an input. |

`covariate_rasters` |
The SpatRaster used as an input. |

`polygon_data` |
A data frame with columns of |

`covariate_data` |
A data frame with columns of |

`aggregation_pixels` |
An array with the value of the aggregation raster for each pixel in the same order as the rows of |

`coordsForFit` |
A matrix with two columns of x, y coordinates of pixels within the polygons. Used to make the spatial field. |

`coordsForPrediction` |
A matrix with two columns of x, y coordinates of pixels in the whole Raster. Used to make predictions. |

`startendindex` |
A matrix with two columns containing the start and end index of the pixels within each polygon. |

`mesh` |
A INLA mesh to be used for the spatial field of the disaggregation model. |

### Examples

```
polygons <- list()
for(i in 1:100) {
row <- ceiling(i/10)
col <- ifelse(i %% 10 != 0, i %% 10, 10)
xmin = 2*(col - 1); xmax = 2*col; ymin = 2*(row - 1); ymax = 2*row
polygons[[i]] <- list(cbind(c(xmin, xmax, xmax, xmin, xmin),
c(ymax, ymax, ymin, ymin, ymax)))
}
polys <- lapply(polygons,sf::st_polygon)
response_df <- data.frame(area_id = 1:100, response = runif(100, min = 0, max = 10))
spdf <- sf::st_sf(response_df,geometry=polys)
r <- terra::rast(nrow=20,ncol=20)
terra::ext(r) <- terra::ext(spdf)
r[] <- sapply(1:terra::ncell(r), function(x) rnorm(1, ifelse(x %% 20 != 0, x %% 20, 20), 3))
r2 <- terra::rast(nrow=20,ncol=20)
terra::ext(r2) <- terra::ext(spdf)
r2[] <- sapply(1:terra::ncell(r), function(x) rnorm(1, ceiling(x/10), 3))
cov_rasters <- c(r, r2)
test_data <- prepare_data(polygon_shapefile = spdf,
covariate_rasters = cov_rasters)
```

*disaggregation*version 0.3.0 Index]