Skip to content

Commit 89e6569

Browse files
committed
Minor changes to get rmds to run
1 parent fc6e8bb commit 89e6569

8 files changed

+70
-98
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
EIDC/
22
.Rhistory
33
Outputs/
4-
4+
*.html
5+
*.RData
6+
*.rdb
7+
*.rdx
8+
*.png
9+
*__packages

01_Summary_Calculations.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ GRFLORA_AWI_SITE <- GRFLORA_ALL %>% ungroup() %>%
190190

191191

192192
BRLEAF_SUMMARY <- left_join(BRLEAF_SUMMARY, GRFLORA_AWI) %>%
193-
mutate(AWI_RICH = tidyr::replace_na(AWI_RICH, 0))
193+
mutate(AWI_RICH = tidyr::replace_na(AWI_RICH, 0)) %>%
194+
left_join(AWI_sites, by = "SITE_NO")
194195

195196
regions <- select(PLDATA7101, SITE, COUNTRY) %>% filter(COUNTRY != "") %>%
196197
left_join(AWI_sites, by = c("SITE" = "SITE_NO")) %>%
@@ -211,7 +212,8 @@ BRLEAF_SUMM_SITE <- BLEAF_META %>%
211212
ASHDIEBACK = sum(ASHDIEBACK, na.rm = TRUE), .groups = "drop") %>%
212213
mutate(ASHDIEBACK = ASHDIEBACK/NPLOT) %>%
213214
inner_join(GRFLORA_SITE) %>%
214-
inner_join(GRFLORA_AWI_SITE)
215+
inner_join(GRFLORA_AWI_SITE) %>%
216+
left_join(AWI_sites, by = "SITE_NO")
215217

216218

