Guest User

Untitled

a guest
Feb 21st, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #This first section of the code allows you to enter various parameters for the
  2. #distribution and see what it looks like.
  3.  
  4. #Enter the values for the distribution
  5. ml <- 4.5 #Most likely value (Make sure it's between min.1 and max.1)
  6. conf <- 10 #Confidence
  7. min.1 <- 3 #Minimum
  8. max.1 <- 5 #Maximum
  9.  
  10. #Rescale and calculate the parameters for the beta distribution
  11. mlRescale <- (ml - min.1) / (max.1 - min.1);
  12. confRescale <- conf + 3
  13.  
  14. a <- mlRescale * (confRescale - 3) + 1
  15. b <- confRescale - a - 1
  16.  
  17. #Plot the result
  18. theta <- seq(.01, .99, len=400)
  19. plot(min.1 + (max.1 - min.1) * theta, dbeta(theta, a, b), type = 'l',
  20. ylim = c(0, max(dbeta(theta, a, b))),
  21. xlab = "Control Strength", ylab = "Probability Density")
  22. segments(c(min.1, max.1), c(0, 0), c(min.1 + .01, min.1 + (max.1 - min.1) * .99),
  23. c(dbeta(.01, a, b), dbeta(.99, a, b)))
  24.  
  25. ################################################################################
  26. #Generate pictures of many different control strength distributions
  27.  
  28. genCSpic <- function(ml.in, conf.in, min.in, max.in, printTF = T) {
  29.  
  30. mlRescale <- (ml.in - min.in) / (max.in - min.in);
  31. confRescale <- conf.in + 3
  32.  
  33. a <- mlRescale * (confRescale - 3) + 1
  34. b <- confRescale - a - 1
  35.  
  36. #Plot the result
  37. theta <- seq(.01, .99, len=400)
  38. if (printTF)
  39. jpeg(paste("Figures", as.character(conf.in), "-m", as.character(ml.in), ".jpg", sep = ""))
  40. plot(min.in + (max.in - min.in) * theta, dbeta(theta, a, b) / (max.in - min.in), type = 'l',
  41. ylim = c(0, max(dbeta(theta, a, b) / (max.in - min.in))),
  42. xlab = "Control Strength", ylab = "Probability Density")
  43. segments(c(min.in, max.in), c(0, 0),
  44. c(min.in + (max.in - min.in) * .01, min.in + (max.in - min.in) * .99),
  45. c(dbeta(.01, a, b) / (max.in - min.in), dbeta(.99, a, b) / (max.in - min.in)))
  46. if (printTF)
  47. dev.off()
  48.  
  49. }
  50.  
  51. for (conf in c(0, 5, 10, 25, 50, 80)) {
  52. for (ml in c(45, 50, 55)) {
  53. genCSpic(ml, conf, 40, 60)
  54. }
  55. }
  56.  
  57. #Look at a few pictures on the screen.
  58. par(mfrow = c(2, 2))
  59. for (conf in c(2, 5, 20, 50)) {
  60. genCSpic(50, conf, 40, 60, F)
  61. }
  62.  
  63. ################################################################################
  64. #Find the percentage of the distribution that falls in the middle 20%
  65.  
  66. ml <- .5
  67. min.1 <- 0
  68. max.1 <- 1
  69.  
  70. count <- 1
  71. pctstore <- rep(0, 8) #This holds the answer.
  72. for (conf in c(0, 2, 5, 10, 20, 50, 80, 100)) {
  73. #Rescale and calculate the parameters for the beta distribution
  74. mlRescale <- (ml - min.1) / (max.1 - min.1);
  75. confRescale <- conf + 3
  76.  
  77. a <- mlRescale * (confRescale - 3) + 1
  78. b <- confRescale - a - 1
  79.  
  80. pctstore[count] <- pbeta(.6, a, b) - pbeta(.4, a, b)
  81. count <- count + 1
  82. }
Add Comment
Please, Sign In to add comment