# [R-sig-Geo] Moving variance calculation from an irregular grid

Rawlins, Barry G bgr at bgs.ac.uk
Tue Dec 16 15:07:31 CET 2008

```To summarise the outcome from my original posting:

I used the ann package (http://spatial.nhh.no/R/etc/) - see instructions on installation from previous thread.

I used this to calculate 12 nearest neighbours in my dataset:

all.tellus <- ann(cbind(data\$X, data\$Y), k=12) # X and Y are the coordinates
out<-as.matrix(all.tellus) # this converts the output to a matrix- probably not the right way to approach this

## then I loop through my dataset to calculate the local variances
## I was not concerned about selecting a max distance on the neighbourhood - but one could do this
require(ann)
require(stats)
for (j in 1:1075121)
vars<-seq(from=1,to=1075121,by=1) # set up my vars vector
vars[j]<-var(data\$CS137[out[j,1:12]]) # writes the variances (my local variances for all points)

My code is probably very poor and many people could probably have written it much better!

"Off list" Edzer Pebebsma told me that there are functions in package gstat that will search a local neighbourhood based on a quad tree. I did not pursue this further but Edzer may be able to suggest which function/approach it is in a subsequent post.

Barry R

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: 15 December 2008 15:10
To: Rawlins, Barry G
Cc: 'r-sig-geo at stat.math.ethz.ch'
Subject: Re: [R-sig-Geo] Moving variance calculation from an irregular grid

On Mon, 15 Dec 2008, Rawlins, Barry G wrote:

> I would like to calculate a moving window variance across a large,
> irregular spatial dataset (1.1 million points) - I have looked for
> various functions but have not been able to find one that will do this.

How irregular? Is the moving window adaptive (k nearest neighbours) or
fixed in distance for a given bandwidth - hence widely varying counts of
points within the window? Do you need the moving window variances for the
same support (points) as the data, or for different points (maybe a grid)?
Could the task be divided up into (overlapping) tiles if memory is a major
constraint? If k nearest neighbours, ANN (approximate nearest neighbours)
has been ported several times to R, but not generalised - I can make one
off-CRAN version available if need be. This thread may be relevant:

https://stat.ethz.ch/pipermail/r-sig-geo/2008-December/004644.html

Roger

>
> Any suggestions  much appreciated.
> Barry R
>
>
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of