---
title: Degradation statistics
subtitle: baRulho:quantifying habitat-induced degradation of (animal) acoustic signals
author: Marcelo Araya-Salas
date: "`r Sys.Date()`"
toc: true
toc-depth: 3
toc-location: left
number-sections: true
highlight-style: pygments
format:
html:
df-print: kable
code-fold: true
code-tools: true
code-copy: true
embed-resources: true
editor_options:
chunk_output_type: console
---
```{r set root directory, echo = FALSE}
# set working directory as one directory above
knitr::opts_knit$set(root.dir = "..")
```
```{r add link to github repo, echo = FALSE, results='asis'}
# print link to github repo if any
if (file.exists("./.git/config")){
config <- readLines("./.git/config")
url <- grep("url", config, value = TRUE)
url <- gsub("\\turl = |.git$", "", url)
cat("\nSource code, data and annotation protocol found at [", url, "](", url, ")", sep = "")
}
```
```{r setup style, echo = FALSE, message = FALSE, warning=FALSE}
# options to customize chunk outputs
knitr::opts_chunk$set(
tidy.opts = list(width.cutoff = 65),
tidy = TRUE,
message = FALSE,
warning = FALSE
)
```
<div class="alert alert-info">
# Purposes {.unnumbered .unlisted}
- Summarize degradation metrics on re-recorded signals from playback experiment at Bosque de Tlalpan, Mexico City, 2019
- Run statistical analyses
</div>
# Load packages {.unnumbered .unlisted}
```{r load packages}
# install/ load packages
sketchy::load_packages(packages = c("remotes", "kableExtra", "knitr", "formatR", "rprojroot", "maRce10/warbleR", "ggplot2", "tidyr", "viridis", "corrplot", "brms", "ggdist", "cowplot", "cmdstanr", "maRce10/brmsish", "emmeans", "ggsignif", "rstan", "loo"))
```
# Custom functions {.unnumbered .unlisted}
```{r custom functions}
my.viridis <- function(...) mako(alpha = 0.5, begin = 0.3, end = 0.7, ...)
# to create several posterior predictive check plots out of a brms fit
custom_ppc1 <- function(fit, group = NULL, ndraws = 30) {
ppc_dens <-
pp_check(fit,
ndraws = ndraws,
type = 'dens_overlay_grouped',
group = group) # shows dens_overlay plot by default
pp_mean <-
pp_check(
fit,
type = "stat_grouped",
stat = "mean",
group = group,
ndraws = ndraws
)
pp_scat <-
pp_check(fit,
type = "scatter_avg_grouped",
group = group,
ndraws = ndraws)
pp_stat2 <- pp_check(fit, type = "stat_2d", ndraws = ndraws)
pp_loo <- pp_check(fit, type = "loo_pit_qq", ndraws = ndraws)
pp_error <-
pp_check(fit,
type = "error_scatter_avg_grouped",
ndraws = ndraws,
group = group)
plot_l <-
list(ppc_dens, pp_mean, pp_scat, pp_error, pp_stat2, pp_loo)
plot_l[c(1, 3:length(plot_l))] <-
lapply(plot_l[c(1, 3:length(plot_l))], function(x)
x + scale_color_viridis_d(
begin = 0.1,
end = 0.8,
alpha = 0.5
) + scale_fill_viridis_d(
begin = 0.1,
end = 0.8,
alpha = 0.5
) + theme_classic())
print(plot_grid(plotlist = plot_l, ncol = 2))
}
custom_ppc <- function(...) suppressMessages(custom_ppc1(...))
```
```{r read data degradation}
#| eval: true
#| echo: false
#| warning: false
#| message: false
#| include: false
degrad_df <- read.csv("./data/processed/barulho_degradation_metrics.csv", stringsAsFactors = FALSE)
degrad_params <- c(bl.rt = "blur.ratio", sp.bl.rt = "spectrum.blur.ratio", env.cr = "envelope.correlation", EA ="excess.attenuation", SPL = "SPL", SNR = "signal.to.noise.ratio", SPCC = "cross.correlation", TSR = "tail.to.signal.ratio", TNR = "tail.to.noise.ratio", sp.cr = "spectrum.correlation", pc1 = "pc1")
master_metadata <- read.csv("./data/raw/consolidated.master.sf_selection_table.csv")
# keep only simulated data (exluding data from a student)
master_metadata <- master_metadata[2:961, ]
# infer original frequency
possb_freqs <- seq(0.5, 10, length.out = 20)
master_metadata$freq <- sapply(1:nrow(master_metadata), function(x)
possb_freqs[which.min(abs((master_metadata$top.freq[x] + master_metadata$bottom.freq[x]) / 2 - possb_freqs))]
)
freq_contour <- freq_ts(master_metadata, length.out = 20, parallel = 22, img = FALSE, path = "./data/raw/master_sound_file", pb = FALSE)
freq_contour$mean.freq <- rowMeans(freq_contour[, 3:ncol(freq_contour)])
freq_contour$orig_sound_file <- master_metadata$orig.sound.file
degrad_df$frequency <- sapply(1:nrow(degrad_df), function(x)
freq_contour$mean.freq[freq_contour$orig_sound_file == degrad_df$sound.id[x]]
)
degrad_df$sim.frequency <- sapply(1:nrow(degrad_df), function(x)
master_metadata$freq[master_metadata$orig.sound.file == degrad_df$sound.id[x]]
)
degrad_df <- separate(degrad_df, col = sound.id, into = c("Duration", "Freq_modulation", "Amp_modulation", "Harmonicity", "delete"), remove = FALSE, sep = "_")
degrad_df$Duration <- ifelse(degrad_df$Duration == 0.1, "short", "long")
degrad_df$`Freq_modulation` <- ifelse(degrad_df$`Freq_modulation` == "BB", "fm", "no_fm")
degrad_df$Harmonicity <- gsub(".wav", "", degrad_df$Harmonicity)
degrad_df$delete <- NULL
degrad_df$treatment.replicates <- paste0("frq:", "_", degrad_df$sim.frequency, "dur:", degrad_df$Duration, "_", degrad_df$Amp_modulation, "_", degrad_df$Freq_modulation)
degrad_df$Freq_modulation <- factor(degrad_df$Freq_modulation, levels = c("no_fm", "fm"))
degrad_df$Amp_modulation[degrad_df$Amp_modulation == "no.am"] <- "no_am"
degrad_df$Amp_modulation <- factor(degrad_df$Amp_modulation, levels = c("no_am", "am"))
degrad_df$duration <- factor(degrad_df$Duration, levels = c("short", "long"))
degrad_df <- degrad_df[degrad_df$Harmonicity != "harm", ]
degrad_df <- degrad_df[degrad_df$distance > 1, ]
degrad_df$frequency <- degrad_df$sim.frequency
degrad_df$duration <- degrad_df$Duration
degrad_df$amplitude.modulation <- degrad_df$Amp_modulation
degrad_df$frequency.modulation <- degrad_df$Freq_modulation
degrad_df$location <- degrad_df$Transect
degrad_df$habitat_structure <- ifelse(degrad_df$Vegetacion == "abierta", "open", "closed")
degrad_df$sim.frequency <- degrad_df$signal.to.noise.ratio.t2 <- degrad_df$SPL <- degrad_df$template <- degrad_df$data.set <- degrad_df$Temperatura <- degrad_df$Harmonicity <- degrad_df$freq <- degrad_df$Humedad <- degrad_df$Vegetacion <- degrad_df$Duration <- degrad_df$orig.sound.file <- degrad_df$Freq_modulation <- degrad_df$Amp_modulation <- degrad_df$spectral.blur.ratio <- degrad_df$Transecto <- NULL
colmns <- c("sound.files", "selec", "start", "end", "bottom.freq", "top.freq", "sound.id", "habitat_structure", "distance", "reference", "frequency", "duration", "treatment.replicates", "location", "frequency.modulation", "amplitude.modulation","blur.ratio", "envelope.correlation", "excess.attenuation", "signal.to.noise.ratio", "cross.correlation", "spectrum.blur.ratio", "spectrum.correlation", "tail.to.signal.ratio")
degrad_df <- degrad_df[, colmns]
write.csv(degrad_df, "./data/processed/tlalpan_degradation_metrics_v01.csv", row.names = FALSE)
```
```{r add PCA, out.width = "100%"}
degrad_df <- read.csv("./data/processed/tlalpan_degradation_metrics_v01.csv")
degrad_df <- degrad_df[grep("marker", degrad_df$sound.id, invert = TRUE), ]
degrad_params <- c("blur.ratio", "spectrum.blur.ratio", "envelope.correlation", "excess.attenuation", "signal.to.noise.ratio", "cross.correlation", "tail.to.signal.ratio", "spectrum.correlation")
comp.cases <- complete.cases(degrad_df[,names(degrad_df) %in% degrad_params])
pca <- prcomp(degrad_df[comp.cases, names(degrad_df) %in% degrad_params], scale. = TRUE)
# add to data
degrad_df$pc1 <- NA
degrad_df$pc1[comp.cases] <- pca$x[, 1]
degrad_df$pc1.1m.rate <- degrad_df$distance
# plot rotation values by PC
pca_rot <- as.data.frame(pca$rotation[, 1:4])
pca_var <- round(summary(pca)$importance[2, ] * 100)
pca_rot_stck <- stack(pca_rot)
pca_rot_stck$variable <- rownames(pca_rot)
pca_rot_stck$values[pca_rot_stck$ind == "PC1"] <- pca_rot_stck$values[pca_rot_stck$ind == "PC1"]
pca_rot_stck$Sign <- ifelse(pca_rot_stck$values > 0, "Positive", "Negative")
pca_rot_stck$rotation <- abs(pca_rot_stck$values)
pca_rot_stck$ind_var <- paste0(pca_rot_stck$ind, " (", sapply(pca_rot_stck$ind, function(x) pca_var[names(pca_var)== x]), "%)")
ggplot(pca_rot_stck, aes(x = variable, y = rotation, fill = Sign)) +
geom_col() +
coord_flip() +
scale_fill_viridis_d(alpha = 0.7, begin = 0.2, end = 0.8) +
facet_wrap(~ ind_var) +
theme_classic()
```
# Correlation among metrics
Raw metrics:
```{r correlation among raw metrics, out.width = "120%"}
cormat <- cor(degrad_df[, degrad_params], use = "pairwise.complete.obs")
rownames(cormat) <- colnames(cormat) <- degrad_params
cols_corr <- colorRampPalette(c(mako(3, direction = 1, begin = 0.2, end = 0.5), "#BEBEBE1A", "white", "#BEBEBE1A", mako(3, direction = 1, begin = 0.7, end = 0.9)))(30)
cp <- corrplot.mixed(cormat, tl.cex = 0.7,
upper.col = cols_corr,
lower.col = cols_corr,
order = "hclust",
lower = "number",
upper = "ellipse",
tl.col = "black")
# sort parameters as in clusters for cross correlation
# degrad_params <- degrad_params[match(rownames(cp$corr), degrad_params)]
#
# degrad_df$treatment.replicates <- sapply(strsplit(degrad_df$sound.id, ".wav"), "[[", 1)
#
# degrad_df$frequency <- as.numeric(sapply(strsplit(degrad_df$sound.id, "_"), "[[", 1))
#
# degrad_df$frequency.modulation <- sapply(strsplit(degrad_df$sound.id, "_"), "[[", 2)
#
# degrad_df$amplitude.modulation <- sapply(strsplit(degrad_df$sound.id, "_"), "[[", 3)
#
degrad_df$habitat.structure <- degrad_df$habitat_structure
degrad_df$habitat_structure <- NULL
#
# degrad_df$duration <- ifelse(grepl("dur:0.1", degrad_df$sound.id), "short", "long")
```
# Data description
- `r length(unique(degrad_df$frequency))` frequencies
- `r length(unique(degrad_df$location))` locations
- `r nrow(degrad_df)` test sounds
- `r length(unique(degrad_df$treatment.replicates))` treatment combinations
Sample sizes per location, transect and signal type:
```{r, results='asis'}
agg <- aggregate(cbind(sound.id, treatment.replicates) ~ location + habitat.structure + distance, degrad_df, function(x) length(unique(x)))
agg$replicates <- agg$sound.id / agg$treatment.replicates
df1 <- knitr::kable(agg, row.names = FALSE,
escape = FALSE, format = "html")
kable_styling(df1, bootstrap_options = c("striped",
"hover", "condensed", "responsive"), full_width = FALSE,
font_size = 15)
```
# Statistical analysis
```{r, eval =FALSE}
iter <- 20000
chains <- 4
priors <- c(prior(normal(0, 6), class = "b"), prior(cauchy(0, 4), class = "sd"))
null_priors <- c(prior(cauchy(0, 4), class = "sd"))
# set frequency to mean-centered and divide by twice the standard deviation
degrad_df$raw_frequency <- degrad_df$frequency
degrad_df$frequency <- (degrad_df$frequency - mean(degrad_df$frequency)) / (2 * sd(degrad_df$frequency))
# set base level for factors
degrad_df$habitat.structure <- factor(degrad_df$habitat.structure, levels = c("open", "closed"))
degrad_df$frequency.modulation <- factor(degrad_df$frequency.modulation, levels = c("no_fm", "fm"))
degrad_df$amplitude.modulation <- factor(degrad_df$amplitude.modulation, levels = c("no_am", "am"))
degrad_df$duration <- factor(degrad_df$duration, levels = c("short", "long"))
degrad_df$location <- as.factor(degrad_df$location)
degrad_df$distance_f <- paste0(degrad_df$distance, "m")
degrad_df$distance_f <- factor(degrad_df$distance_f, levels = c("10m", "30m", "65m", "100m"), ordered = TRUE)
set.seed(123)
mod_pc1 <-
brm(
formula = pc1 ~ frequency + frequency.modulation + amplitude.modulation + duration + habitat.structure + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = priors,
iter = iter,
chains = chains,
cores = chains,
backend = "cmdstanr",
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/regression_model_pc1.RDS",
file_refit = "always"
)
mod_pc1 <- add_criterion(mod_pc1, criterion = c("loo"))
null_mod_pc1 <-
brm(
formula = pc1 ~ 1 + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = null_priors,
iter = iter,
chains = chains,
cores = chains,
backend = "cmdstanr",
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/null_regression_model_pc1.RDS",
file_refit = "always"
)
null_mod_pc1 <- add_criterion(null_mod_pc1, criterion = c("loo"))
mod_blurratio <-
brm(
formula = blur.ratio ~ frequency + frequency.modulation + amplitude.modulation + duration + habitat.structure + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = priors,
iter = iter,
chains = chains,
cores = chains,
backend = "cmdstanr",
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/regression_model_blur_ratio.RDS",
file_refit = "always"
)
mod_blurratio <- add_criterion(mod_blurratio, criterion = c("loo"))
null_mod_blurratio <-
brm(
formula = blur.ratio ~ 1 + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = null_priors,
iter = iter,
chains = chains,
cores = chains,
backend = "cmdstanr",
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/null_regression_model_blur_ratio.RDS",
file_refit = "always"
)
null_mod_blurratio <- add_criterion(null_mod_blurratio, criterion = c("loo"))
mod_ea <-
brm(
formula = excess.attenuation ~ frequency + frequency.modulation + amplitude.modulation + duration + habitat.structure + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = priors,
iter = iter,
chains = chains,
backend = "cmdstanr",
cores = chains,
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/regression_model_excess_attenuation.RDS",
file_refit = "always"
)
mod_ea <- add_criterion(mod_ea, criterion = c("loo"))
null_mod_ea <-
brm(
formula = excess.attenuation ~ 1 + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = null_priors,
iter = iter,
chains = chains,
backend = "cmdstanr",
cores = chains,
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/null_regression_model_excess_attenuation.RDS",
file_refit = "always"
)
null_mod_ea <- add_criterion(null_mod_ea, criterion = c("loo"))
mod_tsr <-
brm(
formula = tail.to.signal.ratio ~ frequency + frequency.modulation + amplitude.modulation + duration + habitat.structure + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = priors,
iter = iter,
chains = chains,
backend = "cmdstanr",
cores = chains,
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/regression_model_tail_to_signal_ratio.RDS",
file_refit = "always"
)
mod_tsr <- add_criterion(mod_tsr, criterion = c("loo"))
null_mod_tsr <-
brm(
formula = tail.to.signal.ratio ~ 1 + mo(distance_f) + (1 | location) + (1 | treatment.replicates),
data = degrad_df,
prior = null_priors,
iter = iter,
chains = chains,
backend = "cmdstanr",
cores = chains,
control = list(adapt_delta = 0.99, max_treedepth = 15),
file = "./data/processed/null_regression_model_tail_to_signal_ratio.RDS",
file_refit = "always"
)
null_mod_tsr <- add_criterion(null_mod_tsr, criterion = c("loo"))
```
## PC1 degradation
Model selection:
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
effects <- c("habitat structure", "frequency modulation", "amplitude modulation", "frequency", "duration")
mod <- readRDS("./data/processed/regression_model_pc1.RDS")
null_mod <- readRDS("./data/processed/null_regression_model_pc1.RDS")
loo_diff <- loo::loo_compare(mod, null_mod)
as.data.frame(loo_diff[,1:2], row.names = c("model", "null model"))
```
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
extended_summary(mod, n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE, trace = TRUE, return = FALSE)
```
Posterior predictive checks:
```{r}
custom_ppc(fit = mod, group = "habitat.structure")
```
## Blur ratio
Model selection:
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
mod <- readRDS("./data/processed/regression_model_blur_ratio.RDS")
null_mod <- readRDS("./data/processed/null_regression_model_blur_ratio.RDS")
loo_diff <- loo::loo_compare(mod, null_mod)
as.data.frame(loo_diff[,1:2], row.names = c("model", "null model"))
```
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
extended_summary(fit = mod, n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE)
```
Posterior predictive checks:
```{r}
custom_ppc(fit = mod, group = "habitat.structure")
```
## Excess attenuation
Model selection:
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
mod <- readRDS("./data/processed/regression_model_excess_attenuation.RDS")
null_mod <- readRDS("./data/processed/null_regression_model_excess_attenuation.RDS")
loo_diff <- loo::loo_compare(mod, null_mod)
as.data.frame(loo_diff[,1:2], row.names = c("model", "null model"))
```
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
extended_summary(fit = mod, n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE)
```
Posterior predictive checks:
```{r}
custom_ppc(fit = mod, group = "habitat.structure")
```
## Tail-to-signal ratio
Model selection:
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
mod <- readRDS("./data/processed/regression_model_tail_to_signal_ratio.RDS")
null_mod <- readRDS("./data/processed/null_regression_model_tail_to_signal_ratio.RDS")
loo_diff <- loo::loo_compare(mod, null_mod)
as.data.frame(loo_diff[,1:2], row.names = c("model", "null model"))
```
```{r, eval = TRUE, results='asis', warning = FALSE, out.width="100%"}
extended_summary(fit = mod, n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE)
# mod <- readRDS("./data/processed/regression_model_int.RDS")
```
Posterior predictive checks:
```{r}
custom_ppc(fit = mod, group = "habitat.structure")
```
## Combined results
```{r, out.width="100%"}
pc1 <- extended_summary(read.file = "./data/processed/regression_model_pc1.RDS", n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE, trace = FALSE, return = TRUE)
# gg_pc1 <- pc1$plot + labs(x = "Degradation (PC1)")
br <- extended_summary(read.file = "./data/processed/regression_model_blur_ratio.RDS", n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE, trace = FALSE, return = TRUE)
# gg_br <- br$plot + labs(x = "Blur ratio", y = "") + theme(axis.text.y = element_blank())
ea <- extended_summary(read.file = "./data/processed/regression_model_excess_attenuation.RDS", n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE, trace = FALSE, return = TRUE)
# gg_ea <- ea$plot + labs(x = "Excess attenuation", y = "") + theme(axis.text.y = element_blank())
tsr <- extended_summary(read.file = "./data/processed/regression_model_tail_to_signal_ratio.RDS", n.posterior = 1000, fill = mako(10)[7], trace.palette = my.viridis, remove.intercepts = TRUE, highlight = TRUE, gsub.pattern = c("b_", "habitat.structureclosed", "amplitude.modulationam", "frequency.modulationfm", "durationlong"), gsub.replacement = c("", "habitat structure", "amplitude modulation", "frequency modulation", "duration"), effects = effects, print.name = FALSE, trace = FALSE, return = TRUE)
# gg_tsr <- tsr$plot + labs(x = "Tail-to-signal ratio", y = "") + theme(axis.text.y = element_blank())
#
# plot_grid(gg_pc1, gg_br, gg_ea, gg_tsr, nrow = 1, rel_widths = c(6, 2, 2, 2))
```
```{r, out.width="100%"}
estimates <- data.frame(pc1 = pc1$coef_table$Estimate
, br = br$coef_table$Estimate, ea = ea$coef_table$Estimate, tsr = tsr$coef_table$Estimate)
names(estimates) <- c("Degradation (PC1)", "Blur ratio", "Excess attenuation", "Tail-to-signal ratio")
# make estimates relative to maximum estimate in data
rel_estimates <- data.frame(lapply(estimates, function(x) x/ max(abs(x)) * 0.8))
names(rel_estimates) <- c("Degradation (PC1)", "Blur ratio", "Excess attenuation", "Tail-to-signal ratio")
# corrplot(as.matrix(estimates), method = "ellipse", )
signif <- data.frame(pc1 = pc1$coef_table$`l-95% CI` * pc1$coef_table$`u-95% CI` > 0, br = br$coef_table$`l-95% CI` * br$coef_table$`u-95% CI` > 0, ea = ea$coef_table$`l-95% CI` * ea$coef_table$`u-95% CI` > 0, tsr = tsr$coef_table$`l-95% CI` * tsr$coef_table$`u-95% CI` > 0)
names(signif) <- c("Degradation (PC1)", "Blur ratio", "Excess attenuation", "Tail-to-signal ratio")
estimates <- as.data.frame(cbind(rownames(pc1$coef_table), stack(estimates)[, 1], stack(rel_estimates)[, 1], stack(signif)))
names(estimates) <- c("predictor", "est", "relavite.est", "sig", "response")
estimates$signif <- ifelse(estimates$sig, "p < 0.05", "N.S.")
# estimates$relavite.est <- ifelse(estimates$sig, estimates$relavite.est, 0)
estimates$response <- factor(estimates$response, levels = rev(c("Degradation (PC1)", "Blur ratio", "Excess attenuation", "Tail-to-signal ratio")))
firstup <- function(x) {
substr(x, 1, 1) <- toupper(substr(x, 1, 1))
x
}
estimates$predictor <- firstup(estimates$predictor)
estimates$predictor <- factor(estimates$predictor, levels = c("Habitat structure", "Frequency", "Duration", "Amplitude modulation", "Frequency modulation"))
gg_tiled <- ggplot(estimates, aes(x = predictor, y = response, fill = relavite.est)) +
geom_tile() +
coord_equal() +
scale_fill_gradient2(low = mako(10)[3], high = mako(10)[7], guide = "none") +
geom_text(aes(label = round(est, 3), color = signif), size = 5) +
scale_color_manual(values = c("gray", "black"), guide = "none") +
labs(x = "", y = "", color = "Significance") +
theme_classic(base_size = 15) +
theme(axis.text.x = element_text(color = "black", angle = 30, vjust = 1, hjust = 1), axis.text.y = element_text(color="black"))
gg_tiled
gg_tiled2 <- ggplot(estimates, aes(x = predictor, y = response, fill = relavite.est)) +
geom_tile() +
coord_equal() +
scale_fill_gradient2(low = mako(10)[3], high = mako(10)[7], guide = "none") +
geom_text(aes(label = round(est, 3), color = signif), size = 7) +
scale_color_manual(values = c("gray", "black"), guide = "none") +
labs(x = "", y = "", color = "Significance") +
theme_classic(base_size = 22) +
theme(axis.text.x = element_text(color = "black", angle = 30, vjust = 1, hjust = 1), axis.text.y = element_text(color="black"))
# # save for presentation
# ggsave(plot = gg_tiled2, filename = "./output/presentation/images/pvalues_grid.jpeg", dpi = 300)
# save for paper
# ggsave(plot = gg_tiled2, filename = "./output/pvalues_grid.jpeg", dpi = 300,width = 12, height = 8)
```
## Combined model metadata
```{r, warning=FALSE}
check_rds_fits(path = "./data/processed/", html = TRUE)
```
<!-- light green box -->
<div class="alert alert-success">
# Takeaways
- Habitat structure seems to drive most of the observed degradation
- Frequency modulation and amplitude modulation were the signal structural features that more strongly affected transmission
- Signal features interact with habitat structure in diverse ways, sometimes increasing and sometimes decreasing degradation
- Most degradation metrics are robust to variation in signal duration
</div>
---
<!-- add packages used, system details and versions -->
<font size="4">Session information</font>
```{r session info, echo=F}
sessionInfo()
```