fitCor {remotePARTS} | R Documentation |
Estimate spatial parameters from time series residuals
Description
fitCor()
estimates parameter values of a distance-based
variance function from the pixel-wise correlations among time series residuals.
Usage
fitCor(
resids,
coords,
distm_FUN = "distm_scaled",
covar_FUN = "covar_exp",
start = list(r = 0.1),
fit.n = 1000,
index,
save_mod = TRUE,
...
)
Arguments
resids |
a matrix of time series residuals, with rows corresponding to pixels and columns to time points |
coords |
a numeric coordinate matrix or data frame, with two columns and rows corresponding to each pixel |
distm_FUN |
a function to calculate a distance matrix from |
covar_FUN |
a function to estimate distance-based covariances |
start |
a named list of starting parameter values for |
fit.n |
an integer indicating how many pixels should be used to estimate parameters. |
index |
an optional index of pixels to use for parameter estimation |
save_mod |
logical: should the nls model be saved in the output? |
... |
additional arguments passed to |
Details
For accurate results, resids
and coords
must be paired matrices.
Rows of both matrices should correspond to the same pixels.
Distances between sapmled pixels are calculated with the function specified by
distm_FUN
. This function can be any that takes a coordinate
matrix as input and returns a distance matrix between points. Some options
provided by remotePARTS
are distm_km()
, which returns distances
in kilometers and distm_scaled()
, which returns distances scaled between
0 and 1.
covar_FUN
can be any function that takes a vector of distances as its
first argument, and at least one parameter as additional arguments. remotePARTS
provides three suitable functions: covar_exp
, covar_exppow
, and
covar_taper
; but user-defined functions are also possible.
Parameters are estimated with stats::nls()
by regressing correlations
among time series residuals on a function of distances specified by covar_FUN
.
start
is used by nls
to determine how many parameters need
estimating, and starting values for those parameters. As such, it is important
that start
has named elements for each parameter in covar_FUN
.
The fit will be performed for all pixels specified in index
, if provided.
Otherwise, a random sample of length fit.n
is used. If fit.n
exceeds the number of pixels, all pixels are used. When random pixels are used,
parameter estimates will be different for each call of the function. For reproducible
results, we recommend taking a random sample of pixels manually and passing in
those values as index
.
Caution: Note that a distance matrix, of size n \times n
must be fit to the
sampled data where n
is either fit.n
or length(index)
.
Take your computer's memory and processing time into consideration when choosing
this size.
Parameter estimates are always returned in the same scale of distances
calculated by distm_FUN
. It is very important that these estimates
are re-scaled by users if output of distm_FUN
use units different from
the desired scale. For example,
if the function covar_FUN = function(d, r, a){-(d/r)^a}
is used
with distm_FUN = "distm_scaled"
, the estimated range parameter r
will be based on a unit-map. Users will likely want to re-scaled it to map
units by multiplying r
by the maximum distance among points on your map.
If the distm_FUN
is on the scale of your map (e.g., "distm_km"),
re-scaling is not needed but may be preferable, since it is scaled to the
maximum distance among the sampled data rather than the true maximum
distance. For example, dividing the range parameter by max.distance
and then multiplying it by the true max distance may provide a better range
estimate.
Value
fitCor
returns a list object of class "remoteCor", which contains
these elements:
- call
the function call
- mod
the
nls
fit object, ifsave_mod=TRUE
- spcor
a vector of the estimated spatial correlation parameters
- max.distance
the maximum distance among the sampled pixels, as calculated by
dist_FUN
.- logLik
the log-likelihood of the fit
Examples
# simulate dummy data
set.seed(19)
time.points = 30 # time series length
map.width = 8 # square map width
coords = expand.grid(x = 1:map.width, y = 1:map.width) # coordinate matrix
## create empty spatiotemporal variables:
X <- matrix(NA, nrow = nrow(coords), ncol = time.points) # response
Z <- matrix(NA, nrow = nrow(coords), ncol = time.points) # predictor
## setup first time point:
Z[, 1] <- .05*coords[,"x"] + .2*coords[,"y"]
X[, 1] <- .5*Z[, 1] + rnorm(nrow(coords), 0, .05) #x at time t
## project through time:
for(t in 2:time.points){
Z[, t] <- Z[, t-1] + rnorm(map.width^2)
X[, t] <- .2*X[, t-1] + .1*Z[, t] + .05*t + rnorm(nrow(coords), 0 , .25)
}
AR.map = fitAR_map(X, coords, formula = y ~ Z, X.list = list(Z = Z), resids.only = FALSE)
# using pre-defined covariance function
## exponential covariance
fitCor(AR.map$residuals, coords, covar_FUN = "covar_exp", start = list(range = .1))
## exponential-power covariance
fitCor(AR.map$residuals, coords, covar_FUN = "covar_exppow", start = list(range = .1, shape = .2))
# user-specified covariance function
fitCor(AR.map$residuals, coords, covar_FUN = function(d, r){d^r}, start = list(r = .1))
# un-scaled distances:
fitCor(AR.map$residuals, coords, distm_FUN = "distm_km", start = list(r = 106))
# specify which pixels to use, for reproducibility
fitCor(AR.map$residuals, coords, index = 1:64)$spcor #all
fitCor(AR.map$residuals, coords, index = 1:20)$spcor #first 20
fitCor(AR.map$residuals, coords, index = 21:64)$spcor # last 43
# randomly select pixels
fitCor(AR.map$residuals, coords, fit.n = 20)$spcor #random 20
fitCor(AR.map$residuals, coords, fit.n = 20)$spcor # different random 20