Last data update: 2014.03.03

R: Find all pairwise distances within a maximum distance.
 LKDist R Documentation

## Find all pairwise distances within a maximum distance.

### Description

These are the lower level functions to compute the distances among two sets of locations but being limited to distances less than a maximum threshold (see delta below ). These functions are useful for generating a sparse matrix on distances and evaluating a compactly supported function (such as the Wendland). The location - location method supports the distance metrics: Euclidean, spherical, componentwise and Manhattan. `LKDistComponent` and `LKDistComponentGrid` return the coordinate-wise distances and are useful for evaluating a tensor product basis functions.

### Usage

```LKDist(x1, x2, delta, max.points = NULL, mean.neighbor = 50,
distance.type = "Euclidean")
LKDistComponents(x1, x2, delta, max.points = NULL, mean.neighbor = 50,
distance.type = "Euclidean")

LKDistGrid(x1, gridList, delta, max.points = NULL, mean.neighbor = NULL,
distance.type = "Euclidean", periodic)

LKDistGridComponents(
x1, gridList, delta,
max.points = NULL, mean.neighbor = NULL,
distance.type = "Euclidean")

directionCosines(x)

LKGridCheck(distance.type, x1, gridList )

LKGridFindNmax(n1, max.points, mean.neighbor, delta, gridList)
```

### Arguments

 `gridList` A list with each component vector that specifies the grid points for an equally spaced grid. Can have class gridList. (See also help on gridlist). `n1` Number of rows of x1. `x` A two column matrix giving longitude/ latitude locations on a sphere. `x1` A matrix with rows indexing locations and columns indexing coordinates. `x2` A matrix with rows indexing locations and columns indexing coordinates. `delta` The maximum distance to find pairwise distances. `max.points` Used for dynamically assigning matrix size this should be larger than the total number of pairwise distances less than delta. `mean.neighbor` Used for dynamically assigning matrix size this is the average number of points that are less that delta in distance to the `x1` locations. `periodic` A logical vector with length ncol( x1). If a component is TRUE then that dimension is treated as periodic. `distance.type` A text string either "Euclidean", "GreatCircle", "Chordal", "Manhattan".

### Value

LKDist and LKDistGrid a list representing a sparse matrix in spind format.

LKDistComponent andLKDistGridComponent a list representing a sparse matrix using the spind format except the `ra` component is now a matrix. The columns of ra being the individual distances for each coordinate.

directionCosine a matrix where rows index the different points and columns index x,y,z.

LKGridFindNmax returns the maximum number of nonzero elements expected in a pairwise distance matrix.

LKGridFindNmax checks that the calling arguments are compatible with the pairwise distance computation.

### Author(s)

Doug Nychka

`spind2spam`, `spind2full`

### Examples

```set.seed( 123)
x<- matrix( runif(100*2), 100,2)

DMatrix<- LKDist( x,x, delta=.1)
# coerce to spam matrix format
DMatrix2<- spind2spam( DMatrix)

# a grid
gridL<- list( x1= seq(0,1,.2), x2= seq( 0,2,.2) , x3= seq( -1,1,.2))
class(gridL)<- "gridList"
x1<- cbind( runif( 100), runif(100)*2, 2*(runif( 100) -.5) )
look<- LKDistGrid( x1, gridL, delta=.45)
# check against rdist.
# look2<- rdist( x1, make.surface.grid(gridL))
# look2[ look2 >= .45] <- 0
# max( abs(look- look2)[look>0] )

# test of periodic option
gridL<- structure(
list( x1= seq(0,1,.02),
x2= seq( 0,1,.02)),
class="gridList")
look1<- LKDistGrid( rbind(c(0,0)), gridL, delta=.35,
periodic=c(TRUE,FALSE))
look2<- spind2full(look1)
image.plot( as.surface( gridL, look2) )

look1<- LKDistGrid( rbind(c(0,0)), gridL, delta=.35,
periodic=c(TRUE,TRUE))
look2<- spind2full(look1)
image.plot( as.surface( gridL, look2) )
```

### Results

```
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(LatticeKrig)
Spam version 1.3-0 (2015-10-24) is loaded.
Type 'help( Spam)' or 'demo( spam)' for a short introduction
and overview of this package.
Help for individual functions is also obtained by adding the
suffix '.spam' to the function name, e.g. 'help( chol.spam)'.

Attaching package: 'spam'

The following objects are masked from 'package:base':

backsolve, forwardsolve

# maps v3.1: updated 'world': all lakes moved to separate new #
# 'lakes' database. Type '?world' or 'news(package="maps")'.  #

> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/LatticeKrig/LKDist.Rd_%03d_medium.png", width=480, height=480)
> ### Name: LKDist
> ### Title: Find all pairwise distances within a maximum distance.
> ### Aliases: LKDist LKDistComponents LKDistGrid LKDistGridComponents
> ###   LKGridCheck LKGridFindNmax directionCosines
> ### Keywords: spatial
>
> ### ** Examples
>
> set.seed( 123)
> x<- matrix( runif(100*2), 100,2)
>
> DMatrix<- LKDist( x,x, delta=.1)
> # coerce to spam matrix format
> DMatrix2<- spind2spam( DMatrix)
>
> # a grid
> gridL<- list( x1= seq(0,1,.2), x2= seq( 0,2,.2) , x3= seq( -1,1,.2))
> class(gridL)<- "gridList"
> x1<- cbind( runif( 100), runif(100)*2, 2*(runif( 100) -.5) )
> look<- LKDistGrid( x1, gridL, delta=.45)
> # check against rdist.
> # look2<- rdist( x1, make.surface.grid(gridL))
> # look2[ look2 >= .45] <- 0
> # max( abs(look- look2)[look>0] )
>
> # test of periodic option
>  gridL<- structure(
+             list( x1= seq(0,1,.02),
+                   x2= seq( 0,1,.02)),
+             class="gridList")
>  look1<- LKDistGrid( rbind(c(0,0)), gridL, delta=.35,
+                      periodic=c(TRUE,FALSE))
>  look2<- spind2full(look1)
>  image.plot( as.surface( gridL, look2) )
>
>  look1<- LKDistGrid( rbind(c(0,0)), gridL, delta=.35,
+                       periodic=c(TRUE,TRUE))
>  look2<- spind2full(look1)
>  image.plot( as.surface( gridL, look2) )
>
>
>
>
>
> dev.off()
null device
1
>

```