Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. cutoff = .001
  2. p<-.0341
  3.  
  4. #adopt a jeffreys prior
  5. alpha<-.5
  6. beta<-.5
  7. flag = 0
  8. N_vec = rep(0,100)
  9. for (j in 1:100){
  10. N = 100000
  11. while (flag == 0){
  12. y=rbinom(1,N,p)
  13. lci=qbeta(0.05,shape1=y+alpha,shape2=N-y+beta)
  14. uci=qbeta(0.95,shape1=y+alpha,shape2=N-y+beta)
  15. if (uci - lci < cutoff) {
  16. flag = 1
  17. N_vec[j] = N
  18. }
  19. N = N + 1000
  20. }
  21. flag = 0
  22. }
  23. hist(N_vec)
  24.  
  25. uci=1
  26. lci=0
  27. flag = 0
  28. N_vec2 = rep(0,100)
  29. for (j in 1:100){
  30. N = 350000
  31. while (flag == 0){
  32. y=rbinom(1,N,p)
  33. lci=qbeta(0.025,shape1=y+alpha,shape2=N-y+beta)
  34. uci=qbeta(0.975,shape1=y+alpha,shape2=N-y+beta)
  35. if (uci - lci < cutoff) {
  36. flag = 1
  37. N_vec2[j] = N
  38. }
  39. N = N + 1000
  40. }
  41. flag = 0
  42. }
  43. hist(N_vec2)
  44.  
  45. uci=1
  46. lci=0
  47. flag = 0
  48. N_vec3 = rep(0,100)
  49. for (j in 1:100){
  50. N = 500000
  51. while (flag == 0){
  52. y=rbinom(1,N,p)
  53. lci=qbeta(0.005,shape1=y+alpha,shape2=N-y+beta)
  54. uci=qbeta(0.995,shape1=y+alpha,shape2=N-y+beta)
  55. if (uci - lci < cutoff) {
  56. flag = 1
  57. N_vec3[j] = N
  58. }
  59. N = N + 1000
  60. }
  61. flag = 0
  62. }
  63. hist(N_vec3)
  64.  
  65. binomial.conf.interval=function (y,conf){
  66. z1=abs(qnorm((1 - conf)/2))
  67. z2=abs(qnorm((1 + conf)/2))
  68. nn=length(y)
  69. phat=sum(y)/nn
  70. se=sqrt(phat∗(1−phat)/nn)
  71. return (c(phat−z1∗se, phat+z2∗se))
  72. }
  73.  
  74. flag = 0
  75. N_vec_clt = rep(0,100)
  76. for (j in 1:100){
  77. N = 100000
  78. while (flag == 0){
  79. y=rbinom(N,1,p)
  80. I = binomial.conf.interval(y,.9)
  81. if (I[2] - I[1] < cutoff) {
  82. flag = 1
  83. N_vec_clt[j] = N
  84. }
  85. N = N + 1000
  86. }
  87. flag = 0
  88. }
  89. hist(N_vec)
  90.  
  91. sample_est = c(median(N_vec), median(N_vec2),median(N_vec3))
  92. sample_est
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement