Skip to content

Commit

Permalink
Solve checklist errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaCartuyvels1 committed Dec 10, 2024
1 parent d9cf93c commit d6872ec
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
4 changes: 3 additions & 1 deletion source/data_analysis_spring/data_analysis_spring_2023.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ Er zijn vier tabellen in de Access database:
Van de eerste twee tabellen wordt een versimpelde versie gemaakt.

```{r load-data}
conn <- odbcConnectAccess2007("../../data/SPRING.accdb")
conn <- odbcConnectAccess2007(
"G:/Gedeelde drives/PRJ_MBAG/4b_bestuivers/data/SPRING.accdb"

Check warning on line 107 in source/data_analysis_spring/data_analysis_spring_2023.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/data_analysis_spring/data_analysis_spring_2023.Rmd,line=107,col=4,[absolute_path_linter] Do not use absolute paths.
)
identifications <- sqlFetch(conn, "identifications")
samples <- sqlFetch(conn, "samples")
Expand Down
85 changes: 51 additions & 34 deletions source/power_analyse.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set.seed(123)
```{r define parameters}
# Simulatieparameters
n_locations <- 50 # Aantal locaties
cyclus <- c(1,3,6) # Lengte cyclus
cyclus <- c(1, 3, 6) # Lengte cyclus
decline_rate <- 0.01 # "Echte" trend in de populatie
# Populatieparameters
Expand All @@ -29,7 +29,7 @@ sd_species_per_loc <- 13 # Standaarddeviatie van soorten per locatie
detect_prob <- 0.01 # Detectiekans per individu
peak_day <- 160 # Piekdag (8 juni)
sd_peak_day <- 25 # Spreiding rond piekdag
sampling_period <- c(91, 243) # Samplingperiode: april-augustus (dag 91 tot 243)
sampling_period <- c(91, 243) # Samplingperiode: april-augustus (dag 91 tot 243)
# Trends
decline_rate_6y <- rnorm(n_species, -0.06, 0.25)
Expand All @@ -41,7 +41,10 @@ flower_effect <- rnorm(1000, mean = 50, sd = 10) # Bloemen (voorbeeld)

```{r gegevens SPRING}
# Gemiddeld aantal individuen per soort per locatie
conn <- odbcConnectAccess2007("../data/SPRING.accdb")
conn <- odbcConnectAccess2007(
"G:/Gedeelde drives/PRJ_MBAG/4b_bestuivers/data/SPRING.accdb"

Check warning on line 45 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=45,col=4,[absolute_path_linter] Do not use absolute paths.
)
identifications <- sqlFetch(conn, "identifications")
RODBC::odbcClose(conn)
Expand All @@ -63,7 +66,7 @@ n_ind_per_loc <- identifications |>
```

```{r species per location}
n_spec_per_loc <- as.integer(pmax(0,
n_spec_per_loc <- as.integer(pmax(0,
rnorm(n_locations,
avg_species_per_loc,
sd_species_per_loc)))
Expand All @@ -77,37 +80,37 @@ simulate_species_presence <- function(n_species,
# Categoriseer soorten in algemeen (eerste 10%) en zeldzaam (rest)
species <- tibble(

Check warning on line 81 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

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

Check warning on line 83 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=83,col=28,[object_usage_linter] no visible binding for global variable '.data'
general_prob,
rare_prob) # De eerste 20% zijn algemene soorten
)
# Wijs aanwezigheid toe
species <- species %>%

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=22,[object_usage_linter] no visible global function definition for '%>%'
mutate(is_present = rbinom(1,

Check warning on line 90 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=90,col=5,[object_usage_linter] no visible global function definition for 'mutate'
size = 1,
prob = presence_prob)) # Aanwezigheidsstatus
if (sum(species$is_present) >= n_present){
prob = .data$presence_prob)) #Aanwezigheidsstatus

Check warning on line 92 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=92,col=39,[object_usage_linter] no visible binding for global variable '.data'
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
filter(.data$is_present == 1) %>% # Filter alleen aanwezige soorten

Check warning on line 97 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=97,col=14,[object_usage_linter] no visible binding for global variable '.data'

Check warning on line 97 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=97,col=37,[object_usage_linter] no visible global function definition for '%>%'
slice_sample(n = n_present) # Sample uit rijen

Check warning on line 98 in source/power_analyse.Rmd

View workflow job for this annotation

GitHub Actions / check project with checklist

file=source/power_analyse.Rmd,line=98,col=7,[object_usage_linter] no visible global function definition for 'slice_sample'
} else {
d <- n_present - sum(species$is_present)
species1 <- species |>
filter(is_present == 1) %>% # Filter alleen aanwezige soorten
slice_sample(n = n_present)
filter(.data$is_present == 1) %>% # Filter alleen aanwezige soorten
slice_sample(n = n_present)
species2 <- species |>
filter(is_present == 0) %>% # Filter alleen aanwezige soorten
filter(.data$is_present == 0) %>% # Filter alleen aanwezige soorten
slice_sample(n = d)
species <- species1 |>
add_row(species2)
}
return(species$species_id)
}
Expand Down Expand Up @@ -140,8 +143,11 @@ 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)
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 All @@ -154,25 +160,28 @@ scaled_yearly_declines <- random_yearly_declines - (mean(random_yearly_declines)

```{r define parameters}
# Functie: Simuleer populatie op een locatie
simulate_population <- function(year, location, species, decline_rate, sampling_days) {
simulate_population <- function(
year, location, species, decline_rate, sampling_days) {
# Basis populatiegrootte per soort
initial_population <- rpois(species, lambda = avg_species_per_loc)
# Jaarlijkse trend
population <- initial_population * exp(decline_rate * year)
# Seizoenseffect (Gaussian peak rond dag 160)
day_effect <- dnorm(sampling_days, mean = peak_day, sd = sd_peak_day)
# Correcties toepassen (temperatuur en bloemen)
temp_corr <- rnorm(length(sampling_days), mean = 1, sd = 0.1)
flower_corr <- rnorm(length(sampling_days), mean = 1, sd = 0.2)
corrected_population <- population * day_effect * temp_corr * flower_corr
# Detectiekans toepassen
observed_population <- rbinom(length(corrected_population), size = corrected_population, prob = detect_prob)
observed_population <- rbinom(length(corrected_population),
size = corrected_population,
prob = detect_prob)
return(observed_population)
}
Expand All @@ -184,9 +193,18 @@ simulated_data <- expand.grid(
) %>%
rowwise() %>%
mutate(
sampling_days = list(sample(seq(sampling_period[1], sampling_period[2]), 2, replace = TRUE)),
decline_rate = if_else(sampling_freq == 1, decline_rate_24y, decline_rate_6y),
observed_counts = list(simulate_population(year, location, n_species, decline_rate, sampling_days))
sampling_days = list(sample(seq(sampling_period[1],
sampling_period[2]),
2,
replace = TRUE)),
decline_rate = if_else(sampling_freq == 1,
decline_rate_24y,
decline_rate_6y),
observed_counts = list(simulate_population(year,
location,
n_species,
decline_rate,
sampling_days))
)
# Resultaten uitbreiden
Expand All @@ -199,12 +217,11 @@ simulated_data <- simulated_data %>%
# Analyse uitvoeren (Poisson-regressie)
model <- glmer(
observed_counts ~ year + corrected_temp + corrected_flowers + (1|location),
observed_counts ~ year + corrected_temp + corrected_flowers + (1 | location),
family = poisson,
data = simulated_data
)
summary(model)
```

0 comments on commit d6872ec

Please sign in to comment.