Advertisement
Guest User

Untitled

a guest
Oct 31st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. ############################################################################################
  2. ##
  3. ## Stability of results from small RCT studies.
  4. ## Source: https://discourse.datamethods.org/t/stability-of-results-from-small-rcts/2287/11
  5. ## Original Author: R_cubed
  6. ## See also: ANDROMEDA-SHOCK trial discussion
  7.  
  8. quants <- c(0.00, 0.025, 0.05, 0.25, 0.50, 0.75, 0.95, 0.975, 1)
  9.  
  10. ## 1. *p*-values under a true (point) null hypothesis.
  11. ## In this scenario, *p*-values take on a uniform distribution. Calling this
  12. ## function with any integer N is equivalent to simulating N studies with a
  13. ## true difference of 0.
  14. null_true_p <- function(n) {
  15. null <- c(runif(n))
  16. result <- quantile(null, probs = quants)
  17. hist(null,
  18. freq = FALSE,
  19. main = expression(paste(H[0], " is True")),
  20. xlab = expression(paste(italic(p),"-values")))
  21. result
  22. }
  23.  
  24.  
  25. ## 2. *p*-values under a true difference (standardized effect size of 0.53)
  26. ## The median *p*-value varies based on how many studies are done, but is
  27. ## frequently 0.75>p≤0.95 , with small numbers of studies (a common scenario
  28. ## in meta-analysis). Note the skew the distribution. But skeptics will
  29. ## almost always be able to find a large number of “contradictory” studies
  30. ## going by “significance” alone.
  31. null_false_p <- function(sample, sim_size)
  32. {
  33. reject <- numeric(sim_size)
  34. for (i in 1:sim_size)
  35. {
  36. x <- rnorm(sample, 100, 15)
  37. y <- rnorm(sample, 108, 15)
  38. reject[i] <- t.test(x,y,alternative="greater")$p.value
  39. result <- quantile(reject, probs = quants)
  40. }
  41. hist(reject,
  42. main = expression(paste(H[alt], " is True")),
  43. freq = FALSE,
  44. xlab = expression(paste(italic(p),"-values")),
  45. sub = "Effect Size=0.53, Sample Size=15")
  46.  
  47. result
  48. }
  49.  
  50. ## 3. Meta-analysis under null
  51. ## The following code simulates a meta-analysis based on *p*-value
  52. ## combination using Stouffer’s method (ie. inverse normal transform).
  53. ## 95% of the (one-tailed) *t* statistics are within the range of
  54. ## ±1.645; 95% are in the range of ±2.326, and 99.9% are in the range
  55. ## of ±3.09 )
  56. true_null_meta <- function(sample, sim_size)
  57. {
  58. stouffer <- numeric(sim_size)
  59. for (i in 1:sim_size)
  60. {
  61. null <- runif(sample)
  62. z_score <- c(qnorm(null))
  63. stouffer[i] <- sum( z_score/sqrt(sample) )
  64. result <- quantile(stouffer, probs = quants)
  65. }
  66. hist(stouffer,
  67. freq = FALSE,
  68. main = expression(paste(H[0], " is True")),
  69. xlab = "Stouffer z-scores",
  70. sub = paste("Number of Meta-Studies",sample))
  71. abline(v=qnorm(0.975), col='red', lty=2)
  72. abline(v=-qnorm(0.975), col='red', lty=2)
  73.  
  74. result
  75. }
  76.  
  77.  
  78. ## 4. Meta-analysis with true effect
  79. ## Finally, here is Stouffer’s method when the alternative is true, and
  80. ## there is a difference. The magnitude of Stouffer’s statistic will depend
  81. ## on the number of studies (as well as the magnitude of effect).
  82. false_null_meta <- function(sample, sim_size) {
  83. z_scores <- numeric(sample)
  84. p_vals <- numeric(sample)
  85. stouffer <- numeric(sim_size)
  86. for (i in 1:sim_size) {
  87. x <- rnorm(sample, 100, 15)
  88. y <- rnorm(sample, 108, 15)
  89. p_vals[i] <- t.test(x,y, alternative = "greater")$p.value
  90. z_scores[i] <- c(qnorm(p_vals[i]))
  91. stouffer[i] <- sum( z_scores / sqrt(sample) )
  92. result_z <- quantile(z_scores, probs = quants)
  93. result_s <- quantile(stouffer, probs = quants)
  94. }
  95. hist(stouffer,
  96. freq = FALSE,
  97. main = expression(paste(H[alt], " is True")),
  98. xlab = "Stouffer z-scores",
  99. sub = paste("Number of Meta-Studies",sample))
  100. abline(v=qnorm(0.975), col='red', lty=2)
  101. abline(v=-qnorm(0.975), col='red', lty=2)
  102. list(result_z=result_z, result_s=result_s)
  103. }
  104.  
  105. N <- 1000
  106. enter <- function() invisible(readline(prompt="Press [enter] to continue"))
  107.  
  108. cat("Distribution of *p*-values with effect size=0\n")
  109. round(null_true_p(N), 3)
  110. enter()
  111.  
  112. cat("Distribution of *p*-values with sample size 15 and effect size=0.53\n")
  113. round(null_false_p(15, N), 3)
  114. enter()
  115.  
  116. cat("Distribution of meta analysis z-scores with 15 studies\n")
  117. round(true_null_meta(15, N), 3)
  118. enter()
  119.  
  120. cat("Distribution of meta analysis z-scores with 15 studies and effect size=0.543\n")
  121. false_null_meta(15, N)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement