-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainSampleGeneration.R
More file actions
executable file
·77 lines (49 loc) · 2.19 KB
/
mainSampleGeneration.R
File metadata and controls
executable file
·77 lines (49 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
pacman::p_load(dplyr, terra,sf,readr)
# updated methods for sampling
pacman::p_load("terra", "dplyr", "readr", "sf", "tictoc", "tmap")
#source
source("functions/samplingWorkflowFunctions.R")
# grid feature
grid <- st_read("data/products/newModelAreas/results10kGrid.gpkg")
# summarize data by subset regions ------------------------------------------
regions <- list.files("data/derived/spatialFiles",
full.names = TRUE)
eco <- regions[grepl(pattern = "us_eco", regions)] |>st_read()
mlra <- regions[grepl(pattern = "MLRA", regions)] |>st_read()
lrr <- regions[grepl(pattern = "LRR", regions)] |>st_read()
koppen <- regions[grepl(pattern = "Koppen", regions)] |>st_read()
# levels of analysis
## geographic stratification (ecoregions, koppen climate, lrr, mlra)
# default inputs ---------------------------------------------------------
# 10k grids
# geographic stratification -----------------------------------------------
# based on the type gather
# options are eco, MLRA, LRR, Koppen
for(areaType in c("eco", "MLRA", "LRR", "Koppen")){
# pull area specific info
geoPaths <- getGeographicAreas(areaType = areaType)
columnID <- geoPaths$columnID
vect <- geoPaths$vect |> terra::vect() |> terra::aggregate(by = columnID)
files <- geoPaths$files
allNames <- unique(vect[,columnID]) |>
as.data.frame() |>
pull()
## sub units within each stratification
### this provides all the require information for the sampling method
subUnits <- purrr::map(.x = allNames,
.f = subUnitSelection,
columnID = columnID,
vect = vect,
files = files)
# Run Random Sampling ----------------------------------------------------
## for each year
## set threshold range
## sample N values and average results
## repeat 20 times
## test to see if 18 of the twenty samples fall within the range, then stop
plan(strategy = "multicore", workers = 12)
rand2 <- furrr::future_map(.x = subUnits, .f = runStratifiedSample) |>
dplyr::bind_rows()
write.csv(x = rand2,file = paste0("data/derived/areaCounts/stratified_",areaType,".csv"))
}