217219
write.csv(BRLEAF_SUMM_SITE, "Outputs/Site level richness.csv",
@@ -459,6 +461,8 @@ Climate_data <- pivot_longer(Clim_data,
459461
ash_plots <- filter(DBH22, SITE_NO < 200 &
460462
SPECIES == "Fraxinus excelsior") %>%
461463
select(SITE_NO, PLOT_NO) %>% unique() %>% mutate(ASHPRESENT = TRUE)
464+
write.csv(ash_plots, "Outputs/ash_plots.csv",
465+
row.names = FALSE)
462466
ash_sites <- unique(ash_plots$SITE_NO)
463467

464468
# deer risk

02_ChangeOverTime.Rmd

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ source("Helper_functions.R")
2626

2727

2828
```{r data read}
29+
BRLEAF_SUMMARY <- read.csv("Outputs/Broadleaf_summary_metrics.csv")
2930
BRLEAF_SUMMARY <- BRLEAF_SUMMARY %>%
3031
mutate(YR = c("1971","2001","2022")[YEAR],
3132
YDAY = (DAYOFYEAR - 200)/30,
@@ -34,8 +35,9 @@ BRLEAF_SUMMARY <- BRLEAF_SUMMARY %>%
3435
regions <- read.csv("Outputs/regions.csv")
3536
```
3637

37-
# Check priors for count models
38+
# Count models
3839

40+
### Check priors
3941

4042
```{r count model sample prior}
4143
mod_pr <- prior(normal(2,1), class = "b", coef = "YR1971") +
@@ -50,7 +52,6 @@ test_mod <- brm(SPECIES_RICH ~ -1 + YR + YDAY + (1|SITE:PLOT) + (YR|SITE),
5052
data = BRLEAF_SUMMARY, family = "poisson",
5153
prior = mod_pr, cores = 4, sample_prior = "only")
5254
plot(test_mod)
53-
pp_check(test_mod)
5455
pp_check(test_mod, "ecdf_overlay", ndraws = 20) +
5556
scale_x_continuous(limits = c(0,200))
5657
```
@@ -286,7 +287,7 @@ basal_emm %>%
286287
# Cover models
287288

288289

289-
## Check priors for cover/gamma models
290+
### Check priors for cover/gamma models
290291

291292

292293
```{r prop model sample prior}
@@ -538,7 +539,8 @@ awi_emm %>%
538539
# Binary models - Regeneration
539540

540541
```{r regen data prep}
541-
REGEN <- REGEN %>% select(-YEAR) %>% rename(YEAR = YR) %>%
542+
REGEN <- read.csv("Outputs/REGEN.csv")
543+
REGEN <- REGEN %>% select(-YEAR) %>%
542544
inner_join(select(BRLEAF_SUMMARY, SITE_NO, PLOT_NO, YEAR, SITE, PLOT, YDAY, YR))
543545
544546
```
@@ -680,22 +682,15 @@ siterich_emm %>%
680682

681683
# Site level AWI proportion
682684

683-
```{r site level data prep awi}
684-
BRLEAF_SUMMARY_SITE <- GRFLORA_SITE %>%
685-
mutate(YR = c("1971","2001","2022")[YEAR],
686-
YDAY = (DAYOFYEAR - 200)/30,
687-
SITE = as.character(SITE_NO)) %>%
688-
left_join(select(BRLEAF_SUMMARY, SITE_NO, AWI_region) %>% distinct())
689-
```
690685

691-
```{r count model sample prior v2}
686+
```{r prop model sample prior v2}
692687
mod_pr <- prior(normal(0,1), class = "b") +
693688
prior(student_t(5, 0, 1), class = "sd")
694689
```
695690

696691
```{r awi site rich mod run}
697692
awi_site_rich_mod <- brm(AWI_RICH | trials(SPECIES_RICH) ~ -1 + YR + YDAY + (1|SITE) + (1|AWI_region),
698-
data = BRLEAF_SUMMARY_SITE, family = binomial(),
693+
data = BRLEAF_SUMM_SITE, family = binomial(),
699694
prior = mod_pr, cores = 4, warmup = 2000, iter = 6000, thin = 4,
700695
control = list(adapt_delta = 0.95),
701696
file = paste0(model_loc, "SITERICH_AWI_BIN"))
@@ -728,22 +723,3 @@ awisite_emm %>%
728723
```
729724

730725

731-
732-
733-
```{r emmeans site beta pairwise comparison table}
734-
sitebeta_emm <- emmeans(site_beta_mod, ~ YR)
735-
pairs(sitebeta_emm, type = "response") %>%
736-
knitr::kable(digits = 3)
737-
```
738-
739-
```{r emmeans site beta plot}
740-
sitebeta_emm %>%
741-
gather_emmeans_draws() %>%
742-
mutate(Year = as.numeric(as.character(YR)),
743-
.value = exp(.value)) %>%
744-
ggplot(aes(x = Year, y = .value)) +
745-
stat_lineribbon(alpha = 1/4, fill = teal) +
746-
theme(axis.text = element_text(size = 12), axis.title = element_text(size = 14)) +
747-
labs(y = "Site level beta diversity")
748-
```
749-

03_AshDieback.Rmd

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ source("Helper_functions.R")
3232

3333

3434
```{r data read}
35+
ash_plots <- read.csv("Outputs/ash_plots.csv")
36+
ash_sites <- unique(ash_plots$SITE_NO)
3537
BRLEAF_SUMMARY <- read.csv("Outputs/Broadleaf_summary_metrics.csv")
3638
BRLEAF_SUMMARY <- BRLEAF_SUMMARY %>%
3739
full_join(ash_plots) %>%
@@ -782,15 +784,6 @@ siterich_emm %>%
782784

783785
# Site level AWI proportion
784786

785-
```{r site level awi data prep}
786-
BRLEAF_SUMMARY_SITE <- GRFLORA_SITE %>%
787-
mutate(YR = c("1971","2001","2022")[YEAR],
788-
YDAY = (DAYOFYEAR - 200)/30,
789-
SITE = as.character(SITE_NO)) %>%
790-
left_join(select(BRLEAF_SUMMARY, SITE_NO, AWI_region) %>% distinct()) %>%
791-
filter(SITE_NO %in% ash_sites)
792-
793-
```
794787

795788
```{r binomial model awi sample prior v2}
796789
mod_pr <- prior(normal(0,1), class = "b") +
@@ -799,7 +792,7 @@ mod_pr <- prior(normal(0,1), class = "b") +
799792

800793
```{r awi site rich mod run}
801794
awisite_mod <- brm(AWI_RICH | trials(SPECIES_RICH) ~ -1 + YR*ASHDIEBACK + YDAY + (1|SITE) + (1|AWI_region),
802-
data = BRLEAF_SUMMARY_SITE, family = binomial(),
795+
data = BRLEAF_SUMM_SITE, family = binomial(),
803796
prior = mod_pr, cores = 4, warmup = 2000, iter = 6000, thin = 4,
804797
control = list(adapt_delta = 0.95),
805798
file = paste0(model_loc, "SITERICH_AWI_BIN"))

04_Deer.Rmd

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ BRLEAF_SUMMARY <- BRLEAF_SUMMARY %>%
4141
PLOT = paste(SITE_NO, PLOT_NO, sep = "_"),
4242
DEER = factor(DEER, c("Low","Moderate","High"),
4343
ordered = TRUE))
44+
regions <- read.csv("Outputs/regions.csv")
4445
```
4546

4647

@@ -802,17 +803,6 @@ siterich_emm %>%
802803

803804
# Site level AWI proportion
804805

805-
```{r site level awi data prep}
806-
GRFLORA_SITE <- read.csv("Outputs/Site level richness with AWI.csv")
807-
BRLEAF_SUMMARY_SITE <- GRFLORA_SITE %>%
808-
mutate(YR = c("1971","2001","2022")[YEAR],
809-
YDAY = (DAYOFYEAR - 200)/30,
810-
SITE = as.character(SITE_NO)) %>%
811-
left_join(select(BRLEAF_SUMMARY, SITE_NO, AWI_region) %>% distinct()) %>%
812-
inner_join(deerrisk) %>%
813-
mutate(DEER = factor(DEER, c("Low","Moderate","High"),
814-
ordered = TRUE))
815-
```
816806

817807
```{r binomial model sample prior site level}
818808
mod_pr <- prior(normal(0,1), class = "b") +
@@ -821,7 +811,7 @@ mod_pr <- prior(normal(0,1), class = "b") +
821811

822812
```{r awi site rich mod run}
823813
awisite_mod <- brm(AWI_RICH | trials(SPECIES_RICH) ~ -1 + YR*mo(DEER) + YDAY + (1|SITE) + (1|AWI_region),
824-
data = BRLEAF_SUMMARY_SITE, family = binomial(),
814+
data = BRLEAF_SUMM_SITE, family = binomial(),
825815
prior = mod_pr, cores = 4, warmup = 2000, iter = 6000, thin = 4,
826816
control = list(adapt_delta = 0.95),
827817
file = paste0(model_loc, "SITERICH_AWI_BIN"))

05_AshDiebackandDeer.Rmd

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ source("Helper_functions.R")
3535
deerrisk <- read.csv("Metadata/DEER_RISK.csv") %>%
3636
mutate(DEER = ifelse(DEER == "None", "Low", DEER))
3737
regions <- read.csv("Outputs/regions.csv")
38+
ash_plots <- read.csv("Outputs/ash_plots.csv")
39+
ash_sites <- unique(ash_plots$SITE_NO)
3840
```
3941

4042
```{r data read}
@@ -804,16 +806,6 @@ siterich_emm %>%
804806

805807
# Site level AWI proportion
806808

807-
```{r awi site level data prep}
808-
BRLEAF_SUMM_SITE <- BRLEAF_SUMM_SITE %>%
809-
mutate(YR = c("1971","2001","2022")[YEAR],
810-
YDAY = (DAYOFYEAR - 200)/30,
811-
SITE = as.character(SITE_NO)) %>%
812-
inner_join(select(BRLEAF_SUMMARY, SITE_NO, AWI_region) %>% distinct()) %>%
813-
inner_join(deerrisk) %>%
814-
mutate(DEER = factor(DEER, c("Low","Moderate","High"),
815-
ordered = TRUE))
816-
```
817809

818810
```{r binomial model sample prior site level}
819811
mod_pr <- prior(normal(0,1), class = "b") +

06_Multivariate_Models.Rmd

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,11 @@ theme_set(theme_classic())
2222
library(patchwork)
2323
teal <- "#0383a4"
2424
two_cols <- c("#999999","#0072B2","#E69F00")
25+
model_loc <- "Outputs/Models/Multivariate_Models/"
2526
```
2627

2728
```{r, include = FALSE}
28-
par_summary <- function(x, digits = 3, ...){
29-
summ <- summary(x)
30-
fixef <- summ$fixed
31-
rand1 <- summ$random[[1]]
32-
rownames(rand1) <- paste0("SITE__",rownames(rand1))
33-
if(length(summ$random)>1){
34-
rand2 <- summ$random[[2]]
35-
rownames(rand2) <- paste0("SITE:PLOT__",rownames(rand2))
36-
} else{
37-
rand2 <- rand1[0,]
38-
}
39-
cor_pars <- summ$cor_pars
40-
spec_pars <- summ$spec_pars
41-
if("mo" %in% names(summ)){
42-
mo <- summ$mo
43-
rownames(mo) <- paste0("smplx__", rownames(mo))
44-
all <- do.call(rbind, list(fixef, rand1, rand2, spec_pars, mo))
45-
} else{
46-
all <- do.call(rbind, list(fixef, rand1, rand2, spec_pars))
47-
}
48-
all[,c("Bulk_ESS","Tail_ESS")] <- floor(all[,c("Bulk_ESS","Tail_ESS")])
49-
knitr::kable(all, digits = digits, ...)
50-
}
29+
source("Helper_functions.R")
5130
```
5231

5332

@@ -57,6 +36,9 @@ par_summary <- function(x, digits = 3, ...){
5736
deerrisk <- read.csv("Metadata/DEER_RISK.csv") %>%
5837
mutate(DEER = ifelse(DEER == "None", "Low", DEER)) %>%
5938
rename(SITE_NO = SITE_NUMBER)
39+
regions <- read.csv("Outputs/regions.csv")
40+
ash_plots <- read.csv("Outputs/ash_plots.csv")
41+
ash_sites <- unique(ash_plots$SITE_NO)
6042
```
6143

6244
```{r data read}
@@ -88,17 +70,13 @@ BRLEAF_SUMMARY <- inner_join(BRLEAF_SUMMARY, Climate_data) %>%
8870
Winter_rainfall = (Winter_rainfall - 300)/100,
8971
Summer_tasmax = (Summer_tasmax - 19),
9072
Winter_tasmin = (Winter_tasmin - 1))
91-
REGEN <- read.csv("Outputs/REGEN_ASHDEER.csv") %>%
73+
REGEN <- read.csv("Outputs/REGEN.csv") %>%
9274
select(SITE_NO, PLOT_NO, YEAR, ends_with("REGEN")) %>% distinct()
9375
BRLEAF_SUMMARY <- inner_join(BRLEAF_SUMMARY, REGEN)
9476
```
9577

9678
# Multivariate Model
9779

98-
```{r mult model loc, include = FALSE}
99-
model_loc <- "Outputs/Models/Multivariate_Models/"
100-
```
101-
10280
## Full model
10381

10482

@@ -195,6 +173,11 @@ pp_check(sem_allclim, resp = "SPECIESRICH", ndraws = 20, type = "ecdf_overlay")
195173
pp_check(sem_allclim, resp = "REGEN", ndraws = 20, type = "ecdf_overlay")
196174
```
197175

176+
```{r}
177+
resid_bysite_mult(sem_allclim)
178+
```
179+
180+
198181

199182
```{r full model kfold}
200183
set.seed(1)
@@ -562,7 +545,7 @@ model_structure <-
562545
```
563546

564547

565-
```{r run winter sr med model}
548+
```{r run winter regen med model}
566549
sem_win_regmed <- brm(model_structure, data = BRLEAF_SUMMARY,
567550
prior = mod_pr_nodeer +
568551
prior(dirichlet(1), class = "simo", resp = "SPECIESRICH", coef = "moDEER1"),
@@ -571,7 +554,7 @@ sem_win_regmed <- brm(model_structure, data = BRLEAF_SUMMARY,
571554
par_summary(sem_win_regmed)
572555
```
573556

574-
```{r winter model kfold}
557+
```{r winter regen med model kfold}
575558
sem_win_regmed <- add_criterion(sem_win_regmed, "kfold", folds = folds,
576559
file = paste0(model_loc, "SEM_WinterClim_RegenMed"))
577560
```

Helper_functions.R

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,40 @@ resid_bysite <- function(x, coldat = regions, ...){
3434
geom_hline(yintercept = 0) + labs(x = "Site", y = "Estimated Residual") +
3535
theme(axis.text.x = element_text(angle = 90, vjust = 0))
3636
p2 <- ggplot(plotdat, aes(y = Estimate)) +
37-
stat_summary(aes(x = REGION), fun.data = "mean_se") +
37+
stat_summary(aes(x = REGION), fun.data = "mean_se",
38+
fun.args = list(mult = 1.96)) +
3839
geom_hline(yintercept = 0) +
3940
labs(x = "Region", y = "Estimated Residual") +
4041
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
4142

4243
gridExtra::grid.arrange(p1,p2, nrow= 1, widths = c(2,1))
4344

4445
}
46+
47+
resid_bysite_mult <- function(x, coldat = regions, ...){
48+
res <- resid(x)
49+
var_names <- dimnames(res)[[3]]
50+
pl_list <- lapply(var_names, function(i){
51+
res_l <- res[,,i]
52+
dat <- x$data
53+
plotdat <- cbind(dat, res_l) %>%
54+
left_join(mutate(coldat, SITE = as.character(SITE)),
55+
by = "SITE")
56+
p1 <- ggplot(plotdat, aes(y = Estimate)) +
57+
geom_boxplot(aes(x = SITE)) +
58+
facet_wrap(~REGION, scales = "free_x") +
59+
geom_hline(yintercept = 0) +
60+
labs(x = "Site", y = "Estimated Residual",
61+
title = i) +
62+
theme(axis.text.x = element_text(angle = 90, vjust = 0))
63+
p2 <- ggplot(plotdat, aes(y = Estimate)) +
64+
stat_summary(aes(x = REGION), fun.data = "mean_se",
65+
fun.args = list(mult = 1.96)) +
66+
geom_hline(yintercept = 0) +
67+
labs(x = "Region", y = "Estimated Residual") +
68+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
69+
pa <- gridExtra::grid.arrange(p1,p2, nrow= 1, widths = c(2,1))
70+
})
71+
72+
73+
}

0 commit comments

Comments
 (0)