Skip to content

Commit

Permalink
fixing errors in the sampling chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
smroecker committed Jan 30, 2025
1 parent 4262118 commit 5d44a5f
Show file tree
Hide file tree
Showing 30 changed files with 1,485 additions and 1,444 deletions.
42 changes: 22 additions & 20 deletions Part1/05-sampling.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ library(ggplot2)
set.seed(4)
# Create a sixteen square polygon
bb <- st_make_grid(st_bbox(c(xmin = 0, xmax = 6, ymin = 0, ymax = 6)), n = 6)
n = 4
bb <- st_make_grid(st_bbox(c(xmin = 0, xmax = n, ymin = 0, ymax = n)), n = n)
grd <- st_as_sf(bb)
grd$ID <- 1:length(bb)
grd <- rmapshaper::ms_explode(grd)
```


Expand Down Expand Up @@ -273,13 +274,14 @@ In stratified random sampling, the sampling region is spatially subset into diff


```{r stratified, fig.width=4, fig.height=4, eval=TRUE}
grd2 <- st_cast(grd, "MULTIPOLYGON")
st_crs(grd2) <- 5070
test <- st_sample(grd2, size = 16, type = "random", by_polygon = TRUE)
idx <- 1:nrow(grd)
test <- sapply(idx, function(x) st_sample(grd[x, ], size = 1))
test <- do.call("rbind", test) |>
as.data.frame() |>
st_as_sf(coords = 1:2)
ggplot() +
geom_sf(data = grd2) +
geom_sf(data = grd) +
geom_sf(data = test) +
ggtitle("Stratified")
```
Expand Down Expand Up @@ -310,11 +312,12 @@ In multistage random sampling, the region is separated into different subsets th
```{r two_stage, fig.width=4, fig.height=4, eval=TRUE}
# Select 8 samples from each square
idx <- sample(1:nrow(grd), size = 2, replace = FALSE)
grd_sub <- grd[idx, ]
test <- sapply(1:2, function(i) {
st_coordinates(st_sample(grd_sub[i, ], size = 8, type = "random"))
test <- sapply(idx, function(i) {
st_sample(grd[i, ], size = 8, type = "random")
})
test <- st_as_sf(as.data.frame(test), coords = 1:2)
test <- do.call("rbind", test) |>
as.data.frame() |>
st_as_sf(coords = 1:2)
ggplot() +
geom_sf(data = grd) +
Expand Down Expand Up @@ -384,7 +387,7 @@ Covariate space coverage sampling is a stratified sampling technique to obtain r
```{r, eval=TRUE}
library(sf)
library(terra)
source("https://raw.githubusercontent.com/ncss-tech/stats_for_soil_survey/master/exercises/fcsc.R")
library(sgsR)
# import volcano DEM, details at http://geomorphometry.org/content/volcano-maungawhau
data("volcano")
Expand All @@ -407,14 +410,13 @@ slope_r <- terrain(volcano_r, v = "slope", unit = "degrees")
rs <- c(volcano_r, slope_r)
# Covariate Space Coverage Sampling
source("https://raw.githubusercontent.com/ncss-tech/stats_for_soil_survey/master/exercises/fcsc.R")
fs <- cscs(rs, vars = c("elev", "slope"), n = 20)
fs_sf <- st_as_sf(fs, coords = c("x", "y"), crs = crs(rs))
n = 20
fs_strata <- strat_kmeans(rs, nStrata = n, iter = 10000)
fs_samp <- sample_nc(rs, nSamp = n, iter = 10000)
# Plot CSCS Samples
plot(volcano_r, axes=FALSE)
points(fs_sf)
plot(fs_strata, col = map.pal("random", n))
plot(fs_samp, col = "black", cex = 1, pch = 19, add = TRUE)
```

### Conditioned Latin Hypercube (cLHS)
Expand Down Expand Up @@ -502,7 +504,7 @@ sys <- spatSample(rs, size = 20, method = "regular", as.points = TRUE)
cs <- clhs(rs2, size = 20, progress = FALSE, simple = FALSE)
# take a CSCS sample
fs <- cscs(rs, vars = c("elev", "slope"), n = 20)
fs <- sample_nc(rs, nSamp = 20) |> extract(rs, y = _) |> select(elev, slope)
# Combind and Extract Samples
s <- rbind(
Expand Down Expand Up @@ -534,7 +536,7 @@ plot(volcano_r, main = "cLHS", cex.main = 2, axes=FALSE)
points(cs$sampled_data, pch = 3, cex = 1.2)
plot(volcano_r, main = "CSCS", cex.main = 2, axes=FALSE)
points(fs_sf, pch = 3, cex = 1.2)
points(fs_samp, pch = 3, cex = 1.2)
```

The overlapping density plots above illustrate the differences between large and small sets of samples using several sampling designs.
Expand Down
4 changes: 2 additions & 2 deletions Part1/book.bib
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ @book{brus2022
edition = {1},
title = {Spatial {Sampling} with {R}},
isbn = {978-1-00-325894-0},
url = {https://www.taylorfrancis.com/books/9781003258940},
url = {https://dickbrus.github.io/SpatialSamplingwithR/},
language = {en},
urldate = {2023-04-01},
publisher = {Chapman and Hall/CRC},
Expand Down Expand Up @@ -126,7 +126,7 @@ @incollection{gelman2020
edition = {1},
title = {16 - {Design} and sample size decisions},
isbn = {9781139161879 9781107023987 9781107676510},
url = {https://www.cambridge.org/highereducation/product/9781139161879/book},
url = {https://avehtari.github.io/ROS-Examples/},
urldate = {2022-01-31},
booktitle = {Regression and {Other} {Stories}},
publisher = {Cambridge University Press},
Expand Down
18 changes: 17 additions & 1 deletion Part1/packages.bib
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@Manual{R-aqp,
title = {aqp: Algorithms for Quantitative Pedology},
author = {Dylan Beaudette and Pierre Roudier and Andrew Brown},
note = {R package version 2.1.1},
note = {R package version 2.1.0},
url = {https://github.com/ncss-tech/aqp},
year = {2025},
}
Expand Down Expand Up @@ -128,6 +128,14 @@ @Manual{R-sf
url = {https://r-spatial.github.io/sf/},
}

@Manual{R-sgsR,
title = {sgsR: Structurally Guided Sampling},
author = {Tristan RH Goodbody and Nicholas C Coops and Martin Queinnec},
year = {2024},
note = {R package version 1.4.5},
url = {https://github.com/tgoodbody/sgsR},
}

@Manual{R-shape,
title = {shape: Functions for Plotting Graphical Shapes, Colors},
author = {Karline Soetaert},
Expand Down Expand Up @@ -307,6 +315,14 @@ @Article{sf2018
number = {1},
}

@Manual{sgsR2023,
title = {sgsR: a structurally guided sampling toolbox for LiDAR-based forest inventories.},
author = {Tristan R.H. Goodbody and Nicholas C. Coops and Martin Queinnec and Joanne C. White and Piotr Tompalski and Andrew T. Hudak and David Auty and Ruben Valbuena and Antoine LeBoeuf and Ian Sinclair and Grant McCartney and Jean-Francois Prieur and Murray E. Woods},
journal = {Forestry: An International Journal of Forest Research},
year = {2023},
doi = {10.1093/forestry/cpac055},
}

@Article{sp2005,
author = {Edzer J. Pebesma and Roger Bivand},
title = {Classes and methods for spatial data in {R}},
Expand Down
Binary file modified Part1/s4ssbook_files/figure-html/clhs-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/seval-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/simple-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/spcosa-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/stratified-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/systematic-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/two_stage-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/unnamed-chunk-204-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Part1/s4ssbook_files/figure-html/unnamed-chunk-205-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions book/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<meta name="author" content="Soil Survey Staff" />


<meta name="date" content="2025-01-28" />
<meta name="date" content="2025-01-30" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -73,7 +73,7 @@

<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
Expand Down Expand Up @@ -145,7 +145,6 @@
div.csl-bib-body { }
div.csl-entry {
clear: both;
margin-bottom: 0em;
}
.hanging div.csl-entry {
margin-left:2em;
Expand Down
Loading

0 comments on commit 5d44a5f

Please sign in to comment.