Guest User

Untitled

a guest
Oct 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. # Listing B.20 Exact binomial sample size calculation
  2. errorProb <- function(targetRate,difference,size) {
  3. pbinom(
  4. ceiling(( targetRate - difference) * size),
  5. size = size, prob = targetRate)
  6. }
  7.  
  8. print(errorProb(0.045 , 0.004, est)) ## [1] 0.04153646
  9.  
  10. binSearchNonPositive <- function(fEventuallyNegative) {
  11. low <- 1
  12. high <- low + 1
  13. while(fEventuallyNegative(high) > 0) {
  14. high <- 2 * high
  15. }
  16. while(high > low + 1) {
  17. m <- low + (high-low) %/% 2
  18. if(fEventuallyNegative(m) > 0) {
  19. low <- m
  20. } else {
  21. high <-m
  22. }
  23. }
  24. high
  25. }
  26.  
  27. actualSize <- function(targetRate, difference, errorProb) {
  28. binSearchNonPositive(function(n) {
  29. errorProb(targetRate, difference, n) - errorProb
  30. })
  31. }
  32.  
  33. size <- actualSize(0.045, 0.004, 0.05)
  34.  
  35. print(size) ## [1] 7623 print(errorProb(0.045,0.004,size)) ## [1] 0.04983659
Add Comment
Please, Sign In to add comment