Dateien nach "/" hochladen

This commit is contained in:
2026-08-18 22:48:25 +00:00
parent 687c12922b
commit b2981a3019
5 changed files with 1588 additions and 0 deletions
@@ -0,0 +1,508 @@
library(data.table)
library(future)
library(future.apply)
simulate_rai_designs <- function(
run_id,
CFG,
SPECIES,
AREA,
year = 1,
species_names = c("Rehwild", "Rotwild", "Gamswild"),
save_each_design = TRUE,
output_dir = "rai_results",
parallel = FALSE,
n_cores = max(1, parallel::detectCores() - 1),
seed = 777
) {
dir.create(
output_dir,
showWarnings = FALSE,
recursive = TRUE
)
# ------------------------------------------------------------
# 1. Tracks nur einmal pro Art simulieren
# ------------------------------------------------------------
message("Simuliere Tracks ...")
tracks <- lapply(
species_names,
function(sp) {
simulate_tracks(
species = sp,
year = year,
pars = SPECIES[[sp]],
area = AREA,
cfg = CFG
)
}
)
names(tracks) <- species_names
# ------------------------------------------------------------
# 2. Alle Sampling-Designs als Tabelle erzeugen
# ------------------------------------------------------------
grid_designs <- data.table(
sampling = "grid",
grid_spacing = CFG$grid_spacings_m,
n_cameras_requested = NA_integer_,
prop_focus_requested = NA_real_
)
random_designs <- CJ(
n_cameras_requested = CFG$camera_numbers,
prop_focus_requested = CFG$allocations_focus
)
random_designs[, `:=`(
sampling = "random",
grid_spacing = NA_real_
)]
designs <- rbindlist(
list(
grid_designs,
random_designs
),
use.names = TRUE,
fill = TRUE
)
designs[, design_id := .I]
message(
"Anzahl Sampling-Designs: ",
nrow(designs)
)
# ------------------------------------------------------------
# 3. Funktion für EIN Sampling-Design
# ------------------------------------------------------------
simulate_one_design <- function(i) {
design <- designs[i]
set.seed(seed + run_id * 100000 + design$design_id)
# ----------------------------------------------------------
# Dateiname schon vorab definieren
# ----------------------------------------------------------
filename <- if (design$sampling == "grid") {
sprintf(
"run_%04d_grid_%04dm.csv",
run_id,
round(design$grid_spacing)
)
} else {
sprintf(
"run_%04d_random_%03dcams_focus_%03d.csv",
run_id,
round(design$n_cameras_requested),
round(100 * design$prop_focus_requested)
)
}
# ----------------------------------------------------------
# Gesamtes Design absichern
# ----------------------------------------------------------
result <- tryCatch({
# --------------------------------------------------------
# Kameradesign erzeugen
# --------------------------------------------------------
if (design$sampling == "grid") {
cfg_design <- CFG
cfg_design$camera_design_mode <- "grid"
cfg_design$grid_random_rotation <- TRUE
cameras <- make_camera_design(
area = AREA,
cfg = cfg_design,
grid_spacing_m = design$grid_spacing
)
} else {
cfg_design <- CFG
cfg_design$camera_design_mode <- "random"
cameras <- make_camera_design(
n_cameras = design$n_cameras_requested,
area = AREA,
cfg = cfg_design,
prop_focus = design$prop_focus_requested
)
}
# --------------------------------------------------------
# Zusätzliche Plausibilitätschecks
# --------------------------------------------------------
if (is.null(cameras) || nrow(cameras) == 0) {
stop("Es konnten keine Kamerastandorte erzeugt werden.")
}
if (!"zone" %in% names(cameras)) {
stop("Das Kameradesign enthält keine Spalte 'zone'.")
}
# --------------------------------------------------------
# Alle Arten
# --------------------------------------------------------
species_results <- lapply(
species_names,
function(sp) {
tryCatch({
det <- simulate_detections(
tracks = tracks[[sp]],
cameras = cameras,
cfg = cfg_design,
verbose = FALSE
)
out <- summarise_camera_counts(
det = det,
cameras = cameras,
year = year,
species = sp,
cfg = cfg_design,
verbose = FALSE
)
out[, `:=`(
run = run_id,
sampling = design$sampling,
grid_spacing =
if (design$sampling == "grid")
design$grid_spacing
else
NA_real_,
cameras = nrow(cameras),
n_cameras_requested =
design$n_cameras_requested,
prop_focus_requested =
design$prop_focus_requested,
prop_focus_realised =
mean(cameras$zone == "Fokus"),
design_id = design$design_id,
simulation_ok = TRUE,
error_stage = NA_character_,
error_message = NA_character_
)]
out
}, error = function(e) {
# Fehler nur für eine Art:
data.table::data.table(
run = run_id,
year = year,
species = sp,
sampling = design$sampling,
grid_spacing =
if (design$sampling == "grid")
design$grid_spacing
else
NA_real_,
cameras = nrow(cameras),
n_cameras_requested =
design$n_cameras_requested,
prop_focus_requested =
design$prop_focus_requested,
prop_focus_realised =
mean(cameras$zone == "Fokus"),
design_id = design$design_id,
simulation_ok = FALSE,
error_stage = "species_simulation",
error_message = conditionMessage(e),
camera_id = NA_character_,
zone = NA_character_,
effort_days = NA_real_,
events = NA_integer_,
RAI_100_days = NA_real_
)
})
}
)
data.table::rbindlist(
species_results,
use.names = TRUE,
fill = TRUE
)
}, error = function(e) {
# --------------------------------------------------------
# Fehler beim gesamten Sampling-Design
# z.B. Mindestabstand nicht realisierbar
# --------------------------------------------------------
data.table::data.table(
run = run_id,
year = year,
species = species_names,
sampling = design$sampling,
grid_spacing =
if (design$sampling == "grid")
design$grid_spacing
else
NA_real_,
cameras = NA_integer_,
n_cameras_requested =
design$n_cameras_requested,
prop_focus_requested =
design$prop_focus_requested,
prop_focus_realised = NA_real_,
design_id = design$design_id,
simulation_ok = FALSE,
error_stage = "camera_design",
error_message = conditionMessage(e),
camera_id = NA_character_,
zone = NA_character_,
effort_days = NA_real_,
events = NA_integer_,
RAI_100_days = NA_real_
)
})
# ----------------------------------------------------------
# Zwischenstand IMMER speichern
# auch wenn das Design fehlgeschlagen ist
# ----------------------------------------------------------
if (save_each_design) {
data.table::fwrite(
result,
file.path(output_dir, filename)
)
}
# Konsolenmeldung
if (all(result$simulation_ok %in% FALSE)) {
message(
"Design ", design$design_id,
" fehlgeschlagen: ",
unique(result$error_message)[1]
)
} else {
message(
"Design ", design$design_id,
" erfolgreich abgeschlossen."
)
}
return(result)
}
# ------------------------------------------------------------
# 4. Designs sequenziell oder parallel
# ------------------------------------------------------------
if (parallel) {
future::plan(
future::multisession,
workers = n_cores
)
on.exit(
future::plan(future::sequential),
add = TRUE
)
message(
"Parallelisierung mit ",
n_cores,
" Workern."
)
results <- future.apply::future_lapply(
seq_len(nrow(designs)),
simulate_one_design,
future.seed = TRUE
)
} else {
results <- lapply(
seq_len(nrow(designs)),
function(i) {
message(
"Design ",
i,
"/",
nrow(designs)
)
simulate_one_design(i)
}
)
}
# ------------------------------------------------------------
# 5. Alles zusammenführen
# ------------------------------------------------------------
all_results <- rbindlist(
results,
use.names = TRUE,
fill = TRUE
)
# ------------------------------------------------------------
# 6. Gesamten Run speichern
# ------------------------------------------------------------
run_file <- file.path(
output_dir,
sprintf(
"run_%04d_all_designs.csv",
run_id
)
)
fwrite(
all_results,
run_file
)
# ------------------------------------------------------------
# 7. RAI-Zusammenfassung
# ------------------------------------------------------------
rai_summary <- all_results[
simulation_ok == TRUE,
.(
mean_RAI = mean(RAI_100_days, na.rm = TRUE),
sd_RAI = sd(RAI_100_days, na.rm = TRUE),
median_RAI = median(RAI_100_days, na.rm = TRUE),
n_cameras = uniqueN(camera_id),
n_zero_cameras = sum(events == 0, na.rm = TRUE),
prop_zero_cameras = mean(
events == 0,
na.rm = TRUE
),
total_events = sum(
events,
na.rm = TRUE
)
),
by = .(
run,
species,
sampling,
grid_spacing,
n_cameras_requested,
prop_focus_requested,
prop_focus_realised,
cameras
)
]
fwrite(
rai_summary,
file.path(
output_dir,
sprintf(
"run_%04d_RAI_summary.csv",
run_id
)
)
)
error_summary <- all_results[
simulation_ok == FALSE,
.(
n_errors = .N
),
by = .(
run,
sampling,
grid_spacing,
n_cameras_requested,
prop_focus_requested,
error_stage,
error_message
)
]
fwrite(
error_summary,
file.path(
output_dir,
sprintf(
"run_%04d_errors.csv",
run_id
)
)
)
return(
list(
camera_level = all_results,
rai_summary = rai_summary,
designs = designs
)
)
}
+245
View File
@@ -0,0 +1,245 @@
#
# Libraries ---------------------------------------------------------------
source("fotofallen_power_simulation_schalenwild_grid.R")
# Data --------------------------------------------------------------------
CFG <- list(
crs = 32632, # Beispiel: UTM 32N; an Projektgebiet anpassen
years = 1:5,
survey_days = 30,
fixes_per_day = 1440/5, # 30-min Schritte; fuer feinere Detektion erhoehen
n_replicates = 500, # fuer finale Analyse eher 500-1000
alpha = 0.05,
target_power = 0.80,
# Kameradesign: "random" = vorgegebene Anzahl zufaelliger Standorte;
# "grid" = zufaellig verschobenes/gedrehtes Raster.
camera_design_mode = "grid",
camera_numbers = c(20, 30, 40, 60),
allocations_focus = c(0.50, 0.75, 0.90),#c(0.35, 0.50, 0.65, 0.80),anteil zufaelliger standorte auf fokus
min_camera_spacing_m = 300,
# Bei Grid-Design bestimmt der Rasterabstand direkt die Kamerazahl.
# Mehrere Werte koennen in der Poweranalyse verglichen werden.
grid_spacings_m = c(300, 400, 500, 700),
grid_random_rotation = TRUE,
grid_clip_to = "outer", # "outer", "focus" oder "buffer"
focus_radius_m = 1000,
buffer_width_m = 1500,
landscape_margin_m = 1800,
detection_radius_m = 20,
p_detect_5m = 0.95,
p_detect_18m = 0.05,
independence_minutes = 11,
# relative Aenderung der Nutzung der Fokusflaeche von Jahr 1 bis Jahr 5:
focus_use_multiplier_end = 0.5,#c(0.80, 0.70, 0.50, 0.40),
# Gesamtpopulation konstant (=0) oder jaehrliche proportionale Aenderung:
annual_population_change = 0, #c(0.00, -0.03, -0.05),
# gleiche Kamerastandorte ueber die Jahre erhoehen Vergleichbarkeit:
permanent_cameras = TRUE
)
# ---- 2. Artprofile ----------------------------------------------------------
# home_range_ha beeinflusst die Streuung um das Streifgebietszentrum.
# step_scale_m beeinflusst kurzfristige Bewegung; attraction_strength die
# Rueckkehr zu Attraktionspunkten. Gamswild kann zusaetzlich durch ein
# Habitat-/Topographie-Raster gewichtet werden (siehe habitat_weight_function).
SPECIES <- list(
Rehwild = list(
n_animals = 20,
home_range_ha = 70,
step_scale_m = 38,
n_attraction_points = 4,
attraction_strength = 0.10,
resting_probability = 0.25,
speed_km_day = 1.00,
group_size_mean = 1.3
),
Rotwild = list(
n_animals = 40,
home_range_ha = 300,
step_scale_m = 105,
n_attraction_points = 3,
attraction_strength = 0.055,
resting_probability = 0.30,
speed_km_day = 3.0,
group_size_mean = 3.5
),
Gamswild = list(
n_animals = 50,
home_range_ha = 150,
step_scale_m = 70,
n_attraction_points = 5,
attraction_strength = 0.075,
resting_probability = 0.10,
speed_km_day = 1.5,
group_size_mean = 4.2
)
)
species_names <- c("Rehwild", "Rotwild", "Gamswild")
AREA <- make_study_area()
# Simulation --------------------------------------------------------------
tracks_demo <- simulate_tracks(
species = "Gamswild",
year = 1,
pars = SPECIES[["Gamswild"]],
area = AREA,
cfg = CFG
)
id_demo <- unique(tracks_demo$animal_id)[1]
one_animal <- tracks_demo[animal_id == id_demo]
one_sf <- st_as_sf(
one_animal,
coords = c("x", "y"),
crs = CFG$crs,
remove = FALSE
)
ggplot() +
geom_sf(data = AREA$zones, aes(fill = zone), alpha = 0.20) +
geom_path(
data = one_animal,
aes(x = x, y = y),
linewidth = 0.35
) +
geom_sf(data = one_sf[1, ], size = 2.5) +
coord_sf() +
labs(
title = paste("Beispielbewegung eines Individuums:", "Gamswild"),
subtitle = id_demo,
fill = "Zone"
) +
theme_bw()
# test run parallel
CFG$n_replicates <- 5
system.time({
ans_test <- run_power_analysis_parallel(
species_names = "Rehwild",
cfg = CFG,
n_cores = 10
)
})
ans_test$summary
CFG$n_replicates <- 5
system.time({
ans_test <- run_power_analysis(
species_names = "Gamswild",
cfg = CFG
)
})
CFG$n_replicates <- 500
CFG$parallel <- TRUE
CFG$n_cores <- 7
# Power Analysis ----------------------------------------------------------
# run in parallel
n.cores <- max(
1,
parallel::detectCores() - 4
)
ans <- run_power_analysis_parallel(
species_names = c(
"Rehwild",
"Rotwild",
"Gamswild"
),
cfg = CFG,
n_cores = n.cores,
seed = 777
)
ans <- run_power_analysis(
species_names = c("Rehwild", "Rotwild", "Gamswild"),
cfg = CFG
)
data.table::fwrite(ans$raw, "power_raw.csv")
data.table::fwrite(ans$summary, "power_summary.csv")
# Output ------------------------------------------------------------------
ans$summary
ggplot(
ans$summary,
aes(
x = n_cameras,
y = power,
group = factor(grid_spacing_m),
linetype = factor(grid_spacing_m)
)
) +
geom_line() +
geom_point() +
geom_hline(
yintercept = CFG$target_power,
linetype = 2
) +
facet_wrap(~ species) +
scale_y_continuous(limits = c(0, 1)) +
labs(
x = "Mittlere tatsächliche Kamerazahl",
y = "Statistische Power",
linetype = "Rasterabstand (m)",
title = "Power zur Erkennung einer Abnahme der Fokusflächennutzung"
) +
theme_bw()
# Auch im Grid-Modus wird gegen die tatsaechlich resultierende Kamerazahl geplottet.
p <- ggplot(ans$summary,
aes(x = n_cameras, y = power,
group = if (identical(CFG$camera_design_mode, "grid"))
factor(grid_spacing_m) else factor(prop_focus),
linetype = if (identical(CFG$camera_design_mode, "grid"))
factor(grid_spacing_m) else factor(prop_focus))) +
geom_line() + geom_point() +
geom_hline(yintercept = CFG$target_power, linetype = 2) +
facet_wrap(~ species) +
scale_y_continuous(limits = c(0,1)) +
labs(x = "Gesamtzahl Kameras", y = "Power",
linetype = if (identical(CFG$camera_design_mode, "grid"))
"Grid-Abstand (m)" else "Anteil Fokus",
title = "Power zur Erkennung einer Abnahme der Fokusflaechennutzung") +
theme_bw()
p
ggsave("power_sampling_design.png", p, width = 10, height = 6, dpi = 300)
# Kleinstes Design je Art mit Ziel-Power:
recommendation <- ans$summary[reaches_target == TRUE][
order(n_cameras, -power), .SD[1], by = species]
print(recommendation)
+362
View File
@@ -0,0 +1,362 @@
CFG <- list(
crs = 32632, # Beispiel: UTM 32N; an Projektgebiet anpassen
years = 1,
survey_days = 90,
fixes_per_day = 1440/5, # 30-min Schritte; fuer feinere Detektion erhoehen
n_replicates = 100, # fuer finale Analyse eher 500-1000
alpha = 0.05,
target_power = 0.80,
# Kameradesign: "random" = vorgegebene Anzahl zufaelliger Standorte;
# "grid" = zufaellig verschobenes/gedrehtes Raster.
camera_design_mode = "grid",
camera_numbers = c(20, 30, 40),
allocations_focus = c(0.50, 0.75, 0.90),#c(0.35, 0.50, 0.65, 0.80),anteil zufaelliger standorte auf fokus
min_camera_spacing_m = 300,
# Bei Grid-Design bestimmt der Rasterabstand direkt die Kamerazahl.
# Mehrere Werte koennen in der Poweranalyse verglichen werden.
grid_spacings_m = c(300, 500, 700),
grid_random_rotation = TRUE,
grid_clip_to = "buffer", # "outer", "focus" oder "buffer"
focus_radius_m = 1000,
buffer_width_m = 1500,
landscape_margin_m = 1800,
detection_radius_m = 20,
p_detect_5m = 0.95,
p_detect_18m = 0.05,
independence_minutes = 11,
# relative Aenderung der Nutzung der Fokusflaeche von Jahr 1 bis Jahr 5:
focus_use_multiplier_end = 0.5,#c(0.80, 0.70, 0.50, 0.40),
# Gesamtpopulation konstant (=0) oder jaehrliche proportionale Aenderung:
annual_population_change = 0, #c(0.00, -0.03, -0.05),
# gleiche Kamerastandorte ueber die Jahre erhoehen Vergleichbarkeit:
permanent_cameras = TRUE
)
SPECIES <- list(
Rehwild = list(
n_animals = 20,
home_range_ha = 70,
step_scale_m = 38,
n_attraction_points = 4,
attraction_strength = 0.10,
resting_probability = 0.25,
speed_km_day = 1.00,
group_size_mean = 1.3
),
Rotwild = list(
n_animals = 40,
home_range_ha = 300,
step_scale_m = 105,
n_attraction_points = 3,
attraction_strength = 0.055,
resting_probability = 0.30,
speed_km_day = 3.0,
group_size_mean = 3.5
),
Gamswild = list(
n_animals = 50,
home_range_ha = 150,
step_scale_m = 70,
n_attraction_points = 5,
attraction_strength = 0.075,
resting_probability = 0.10,
speed_km_day = 1.5,
group_size_mean = 4.2
)
)
species_names <- c("Rehwild", "Rotwild", "Gamswild")
AREA <- make_study_area()
# One Simulation ----------------------------------------------------------
run <- 1
# tracks
tracks_roe <- simulate_tracks(
species = "Rehwild",
year = 1,
pars = SPECIES$Rehwild,
area = AREA,
cfg = CFG
)
tracks_red <- simulate_tracks(
species = "Rotwild",
year = 1,
pars = SPECIES$Rotwild,
area = AREA,
cfg = CFG
)
tracks_cham <- simulate_tracks(
species = "Gamswild",
year = 1,
pars = SPECIES$Gamswild,
area = AREA,
cfg = CFG
)
# grid sampling design --------------------- ##
cfg_grid <- CFG
cfg_grid$camera_design_mode <- "grid"
cfg_grid$grid_random_rotation <- TRUE
grids <- list()
n <- 1
for(gsm in CFG$grid_spacings_m){
# make grid
cams_grid <- make_camera_design(
area = AREA,
cfg = cfg_grid,
grid_spacing_m = gsm
)
# get detections
detections_roe_grid <- simulate_detections(
tracks = tracks_roe,
cameras = cams_grid,
cfg = cfg_grid
)
detections_red_grid <- simulate_detections(
tracks = tracks_red,
cameras = cams_grid,
cfg = cfg_grid
)
detections_cham_grid <- simulate_detections(
tracks = tracks_cham,
cameras = cams_grid,
cfg = cfg_grid
)
# summarise detections
camera_counts_roe_grid <- summarise_camera_counts(
det = detections_roe_grid,
cameras = cams_grid,
year = 1,
species = "Rehwild",
cfg = cfg_grid
)
camera_counts_red_grid <- summarise_camera_counts(
det = detections_red_grid,
cameras = cams_grid,
year = 1,
species = "Rotwild",
cfg = cfg_grid
)
camera_counts_cham_grid <- summarise_camera_counts(
det = detections_cham_grid,
cameras = cams_grid,
year = 1,
species = "Gamswild",
cfg = cfg_grid
)
# combine data frame
camera_counts <- list(
camera_counts_roe_grid,
camera_counts_red_grid,
camera_counts_cham_grid
) %>% bind_rows()
camera_counts[, ':=' (sampling = "grid",
grid_spacing = gsm,
cameras = nrow(cams_grid))]
grids[[n]] <- camera_counts
n <- n + 1
}
grids_results <- grids %>% bind_rows()
# random sampling design --------------------- ##
cfg_random <- CFG
cfg_random$camera_design_mode <- "random"
cfg_random$random_random_rotation <- TRUE
randoms <- list()
n <- 1
for(cms in CFG$camera_numbers){
for(af in CFG$allocations_focus){
cms <- 20
af <- 0.5
cams_random <- make_camera_design(
n_cameras = cms,
area = AREA,
cfg = cfg_random,
prop_focus = af)
# random sampling
detections_roe_random <- simulate_detections(
tracks = tracks_roe,
cameras = cams_random,
cfg = cfg_random
)
detections_red_random <- simulate_detections(
tracks = tracks_red,
cameras = cams_random,
cfg = cfg_random
)
detections_cham_random <- simulate_detections(
tracks = tracks_cham,
cameras = cams_random,
cfg = cfg_random
)
# random sampling
camera_counts_roe_random <- summarise_camera_counts(
det = detections_roe_random,
cameras = cams_random,
year = 1,
species = "Rehwild",
cfg = cfg_random
)
camera_counts_red_random <- summarise_camera_counts(
det = detections_red_random,
cameras = cams_random,
year = 1,
species = "Rotwild",
cfg = cfg_random
)
camera_counts_cham_random <- summarise_camera_counts(
det = detections_cham_random,
cameras = cams_random,
year = 1,
species = "Gamswild",
cfg = cfg_random
)
# combine data frame
camera_counts <- list(
camera_counts_roe_random,
camera_counts_red_random,
camera_counts_cham_random
) %>% bind_rows()
camera_counts[, ':=' (sampling = "random",
grid_spacing = NA_integer_,
cameras = cms,
prop_focus = af)]
randoms[[n]] <- camera_counts
n <- n + 1
}
}
randoms_results <- randoms %>% bind_rows()
run_results <- list(grids_results, randoms_results) %>% bind_rows()
run_results[, run := run]
# Optimized Code ----------------------------------------------------------
source("fotofallen_power_simulation_schalenwild_rai.R")
res <- simulate_rai_designs(
run_id = 1,
CFG = CFG,
SPECIES = SPECIES,
AREA = AREA,
save_each_design = TRUE,
output_dir = "rai_results",
parallel = TRUE,
n_cores = 4
)
for (run_id in 1:100) {
message(
"\n==============================",
"\nRUN ", run_id,
"\n=============================="
)
simulate_rai_designs(
run_id = run_id,
CFG = CFG,
SPECIES = SPECIES,
AREA = AREA,
parallel = TRUE,
n_cores = 6,
output_dir = "rai_results"
)
}
# highly efficient parallelisation ------------------------ ##
future::plan(
future::multisession,
workers = 12
)
all_runs <- future.apply::future_lapply(
10:100,
function(run_id) {
simulate_rai_designs(
run_id = run_id,
CFG = CFG,
SPECIES = SPECIES,
AREA = AREA,
parallel = FALSE,
save_each_design = TRUE,
output_dir = "rai_results"
)
},
future.seed = TRUE
)
+33
View File
@@ -0,0 +1,33 @@
results <- list.files("rai_results", pattern = "grid|random", full.names = T)
results <- results %>% map_dfr(., function(r){read.csv(r)})
results %>% count(run)
library(ggplot2)
ggplot(results, aes(species, RAI_100_days, fill=zone)) +
geom_violin(aes(fill = zone))+
facet_grid(. ~ sampling)
ggplot(results, aes(species, RAI_100_days)) +
geom_boxplot(aes())+
facet_grid(. ~ sampling)
results %>% filter(sampling == "random") %>% count(cameras)
ggplot(results %>% filter(sampling == "random"), aes(species, RAI_100_days)) +
geom_boxplot(aes())+
facet_grid( prop_focus_requested ~ cameras)
ggplot(results %>% filter(sampling == "grid"), aes(species, RAI_100_days)) +
geom_boxplot(aes())+
facet_grid( grid_spacing ~ cameras)
+440
View File
@@ -0,0 +1,440 @@
library(tidytable)
# Make CFG ----------------------------------------------------------------
CFG
CFG <- list(
crs = 32632, # Beispiel: UTM 32N; an Projektgebiet anpassen
years = 1,
survey_days = 90,
fixes_per_day = 1440/5, # 30-min Schritte; fuer feinere Detektion erhoehen
n_replicates = 100, # fuer finale Analyse eher 500-1000
alpha = 0.05,
target_power = 0.80,
# Kameradesign: "random" = vorgegebene Anzahl zufaelliger Standorte;
# "grid" = zufaellig verschobenes/gedrehtes Raster.
camera_design_mode = "grid",
camera_numbers = c(20, 30, 40),
allocations_focus = c(0.50, 0.75, 0.90),#c(0.35, 0.50, 0.65, 0.80),anteil zufaelliger standorte auf fokus
min_camera_spacing_m = 300,
# Bei Grid-Design bestimmt der Rasterabstand direkt die Kamerazahl.
# Mehrere Werte koennen in der Poweranalyse verglichen werden.
grid_spacings_m = c(300, 500, 700),
grid_random_rotation = TRUE,
grid_clip_to = "buffer", # "outer", "focus" oder "buffer"
focus_radius_m = 1000,
buffer_width_m = 1500,
landscape_margin_m = 1800,
detection_radius_m = 20,
p_detect_5m = 0.95,
p_detect_18m = 0.05,
independence_minutes = 11,
# relative Aenderung der Nutzung der Fokusflaeche von Jahr 1 bis Jahr 5:
focus_use_multiplier_end = 0.5,#c(0.80, 0.70, 0.50, 0.40),
# Gesamtpopulation konstant (=0) oder jaehrliche proportionale Aenderung:
annual_population_change = 0, #c(0.00, -0.03, -0.05),
# gleiche Kamerastandorte ueber die Jahre erhoehen Vergleichbarkeit:
permanent_cameras = TRUE
)
# Define Movement Data ----------------------------------------------------
SPECIES <- list(
Rehwild = list(
n_animals = 20,
home_range_ha = 70,
step_scale_m = 38,
n_attraction_points = 4,
attraction_strength = 0.10,
resting_probability = 0.25,
speed_km_day = 1.00,
group_size_mean = 1.3
),
Rotwild = list(
n_animals = 40,
home_range_ha = 300,
step_scale_m = 105,
n_attraction_points = 3,
attraction_strength = 0.055,
resting_probability = 0.30,
speed_km_day = 3.0,
group_size_mean = 3.5
),
Gamswild = list(
n_animals = 50,
home_range_ha = 150,
step_scale_m = 70,
n_attraction_points = 5,
attraction_strength = 0.075,
resting_probability = 0.10,
speed_km_day = 1.5,
group_size_mean = 4.2
)
)
species_names <- c("Rehwild", "Rotwild", "Gamswild")
SPECIES
# Simulate Tracks ---------------------------------------------------------
tracks_roe <- simulate_tracks(
species = "Rehwild",# "Rotwild", "Gamswild",
year = 1,
pars = SPECIES$Rehwild,
area = AREA,
cfg = CFG
)
tracks_red <- simulate_tracks(
species = "Rotwild", #"Rehwild", #"Gamswild",
year = 1,
pars = SPECIES$Rotwild,
area = AREA,
cfg = CFG
)
tracks_cham <- simulate_tracks(
species = "Gamswild", #"Rotwild", #"Rehwild", #
year = 1,
pars = SPECIES$Gamswild,
area = AREA,
cfg = CFG
)
# Simulate Camera Locations -----------------------------------------------
# grid sampling design --------------------- ##
cfg_grid <- CFG
cfg_grid$camera_design_mode <- "grid"
cfg_grid$grid_random_rotation <- TRUE
cams_grid <- make_camera_design(
area = AREA,
cfg = cfg_grid,
grid_spacing_m = 300
)
# random sampling design --------------------- ##
cfg_random <- CFG
cfg_random$camera_design_mode <- "random"
cfg_random$random_random_rotation <- TRUE
cfg_random$camera_numbers <- 30
cams_random <- make_camera_design(
n_cameras = cfg_random$camera_numbers,
area = AREA,
cfg = cfg_random,
prop_focus = .75
)
# Plot Tracks -------------------------------------------------------------
# single individuals as an example
id_demo <- unique(tracks_demo$animal_id)[1]
one_animal <- tracks_demo[animal_id == id_demo]
one_sf <- st_as_sf(
one_animal,
coords = c("x", "y"),
crs = CFG$crs,
remove = FALSE
)
animals <- st_as_sf(
tracks_demo,
coords = c("x", "y"),
crs = CFG$crs,
remove = FALSE
)
ggplot() +
geom_sf(data = AREA$zones, aes(fill = zone), alpha = 0.20) +
geom_path(
data = animals,
aes(x = x, y = y, group = animal_id, color=animal_id),
linewidth = 0.35, alpha=.3
) +
geom_sf(data = animals[1, ], size = 2.5) +
coord_sf() +
labs(
title = paste("Beispielbewegung eines Individuums:", "Rehwild"),
#subtitle = id_demo,
fill = "Zone"
) +
theme_bw()
tracks_demo <- simulate_tracks(
species = "Rotwild",
year = 1,
pars = SPECIES[["Rotwild"]],
area = AREA,
cfg = CFG
)
id_demo <- unique(tracks_demo$animal_id)[1]
one_animal <- tracks_demo[animal_id == id_demo]
one_sf <- st_as_sf(
one_animal,
coords = c("x", "y"),
crs = CFG$crs,
remove = FALSE
)
ggplot() +
geom_sf(data = AREA$zones, aes(fill = zone), alpha = 0.20) +
geom_path(
data = one_animal,
aes(x = x, y = y),
linewidth = 0.35
) +
geom_sf(data = one_sf[1, ], size = 2.5) +
coord_sf() +
labs(
title = paste("Beispielbewegung eines Individuums:", "Rotwild"),
subtitle = id_demo,
fill = "Zone"
) +
theme_bw()
# add camera locations
ggplot() +
geom_sf(data = AREA$zones, aes(fill = zone), alpha = 0.25) +
geom_sf(data = cams_demo, shape = 21, size = 2.5) +
coord_sf() +
labs(
title = paste("Zufällig verschobenes Kameraraster:",
300, "m"),
subtitle = paste(nrow(cams_demo), "potenzielle Kamerastandorte"),
fill = "Zone"
) +
theme_bw()
# Simulate Detections -----------------------------------------------------
# grid sampling
detections_roe_grid <- simulate_detections(
tracks = tracks_roe,
cameras = cams_grid,
cfg = cfg_grid
)
detections_red_grid <- simulate_detections(
tracks = tracks_red,
cameras = cams_grid,
cfg = cfg_grid
)
detections_cham_grid <- simulate_detections(
tracks = tracks_cham,
cameras = cams_grid,
cfg = cfg_grid
)
# random sampling
detections_roe_random <- simulate_detections(
tracks = tracks_roe,
cameras = cams_random,
cfg = cfg_random
)
detections_red_random <- simulate_detections(
tracks = tracks_red,
cameras = cams_random,
cfg = cfg_random
)
detections_cham_random <- simulate_detections(
tracks = tracks_cham,
cameras = cams_random,
cfg = cfg_random
)
# # pruefen ob tiere in kameranähe gelangen
#
# tracks_sf <- sf::st_as_sf(
# tracks_demo,
# coords = c("x", "y"),
# crs = CFG$crs,
# remove = FALSE
# )
#
# nearest_camera <- sf::st_nearest_feature(
# tracks_sf,
# cams_demo
# )
#
# distance_to_camera <- as.numeric(
# sf::st_distance(
# tracks_sf,
# cams_demo[nearest_camera, ],
# by_element = TRUE
# )
# )
#
# summary(distance_to_camera)
#
# sum(distance_to_camera <= CFG$detection_radius_m)
#
#
# # schneller funktionstest
#
# CFG_test <- CFG
# CFG_test$fixes_per_day <- 288
# CFG_test$detection_radius_m <- 100
# CFG_test$p_detect_5m <- 0.99
# CFG_test$p_detect_18m <- 0.50
#
# tracks_test <- simulate_tracks(
# "Rehwild",
# 1,
# SPECIES$Rehwild,
# AREA,
# CFG_test
# )
#
# cameras <- make_camera_design(
# area = AREA,
# cfg = CFG_test,
# grid_spacing_m = 250
# )
#
# det_test <- simulate_detections(
# tracks_test,
# cams_test,
# CFG_test
# )
#
# print(det_test)
#
# summarise_camera_counts(
# det = det_test,
# cameras = cams_test,
# year = 1,
# species = "Rehwild",
# cfg = CFG_test
# )
# Summarise Detections ----------------------------------------------------
# grid sampling
camera_counts_roe_grid <- summarise_camera_counts(
det = detections_roe_grid,
cameras = cams_grid,
year = 1,
species = "Rehwild",
cfg = cfg_grid
)
camera_counts_red_grid <- summarise_camera_counts(
det = detections_red_grid,
cameras = cams_grid,
year = 1,
species = "Rotwild",
cfg = cfg_grid
)
camera_counts_cham_grid <- summarise_camera_counts(
det = detections_cham_grid,
cameras = cams_grid,
year = 1,
species = "Gamswild",
cfg = cfg_grid
)
# random sampling
camera_counts_roe_random <- summarise_camera_counts(
det = detections_roe_random,
cameras = cams_random,
year = 1,
species = "Rehwild",
cfg = cfg_random
)
camera_counts_red_random <- summarise_camera_counts(
det = detections_red_random,
cameras = cams_random,
year = 1,
species = "Rotwild",
cfg = cfg_random
)
camera_counts_cham_random <- summarise_camera_counts(
det = detections_cham_random,
cameras = cams_random,
year = 1,
species = "Gamswild",
cfg = cfg_random
)
# combine data frame
camera_counts <- list(
camera_counts_roe_grid[, sampling := "grid"],
camera_counts_red_grid[, sampling := "grid"],
camera_counts_cham_grid[, sampling := "grid"],
camera_counts_roe_random[, sampling := "random"],
camera_counts_red_random[, sampling := "random"],
camera_counts_cham_random[, sampling := "random"]
) %>% bind_rows()
# Test Statistics ---------------------------------------------------------
# RAI
ggplot(camera_counts, aes(species, RAI_100_days)) +
geom_boxplot() +
facet_grid(sampling~zone)
# REM Density
rem_density_index(camera_counts = camera_counts_roe_grid,
species_pars = SPECIES$Rehwild,
cfg = cfg_grid)
2*cfg_grid$detection_radius_m /1000
camera_counts_roe_grid[, density_index := (events/effort_days)/pmax(1e-9, SPECIES$Rehwild$speed_km_day*0.04)]
camera_counts_roe_grid
camera_counts_roe_grid
# compare multiple years (if simulated)
fit_change_test(camera_counts_roe_grid)