我想腳本隨機分配例程。抽樣設計有一個區域被分成許多多邊形或地層。一套但不同數量的樣本將隨機分配到每個層(最少2個樣本,但某些層中多達7個)。因此,我有一個地層的形狀文件,並在其屬性表中列出了每個地層所需的地層名稱和樣本數量。分配隨機樣本到多邊形
- STRATA;樣品
- 440; 4
- 441; 2
- 5Z3; 4
- 5Z1; 7
- 560; 2
我發現這些類型的採樣設計(http://casoilresource.lawr.ucdavis.edu/drupal/book/export/html/519)的一些很好的文檔,雖然我有一些問題實現我自己的需要的例程。在此鏈接之後,我一直在使用rgdal和maptools軟件包。我的工作腳本如下:
# read in strata boundary shapefile
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(i) slot(i, 'ID'))
sample <- sapply(allocation, function(i) nrow([email protected]))
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
[email protected] <- [email protected]
writeOGR(allocation.final, ".", "allocation", driver='ESRI Shapefile')
但是,每層的採樣強度不是靜態的(其中我有n = 4)。我需要這個來反映屬性表中的列,它表示給定地層所需的樣本數量。我還想將分層名稱分配回已分配的採樣地點。
理想情況下,例程將遍歷每個多邊形,並隨機分配n個樣本(如屬性表中所示)並寫入shapefile。任何方向將不勝感激。我對這個節目相對比較陌生,所以如果我錯過了某些明顯的東西,我很抱歉。
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
[email protected] 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 ...
[email protected] polygons :List of 66
.. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
.. .. .. [email protected] Polygons :List of 1
.. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
.. .. .. .. .. .. [email protected] labpt : num [1:2] -68.3 40.4
.. .. .. .. .. .. [email protected] area : num 0.769
.. .. .. .. .. .. [email protected] hole : logi FALSE
.. .. .. .. .. .. [email protected] ringDir: int 1
.. .. .. .. .. .. [email protected] coords : num [1:654, 1:2] -66.3 -66.3 -66.4 -66.4 -66.4 ...
.. .. .. [email protected] plotOrder: int 1
.. .. .. [email protected] labpt : num [1:2] -68.3 40.4
.. .. .. [email protected] ID : chr "0"
.. .. .. [email protected] area : num 0.769