Guest User

Untitled

a guest
Apr 26th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. plot_g_cdf <- function(x, p)
  2. {
  3. plot(x+1, pgeom(x, prob = p), xlab = "X", ylab = "Y", type = "s", main = "Geometric CDF")
  4. }
  5.  
  6. plot_g_pdf <- function(x, p)
  7. {
  8. plot(x+1, dgeom(x, prob = p), xlab = "X", ylab = "Y", type = "h", main = "Geometric PDF")
  9. }
  10.  
  11.  
  12. geometric <- function()
  13. {
  14. x <- winDialogString("Enter start of range: ", " ")
  15. y <- winDialogString("Enter end of range: ", " ")
  16. x <- as.numeric(x)
  17. y <- as.numeric(y)
  18. range <- seq(x,y)
  19.  
  20. x <- winDialogString("Enter probability: ", " ")
  21. probability <- as.numeric(x)
  22.  
  23. x <- menu(c("CDF", "PDF"), graphics = TRUE, title = "CDF or PDF?")
  24.  
  25. if(x==1){
  26. print(round(pgeom(range,probability), 2))
  27. plot_g_cdf(range,probability)
  28. }
  29. else if(x==2){
  30. print(round(dgeom(range,probability), 2))
  31. plot_g_pdf(range,probability)
  32. }
  33. }
  34.  
  35. plot_p_cdf <- function(x, l)
  36. {
  37. plot (x, ppois(x, l), xlab = "X", ylab = "Y",type = "s", main= "Poisson CDF")
  38. }
  39.  
  40. plot_p_pdf <- function(x, l)
  41. {
  42. plot (x, dpois(x, l), xlab = "X", ylab = "Y",type = "h", main= "Poisson PDF")
  43. }
  44.  
  45.  
  46. poisson <- function()
  47. {
  48. x <- winDialogString("Enter start of range: ", " ")
  49. y <- winDialogString("Enter end of range: ", " ")
  50. x <- as.numeric(x)
  51. y <- as.numeric(y)
  52. range <- seq(x,y)
  53.  
  54. x <- winDialogString("Enter lambda: ", " ")
  55. lambda <- as.numeric(x)
  56.  
  57. x <- menu(c("CDF", "PDF"), graphics = TRUE, title = "CDF or PDF?")
  58.  
  59. if(x==1){
  60. print(round(ppois(range,lambda), 2))
  61. plot_p_cdf(range,lambda)
  62. }
  63. else if(x==2){
  64. print(round(dpois(range,lambda), 2))
  65. plot_p_pdf(range,lambda)
  66. }
  67. }
  68.  
  69. plot_b_cdf <- function(x, s, p)
  70. {
  71. plot(x, pbinom(x, size = s, prob = p), xlab = "X", ylab = "Y", ylim = c(0, 1), type = "s", main = "Binomial CDF")
  72. }
  73.  
  74. plot_b_pdf <- function(x, s, p)
  75. {
  76. plot(x, dbinom(x, size = s, prob = p), xlab = "X", ylab = "Y", type = "h", main = "Binomial CDF")
  77. }
  78.  
  79.  
  80. binomial <- function()
  81. {
  82. x <- winDialogString("Enter start of range: ", " ")
  83. y <- winDialogString("Enter end of range: ", " ")
  84. x <- as.numeric(x)
  85. y <- as.numeric(y)
  86. range <- seq(x,y)
  87.  
  88. x <- winDialogString("Enter size: ", " ")
  89. size <- as.numeric(x)
  90.  
  91. x <- winDialogString("Enter probability: ", " ")
  92. probability <- as.numeric(x)
  93.  
  94. x <- menu(c("CDF", "PDF"), graphics = TRUE, title = "CDF or PDF?")
  95.  
  96. if(x==1){
  97. print(round(pbinom(range,size,probability), 2))
  98. plot_b_cdf(range, size, probability)
  99. }
  100. else if(x==2){
  101. print(round(dbinom(range,size,probability), 2))
  102. plot_b_pdf(range, size, probability)
  103. }
  104. }
  105.  
  106. plot_hg_cdf <- function(x, m, l, n)
  107. {
  108. plot(x, phyper(x, m, l, n), xlab = "X", type = "s", ylab = "Y", main = "HyperGeometric CDF")
  109. }
  110.  
  111. plot_hg_pdf <- function(x, m, l, n)
  112. {
  113. plot(x, dhyper(x, m, l, n), xlab = "X", type = "h", ylab = "Y", main = "HyperGeometric PDF")
  114. }
  115.  
  116.  
  117. hypergeometric <- function()
  118. {
  119. x <- winDialogString("Enter start of range: ", " ")
  120. y <- winDialogString("Enter end of range: ", " ")
  121. x <- as.numeric(x)
  122. y <- as.numeric(y)
  123. range <- seq(x,y)
  124.  
  125. x <- winDialogString("Enter success possible: ", " ")
  126. success <- as.numeric(x)
  127.  
  128. x <- winDialogString("Enter failures possible: ", " ")
  129. failure <- as.numeric(x)
  130.  
  131. x <- winDialogString("Enter amount: ", " ")
  132. amount <- as.numeric(x)
  133.  
  134. x <- menu(c("CDF", "PDF"), graphics = TRUE, title = "CDF or PDF?")
  135.  
  136. if(x==1){
  137. plot_hg_cdf(range,success,failure,amount)
  138. print(round(phyper(range,success,failure,amount), 2))
  139. }
  140. else if(x==2){
  141. print(round(dhyper(range,success,failure,amount), 2))
  142. plot_hg_pdf(range,success,failure,amount)
  143. }
  144. }
  145.  
  146. run <- function()
  147. {
  148. answer <- 1
  149. while(answer==1)
  150. {
  151. selection <- menu(c("Hypgeometric", "Binomial", "Geometric", "Poisson"), graphics = TRUE, title = "Which distribution?")
  152.  
  153. if(selection==1)
  154. {
  155. hypergeometric()
  156. }
  157. else if(selection==2)
  158. {
  159. binomial()
  160. }
  161. else if(selection==3)
  162. {
  163. geometric()
  164. }
  165. else if(selection==4)
  166. {
  167. poisson()
  168. }
  169.  
  170. answer <- menu(c("Yes", "No"), graphics = TRUE, title = "Would you like to plot again?")
  171. }
  172.  
  173. }
Add Comment
Please, Sign In to add comment