# Internal functions for LatticeKrig package.

### Description

Some internal functions for `LKrig`

that estimate the
coefficients of the basis functions and compute the likelihood.

### Usage

1 2 3 4 5 6 7 8 | ```
createLKrigObject(x, y, weights = NULL, Z, X, U, LKinfo, verbose = FALSE)
LKrigMakewU(object, verbose = FALSE)
LKrigMakewX(object, verbose = FALSE)
LKrig.coef( GCholesky, wX, wU, wy, lambda, verbose = FALSE)
LKrig.lnPlike( GCholesky, Q, quad.form, nObs, nReps, weights, LKinfo)
LKrig.lnPlikeOLD(Mc, Q, wy, residuals, weights, LKinfo)
LKrig.traceA(GCholesky, wX, wU, lambda, weights, NtrA, iseed = NA)
LKrigUnrollZGrid( grid.list, ZGrid=NULL)
``` |

### Arguments

`grid.list` |
The grid for evaluating surface |

`GCholesky` |
SPAM cholesky decomposition of the "G" matrix. |

`iseed` |
Random seed used to generate the Monte Carlo samples. Keep the same to compare results with mKrig and also for multiple values of lambda. |

`lambda` |
The ratio of the nugget variance (sigma squared) to the parameter controlling the marginal variance of the process (called rho in fields). |

`LKinfo` |
The LKinfo object. See help(LKinfo) |

`Mc` |
Cholesky decomposition of regression matrix. |

`NtrA` |
Number of Monte Carlo samples to estimate trace. Default is 20 in LKrig. |

`nObs` |
Number of observations. |

`nReps` |
Number of replicate fields. |

`object` |
The LKrig object. |

`Q` |
Precision matrix for coefficients. |

`quad.form` |
The part of the log likelihood that is a quadratic form.
(This is typically found in |

`residuals` |
Residuals from fitting spatial process. |

`U` |
The matrix that maps the d.coef coefficients of the fixed component (typically a low order polynomial) part of the observation model. |

`verbose` |
If TRUE intermediate debugging information is printed. |

`weights` |
A vector that is proportional to the reciprocal variances of the errors. I.e. errors are assumed to be uncorrelated with variances sigma^2/weights. |

`wU` |
Weighted U matrix the fixed part of the model. |

`wX` |
Weighted X matrix (in spam format) related to nonparametric (stochastic) part of model. Here weights refer to the sqrt(weights). NOTE: predicted values are U%*%d.coef + X%*%c.coef |

`wy` |
Weighted observations. |

`X` |
The matrix that maps the c.coef coefficients into the nonparametric component (spatial process) part of the observation model. |

`x` |
Matrix of spatial locations passed to LKrig. |

`y` |
Vector or matrix of observations passed to LKrig. |

`Z` |
A matrix of covariates. |

`ZGrid` |
A list or array with the covariates on the same grid as that specified by the grid.list argument. |

### Details

The LatticeKrig article can be used as a reference for the matrix computations
and the G matrix from those formulas figures prominently. The GCholesky object
in these functions is the cholesky decompoistion of this matrix. For
compatibility with older version of this package this object may also be named
as `Mc`

( Cholesky of the M matrix) but the user should not identify this M with that in the article. Ideally all coding using Mc should be changed to GCholesky.

`createLKrigObject`

Based on the arguments passed into LKrig forms the
prototype LKrig object. This object is added to as one computes additional steps in the LKrig function.

`LKrigMakewU`

and `LKrigMakewX`

construct the weighted U and X matrices from what is passed. In the case of observations that are point locations wU is found the weights and using the fixedFunction and wX is found from the weights and the multiresolution basis functions. Note that X and wX are assumed to be in spam
sparse matrix format.

`LKrig.coef`

and `LKrig.lnPlike`

are two low level functions
to find the basis function coefficients and to evaluate the
likelihood. The coefficients (`c.mKrig`

) are also found because
they provide for shortcut formulas for the standard errors and MLE
estimates. These coefficients are identical to the basis coefficients
(`c.coef`

) found for usual Kriging in the mKrig
function. `LKrig.lnPlike`

also finds the profile MLE of sigma and
rho given a fixed value for lambda (and `alpha`

and
`a.wght`

). See the source for LKrig and also MLE.LKrig to see
how these functions are used.

`LKrig.traceA`

finds an estimate of the effective degrees of
freedom of the smoothing matrix based a simple Monte Carlo scheme. The
smoothing matrix A is the matrix for fixed covariance parameters so
that y.hat = A y, where y.hat are the predicted values at the data
locations. trace(A) is the effective degrees of freedom. If e are
iid N(0,1) then the expected value of t(e)% * % A % * % e is equal
to the trace of A. This is the basis for estimating the trace and the
standard error for this estimate is based on `NtrA`

independent
samples.

`dfind2d`

is a fast FORTRAN subroutine to find nearest neighbors
within a fixed distance and is called by `Wendland.basis`

. The
function `dfind3d`

is currently not used but is intended for
future use to determine chordal distance between points on a sphere or
cylinder.

`LKrigDefaultFixedFunction`

Is called to construct the fixed part of the
spatial model. The default is a polynomial of degree (m-1).

### Value

- LKrig.coef
a list with components d.coef the coefficients of the spatial dirft and for covariates (Z) and c.coef the basis function coefficients. The logical vector ind.drift from the LKrig object indicates with components of d.coef are associated with the polynomial spatial drift and which are other fixed spatial covariates.

- LKrig.lnPlike
has the components:

- lnProfileLike
the log likelihood profiled for lambda, alpha and a.wght

- rho.MLE
the MLE of rho given lambda, alpha and a.wght

- shat.MLE
the MLE of sigma given lambda, alpha and a.wght

- quad.form
the quadratic form in the exponent of the multivariate normal likelihood

- lnDetCov
the log determinant of the covariance matrix in the likelihood

- LKrigDefaultFixedFunction
A matrix with dimension nrow(x) and columns of the number of polynomial terms and the number of columns of Z if given.

### Author(s)

Doug Nychka

### References

Nychka, D., Bandyopadhyay, S., Hammerling, D., Lindgren, F., & Sain, S. (2015). A multiresolution gaussian process model for the analysis of large spatial datasets.Journal of Computational and Graphical Statistics, 24(2), 579-599.

### See Also

LKrig, LKrig.basis