This function builds a spatial filter by iteratively adding eigenvectors to a linear model. Only eigenvectors with a p-value below a threshold are retained and cumulatively added to the filter (default <= 0.05). The function can optionally divide the data by one or more categorical variables (e.g., mode, education level) and calculate separate filters for each subset. The result is a single data frame containing the identifier, the response variable, the computed filter, and the grouping columns (if any).
Usage
calculate_spatial_filter(
spatial_data_frame,
matrix_eigv,
by = "ID",
group_vars = NULL,
response = "access",
ev_prefix = "EV",
threshold = 0.05
)Arguments
- spatial_data_frame
A data frame containing the spatial variable (
response) and the identifier (join key). Must contain a column named byresponseand a column matchingby(or the left side ofbyif it is named).- matrix_eigv
A data frame containing the eigenvectors (columns starting with
ev_prefix) and an identifier (join key) matching the right side ofby.- by
A character string or a named character vector of length 1 specifying the join key(s). If a single string, it is used as the column name in both tables. If named (e.g.
c("ID" = "CTUID")), the name is the column inspatial_data_frameand the value is the column inmatrix_eigv. Default:"ID".- group_vars
Optional character vector naming columns in
spatial_data_frameby which to divide the data before computing the filter. For each unique combination of these variables, a separate filter is built. IfNULL(default), a single filter is computed for all rows.- response
Character string naming the response (e.g.,
y, dependent) variable inspatial_data_frame. Default:"access".- ev_prefix
Character string giving the prefix of the eigenvector columns in
matrix_eigv. Default:"EV".- threshold
Numeric p-value threshold for including an eigenvector in the filter (two-sided test). Default:
0.05.
Value
A data frame with the following columns:
the identifier column (named as in
byor its left side)the response variable (named
response)the spatial filter, named
Sf_<response>any grouping columns specified in
group_vars(if provided)
The rows are in the same order as the original spatial_data_frame.
Details
The function performs a forward selection procedure:
Join
spatial_data_framewithmatrix_eigvon the specified key.Initialize
Sf = 0andx = 1.For each eigenvector (in the order they appear in the joined data), fit the model
response ~ x + Sf + EV_i.If the coefficient of
EV_ihas a p-value <threshold, addcoef(EV_i) * EV_itoSf.Return a data frame with only the ID, response, filter, and groups.
The same procedure is applied independently to each group defined by
group_vars, and the results are row-bound into a single data frame.
References
This function is based on the study of Le Gallo, J., & Paez, A. (2013).
Le Gallo, J., & Paez, A. (2013). Using synthetic variables in instrumental variable estimation of spatial series models.. Environment and Planning A, 45(9), 2227-2242. doi:10.1068/a45443
Examples
if (FALSE) { # \dontrun{
# Single group (no splitting)
result_df <- calculate_spatial_filter(spatial_data_frame = my_access,
matrix_eigv = eigen_matrix)
# Split by transportation mode (column "PwMode")
result_by_mode <- calculate_spatial_filter(spatial_data_frame = SA_mode,
matrix_eigv = matrix_eigv,
by = "PRCDDA",
group_vars = "PwMode",
response = "SA_im")
# Split by combination of education and mode
result_by_edu_mode <- calculate_spatial_filter(spatial_data_frame = my_data,
matrix_eigv = eigen_matrix,
by = c("CTUID" = "ID"),
group_vars = c("Education", "Mode"),
response = "SA_i",
ev_prefix = "EIG",
threshold = 0.10)
} # }