Skip to content

Commit

Permalink
Add info SPRING
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaCartuyvels1 committed Dec 10, 2024
1 parent 38a9bc3 commit 2b186a5
Showing 1 changed file with 72 additions and 16 deletions.
88 changes: 72 additions & 16 deletions source/power_analyse.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ output: html_document
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(RODBC)
library(here)
library(tidyverse)
library(lme4)
Expand All @@ -16,9 +18,9 @@ set.seed(123)

```{r define parameters}
# Simulatieparameters
years <- 0:6 # Aantal jaren (24 jaar)
sampling_freq <- c(1, 3, 6) # Frequenties: jaarlijks, elke 3 of 6 jaar
n_locations <- 50 # Aantal locaties
cyclus <- c(1,3,6) # Lengte cyclus

Check warning on line 22 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=22,col=15,[commas_linter] Commas should always have a space after.

Check warning on line 22 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=22,col=17,[commas_linter] Commas should always have a space after.
decline_rate <- 0.01 # "Echte" trend in de populatie
# Populatieparameters
n_species <- 819 # Totaal aantal soorten
Expand All @@ -30,29 +32,52 @@ sd_peak_day <- 25 # Spreiding rond piekdag
sampling_period <- c(91, 243) # Samplingperiode: april-augustus (dag 91 tot 243)

Check warning on line 32 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=32,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 85 characters.
# Trends
decline_rate_6y <- rnorm(n_species, -0.06, 25)
decline_rate_6y <- rnorm(n_species, -0.06, 0.25)
# Correctiefactoren
temp_effect <- rnorm(1000, mean = 20, sd = 5) # Temperatuur (voorbeeld)
flower_effect <- rnorm(1000, mean = 50, sd = 10) # Bloemen (voorbeeld)
```

```{r startpopulatie}
```{r gegevens SPRING}
# Gemiddeld aantal individuen per soort per locatie
conn <- odbcConnectAccess2007("../data/SPRING.accdb")
identifications <- sqlFetch(conn, "identifications")
RODBC::odbcClose(conn)
identifications <- identifications %>%
janitor::clean_names() %>%
as_tibble() %>%
rename(
no_ind = "no_males_females",
species_nm = "species_nm_author_year"
) %>%
rename(
family = familiy
)
n_ind_per_loc <- identifications |>
group_by(species_nm) |>
summarise(mean = mean(no_ind, na.rm = TRUE),
sd = sd(no_ind, na.rm = ))
```

```{r species per location}
n_spec_per_loc <- as.integer(pmax(0,

Check warning on line 66 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=66,col=37,[trailing_whitespace_linter] Trailing whitespace is superfluous.
rnorm(n_locations,
avg_species_per_loc,
sd_species_per_loc)))
```

```{r}
```{r which species are present}
simulate_species_presence <- function(n_species,
n_present,
general_prob,
rare_prob) {
# Categoriseer soorten in algemeen (eerste 20%) en zeldzaam (rest)
# Categoriseer soorten in algemeen (eerste 10%) en zeldzaam (rest)
species <- tibble(

Check warning on line 78 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=78,col=14,[object_usage_linter] no visible global function definition for 'tibble'
species_id = 1:n_species,
presence_prob = ifelse(species_id <= round(0.2 * n_species),
presence_prob = ifelse(species_id <= round(0.1 * n_species),

Check warning on line 80 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=80,col=28,[object_usage_linter] no visible binding for global variable 'species_id'
general_prob,
rare_prob) # De eerste 20% zijn algemene soorten
)
Expand All @@ -61,16 +86,31 @@ simulate_species_presence <- function(n_species,
species <- species %>%

Check warning on line 86 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=86,col=22,[object_usage_linter] no visible global function definition for '%>%'
mutate(is_present = rbinom(1,

Check warning on line 87 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=87,col=5,[object_usage_linter] no visible global function definition for 'mutate'
size = 1,
prob = presence_prob) # Aanwezigheidsstatus
) %>%
filter(is_present == 1) %>% # Filter alleen aanwezige soorten
slice_sample(n = n_present) # Selecteer willekeurig het aantal soorten per locatie
prob = presence_prob)) # Aanwezigheidsstatus

Check warning on line 89 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=89,col=39,[object_usage_linter] no visible binding for global variable 'presence_prob'
if (sum(species$is_present) >= n_present){
species <- species |>
filter(is_present == 1) %>% # Filter alleen aanwezige soorten
slice_sample(n = n_present) # Selecteer willekeurig het aantal soorten per locatie
} else {
d <- n_present - sum(species$is_present)
species1 <- species |>
filter(is_present == 1) %>% # Filter alleen aanwezige soorten
slice_sample(n = n_present)
species2 <- species |>
filter(is_present == 0) %>% # Filter alleen aanwezige soorten
slice_sample(n = d)
species <- species1 |>
add_row(species2)
}
return(species$species_id)
}
```
```{r}
# Simuleer soorten per locatie
location_species <- tibble(
location_id = 1:n_locations,
Expand All @@ -80,15 +120,31 @@ location_species <- tibble(
mutate(
present_species = list(simulate_species_presence(
n_species = n_species,
n_present = n_spec_per_loc,
general_prob = 0.8,
rare_prob = 0.2
n_present = species_count,
general_prob = 0.95,
rare_prob = 0.25
))
)
```

```{r initial abundances}
sim_pop <- location_species |>
unnest(present_species) |>
mutate(init_pop = rpois(n(), avg_species_per_loc))
```


```{r}
total_years <- 6 # Number of years
total_decline <- -0.06 # Total decline over the period (-6%)
# Generate random yearly declines
random_yearly_declines <- runif(total_years, min = -0.5, max = 0.5) # Random values in range
scaled_yearly_declines <- random_yearly_declines - (mean(random_yearly_declines) - total_decline)
```



Expand Down

0 comments on commit 2b186a5

Please sign in to comment.