Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. library(magrittr)
  2.  
  3.  
  4. # Function Declaration ----------------------------------------------------
  5.  
  6. type_1_errors <- function(n_coeff, alpha, n_samples = 100000) {
  7. # Resampling approach to Type I Error estimation
  8. #
  9. # Args:
  10. # n_coeff (int): Number of coefficients in model.
  11. # alpha (dbl): Confidence level to accept alternative hypothesis.
  12. # n_samples (int): Number of iterations to build sampling distribution.
  13. # Defaults to 100,000.
  14.  
  15. p <- 1- alpha
  16. samples <- rbinom(n_samples, n_coeff, p)
  17. outcome_probs <-samples %>%
  18. table() %>%
  19. prop.table()
  20.  
  21. expected_value <- sum(as.double(names(outcome_probs)) * outcome_probs) # weighted average of outcomes
  22.  
  23. list(
  24. n_coeff = n_coeff,
  25. alpha = alpha,
  26. outcomes = outcome_probs,
  27. percent_outcome = outcome_probs * 100,
  28. expected_errors = expected_value,
  29. raw = samples
  30. )
  31. }
  32.  
  33.  
  34. # Example -----------------------------------------------------------------
  35.  
  36. n_coeff <- 63
  37. alpha <- 0.95
  38. error <- type_1_errors(n_coeff, alpha)
  39.  
  40. print(paste0('model with ', n_coeff, ' coefficients ...'))
  41. print(paste0('expected type I errors with alpha=', error$alpha, ': ', error$expected_errors))
  42. print('error distribution (%):')
  43. print(error$percent_outcome)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement