[R-sig-Geo] Spatial Join - over and weighted mean

Edzer Pebesma edzer.pebesma at uni-muenster.de
Fri Mar 30 15:10:17 CEST 2012



On 03/30/2012 02:29 PM, Els Ducheyne wrote:
> Dear Edzer
> I have a point shapefile containing values of vaccination coverage (Z) estimated via a sample (SampleSize)
> The aim is to obtain a weighted mean of vaccination coverage per admin unit
> 
> Below sample code
> 
>> studyzone <- readShapePoly(paste(backgroundfilesdir,"GDAM_studyzone.shp",sep=""))
>> slot(studyzone,'data') <- as.data.frame(slot(studyzone,"data")$OBJECTID)
>> names(slot(studyzone,'data')) <- c("OBJECTID")
>> shapefilename <- paste(shapefiledir,shortNameIndicator,"_",production.date,sep="")
>> point.shapefile <- readShapePoints(paste(shapefilename,".shp",sep=""))
>> summary(point.shapefile)
> Object of class SpatialPointsDataFrame
> Coordinates:
>             min   max
> coords.x1   0.0 41.96
> coords.x2 -11.1 16.73
> Is projected: NA 
> proj4string : [NA]
> Number of points: 76
> Data attributes:
>        Id              Z            SampleSize    
>  Min.   : 1.00   Min.   :0.0400   Min.   : 388.0  
>  1st Qu.:19.75   1st Qu.:0.2200   1st Qu.: 549.2  
>  Median :38.50   Median :0.4300   Median : 659.0  
>  Mean   :38.50   Mean   :0.5437   Mean   :1562.1  
>  3rd Qu.:57.25   3rd Qu.:0.7450   3rd Qu.:2480.8  
>  Max.   :76.00   Max.   :2.4000   Max.   :6487.0  
>                                   NA's   :   2.0  
> 
> 
> For the moment I've used the following function to obtain the normal mean
> aggregate.data<-over(studyzone,point.shapefile,fn=mean)
> 
> I know I should define the weighted.mean by weighted.mean(slot(point.shapefile,'data')$Z,slot(point.shapefile,'data')$SampleSize,na.rm=T) but how to include this in the over function is not clear to me

I don't think you can, as it takes two arguments that are both data
columns. over() essentially applies fun() to each of the columns in the
data.frame slot, but cannot combine two columns in a single function call.

You can pre-weight your values though, so that mean() results in the
weighted mean you want.

To access slot(point.shapefile,'data')$Z it is recommended to use
point.shapefile$Z

> 
> Thanks for your help
> Els
> 
> On 30 Mar 2012, at 14:17, Edzer Pebesma wrote:
> 
>> Els, what being weighted by what exactly? Any chance you can illustrate
>> your problem by something we can reproduce?
>>
>> On 03/30/2012 01:58 PM, Els Ducheyne wrote:
>>> Dear R-SIG-GEO list
>>>
>>> I want to aggregate the mean value of points in a polygon using the over function. 
>>> #Read the shapefile with the measurements
>>> shapefilename <- paste(shapefiledir,shortNameIndicator,"_",production.date,sep="")
>>> point.shapefile <- readShapePoints(paste(shapefilename,".shp",sep=""))
>>> names(slot(point.shapefile,'data')) <- c("ID",shortNameIndicator)
>>>
>>>
>>> #aggregate per polygon by mean and save as shapefile
>>> aggregate.data<-over(studyzone,point.shapefile,fn=mean)
>>> row.names(aggregate.data) <- row.names(aggregate.data)
>>> outPolDF <- SpatialPolygonsDataFrame(studyzone,aggregate.data,match.ID=T)
>>> writePolyShape(outPolDF,paste(shapefiledir,shortNameIndicator,"_poly_",production.date,".shp",sep=""))
>>>
>>>
>>> While this works using the mean function I am unsure how to proceed for a weighted mean - does anyone have a suggestion?
>>>
>>> Many thanks
>>> Els
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>> -- 
>> Edzer Pebesma
>> Institute for Geoinformatics (ifgi), University of Münster
>> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
>> 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
>> http://www.52north.org/geostatistics      e.pebesma at wwu.de
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list