Question

I was wondering if any one could explain to me how the geoR package calculates the covariance function? I mean how you would do it by hand?

library(geoR)
#suppose I have the following coordinates
X = c(60,30,20,40)
Y = c(50,20,50,50)
my_coordinates = cbind(X,Y)
print(my_coordinates)

#computing covariance
my_cov= varcov.spatial(my_coordinates,cov.model="exp", cov.pars=c(0.2,25))
print(my_cov)

And you get:

         [,1]       [,2]       [,3]       [,4]
[1,] 0.20000000 0.03664442 0.04037930 0.08986579
[2,] 0.03664442 0.20000000 0.05645288 0.05645288
[3,] 0.04037930 0.05645288 0.20000000 0.08986579
[4,] 0.08986579 0.05645288 0.08986579 0.20000000

However, one might want to do it in Matlab as well.

Was it helpful?

Solution

The best way to find out how a package or function does something is to look at the source code. This is one of the awesome things about open source projects, you can do this.

try typing varcov.spatial or searching through the unpacked package tar ball for the function definition

To calculate the covariance (which is dependant on the distance between points), you need to calculate

  • the distance between your points (you really only need the lower triangle, as it will be symmetric
  • The value of the covariance function at each distance
  • form the full symmetric variance covariance matrix from these calculated covariances.

The covariance functions are defined in ?cov.spatial. You can call cov.spatial to calculate these in R (exactly what geoR::varcov.spatial does)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top