[R-sig-Geo] calculate geometric mean of mass of points

Dylan Beaudette debeaudette at ucdavis.edu
Wed Nov 19 21:22:51 CET 2008


On Wednesday 19 November 2008, Ashton Shortridge wrote:
> On Wednesday 19 November 2008 02:33:46 pm Alessandro wrote:
> > I have a points shape and I wish to calculate the geometric centre of
> > this points shape and create a new points with R. But It's over my
> > knowledge. Is there suggestions?
>
> This seems straightforward, even if it is not implemented in a package.
> Wikipedia has some basic info on the geometric mean:
> http://en.wikipedia.org/wiki/Geometric_mean
>
> All you seem to have to do is read the shapefile in, grab the x's and y's
> in two vector (called, say, x and y) and then:
> totalX <- 1
> for(i in x) {totalX <- totalX * i}
> totalY <- 1
> for(i in y) {totalY <- totalY * i}
>
> geomnX <- totalX^(1/length(x))
> geomnY <- totalY^(1/length(y))
>
> You could then make an sp object out of this pair and export it as a
> shapefile, if you wished.
>
> There is probably something slick you could do with lapply or something to
> avoid the for loops in the code above, but I'm doing this off the top of my
> head.
>
> Also note that if any of your coordinate values are zero then the mean will
> be zero and may not represent a centered value very well. Also note that
> negative coordinate values are problematic. This solution assumes that all
> your coordinates are positive.
>
> Yours,
>
> Ashton

Here is another solution, based on the definition of the geometric mean 
w/respect to logarithms. 

geom-mean = e^{ 1/n * sum{ln x} }


# only works for positive coordinates !!
x <- runif(n=100, min=1, max=100) ; y <- runif(n=100, min=1, max=100)

# plot points
plot(x,y)

# plot the arithmetic mean point
points(mean(x), mean(y), pch=16, col='red')

# plot the geometric mean point
points(exp(mean(log(x))), exp(mean(log(y))), pch=16, col='blue')

Dylan


-- 
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341




More information about the R-sig-Geo mailing list