These are the estimates, summed over loci.
Stepping-Stone_KOD Model
For the model with ongoing gene flow between Kure and Oahu
Theta Estimates
And here I run the code for the \(\theta\) estimates
parameter_type <- "Theta"
parameters <- names(posterior) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
thetas <- summarize_posterior(posterior, parameter_type = parameter_type,
prior_params = c(0,0.001,0.1))
#convert to long format
thetas_long <- thetas %>% dplyr::select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density")
theta.colors <- rev(brewer.pal(9,"Spectral"))
theta.colors.light <- lighten(theta.colors)
theta.colors.dark <- darken(theta.colors)
#plot
thetas_density <- ggplot(thetas_long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
#scale_x_log10(limits = c(5e-4,1e-2)) +
scale_color_discrete(type = theta.colors,
labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted") +
ylim(0,0.175) + xlim(0,0.01)
thetas_ridges <- ggplot(thetas_long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity") +
scale_fill_discrete(type = theta.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$\\Theta$")) + xlim(0,0.01)
thetas_violins <- ggplot(thetas_long, aes(x = Parameter, y = x,
violinwidth = Density*10,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_fill_discrete(type = theta.colors, labels = parameter_key) +
scale_x_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(y = TeX("$\\Theta$")) + ylim(0,0.01)
# get stats for each summed-over-loci posterior
theta_stats <- parameter_key2 %>% map_dfr(.f = ~ posterior_stats(df = thetas,
parameter = .x),
.id = "parameter")
# write_csv(theta_stats, "tables/stepping.stone.KO.D/theta_stats.csv")
#
# ggsave("figures/stepping.stone.KO.D/thetas_density.jpg",thetas_density)
# ggsave("figures/stepping.stone.KO.D/thetas_ridges.jpg",thetas_ridges)
# ggsave("figures/stepping.stone.KO.D/thetas_violins.jpg",thetas_violins)
#
# ggsave("figures/stepping.stone.KO.D/thetas_density.pdf",thetas_density)
# ggsave("figures/stepping.stone.KO.D/thetas_ridges.pdf",thetas_ridges)
# ggsave("figures/stepping.stone.KO.D/thetas_violins.pdf",thetas_violins)
#https://adiradaniel.netlify.app/post/ggmultipane/#using-ggpubrggarrange
#to make multipanel figures
Theta estimates:
thetastats <- read_csv("./tables/stepping.stone.KO.D/theta_stats.csv")
## Rows: 9 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): parameter
## dbl (8): 2.5%, 25%, 50%, 75%, 97.5%, mode, mean, sd
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
thetastats
## # A tibble: 9 × 9
## parameter `2.5%` `25%` `50%` `75%` `97.5%` mode mean sd
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 theta Kure 0.00543 0.00559 0.00568 0.00575 0.00596 0.00569 0.00567 1.61e-4
## 2 theta P&H 0.00112 0.00123 0.00129 0.00136 0.00148 0.00128 0.00130 9.25e-5
## 3 theta Maro 0.00674 0.00698 0.00699 0.00700 0.00701 0.00699 0.00698 5.73e-5
## 4 theta FFS 0.00621 0.00678 0.00680 0.00682 0.00687 0.00680 0.00678 1.37e-4
## 5 theta Kauai 0.00797 0.00801 0.00803 0.00804 0.00807 0.00803 0.00802 2.73e-5
## 6 theta Oahu 0.00762 0.00765 0.00767 0.00768 0.00772 0.00767 0.00767 2.62e-5
## 7 theta Molokai 0.00678 0.00684 0.00686 0.00689 0.00775 0.00685 0.00693 2.44e-4
## 8 theta Maui 0.00771 0.00779 0.00783 0.00785 0.00789 0.00784 0.00781 4.82e-5
## 9 theta Big Is. 0.00632 0.00638 0.00640 0.00643 0.00649 0.00640 0.00639 1.10e-4

Theta Violins
m/mu estimates
parameter_type <- "M"
parameters <- names(posteriors12) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
Ms <- summarize_posterior(posteriors12, parameter_type = parameter_type,
prior_params = c(0,1,1000), n = 2^14)
#convert to long format
Ms_long <- Ms %>% select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density") %>%
# filter(Parameter != "M_6_1")
migration.colors <- c(rbind(theta.colors, theta.colors.dark))[4:18]
#plot
Ms_density <- ggplot(Ms_long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
scale_x_log10(limits = c(1e-2,100)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted")
#plot
M_ridges <- ggplot(Ms_long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity") +
scale_x_log10(limits = c(1e-2,100)) +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$\\frac{m}{\\mu}$"))
M_violins <- ggplot(Ms_long, aes(x = Parameter, y = x, violinwidth = Density,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_y_log10(limits = c(1e-2,100)) +
scale_fill_discrete(type = migration.colors, labels = parameter_key)
# get stats for each summed-over-loci posterior
Ms_stats <- parameter_key2 %>% map_dfr(.f = ~ posterior_stats(df = Ms,
parameter = .x),
.id = "parameter")
# write_csv(Ms_stats, "tables/stepping.stone.KO.D/Mmu_stats.csv")
#
ggsave("figures/stepping.stone.KO.D/Mmu_density.jpg",Ms_density)
ggsave("figures/stepping.stone.KO.D/Mmu_ridges.jpg",M_ridges)
ggsave("figures/stepping.stone.KO.D/Mmu_violins.jpg",M_violins)
ggsave("figures/stepping.stone.KO.D/Mmu_density.pdf",Ms_density)
ggsave("figures/stepping.stone.KO.D/Mmu_ridges.pdf",M_ridges)
ggsave("figures/stepping.stone.KO.D/Mmu_violins.pdf",M_violins)
M_ridges2<-M_ridges + theme(legend.position = "none")
ggsave(filename = "M_ridges_nolegend.pdf",plot=M_ridges2, path = "./figures/stepping.stone.KO.D/", width = 10.7, height = 6.42)
M/mu estimates:
mmustats <- read_csv("./tables/stepping.stone.KO.D/Mmu_stats.csv")
## Rows: 15 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): parameter
## dbl (8): 2.5%, 25%, 50%, 75%, 97.5%, mode, mean, sd
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mmustats
## # A tibble: 15 × 9
## parameter `2.5%` `25%` `50%` `75%` `97.5%` mode mean sd
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 m/mu Oahu to Kure 66.3 66.7 67.3 68.3 71.6 66.3 67.7 1.46
## 2 m/mu Maro to P&H 1.16 1.16 1.16 1.28 1.40 1.16 1.21 0.143
## 3 m/mu P&H to Maro 1.10 1.16 1.22 1.22 1.28 1.16 1.20 0.0680
## 4 m/mu FFS to Maro 0.244 0.244 0.244 0.244 0.305 0.244 0.249 0.0435
## 5 m/mu Maro to FFS 1.10 1.22 1.28 1.34 1.46 1.28 1.28 0.117
## 6 m/mu Kauai to FFS 0.549 0.671 0.732 0.794 0.916 0.671 0.724 0.104
## 7 m/mu FFS to Kauai 0.122 0.183 0.183 0.183 0.244 0.183 0.174 0.0461
## 8 m/mu Oahu to Kauai 0.183 0.183 0.244 0.488 0.610 0.183 0.308 0.183
## 9 m/mu Kauai to Oahu 0.305 0.427 0.488 0.488 0.610 0.488 0.465 0.0897
## 10 m/mu Molokai to Oahu 0.183 0.244 0.244 0.244 0.305 0.244 0.236 0.0499
## 11 m/mu Oahu to Molokai 0.366 0.427 0.427 0.488 0.549 0.427 0.454 0.0632
## 12 m/mu Maui to Molokai 1.04 1.22 1.28 1.40 1.65 1.28 1.30 0.149
## 13 m/mu Molokai to Maui 0.183 0.183 0.244 0.244 0.244 0.244 0.217 0.0439
## 14 m/mu Big Is. to Maui 0.732 0.916 1.10 1.16 1.22 1.16 1.04 0.173
## 15 m/mu Maui to Big Is. 1.95 1.95 1.95 2.01 2.01 1.95 1.98 0.0454

Mmu Violins
4Nem estimates
parameter_type <- "Nm"
parameters <- names(posteriors12) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
nms <- summarize_posterior(posteriors12, parameter_type = parameter_type,
prior_params = c(0,1,1), n = 2^17)
#convert to long format
nms_long <- nms %>% select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density") %>%
filter(Parameter != "Nm_6_1")
migration.colors <- c(rbind(theta.colors, theta.colors.dark))[4:18]
#plot
Nms_density <- ggplot(nms_long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
#scale_x_log10(limits = c(5e-4,1e-2)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted") +
ylim(0,0.125) + xlim(0,0.003)
#plot
nm_ridges <- ggplot(nms_long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity") +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$4N_em$")) +
xlim(0,0.003)
nm_violins <- ggplot(nms_long, aes(x = Parameter, y = x, violinwidth = Density*20,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
ylim(0,0.003)
# get stats for each summed-over-loci posterior
nms_stats <- parameter_key2 %>% map_dfr(.f = ~ posterior_stats(df = nms,
parameter = .x),
.id = "parameter")
# write_csv(nms_stats, "tables/stepping.stone.KO.D/4Nm_stats.csv")
#
# ggsave("figures/stepping.stone.KO.D/4Nm_density.jpg",Nms_density)
# ggsave("figures/stepping.stone.KO.D/4Nm_ridges.jpg",nm_ridges)
# ggsave("figures/stepping.stone.KO.D/4Nm_violins.jpg",nm_violins)
#
# ggsave("figures/stepping.stone.KO.D/4Nm_density.pdf",Nms_density)
# ggsave("figures/stepping.stone.KO.D/4Nm_ridges.pdf",nm_ridges)
# ggsave("figures/stepping.stone.KO.D/4Nm_violins.pdf",nm_violins)
# Nm_ridges2 <- Nm_ridges + theme(legend.position = "none")
# ggsave("figures/stepping.stone.KO.D/4Nm_ridges.pdf",nm_ridges)
\(4N_em\) estimates:
nmstats <- read_csv("./tables/stepping.stone.KO.D/4Nm_stats.csv")
## Rows: 15 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): parameter
## dbl (8): 2.5%, 25%, 50%, 75%, 97.5%, mode, mean, sd
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nmstats
## # A tibble: 15 × 9
## parameter `2.5%` `25%` `50%` `75%` `97.5%` mode mean sd
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 4Nem Oahu to… 9.09e-1 9.66e-1 9.83e-1 9.93e-1 9.99e-1 1 e+0 9.75e-1 2.46e-2
## 2 4Nem Maro to… 3.66e-4 4.27e-4 4.58e-4 4.96e-4 5.72e-4 4.50e-4 4.64e-4 5.47e-5
## 3 4Nem P&H to … 6.03e-4 6.71e-4 7.17e-4 7.63e-4 8.54e-4 7.10e-4 7.19e-4 6.61e-5
## 4 4Nem FFS to … 1.98e-4 2.21e-4 2.37e-4 2.59e-4 2.98e-4 2.37e-4 2.41e-4 2.66e-5
## 5 4Nem Maro to… 1.35e-3 1.52e-3 1.62e-3 1.72e-3 1.94e-3 1.61e-3 1.63e-3 1.51e-4
## 6 4Nem Kauai t… 6.49e-4 7.40e-4 7.93e-4 8.47e-4 9.69e-4 7.86e-4 7.97e-4 8.23e-5
## 7 4Nem FFS to … 8.70e-4 1.04e-3 1.14e-3 1.27e-3 1.54e-3 1.12e-3 1.16e-3 1.73e-4
## 8 4Nem Oahu to… 1.49e-3 1.74e-3 1.87e-3 2.01e-3 2.27e-3 1.86e-3 1.87e-3 2.01e-4
## 9 4Nem Kauai t… 1.08e-3 1.23e-3 1.31e-3 1.40e-3 1.57e-3 1.30e-3 1.31e-3 1.26e-4
## 10 4Nem Molokai… 9.99e-4 1.17e-3 1.26e-3 1.36e-3 1.56e-3 1.24e-3 1.26e-3 1.43e-4
## 11 4Nem Oahu to… 7.10e-4 8.24e-4 8.93e-4 9.61e-4 1.11e-3 8.85e-4 8.97e-4 1.03e-4
## 12 4Nem Maui to… 1.46e-3 1.65e-3 1.75e-3 1.86e-3 2.08e-3 1.75e-3 1.76e-3 1.61e-4
## 13 4Nem Molokai… 4.35e-4 5.04e-4 5.42e-4 5.80e-4 6.56e-4 5.34e-4 5.40e-4 5.67e-5
## 14 4Nem Big Is.… 1.26e-3 1.40e-3 1.47e-3 1.56e-3 1.75e-3 1.46e-3 1.48e-3 1.26e-4
## 15 4Nem Maui to… 1.17e-3 1.39e-3 1.52e-3 1.66e-3 2.05e-3 1.52e-3 1.54e-3 2.22e-4

##### 4NeM Estimate for Oahu - Kure
This parameter is orders of magnitude larger than the others, and
effectively can’t be summarized under the same prior limits. So this
code is just rejigged to focus on the Oahu-Kure parameter
parameter_type <- "Nm"
parameters <- names(posteriors12) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
nms61 <- summarize_posterior(posteriors12, parameter_type = parameter_type,
prior_params = c(1,1,1000), n = 2^14)
#convert to long format
nms_61long <- nms61 %>% select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density") %>%
filter(Parameter == "Nm_6_1")
migration.colors <- c(rbind(theta.colors, theta.colors.dark))
#plot
Nms_61density <- ggplot(nms_61long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
#scale_x_log10(limits = c(5e-4,1e-2)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted") +
ylim(0,0.125) + xlim(0,100)
#plot
nm_61ridges <- ggplot(nms_61long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity") +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$4N_em$")) +
xlim(0,50)
nm_61violins <- ggplot(nms_61long, aes(x = Parameter, y = x, violinwidth = Density*20,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
ylim(0,50)
# get stats for each summed-over-loci posterior
nms_61stats <-posterior_stats(df = nms61, parameter = "Nm_6_1")
nm61stats <- read.csv("./tables/stepping.stone.KO.D/4Nm_Oahu_Kure.csv")
nm61stats
## X x
## 1 2.5% 34.6597693
## 2 25% 34.7207471
## 3 50% 34.7207471
## 4 75% 34.7207471
## 5 97.5% 38.1355063
## 6 mode 34.7207471
## 7 mean 34.7966844
## 8 sd 0.8936556
Divergence Estimate Oahu-Kure
This is the divergence
parameter_type <- "D"
getx <- posteriors12 %>% filter(Locus == 1) %>%
dplyr::select("D_6_1")
dens <- tibble(.rows = 2^15, x = density(getx[,1], n=2^15,
from = 0,
to = 0.0001,
bw = "nrd0")$x)
dens2 <- posteriors12 %>%
select(starts_with(c("Steps","Locus","rep",
paste0(parameter_type,"_")))) %>%
pivot_wider(names_from = "Locus", values_from =
starts_with(paste0(parameter_type,"_")),
names_sep = "-") %>%
select(4:112) %>%
map_dfc(function(x) density(x, n = 2^15, from = 0, to = 0.001, bw = "nrd0")$y)
dens3<-bind_cols(dens,dens2)
dens3$prior <- dexp(dens3$x, rate = 1/0.001,log = F)
dens3$prior <- dens3$prior/sum(dens3$prior)
dens3$logPrior <- log(dens3$prior)
dens4 <- dens3 %>%
#remove the prior, standardize
mutate(across(starts_with(parameter_type),~ remove_prior(densityd = .x,
prior =dens$prior,threshold = 1e-10) ))
names(dens4)[2:110] <- paste0("D_6_1-",names(dens4)[1:109])
dens5 <- sum_over_loci(df = dens4, parameter = "D_6_1")
#convert to long format
D_61long <- dens5 %>% select(all_of(c("x","prior","D_6_1"))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density") %>%
filter(Parameter == "D_6_1")
migration.colors <- c(rbind(theta.colors, theta.colors.dark))
#plot
D_61density <- ggplot(D_61long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
#scale_x_log10(limits = c(5e-5,1e-2)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted") +
ylim(0,0.0004) + xlim(0,0.0001) + theme(legend.position = "none")
# get stats for each summed-over-loci posterior
D_61stats <-posterior_stats(df = dens5, parameter = "D_6_1")
# write_csv(data.frame(D_61stats), "tables/stepping.stone.KO.D/D_6_1_stats.csv")
#
# ggsave("figures/stepping.stone.KO.D/D_6_1.jpg",D_61density)
Divergence Density
D61stats <- read.csv("./tables/stepping.stone.KO.D/D_6_1_stats.csv")
D61stats
## D_61stats
## 1 5.261391e-05
## 2 5.735954e-05
## 3 6.005432e-05
## 4 6.291086e-05
## 5 6.875210e-05
## 6 5.970336e-05
## 7 6.022191e-05
## 8 4.123291e-06
Divergence time in coalescent units following along on page 25 here.
Assuming 1 year generation time Beerli et al. (2019)
D <- 5.970336e-05
tau <- D / (0.00767+0.00569)
tau
## [1] 0.004468814
t <- D / 1.2e-8
t
## [1] 4975.28
Divergence time is estimated at 4975.28 years before present
Stepping-Stone KO.d model
This is the same model, without ongoing gene flow between Kure and
Oahu
Read in the posteriors and make a labeling key
#
Dir <- "/Users/eric/Library/CloudStorage/OneDrive-ThePennsylvaniaStateUniversity/Documents/Research/Ptuberculosa/migrate/run4.7"
winningModel <- "stepping.stone.KOd"
posteriors <- list()
#read in the posterior for two replicates this may take 5 minutes
for(r in 1:2){
print(paste("Loading Rep",r))
posteriors[[r]] <- read.table(file.path(Dir,paste0("rep",r),winningModel,
"bayesallfile"), header=T)
}
posteriors <- lapply(posteriors, select, starts_with(c("Locus","Steps","Theta_", "M_",
"Nm_","D_","d_")))
#combine into one, and then keep only the first 2 reps (based on diagnostics below)
posteriors12 <- posteriors %>% bind_rows(.id = "rep") %>%
migrants.per.gen() %>% mutate(rep = as.integer(rep))
#posteriors12_long <- posteriors12 %>% pivot_longer(starts_with(c("Theta_", "M_",
# "Nm_","D_","d_")),
# names_to = "parameter",
# values_to = "value")
# read in a population key to translate numbers into names in figures
popkey <- read_csv("popkey.csv")
names(popkey$Pop) <- popkey$Index
#make a new labeling key
posterior_parameters <- names(posteriors12) %>% str_subset("Theta|M_|Nm_|D_|d_")
parameter_key <- posterior_parameters %>%
str_replace("(\\d)_(\\d)", "\\1 to \\2") %>%
str_replace("M_", "m/mu " ) %>%
str_replace("Theta_", "theta ") %>%
str_replace_all(popkey$Pop) %>%
str_replace("Nm_", "4Nem ")
names(parameter_key) <- posterior_parameters
Check Convergence
# posteriors.grouped <- posteriors %>%
# select(all_of(c("Locus",
# "Steps",
# posterior_parameters))) %>%
# group_by(rep)
# posterior.reps <- group_split(posterior.grouped)
# posterior.key <- group_keys(posterior.grouped)
#thin by taking every other step
posteriors2 <- lapply(posteriors,filter,Steps%%500==0)
# select parameters only
posteriors2 <- lapply(posteriors2, select, starts_with(c("Theta_", "M_",
"Nm_","D_","d_")))
posteriors.mcmc <- mcmc.list(lapply(posteriors2,mcmc,thin=500))
posterior.ggs <- ggs(posteriors.mcmc, burnin = F)
ggmcmc(posterior.ggs, param_page = 4)
#if I wanted to pull out parameters by locus, but I don't - Peter calculates Effective Sizes on the whole posterior
posteriors3 <- posteriors15 %>% select(starts_with(c("Replicate","Steps","Locus","Theta_", "M_",
"Nm_","D_","d_"))) %>% mutate(Locus_name = Locus)
posteriors3_Nm <- posteriors3 %>% select(starts_with(c("Replicate","Steps","Locus",
"Nm_"))
posteriors3_Nm <- posteriors3 %>% pivot_wider(id_cols = c("Steps", "Replicate","Locus"),
names_from = "Locus_name",
values_from =starts_with(c("Theta_", "M_",
"Nm_","D_","d_")), names_sep = "-L")
posteriors3_Nm.mcmc <- mcmc(posteriors3_Nm)
effectiveSize(posteriors3_Nm.mcmc)
Effective Sizes
Effective sizes are good
effectiveSize(posteriors2[[1]]) (first replicate)
Theta_1 Theta_2 Theta_3 Theta_4 Theta_5 Theta_6 Theta_7 Theta_8 Theta_9 M_3_2 M_2_3 M_4_3 M_3_4
140194.20 99698.77 114330.54 65325.45 59135.13 73193.97 109083.37 70626.00 107475.10 50591.32 172911.83 90997.00 269569.09
M_5_4 M_4_5 M_6_5 M_5_6 M_7_6 M_6_7 M_8_7 M_7_8 M_9_8 M_8_9 D_6_1
327963.24 297808.14 273528.90 337943.73 270026.70 354645.35 350985.75 653891.00 653891.00 653891.00 170758.40
Gelman-Rubin Diagnostic
Gelman-Rubin diagnostic across 5 replicates is a bit high for some M_
parameters, but all below 1.2.
gelman.diag(posteriors.mcmc, autoburnin = F)
Potential scale reduction factors:
Point est. Upper C.I.
Theta_1 1.00 1.00
Theta_2 1.00 1.01
Theta_3 1.01 1.02
Theta_4 1.01 1.01
Theta_5 1.01 1.03
Theta_6 1.00 1.00
Theta_7 1.00 1.00
Theta_8 1.00 1.01
Theta_9 1.01 1.01
M_3_2 1.14 1.17
M_2_3 1.13 1.13
M_4_3 1.12 1.12
M_3_4 1.07 1.07
M_5_4 1.15 1.15
M_4_5 1.00 1.00
M_6_5 1.01 1.01
M_5_6 1.12 1.12
M_7_6 1.06 1.06
M_6_7 1.10 1.10
M_8_7 1.23 1.24
M_7_8 1.10 1.10
M_9_8 1.12 1.12
M_8_9 1.21 1.25
D_6_1 1.00 1.00
Multivariate psrf
1.01
gelman.diag(posteriors.mcmc, transform = T,autoburnin=F)
Potential scale reduction factors:
Potential scale reduction factors:
Point est. Upper C.I.
Theta_1 1.00 1.00
Theta_2 1.00 1.00
Theta_3 1.01 1.02
Theta_4 1.00 1.01
Theta_5 1.01 1.02
Theta_6 1.00 1.00
Theta_7 1.00 1.00
Theta_8 1.00 1.00
Theta_9 1.00 1.00
M_3_2 1.01 1.01
M_2_3 1.00 1.01
M_4_3 1.01 1.02
M_3_4 1.00 1.01
M_5_4 1.00 1.01
M_4_5 1.00 1.01
M_6_5 1.00 1.01
M_5_6 1.00 1.01
M_7_6 1.00 1.00
M_6_7 1.01 1.01
M_8_7 1.01 1.02
M_7_8 1.01 1.01
M_9_8 1.01 1.02
M_8_9 1.01 1.02
D_6_1 1.00 1.00
Multivariate psrf
1.02
Theta Estimates
And here I run the code for the \(\theta\) estimates
parameter_type <- "Theta"
parameters <- names(posteriors12) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
thetas <- summarize_posterior(posteriors12, parameter_type = parameter_type,
prior_params = c(0,0.001,0.1))
#convert to long format
thetas_long <- thetas %>% select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density")
theta.colors <- rev(brewer.pal(9,"Spectral"))
theta.colors.light <- lighten(theta.colors)
theta.colors.dark <- darken(theta.colors)
#plot
thetas_density <- ggplot(thetas_long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(linewidth= 1) +
#scale_x_log10(limits = c(5e-4,1e-2)) +
scale_color_discrete(type = theta.colors,
labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted") +
ylim(0,0.175) + xlim(0,0.01)
thetas_ridges <- ggplot(thetas_long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity", scale = 5) +
scale_fill_discrete(type = theta.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$\\Theta$")) + xlim(0,0.01)
thetas_violins <- ggplot(thetas_long, aes(x = Parameter, y = x,
violinwidth = Density*10,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_fill_discrete(type = theta.colors, labels = parameter_key) +
scale_x_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(y = TeX("$\\Theta$")) + ylim(0,0.01)
# get stats for each summed-over-loci posterior
theta_stats <- parameter_key2 %>% map_dfr(.f = ~ posterior_stats(df = thetas,
parameter = .x),
.id = "parameter")
#write_csv(theta_stats,"./tables/stepping.stone.KOd/theta_stats.csv")
#
# ggsave(filename = "thetas_density.jpg",plot=thetas_density,
# path = "./figures/stepping.stone.KOd")
# ggsave(filename = "thetas_ridges.jpg",plot=thetas_ridges,
# path = "./figures/stepping.stone.KOd")
# ggsave(filename = "thetas_violins.jpg",plot=thetas_violins,
# path = "./figures/stepping.stone.KOd")
#
# ggsave(filename = "thetas_density.pdf",plot=thetas_density,
# path = "./figures/stepping.stone.KOd")
# ggsave(filename = "thetas_ridges.pdf",plot=thetas_ridges,
# path = "./figures/stepping.stone.KOd")
# ggsave(filename = "thetas_violins.pdf",plot=thetas_violins,
# path = "./figures/stepping.stone.KOd")
#
Theta estimates:
thetastats <- read_csv("./tables/stepping.stone.KOd/theta_stats.csv")
## Rows: 9 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): parameter
## dbl (8): 2.5%, 25%, 50%, 75%, 97.5%, mode, mean, sd
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
thetastats
## # A tibble: 9 × 9
## parameter `2.5%` `25%` `50%` `75%` `97.5%` mode mean sd
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 theta Kure 0.00393 0.00418 0.00425 0.00435 0.00520 0.00423 0.00429 2.42e-4
## 2 theta P&H 0.00132 0.00146 0.00152 0.00159 0.00173 0.00150 0.00153 1.04e-4
## 3 theta Maro 0.00667 0.00670 0.00671 0.00672 0.00675 0.00671 0.00671 2.10e-5
## 4 theta FFS 0.00778 0.00780 0.00782 0.00783 0.00786 0.00782 0.00781 6.22e-5
## 5 theta Kauai 0.00773 0.00778 0.00780 0.00783 0.00788 0.00779 0.00780 3.97e-5
## 6 theta Oahu 0.00740 0.00745 0.00747 0.00749 0.00753 0.00748 0.00747 3.33e-5
## 7 theta Molokai 0.00691 0.00697 0.00701 0.00707 0.00716 0.00699 0.00702 7.19e-5
## 8 theta Maui 0.00726 0.00731 0.00734 0.00736 0.00740 0.00734 0.00734 4.51e-5
## 9 theta Big Is. 0.00498 0.00513 0.00536 0.00545 0.00623 0.00541 0.00540 3.62e-4
Theta Ridges
m/mu estimates
parameter_type <- "M"
parameters <- names(posteriors12) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
Ms <- summarize_posterior(posteriors12, parameter_type = parameter_type,
prior_params = c(0,1,1000), n = 2^14)
#convert to long format
Ms_long <- Ms %>% select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density")
migration.colors <- c(rbind(theta.colors, theta.colors.dark))[4:18]
#plot
M_density <- ggplot(Ms_long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
scale_x_log10(limits = c(1e-2,100)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted")
#plot
M_ridges <- ggplot(Ms_long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity") +
scale_x_log10(limits = c(1e-2,100)) +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$\\frac{m}{\\mu}$"))
M_violins <- ggplot(Ms_long, aes(x = Parameter, y = x, violinwidth = Density,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_y_log10(limits = c(1e-2,100)) +
scale_fill_discrete(type = migration.colors, labels = parameter_key)
# get stats for each summed-over-loci posterior
Ms_stats <- parameter_key2 %>% map_dfr(.f = ~ posterior_stats(df = Ms,
parameter = .x),
.id = "parameter")
ggsave(filename = "M_density.jpg",plot=M_density, path = "./figures/stepping.stone.KOd")
ggsave(filename = "M_ridges.jpg",plot=M_ridges, path = "./figures/stepping.stone.KOd")
ggsave(filename = "M_violins.jpg",plot=M_violins, path = "./figures/stepping.stone.KOd")
write_csv(Ms_stats,"./tables/stepping.stone.KOd/Mmu_stats.csv")
ggsave(filename = "M_density.pdf",plot=M_density, path = "./figures/stepping.stone.KOd")
ggsave(filename = "M_ridges.pdf",plot=M_ridges, path = "./figures/stepping.stone.KOd")
ggsave(filename = "M_violins.pdf",plot=M_violins, path = "./figures/stepping.stone.KOd")
M_ridges2 <- M_ridges + theme(legend.position="none")
ggsave(filename = "M_ridges_nolegend.pdf",plot=M_ridges2, path = "./figures/stepping.stone.KOd", width = 10.7, height = 6.42)
M/mu estimates:
mmustats <- read_csv("./tables/stepping.stone.KOd/Mmu_stats.csv")
## Rows: 14 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): parameter
## dbl (8): 2.5%, 25%, 50%, 75%, 97.5%, mode, mean, sd
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
mmustats
## # A tibble: 14 × 9
## parameter `2.5%` `25%` `50%` `75%` `97.5%` mode mean sd
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 m/mu Maro to P&H 1.28 1.34 1.40 1.40 1.46 1.40 1.37 0.0663
## 2 m/mu P&H to Maro 0.916 1.04 1.04 1.10 1.28 1.04 1.07 0.0989
## 3 m/mu FFS to Maro 0.244 0.244 0.244 0.244 0.305 0.244 0.249 0.0434
## 4 m/mu Maro to FFS 1.53 2.01 2.08 2.08 2.14 2.08 1.96 0.251
## 5 m/mu Kauai to FFS 0.183 0.244 0.244 0.244 0.305 0.244 0.253 0.0596
## 6 m/mu FFS to Kauai 0.183 0.244 0.244 0.244 0.305 0.244 0.244 0.0532
## 7 m/mu Oahu to Kauai 0.122 0.122 0.183 0.244 0.305 0.183 0.182 0.0634
## 8 m/mu Kauai to Oahu 0.183 0.183 0.183 0.244 0.305 0.183 0.212 0.0582
## 9 m/mu Molokai to Oahu 0.122 0.183 0.183 0.183 0.244 0.183 0.186 0.0449
## 10 m/mu Oahu to Molokai 0.305 0.366 0.366 0.366 0.427 0.366 0.364 0.0540
## 11 m/mu Maui to Molokai 1.04 1.16 1.22 1.28 1.34 1.16 1.20 0.110
## 12 m/mu Molokai to Maui 0.183 0.183 0.244 0.244 0.244 0.244 0.222 0.0435
## 13 m/mu Big Is. to Maui 1.04 1.10 1.16 1.16 1.16 1.16 1.13 0.0548
## 14 m/mu Maui to Big Is. 0.916 0.977 0.977 1.04 1.04 0.977 0.992 0.0461
Mmu Ridges
4Nem estimates
parameter_type <- "Nm"
parameters <- names(posteriors12) %>%
str_subset(parameter_type)
parameter_key2 <- parameters
names(parameter_key2) <- parameter_key[names(parameter_key) %in% parameter_key2]
#summarize the posterior for theta
nms <- summarize_posterior(posteriors12, parameter_type = parameter_type,
prior_params = c(0,1,100), n = 2^17)
#convert to long format
nms_long <- nms %>% select(all_of(c("x","prior",parameters))) %>%
pivot_longer(starts_with(c(paste0(parameter_type,"_"))),
names_to = "Parameter",
values_to = "Density") %>%
filter(Parameter != "Nm_6_1")
migration.colors <- c(rbind(theta.colors, theta.colors.dark))[4:18]
#plot
Nm_density <- ggplot(nms_long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
#scale_x_log10(limits = c(5e-4,1e-2)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
geom_line(aes(x = x, y=prior), color = "grey",
linetype="dotted") +
ylim(0,0.125) + xlim(0,0.003)
#plot
Nm_ridges <- ggplot(nms_long, aes(x = x, y = Parameter, height = Density,
fill = Parameter)) +
geom_density_ridges(stat = "identity") +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
scale_y_discrete(limits = rev, labels = NULL, breaks = NULL) +
labs(x = TeX("$4N_em$")) +
xlim(0,0.003)
Nm_violins <- ggplot(nms_long, aes(x = Parameter, y = x, violinwidth = Density*20,
fill = Parameter)) +
geom_violin(stat = "identity") +
scale_fill_discrete(type = migration.colors, labels = parameter_key) +
ylim(0,0.003)
# get stats for each summed-over-loci posterior
Nm_stats <- parameter_key2 %>% map_dfr(.f = ~ posterior_stats(df = nms,
parameter = .x),
.id = "parameter")
# ggsave(filename = "Nm_density.jpg",plot=Nm_density, path = "./figures/stepping.stone.KOd")
# ggsave(filename = "Nm_ridges.jpg",plot=Nm_ridges, path = "./figures/stepping.stone.KOd")
# ggsave(filename = "Nm_violins.jpg",plot=Nm_violins, path = "./figures/stepping.stone.KOd")
#
# ggsave(filename = "Nm_density.pdf",plot=Nm_density, path = "./figures/stepping.stone.KOd")
# ggsave(filename = "Nm_ridges.pdf",plot=Nm_ridges, path = "./figures/stepping.stone.KOd")
# ggsave(filename = "Nm_violins.pdf",plot=Nm_violins, path = "./figures/stepping.stone.KOd")
#
# write_csv(Nm_stats,"./tables/stepping.stone.KOd/Nm_stats.csv")
#
# Nm_ridges2 <- Nm_ridges + theme(legend.position="none")
# ggsave(filename = "Nm_ridges_nolegend.pdf",plot=Nm_ridges2, path = "./figures/stepping.stone.KOd", width = 10.7, height = 6.42)
# ggsave(filename = "Nm_ridges.pdf",plot=Nm_ridges, path = "./figures/stepping.stone.KOd", width = 10.7, height = 6.42)
\(4N_em\) estimates:
nmstats <- read_csv("./tables/stepping.stone.KOd/Nm_stats.csv")
## Rows: 14 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): parameter
## dbl (8): 2.5%, 25%, 50%, 75%, 97.5%, mode, mean, sd
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nmstats
## # A tibble: 14 × 9
## parameter `2.5%` `25%` `50%` `75%` `97.5%` mode mean sd
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 4Nem Maro to… 4.73e-4 5.57e-4 6.10e-4 6.64e-4 7.86e-4 6.03e-4 6.14e-4 8.12e-5
## 2 4Nem P&H to … 5.42e-4 6.18e-4 6.64e-4 7.02e-4 7.93e-4 6.56e-4 6.62e-4 6.50e-5
## 3 4Nem FFS to … 2.06e-4 2.37e-4 2.52e-4 2.67e-4 3.05e-4 2.44e-4 2.51e-4 2.71e-5
## 4 4Nem Maro to… 1.17e-3 1.32e-3 1.40e-3 1.50e-3 1.71e-3 1.39e-3 1.41e-3 1.39e-4
## 5 4Nem Kauai t… 5.19e-4 5.95e-4 6.33e-4 6.79e-4 7.71e-4 6.26e-4 6.36e-4 6.46e-5
## 6 4Nem FFS to … 1.21e-3 1.41e-3 1.53e-3 1.66e-3 1.95e-3 1.48e-3 1.54e-3 1.87e-4
## 7 4Nem Oahu to… 1.10e-3 1.29e-3 1.40e-3 1.50e-3 1.77e-3 1.41e-3 1.40e-3 1.71e-4
## 8 4Nem Kauai t… 9.23e-4 1.06e-3 1.14e-3 1.24e-3 1.43e-3 1.13e-3 1.15e-3 1.28e-4
## 9 4Nem Molokai… 8.54e-4 9.77e-4 1.05e-3 1.13e-3 1.30e-3 1.04e-3 1.06e-3 1.17e-4
## 10 4Nem Oahu to… 6.94e-4 7.86e-4 8.39e-4 9.00e-4 1.03e-3 8.32e-4 8.47e-4 8.83e-5
## 11 4Nem Maui to… 1.10e-3 1.27e-3 1.36e-3 1.46e-3 1.66e-3 1.36e-3 1.36e-3 1.46e-4
## 12 4Nem Molokai… 4.27e-4 4.81e-4 5.11e-4 5.42e-4 6.10e-4 5.04e-4 5.11e-4 4.87e-5
## 13 4Nem Big Is.… 1.11e-3 1.24e-3 1.31e-3 1.39e-3 1.56e-3 1.30e-3 1.32e-3 1.15e-4
## 14 4Nem Maui to… 1.25e-3 1.76e-3 2.11e-3 2.37e-3 2.80e-3 2.23e-3 2.07e-3 4.23e-4
4Nem Ridges
#### Divergence Estimate Oahu-Kure
This is the divergence
parameter_type <- "D"
getx <- posteriors12 %>% filter(Locus == 1) %>%
dplyr::select("D_6_1")
dens <- tibble(.rows = 2^15, x = density(getx[,1], n=2^15,
from = 0,
to = 0.0001,
bw = "nrd0")$x)
dens2 <- posteriors12 %>%
select(starts_with(c("Steps","Locus","rep",
paste0(parameter_type,"_")))) %>%
pivot_wider(names_from = "Locus", values_from =
starts_with(paste0(parameter_type,"_")),
names_sep = "-") %>%
select(3:111) %>%
map_dfc(function(x) density(x, n = 2^15, from = 0, to = 0.001, bw = "nrd0")$y)
dens2<-bind_cols(dens,dens2)
dens2$prior <- dexp(dens2$x, rate = 1/0.001,log = F)
dens2$prior <- dens2$prior/sum(dens2$prior)
dens2$logPrior <- log(dens2$prior)
dens4 <- dens2 %>%
#remove the prior, standardize
mutate(across(starts_with(parameter_type),
~ remove_prior(densityd = .x,
prior = dens2$prior,threshold = 1e-10) ))
names(dens4)[1:109] <- paste0("D_6_1-",names(dens4)[2:110])
dens5 <- sum_over_loci(df = dens4, parameter = "D_6_1")
dens5 <- bind_cols(dens5, x=dens$x)
#convert to long format
D_61long <- dens5 %>%
pivot_longer(starts_with(c(paste0("D","_"))),
names_to = "Parameter",
values_to = "Density") %>%
filter(Parameter == "D_6_1")
#D_61long <- bind_cols(D_61long,x=dens$x)
#migration.colors <- c(rbind(theta.colors, theta.colors.dark))
#plot
D_61density <- ggplot(D_61long, aes(x = x, y = Density,
color = Parameter)) +
geom_line(size = 1) +
#scale_x_log10(limits = c(5e-5,1e-2)) +
scale_color_discrete(type = migration.colors, labels = parameter_key) +
#geom_line(aes(x = x, y=prior), color = "grey",
# linetype="dotted") +
ylim(0,0.0003) + xlim(0,0.0001) + theme(legend.position = "none")
# get stats for each summed-over-loci posterior
D_61stats <-posterior_stats(df = dens5, parameter = "D_6_1")
#ggsave(filename = "D_6_1.jpg",plot=D_61density, path = "./figures/stepping.stone.KOd")
#write.csv(D_61stats ,"./tables/stepping.stone.KOd/D_6_1.csv",quote=F)
Divergence Density
D61stats <- read.csv("./tables/stepping.stone.KOd/D_6_1.csv")
D61stats
## X x
## 1 2.5% 5.244911e-05
## 2 25% 5.739006e-05
## 3 50% 6.014893e-05
## 4 75% 6.302683e-05
## 5 97.5% 6.880093e-05
## 6 mode 5.988647e-05
## 7 mean 6.027578e-05
## 8 sd 4.178363e-06