Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. progesterone <- c(1513, 2025)
  2. placebo <- c(1459, 2013)
  3. (tab <- as.data.frame(rbind(progesterone, placebo)))
  4. names(tab) <- c("pass", "total")
  5. tab$fail <- tab$total - tab$pass
  6. tab$treatment <- c(1, 0)
  7. chisq.test(tab[c("pass", "fail")], correct = FALSE)
  8. # chisq.test(long.tab$treatment, long.tab$pass, correct = FALSE)
  9. summary(glm(cbind(pass, fail) ~ treatment, binomial, tab))
  10.  
  11. long.tab <- data.frame(treatment = c(rep(1, tab$total[tab$treatment == 1]),
  12. rep(0, tab$total[tab$treatment == 0])),
  13. pass = c(rep(1, tab$pass[tab$treatment == 1]),
  14. rep(0, tab$fail[tab$treatment == 1]),
  15. rep(1, tab$pass[tab$treatment == 0]),
  16. rep(0, tab$fail[tab$treatment == 0])))
  17. summary(lm(pass ~ treatment, long.tab))
  18. summary(glm(pass ~ treatment, quasipoisson, long.tab))
  19.  
  20. library(rstan)
  21. options(mc.cores = parallel::detectCores())
  22. rstan_options(auto_write = TRUE)
  23.  
  24. # Place stan script in stan_scripts folder
  25. # Uncomment next two lines to compile stan script
  26. # saveRDS(stan_model(stanc_ret = stanc(file = "stan_scripts/two_group_logistic.stan")),
  27. # "stan_scripts/two_group_logistic.rds")
  28. two.grp.stan <- readRDS("stan_scripts/two_group_logistic.rds")
  29. dat <- list(sd_m = 4 / qnorm(.975), sd_m_diff = 2 / qnorm(.975),
  30. pass = rev(tab$pass), total = rev(tab$total))
  31.  
  32. two.grp.fit <- sampling(two.grp.stan, data = dat, iter = 4e3)
  33. print(two.grp.fit, digits_summary = 3,
  34. c("m0", "m_diff", "means_prob", "odds_ratio", "prob_ratio", "prob_diff"))
  35.  
  36. eff.df <- as.data.frame(two.grp.fit, c("odds_ratio", "prob_ratio", "prob_diff"))
  37. apply(eff.df, 2, function (x) quantile(x, .99))
  38. eff.df$odds_ratio.eff <- seq(1, 1.35, length.out = nrow(eff.df))
  39. eff.df$odds_ratio.p <- sapply(eff.df$odds_ratio.eff, function (x) mean(eff.df$odds_ratio > x))
  40. eff.df$prob_ratio.eff <- seq(1, 1.08, length.out = nrow(eff.df))
  41. eff.df$prob_ratio.p <- sapply(eff.df$prob_ratio.eff, function (x) mean(eff.df$prob_ratio > x))
  42. eff.df$prob_diff.eff <- seq(0, .06, length.out = nrow(eff.df))
  43. eff.df$prob_diff.p <- sapply(eff.df$prob_diff.eff, function (x) mean(eff.df$prob_diff > x))
  44. eff.df$post.ID <- 1:nrow(eff.df)
  45.  
  46. names(eff.df)
  47. eff.df <- reshape(eff.df, direction = "long", varying = list(1:3, c(4, 6, 8), c(5, 7, 9)),
  48. idvar = "post.ID", times = c("Odds Ratio", "Risk Ratio", "Risk Difference"),
  49. v.names = c("Obs", "ES", "P"))
  50. eff.df$time <- factor(eff.df$time, levels = c("Odds Ratio", "Risk Ratio", "Risk Difference"))
  51.  
  52. library(ggplot2)
  53. library(scales)
  54. theme_set(theme_bw())
  55. library(dplyr)
  56.  
  57. eff.df %>%
  58. group_by(time) %>% mutate(eff.mean = median(Obs)) %>%
  59. ggplot(aes(ES, P)) + geom_line() +
  60. geom_vline(aes(xintercept = eff.mean), linetype = 2, alpha = .5) +
  61. facet_wrap(~ time, scales = "free_x") +
  62. scale_y_continuous(labels = percent_format(), breaks = seq(0, 1, .1)) +
  63. labs(x = "Plausible Effect Size", y = "Prob.(Observed Effect Size > Plausible Effect Size)",
  64. subtitle = "Probability progesterone had an effect exceeding varying effect thresholds") +
  65. theme(panel.border = element_blank(), axis.ticks = element_blank(),
  66. strip.background = element_blank(), panel.spacing = unit(1, "cm"))
  67. ggsave("progesterone_eff.pdf", height = 5, width = 6.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement