I’ve been trying to implement an R script for stratified random sampling
regime. The sampling design has a region divided into strata. These strata
are sampled at varying intensities with n number of locations then randomly
allocated in each stratum area. I have a shapefile of these strata, and in
its' attribute table, the strata names and number of sampling locations
desired in each, as follows:



STRATUM

SAMPLES

5Z9

2

5Z8

2

5Z7

3

440

7

441

2

450

3

455

5

…

…


I’ve been working with the following script using the sp and rgdal packages:



*# read in strata boundary shapefile (.shp) layer, will read .prj file as
well*

strataboundaries <- readOGR('strataboundaries.shp',
layer='strataboundaries')

*# sample allocation to strata*

allocation <- sapply(slot(strataboundaries, 'polygons'), function(i)
spsample(i, n=4, type='random'))

allocation.merge <- do.call('rbind', allocation)

stratumID <- sapply(slot(strataboundaries, 'polygons'),
function(strataboundaries) slot(strataboundaries, 'ID'))

sample <- sapply(allocation, function(i) nrow(i@coords))

sampleID <- rep(stratumID, sample)

allocation.final <- SpatialPointsDataFrame(allocation.merge,

data=data.frame(poly_id=sampleID))

plot(strataboundaries, col="lightcyan", bborder="black", axes=TRUE,
bg="lightsteelblue1")

points(allocation.final, col="red", pch=3, cex=0.8)

*# write out shapefile containing sampling locations*

allocation.final@proj4string <- strataboundaries@proj4string

writeOGR(allocation.final, ".", "allocation", driver='ESRI Shapefile')



> class(strataboundaries)

[1] "SpatialPolygonsDataFrame"

attr(,"package")

[1] "sp"



> str(strataboundaries)

Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots

  ..@ data       :'data.frame':       66 obs. of  3 variables:

  .. ..$ OBJECTID_1: int [1:66] 1 2 3 4 5 6 7 8 9 10 ...

  .. ..$ Stratum1  : Factor w/ 66 levels "440","441","442",..: 65 64 63 62
61 60 12 11 7 49 ...

  .. ..$ Primary   : int [1:66] 2 2 4 5 2 7 2 2 2 2 ...

  ..@ polygons   :List of 66

  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots

  .. .. .. ..@ Polygons :List of 1

  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots

  .. .. .. .. .. .. ..@ labpt  : num [1:2] -68.3 40.4

  .. .. .. .. .. .. ..@ area   : num 0.769

  .. .. .. .. .. .. ..@ hole   : logi FALSE

  .. .. .. .. .. .. ..@ ringDir: int 1

  .. .. .. .. .. .. ..@ coords : num [1:654, 1:2] -66.3 -66.3 -66.4 -66.4
-66.4 ...

  .. .. .. ..@ plotOrder: int 1

  .. .. .. ..@ labpt    : num [1:2] -68.3 40.4

  .. .. .. ..@ ID       : chr "0"

  .. .. .. ..@ area     : num 0.769

  .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots

  .. .. .. ..@ Polygons :List of 1

  .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots

  .. .. .. .. .. .. ..@ labpt  : num [1:2] -69.1 40.6

  .. .. .. .. .. .. ..@ area   : num 1.12

  .. .. .. .. .. .. ..@ hole   : logi FALSE

  .. .. .. .. .. .. ..@ ringDir: int 1

  .. .. .. .. .. .. ..@ coords : num [1:3089, 1:2] -68.2 -68.2 -68.2 -68.2
-68.2 ...

  .. .. .. ..@ plotOrder: int 1

  .. .. .. ..@ labpt    : num [1:2] -69.1 40.6

  .. .. .. ..@ ID       : chr "1"

  .. .. .. ..@ area     : num 1.12



One issue I’ve been having is in trying to assign the sampling intensity
from the attributes table (‘Samples’), which corresponds to the respective
strata, instead of the static n=4 as shown. Is there a way I can recall
these from the attributes table? Similarly, I’ve also been trying to work
around the extraction of the original ID line [ids <-  sapply(slot(x,
'polygons'), function(i) slot(I, 'ID')]. My understanding is that the IDs
are extracted from the slots for each polygon in the shapefile, which are
assigned values from 0 to n. However, these strata have set names
(contained in the attribute table under 'Stratum'), which I am instead
trying to assign to the sampling locations that have been randomly
allocated to a respective strata.

 Any direction would be greatly appreciated. Thanks in advance.

	[[alternative HTML version deleted]]

