| Title: | Spatial Misalignment: Interpolation, Linkage, and Estimation |
|---|---|
| Description: | Provides functions to estimate, predict and interpolate areal data. For estimation and prediction we assume areal data is an average of an underlying continuous spatial process as in Moraga et al. (2017) <doi:10.1016/j.spasta.2017.04.006>, Johnson et al. (2020) <doi:10.1186/s12942-020-00200-w>, and Wilson and Wakefield (2020) <doi:10.1093/biostatistics/kxy041>. The interpolation methodology is (mostly) based on Goodchild and Lam (1980, ISSN:01652273). |
| Authors: | Lucas da Cunha Godoy [aut, cre] (ORCID: <https://orcid.org/0000-0003-4265-972X>) |
| Maintainer: | Lucas da Cunha Godoy <[email protected]> |
| License: | GPL-3 |
| Version: | 1.1.0 |
| Built: | 2026-05-07 09:03:00 UTC |
| Source: | https://github.com/lcgodoy/smile |
This function estimates variables observed at a "source" region into a "target" region. "Source" and "target" regions represent two different ways to divide a city, for example. For more details, see https://lcgodoy.me/smile/articles/sai.html.
ai(source, target, vars) ai_var(source, target, vars, vars_var, sc_vars = FALSE, var_method = "CS")ai(source, target, vars) ai_var(source, target, vars, vars_var, sc_vars = FALSE, var_method = "CS")
source |
a |
target |
a |
vars |
a |
vars_var |
a scalar of type |
sc_vars |
boolean indicating whether |
var_method |
a |
the target (of type sf) with estimates of the variables
observed at the source data.
Function designed to find the phi parameter such that the
correlation between points within a given distance d is at most a
given value.
find_phi( d, nu, kappa, mu2, family = "matern", range = c(1e-04, 1000), cut = 0.05 )find_phi( d, nu, kappa, mu2, family = "matern", range = c(1e-04, 1000), cut = 0.05 )
d |
maximum distance for spatial dependence equal to |
nu |
smoothness parameter associated with the Matern cov. function. |
kappa |
one of the smoothness parameters associated with the Generalized Wendland covariance function |
mu2 |
one of the smoothness parameters associated with the Generalized Wendland covariance function |
family |
covariance function family, the options are |
range |
Minimum and maximum distance to be considered. The default is
|
cut |
desired spatial correlation at a distance |
a numeric value indicating the range parameter such that the
spatial correlation between two points at distance d is
cut.
This function fits a spatial process model to areal data by estimating the parameters of an underlying continuous process. It leverages the stats::optim function for Maximum Likelihood estimation, providing flexibility in optimization algorithms and control parameters.
fit_spm(x, ...) ## S3 method for class 'spm' fit_spm( x, model, theta_st, nu = NULL, kappa = 1, mu2 = 1.5, apply_exp = FALSE, opt_method = "Nelder-Mead", control_opt = list(), comp_hess = TRUE, ... ) fit_spm2( x, model, nu, kappa = 1, mu2 = 1.5, comp_hess = TRUE, phi_min, phi_max, nphi = 10, cores = getOption("mc.cores", 1L) )fit_spm(x, ...) ## S3 method for class 'spm' fit_spm( x, model, theta_st, nu = NULL, kappa = 1, mu2 = 1.5, apply_exp = FALSE, opt_method = "Nelder-Mead", control_opt = list(), comp_hess = TRUE, ... ) fit_spm2( x, model, nu, kappa = 1, mu2 = 1.5, comp_hess = TRUE, phi_min, phi_max, nphi = 10, cores = getOption("mc.cores", 1L) )
x |
An object of type |
... |
Additional parameters passed to stats::optim. |
model |
A |
theta_st |
A named |
nu |
A |
kappa |
|
mu2 |
The smoothness parameter |
apply_exp |
A |
opt_method |
A |
control_opt |
A named |
comp_hess |
A |
phi_min |
A |
phi_max |
A |
nphi |
A |
cores |
An |
The function utilizes optimization algorithms from stats::optim
to determine Maximum Likelihood estimators (MLEs) and their standard
errors for a model adapted for areal data. Users can customize the
optimization process by providing control parameters through the
control_opt argument (a named list, see stats::optim for details),
specifying lower and upper bounds for parameters, and selecting the
desired optimization algorithm via opt_method (must be available in
stats::optim).
Additionally, the function supports various covariance functions,
including Matern, Exponential, Powered Exponential, Gaussian, and
Spherical. The apply_exp argument, a logical scalar, allows for
exponentiation of non-negative parameters, enabling optimization over the
entire real line.
The underlying model assumes a point-level process:
where
.
The observed areal data is then assumed to behave as the average of the process
over each area:
.
An spm_fit object containing the estimated model parameters.
data(liv_lsoa) ## loading the LSOA data msoa_spm <- sf_to_spm(sf_obj = liv_msoa, n_pts = 500, type = "regular", by_polygon = FALSE, poly_ids = "msoa11cd", var_ids = "leb_est") ## fitting model theta_st_msoa <- c("phi" = 1) # initial value for the range parameter fit_msoa <- fit_spm(x = msoa_spm, theta_st = theta_st_msoa, model = "matern", nu = .5, apply_exp = TRUE, opt_method = "L-BFGS-B", control = list(maxit = 500)) AIC(fit_msoa) summary_spm_fit(fit_msoa, sig = .05)data(liv_lsoa) ## loading the LSOA data msoa_spm <- sf_to_spm(sf_obj = liv_msoa, n_pts = 500, type = "regular", by_polygon = FALSE, poly_ids = "msoa11cd", var_ids = "leb_est") ## fitting model theta_st_msoa <- c("phi" = 1) # initial value for the range parameter fit_msoa <- fit_spm(x = msoa_spm, theta_st = theta_st_msoa, model = "matern", nu = .5, apply_exp = TRUE, opt_method = "L-BFGS-B", control = list(maxit = 500)) AIC(fit_msoa) summary_spm_fit(fit_msoa, sig = .05)
spm_fit
objects.Akaike's (and Bayesian) An Information Criterion for spm_fit
objects.
## S3 method for class 'spm_fit' AIC(object, ..., k = 2) ## S3 method for class 'spm_fit' BIC(object, ...)## S3 method for class 'spm_fit' AIC(object, ..., k = 2) ## S3 method for class 'spm_fit' BIC(object, ...)
object |
a |
... |
optionally more fitted model objects. |
k |
|
a numeric scalar corresponding to the goodness of fit
measure.
A dataset containing containing the LSOA's for Liverpool along with estimates for Index of Multiple Deprivation. Data taken from Johnson et al. 2020
liv_lsoaliv_lsoa
A sf data frame with 298 rows and 6 variables:
LSOA code
LSOA name
Male population
Female population
Index of Multiple Deprivation
LMSOA area, in
The data was projected to EPSG 27700 and units changed to km
https://ij-healthgeographics.biomedcentral.com/articles/10.1186/s12942-020-00200-w
A dataset containing containing the MSOA's for Liverpool along with estimates for Life Expectancy at Birth. Data taken from Johnson et al. 2020
liv_msoaliv_msoa
A sf data frame with 61 rows and 4 variables:
MSOA code
MSOA name
Estimated life expectancy at birth, in years
MSOA area, in
The data was projected to EPSG 27700 and units changed to km
https://ij-healthgeographics.biomedcentral.com/articles/10.1186/s12942-020-00200-w
A dataset containing containing the census tracts for the city of Nova Lima in Minas Gerais - Brazil.
nl_ctnl_ct
A sf data frame with 113 rows and 14 variables:
unique identifier
average household density
variance of the household density
average income per household
variance of the income per household
population in the census tract
average age of the inhabitants in the census tract
variance of the variable age in the census tract
proportion of women
proportion of people with 55 years of age or older
illiteracy rate
proportion of self-declared white people
proportion of self-declared black people
proportion of self-declared native people
The data is project using the SIRGAS 2000.
Realizes predictions that can be useful when researchers are interested in predict a variable observed in one political division of a city (or state) on another division of the same region.
predict_spm(x, ...) ## S3 method for class 'spm_fit' predict_spm(x, .aggregate = TRUE, ...) ## S3 method for class 'sf' predict_spm(x, spm_obj, n_pts, type, outer_poly = NULL, id_var, ...)predict_spm(x, ...) ## S3 method for class 'spm_fit' predict_spm(x, .aggregate = TRUE, ...) ## S3 method for class 'sf' predict_spm(x, spm_obj, n_pts, type, outer_poly = NULL, id_var, ...)
x |
a |
... |
additional parameters |
.aggregate |
|
spm_obj |
an object of either class |
n_pts |
a |
type |
|
outer_poly |
(object) |
id_var |
if |
a list of size 4 belonging to the class spm_pred. This
list contains the predicted values and the mean and covariance matrix
associated with the conditional distribution used to compute the
predictions.
data(liv_lsoa) ## loading the LSOA data data(liv_msoa) ## loading the MSOA data msoa_spm <- sf_to_spm(sf_obj = liv_msoa, n_pts = 500, type = "regular", by_polygon = FALSE, poly_ids = "msoa11cd", var_ids = "leb_est") ## fitting model theta_st_msoa <- c("phi" = 1) # initial value for the range parameter fit_msoa <- fit_spm(x = msoa_spm, theta_st = theta_st_msoa, model = "matern", nu = .5, apply_exp = TRUE, opt_method = "L-BFGS-B", control = list(maxit = 500)) pred_lsoa <- predict_spm(x = liv_lsoa, spm_obj = fit_msoa, id_var = "lsoa11cd")data(liv_lsoa) ## loading the LSOA data data(liv_msoa) ## loading the MSOA data msoa_spm <- sf_to_spm(sf_obj = liv_msoa, n_pts = 500, type = "regular", by_polygon = FALSE, poly_ids = "msoa11cd", var_ids = "leb_est") ## fitting model theta_st_msoa <- c("phi" = 1) # initial value for the range parameter fit_msoa <- fit_spm(x = msoa_spm, theta_st = theta_st_msoa, model = "matern", nu = .5, apply_exp = TRUE, opt_method = "L-BFGS-B", control = list(maxit = 500)) pred_lsoa <- predict_spm(x = liv_lsoa, spm_obj = fit_msoa, id_var = "lsoa11cd")
This function computes the smallest eigenvalue of a correlation
matrix derived from the power exponential correlation function. It
evaluates this across a grid of values for the power parameter (nu) and
the practical range parameter (rho), based on a provided distance
matrix.
sev_pexp(range_nu, range_rho, grid_len = 50, dmat)sev_pexp(range_nu, range_rho, grid_len = 50, dmat)
range_nu |
A numeric vector of length 2, specifying the minimum and
maximum values for the power parameter |
range_rho |
A numeric vector of length 2, specifying the minimum and
maximum values for the practical range parameter |
grid_len |
An integer specifying the number of points to create for
both |
dmat |
A numeric matrix representing the distance matrix between locations. The distances should be non-negative. |
The practical range rho is defined here as the distance at which
the correlation is 0.1. The internal scale parameter phi is calculated
as phi = rho / (log(10)^(1/nu)). The power exponential correlation
function is assumed to be of the form C(h) = exp(-(h/phi)^nu), where h is
distance. The function smile:::pexp_cov is used internally to compute
the covariance/correlation matrix with a sill of 1.
The function first creates a grid of nu and rho parameters. For
each pair of (rho, nu) in the grid: 1. It calculates the scale
parameter phi for the power exponential correlation function, where phi = rho / (log(10)^(1/nu)). This definition implies that the correlation is
0.1 at the distance rho. 2. It computes the power exponential
correlation matrix using smile:::pexp_cov(dists = dmat, sill = 1, range = phi, smooth = nu). Note the use of an internal function from the smile
package. 3. It calculates the eigenvalues of this correlation matrix.
4. The minimum eigenvalue is extracted. The final output is a tibble
containing all parameter combinations and their corresponding minimum
eigenvalues.
A tibble with three columns:
rho |
The practical range parameter value. |
nu |
The power parameter value. |
lambda |
The smallest eigenvalue of the power exponential correlation
matrix corresponding to the |
sf to spm
Transforming a sf into a spm object (Internal use)
single_sf_to_spm( sf_obj, n_pts, type = "regular", by_polygon = FALSE, poly_ids = NULL, var_ids = NULL ) sf_to_spm( sf_obj, n_pts, type = "regular", by_polygon = FALSE, poly_ids = NULL, var_ids = NULL )single_sf_to_spm( sf_obj, n_pts, type = "regular", by_polygon = FALSE, poly_ids = NULL, var_ids = NULL ) sf_to_spm( sf_obj, n_pts, type = "regular", by_polygon = FALSE, poly_ids = NULL, var_ids = NULL )
sf_obj |
a |
n_pts |
a |
type |
a |
by_polygon |
a |
poly_ids |
a |
var_ids |
a scalar or vector of type |
a named list of size 6 belonging to the class
spm. This list stores all the objects necessary to fit models
using the fit_spm.
data(liv_lsoa) # loading the LSOA data msoa_spm <- sf_to_spm(sf_obj = liv_msoa, n_pts = 1000, type = "regular", by_polygon = FALSE, poly_ids = "msoa11cd", var_ids = "leb_est")data(liv_lsoa) # loading the LSOA data msoa_spm <- sf_to_spm(sf_obj = liv_msoa, n_pts = 1000, type = "regular", by_polygon = FALSE, poly_ids = "msoa11cd", var_ids = "leb_est")
spm_fit
Provides a data.frame with point estimates and
confidence intervals for the parameters of the model fitted using the
spm_fit function.
summary_spm_fit(x, sig = 0.05)summary_spm_fit(x, sig = 0.05)
x |
a |
sig |
a real number between 0 and 1 indicating significance level to be used to compute the confidence intervals for the parameter estimates. |
a data.frame summarizing the parameters estimated by the
fit_spm function.
Reminder, have to create an example. This will be exported after we submit the paper for publication.
vdl(coords_sf, areal_sf, vars, buff)vdl(coords_sf, areal_sf, vars, buff)
coords_sf |
|
areal_sf |
|
vars |
a |
buff |
scalar |
a sf object for the coords_sf spatial data set.
Reminder, have to create an example. This will be exported after we submit the paper for publication.
vdl_var(coords_sf, areal_sf, res_var, variance, var_method = "CS", buff)vdl_var(coords_sf, areal_sf, res_var, variance, var_method = "CS", buff)
coords_sf |
|
areal_sf |
|
res_var |
a |
variance |
a |
var_method |
a |
buff |
scalar |
a sf object, containing the id_coords variable and the
list_vars for the coords_sf spatial data set.