---
title: "R Notebook"
output: html_notebook
---
# Packages used
```{r}
library(tidyverse)
library(tidybayes)
library(cmdstanr)
library(bayesplot)
library(broom)
library(readxl)
library(brms)
library(ggeffects)
library(ggdist) # Special geoms for posterior distributions
library(jtools)
library(sjPlot)
library(emmeans)
library(easystats)
library(rstanarm)
library(modelbased)
library(priorsense)
```
# Dataset organization
```{r}
hdgood <- read_xlsx("SupplementaryData1.xlsx", sheet = "GoodData_corr", na = c("", "#N/A", "NA"))
hdgood_hab2 <- hdgood %>%
filter(!is.na(ID)) %>%
filter(block != 4) %>%
mutate(block = factor(block)) %>%
mutate(sound_condition = fct_relevel(sound_condition, c("noise", "dog", "chimp", "hum", "music")))%>%
filter(!is.na(aggr)) %>%
filter(!is.na(hab)) %>%
mutate(hab = factor(hab)) %>%
filter(hab != "h0") %>%
mutate(hab = fct_relevel(hab, c("h1", "h2", "dog", "hum", "chimp", "music"))) %>%
filter(hab != "h1")
```
# Modelling
## Full modell
```{r}
bayesbin_newpri <- brm(aggr ~ block*species*hab2 + (1|ID),
data = hdgood_hab2,
family = bernoulli(link = "logit"),
priors <- c(
prior(student_t(3, 0, 2.5), class = "Intercept"),
prior(cauchy(0, 2.5), class = "b"),
prior(student_t(3, 0, 2.5), class = "sd")
),
cores = 4,
chains = 4,
iter = 20000,
backend = "cmdstanr",
seed = 1221,
control=list(adapt_delta=0.99, step_size = 0.01, max_treedepth =15),
file = "habdishab_upd_newpriors")
```
### Diagnostics
```{r}
fixef(bayesbin_newpri) %>%
inv_logit_scaled() %>%
round(digits = 3)
```
```{r}
plot(bayesbin_newpri)
```
```{r}
pp_check(bayesbin_newpri, type = "dens_overlay", ndraws = 100)
```
```{r}
summary(bayesbin_newpri)
```
```{r}
conditional_effects(bayesbin_newpri, dpar = "mu", points = T)
```
## Model selection with reduced models
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri <- update(bayesbin_newpri, formula. = . ~ . - block:species:hab2,
file = "habdishab_1a")
# Compare models using LOO
loo_full <- loo(bayesbin_newpri)
loo_reduced <- loo(reduced_model_newpri)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_bs <- update(reduced_model_newpri, formula. = . ~ . - block:species,
file = "habdishab_2a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri)
loo_reduced <- loo(reduced_model_newpri_bs)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_hs <- update(reduced_model_newpri, formula. = . ~ . - species:hab2,
file = "habdishab_3a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri)
loo_reduced <- loo(reduced_model_newpri_hs)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_bh <- update(reduced_model_newpri, formula. = . ~ . - block:hab2,
file = "habdishab_4a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri)
loo_reduced <- loo(reduced_model_newpri_bh)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_bs_hs <- update(reduced_model_newpri_bs, formula. = . ~ . - species:hab2,
file = "habdishab_5a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri_bs)
loo_reduced <- loo(reduced_model_newpri_bs_hs)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_bs_bh <- update(reduced_model_newpri_bs, formula. = . ~ . - block:hab2,
file = "habdishab_6a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri_bs)
loo_reduced <- loo(reduced_model_newpri_bs_bh)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_bs_bh_hs <- update(reduced_model_newpri_bs_bh, formula. = . ~ . - species:hab2,
file = "habdishab_7a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri_bs_bh)
loo_reduced <- loo(reduced_model_newpri_bs_bh_hs)
loo_compare(loo_full, loo_reduced)
```
```{r}
# Fit a reduced model without the interaction term
reduced_model_newpri_bs_bh_b <- update(reduced_model_newpri_bs_bh, formula. = . ~ . - block,
file = "habdishab_8a")
# Compare models using LOO
loo_full <- loo(reduced_model_newpri_bs_bh)
loo_reduced <- loo(reduced_model_newpri_bs_bh_b)
loo_compare(loo_full, loo_reduced)
```
### Model comparisons
```{r}
loo_reduced_bs_bh_hs <- loo(reduced_model_newpri_bs_bh_hs)
loo_reduced_bs_bh <- loo(reduced_model_newpri_bs_bh)
loo_reduced_bs_hs <- loo(reduced_model_newpri_bs_hs)
loo_reduced_bs <- loo(reduced_model_newpri_bs)
loo_reduced_bh <- loo(reduced_model_newpri_bh)
loo_reduced_hs <- loo(reduced_model_newpri_hs)
loo_reduced <- loo(reduced_model_newpri)
loo_full <- loo(bayesbin_newpri)
loo_compare(loo_full, loo_reduced, loo_reduced_hs, loo_reduced_bh, loo_reduced_bs, loo_reduced_bs_hs, loo_reduced_bs_bh, loo_reduced_bs_bh_hs)
```
### prior diagnostics
```{r}
powerscale_sensitivity(reduced_model_newpri_bs)
```
```{r}
conditional_effects(reduced_model_newpri_bs, dpar = "mu", points = T)
```
```{r}
##Formatting Fig4
ce <- conditional_effects(reduced_model_newpri_bs,
dpar = "mu",
effects = "block:hab2",
prob = 0.89)
#
plots <- plot(ce, points = FALSE, ask = FALSE)
p <- plots[[1]]
p
p <- p +
theme_classic(base_size = 22) +
labs(
x = "Block",
y = "Probability of reaction",
colour = "Sound condition",
fill = "Sound condition"
) +
scale_colour_manual(
values = c(TestSound = "#0072B2", h2 = "#D55E00"),
labels = c(TestSound = "Test sound", h2 = "Noise"),
breaks = c("TestSound", "h2")
) +
scale_fill_manual(
values = c(TestSound = "#56B4E9", h2 = "#E69F00"),
labels = c(TestSound = "Test sound", h2 = "Noise"),
breaks = c("TestSound", "h2")
)+
theme(
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold")
)
p
library(cowplot)
plot_grid(p, labels = c("a"), label_size = 16, label_fontface = "bold")
fig <- plot_grid(
p,
labels = c(""),
label_size = 22,
label_fontface = "bold"
)
ggsave(
filename = "Fig4.tiff",
plot = fig,
width = 10, height = 6,
units = "in",
dpi = 600,
device = "tiff",
compression = "lzw"
)
```
```{r}
##Formatting Fig6
ce_species <- conditional_effects(
reduced_model_newpri_bs,
dpar = "mu",
effects = "species:hab2",
prob = 0.89
)
#
p_species <- plot(ce_species, points = FALSE)[[1]] +
theme_classic(base_size = 22) +
labs(
x = "Sound type",
y = "Probability of reaction",
colour = "Sound condition",
fill = "Sound condition"
) +
scale_colour_manual(
values = c(TestSound = "#0072B2", h2 = "#D55E00"),
labels = c(TestSound = "Test sound", h2 = "Noise"),
breaks = c("TestSound", "h2")
) +
scale_fill_manual(
values = c(TestSound = "#56B4E9", h2 = "#E69F00"),
labels = c(TestSound = "Test sound", h2 = "Noise"),
breaks = c("TestSound", "h2")
) +
scale_x_discrete(
limits = c("dog","hum","chimp","music"),
labels = c(
"chimp" = "Chimpanzee",
"dog" = "Dog",
"hum" = "Human",
"music" = "Music"
)
) +
theme(
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold")
)
p_species
plot_grid(p_species, labels = c(""), label_size = 16, label_fontface = "bold")
fig <- plot_grid(
p_species,
labels = c(""),
label_size = 22,
label_fontface = "bold"
)
####save
ggsave(
filename = "Fig6.tiff",
plot = fig,
width = 10, height = 6,
units = "in",
dpi = 600,
device = "tiff",
compression = "lzw"
)
```
### Post-hoc tests
```{r}
prior_model <- unupdate(reduced_model_newpri_bs)
```
```{r}
reduced_model_newpri_bs |>
emmeans(pairwise ~ block|hab2, type = "response", level = .89)
```
```{r}
ph1 <- emmeans(reduced_model_newpri_bs, ~ block|hab2, level = .89)
pd(ph1)
rope_res <- rope(ph1, range = c(-0.18, 0.18), ci = .89)
rope_res
plot(rope_res)
ph1a <- emmeans(prior_model, ~ block|hab2, level = .89)
bf <- bayesfactor_parameters(ph1, prior = ph1a)
bf
interpret_bf(exp(bf$log_BF), include_value = TRUE)
```
```{r}
p2 <- plot(rope_res) +
theme_classic(base_size = 22) +
labs(
x = "Contrast",
y = "Sound condition x Block",
title = "",
fill = "CrI" # CI -> CrI
) +
scale_y_discrete(
limits = c(
# ALUL: NOISE -> FELÜL: TEST SOUND
"3 h2", "2 h2", "1 h2",
"3 TestSound", "2 TestSound", "1 TestSound"
),
labels = c(
"1 TestSound" = "TS – B1",
"2 TestSound" = "TS – B2",
"3 TestSound" = "TS – B3",
"1 h2" = "N – B1",
"2 h2" = "N – B2",
"3 h2" = "N – B3"
)
) +
theme(
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5, face = "bold")
)
#
p2
# (a)
fig2 <- plot_grid(
p2,
labels = c("a"),
label_size = 22,
label_fontface = "bold"
)
###save
ggsave(
filename = "Fig5a.tiff",
plot = fig2,
width = 10,
height = 6,
units = "in",
dpi = 600,
device = "tiff",
compression = "lzw"
)
```
```{r}
contr <- pairs(ph1)
pd(contr, as_p = F)
rope(contr, range = c(-0.18, 0.18), ci = .89)
ph1b <- pairs(ph1a)
bf <- bayesfactor_parameters(contr, prior = ph1b)
bf
interpret_bf(exp(bf$log_BF), include_value = TRUE)
plot(rope(contr, range = c(-0.18, 0.18), ci = .89))
```
```{r}
reduced_model_newpri_bs |>
emmeans( ~ hab2|block, type = "response", level = .89) |>
pairs(reverse = T)
```
```{r}
ph2 <- emmeans(reduced_model_newpri_bs, ~ hab2|block, level = .89)
ph2a <- emmeans(prior_model, ~ hab2|block, level = .89)
pd(ph2)
rope(ph2, range = c(-0.18, 0.18), ci = .89)
plot(rope(ph2, range = c(-0.18, 0.18), ci = .89))
```
```{r}
contr2 <- pairs(ph2, reverse = T)
pd(contr2, as_p = F)
rope_res <- rope(contr2, range = c(-0.18, 0.18), ci = .89)
rope_res
ph2b <- pairs(ph2a)
bf <- bayesfactor_parameters(contr2, prior = ph2b)
bf
interpret_bf(exp(bf$log_BF), include_value = TRUE)
plot(rope_res)
```
```{r}
###Formatting Fig5b
p3 <- plot(rope_res) +
theme_classic(base_size = 22) +
labs(
x = "Contrast",
y = "Sound condition × Block",
title = "",
fill = "CrI"
) +
scale_y_discrete(
labels = c(
"TS – N – B3",
"TS – N – B2",
"TS – N – B1"
)
) +
theme(
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5, face = "bold")
)
#
p3
plot_grid(p3, labels = c("b"), label_size = 22, label_fontface = "bold")
fig3 <- plot_grid(
p3,
labels = c("b"),
label_size = 22,
label_fontface = "bold"
)
####Save
ggsave(
filename = "Fig5b.tiff",
plot = fig3,
width = 10, height = 6,
units = "in",
dpi = 600,
device = "tiff",
compression = "lzw"
)
```
```{r}
reduced_model_newpri_bs |>
emmeans( ~ species|hab2, type = "response", level = .89)
reduced_model_newpri_bs |>
emmeans( ~ species|hab2, type = "response", level = .89)|>
pairs(reverse = T)
```
```{r}
ph1 <- emmeans(reduced_model_newpri_bs, ~ species*hab2, level = .89)
ph1a <- emmeans(prior_model, ~ species*hab2, level = .89)
pd(ph1)
rope_res <- rope(ph1, range = c(-0.18, 0.18), ci = .89)
plot(rope_res)
bf <- bayesfactor_parameters(ph1, prior = ph1a)
bf
interpret_bf(exp(bf$log_BF), include_value = TRUE)
```
```{r}
###Formatting Fig7a
p4 <- plot(rope_res) +
theme_classic(base_size = 22) +
labs(
x = "Contrast",
y = "Sound type x Sound condition",
title = "",
fill = "CrI"
) +
scale_y_discrete(
limits = c(
# alulról felfelé! -> így felül D–H–C–M lesz
"music h2", "chimp h2", "hum h2", "dog h2", # N blokk
"music TestSound", "chimp TestSound", "hum TestSound", "dog TestSound" # TS blokk
),
labels = c(
"dog h2" = "D – N",
"hum h2" = "H – N",
"chimp h2" = "C – N",
"music h2" = "M – N",
"dog TestSound" = "D – TS",
"hum TestSound" = "H – TS",
"chimp TestSound" = "C – TS",
"music TestSound" = "M – TS"
)
)+
theme(
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5, face = "bold")
)
#
p4
plot_grid(p4, labels = c("a"), label_size = 22, label_fontface = "bold")
fig4 <- plot_grid(
p4,
labels = c("a"),
label_size = 22,
label_fontface = "bold"
)
ggsave(
filename = "Fig7a.tiff",
plot = fig4,
width = 10, height = 6,
units = "in",
dpi = 600,
device = "tiff",
compression = "lzw"
)
```
```{r}
contr <- pairs(ph1, by = "hab2", reverse = T)
pd(contr, as_p = F)
r <- rope(contr, range = c(-0.18, 0.18), ci = .89)
r
plot(r)
ph1b <- pairs(ph1a)
bf <- bayesfactor_parameters(contr, prior = ph1b)
bf
interpret_bf(exp(bf$log_BF), include_value = TRUE)
```
```{r}
####Formatting Fig7b
# 2) Plot
#
abbr_label <- function(x) {
#
src_map <- c(dog = "D", hum = "H", music = "M", chimp = "C")
cond_map <- c(h2 = "N", testsound = "TS")
vapply(x, function(s) {
#
m <- regexec("^\\s*([A-Za-z]+)\\s*-\\s*([A-Za-z]+)\\s+([A-Za-z0-9]+)\\s*$", s)
mm <- regmatches(s, m)[[1]]
if (length(mm) == 4) {
a <- tolower(mm[2]); b <- tolower(mm[3]); cnd <- tolower(mm[4])
aL <- src_map[a]; bL <- src_map[b]; cL <- cond_map[cnd]
if (is.na(aL) || is.na(bL) || is.na(cL)) return(s)
paste(aL, "-", bL, "-", cL)
} else {
s
}
}, character(1))
}
p5 <- plot(r) +
theme_classic(base_size = 22) +
labs(x = "Contrast", y = "Sound type x Sound condition", title = "", fill = "CrI") +
scale_y_discrete(
limits = function(x) rev(x),
labels = abbr_label
) +
theme(
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"),
plot.title = element_text(hjust = 0.5, face = "bold")
)
###save
fig7b <- cowplot::plot_grid(p5, labels = "b", label_size = 22, label_fontface = "bold")
ggsave("Fig7b.tiff", plot = p5, width = 10, height = 6, units = "in", dpi = 600, device = "tiff",
compression = "lzw")
```
```{r}
reduced_model_newpri_bs |>
emmeans( ~ hab2|species, dpar = "mu", type = "response", level = .89)|>
pairs(reverse = T)
```
```{r}
contr <- pairs(ph1, by = "species", reverse = T)
pd(contr, as_p = F)
rope(contr, range = c(-0.18, 0.18), ci = .89)
plot(rope(contr, range = c(-0.18, 0.18), ci = .89))
```