
plot_linear_data = function(linear_data, alpha, beta) {
  ggplot(data.frame(x=linear_data$x, y=linear_data$y), mapping=aes(x=x, y=y)) +
    geom_point(color = "steelblue") +
    geom_abline(slope = beta, intercept = alpha, col = "darkgreen") +
    ggtitle("Linear regression", subtitle = paste0("alpha = ", alpha, ", beta = ", beta)) +
    theme_bw()
}

plot_comparison_linear_regression = function(fit_mcmc, fit_pathfinder) {
  draws = dplyr::tibble()
  for (par in c("alpha", "beta")) {
    draws_mcmcm <- as.data.frame(fit_mcmc$draws(variables = c(par), format = "draws_matrix")) %>%
      `colnames<-`("value") %>%
      mutate(algorithm = "mcmc", par_name=par)
    draws_pf <- as.data.frame(fit_pathfinder$draws(variables = c(par), format = "draws_matrix")) %>%
      `colnames<-`("value") %>%
      mutate(algorithm = "pathfinder", par_name=par)
    draws <- dplyr::bind_rows(draws, draws_mcmcm, draws_pf)
  }

  draws %>%
    ggplot(mapping = aes(x=value, fill=algorithm, col=algorithm)) +
    geom_density(alpha=.5) +
    facet_wrap(~ par_name, scales = "free") +
    theme_bw() +
    scale_fill_manual(values = c("deepskyblue3", "indianred3")) +
    scale_color_manual(values = c("deepskyblue3", "indianred3")) +
    theme(legend.position = "bottom")
}

plot_comparison_mixture_means = function(fit_mcmc, fit_pathfinder) {
  draws = dplyr::tibble()
  for (par in c("mu[1]", "mu[2]")) {
    draws_mcmcm <- as.data.frame(fit_mcmc$draws(variables = c(par), format = "draws_matrix")) %>%
      `colnames<-`("value") %>%
      mutate(algorithm = "mcmc", par_name=par)
    draws_pf <- as.data.frame(fit_pathfinder$draws(variables = c(par), format = "draws_matrix")) %>%
      `colnames<-`("value") %>%
      mutate(algorithm = "pathfinder", par_name=par)
    draws <- dplyr::bind_rows(draws, draws_mcmcm, draws_pf)
  }
  
  draws %>%
    ggplot(mapping = aes(x=value, fill=algorithm, col=algorithm)) +
    geom_density(alpha=.5) +
    facet_wrap(~ par_name, scales = "free") +
    theme_bw() +
    scale_fill_manual(values = c("deepskyblue3", "indianred3")) +
    scale_color_manual(values = c("deepskyblue3", "indianred3")) +
    theme(legend.position = "bottom")
}

plot_comparison_mixture_proportions = function(fit_mcmc, fit_pathfinder) {
  draws = dplyr::tibble()
  for (par in c("theta[1]", "theta[2]")) {
    draws_mcmcm <- as.data.frame(fit_mcmc$draws(variables = c(par), format = "draws_matrix")) %>%
      `colnames<-`("value") %>%
      mutate(algorithm = "mcmc", par_name=par)
    draws_pf <- as.data.frame(fit_pathfinder$draws(variables = c(par), format = "draws_matrix")) %>%
      `colnames<-`("value") %>%
      mutate(algorithm = "pathfinder", par_name=par)
    draws <- dplyr::bind_rows(draws, draws_mcmcm, draws_pf)
  }
  
  draws %>%
    ggplot(mapping = aes(x=value, fill=algorithm, col=algorithm)) +
    geom_density(alpha=.5) +
    facet_wrap(~ par_name, scales = "free") +
    theme_bw() +
    scale_fill_manual(values = c("deepskyblue3", "indianred3")) +
    scale_color_manual(values = c("deepskyblue3", "indianred3")) +
    theme(legend.position = "bottom")
}
