Fitting an underlying continuous process to areal data

## Usage

```
fit_spm(x, ...)
# S3 method for spm
fit_spm(
x,
model,
theta_st,
nu = NULL,
tr = NULL,
kappa = 1,
mu2 = 1.5,
apply_exp = FALSE,
opt_method = "Nelder-Mead",
control_opt = list(),
comp_hess = TRUE,
...
)
fit_spm2(
x,
model,
nu,
tr,
kappa = 1,
mu2 = 1.5,
comp_hess = TRUE,
phi_min,
phi_max,
nphi = 10
)
```

## Arguments

- x
an object of type

`spm`

. Note that, the dimension of`theta_st`

depends on the 2 factors. 1) the number of variables being analyzed, and 2) if the input is a`spm`

object.- ...
additionnal parameters, either passed to

`optim`

.- model
a

`character`

scalar indicating the family of the covariance function to be used. The options are`c("matern", "pexp", "gaussian", "spherical", "gw")`

.- theta_st
a

`numeric`

(named) vector containing the initial parameters.- nu
a

`numeric`

value indicating either the \(\nu\) paramater from the Matern covariance function (controlling the process differentiability), or the "pexp" for the Powered Exponential family. If the`model`

chosen by the user is Matern and`nu`

is not informed, it is automatically set to .5. On the other hand, if the user choses the Powered Exponential family and do not inform`nu`

, then it is set to 1. In both cases, the covariance function becomes the so covalled exponential covariance function.- tr
tapper range

- kappa
\(\kappa \in \{0, \ldots, 3 \}\) parameter for the GW cov function.

- mu2
the smoothness parameter \(\mu\) for the GW function.

- apply_exp
a

`logical`

scalar indicating wheter the parameters that cannot assume negative values should be exponentiate or not.- opt_method
a

`character`

scalar indicating the optimization algorithm to be used. For details, see optim.- control_opt
a named

`list`

containing the control arguments for the optimization algorithm to be used. For details, see optim.- comp_hess
a

`boolean`

indicating whether the Hessian matrix should be computed.- phi_min
a

`numeric`

scalar representing the minimum \(phi\) value to look for.- phi_max
a

`numeric`

scalar representing the maximum \(phi\) value to look for.- nphi
a

`numeric`

scalar indicating the number of values to compute a grid-search over \(phi\).

## Details

This function uses the `optim`

function optimization
algorithms to find the Maximum Likelihood estimators, and their standard
errors, from a model adapted from. The function allows the user to input
the control parameters from the optim function through the argument
`control_opt`

, which is a named list. Additionally, the one can
input lower and upper boundaries for the optimization problem, as well
as the preferred optimization algorithm (as long as it is available for
`optim`

). The preferred algorithm is selected by the argument
`opt_method`

. In addition to the control of the optimization, the
user can select a covariance function among the following: Matern,
Exponential, Powered Exponential, Gaussian, and Spherical. The parameter
`apply_exp`

is a `logical`

scalar such that, if set to
`TRUE`

, the \(\exp\) function is applied to the nonnegative
parameters, allowing the optimization algorithm to search for all the
parameters over the real numbers.

## Examples

```
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)
#> [1] 296.2574
summary_spm_fit(fit_msoa, sig = .05)
#>
#> optimization algorithm converged: yes
#>
#> par estimate se ci
#> 1 mu 75.9070141 0.7693848 [74.399; 77.415]
#> 2 sigsq 23.6005460 4.3007116 [15.171; 32.030]
#> 3 phi 0.7747194 0.1985900 [0.385; 1.164]
```