Guest User

Untitled

a guest
Apr 26th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.91 KB | None | 0 0
  1. i = 1
  2. while(i == 1)
  3. {
  4.     dist<-menu(c("Binomial","Geometric","Hypergeometric","Poisson"), graphics = TRUE, title = "Which distribution?")
  5.  
  6.     if (dist == 1)
  7.         print("Binomial was selected")
  8.     else if(dist == 2)
  9.         print("Geometric was selected")
  10.     else if(dist == 3)
  11.         print("Hypergeometric was selected")
  12.     else if(dist == 4)
  13.         print("Poisson was selected")
  14.  
  15.    
  16.     type<-menu(c("PDF","CDF"), graphics = TRUE, title = "PDF or CDF")
  17.  
  18.     if (type == 1)
  19.         print("PDF was selected")
  20.     else if(type == 2)
  21.         print("CDF was selected")
  22.  
  23.     if(dist == 1)
  24.     {
  25.         if(type == 1)
  26.         {
  27.            
  28.             x <- winDialogString("Please enter the N'th term","")
  29.             x <- as.integer(x)
  30.  
  31.             x1 <- 0:x
  32.             x1 <- as.integer(x1)
  33.  
  34.             size <- winDialogString("Enter Sample Size","")
  35.             size <- as.integer(size)
  36.  
  37.             if (size <= 0)
  38.             {
  39.                 print("Error : Sample Size must be greater than 0")     #Validility check to make sure Size is > 0
  40.                 break
  41.             }
  42.  
  43.  
  44.             prob <- winDialogString("Enter the probability","")
  45.             prob <- as.double(prob)
  46.  
  47.             if (prob < 0 || prob > 1)
  48.             {
  49.                 print("Error : Probability must be between 0 and 1")        #Validility check to make sure Probability is between 0 and 1
  50.                 break
  51.             }
  52.                    
  53.             pdf<-dbinom(x1, size, prob)                
  54.  
  55.            
  56.             print(round(pdf, 2))                            #prints of the probability rounded to two decimal places
  57.             plot(x1, pdf,
  58.                 xlab = "X = Number of Trials",
  59.                 ylab = "P(X<=x)",
  60.                 type = "h",main = "Binomial Pdf")
  61.  
  62.         }
  63.  
  64.         if(type == 2)
  65.         {
  66.             x <- winDialogString("Enter the n'th term","")
  67.             x <- as.integer(x)
  68.  
  69.             x1 <- 0:x
  70.             x1 <- as.integer(x1)
  71.  
  72.             size <- winDialogString("Enter sample space size","")
  73.             size <- as.integer(size)
  74.  
  75.            
  76.             if (size <= 0)
  77.             {
  78.                 print("Error : Sample Size must be greater than 0")
  79.                 break
  80.             }
  81.  
  82.  
  83.             prob <- winDialogString("Enter the probability","")
  84.             prob <- as.double(prob)
  85.            
  86.             if (prob < 0 || prob > 1)
  87.             {
  88.                 print("Error : Probability must be between 0 and 1")
  89.                 break
  90.             }
  91.  
  92.                        
  93.             cdf<-pbinom(x1, size, prob)                
  94.            
  95.            
  96.             print(round(cdf, 2))
  97.             plot(x1, cdf,
  98.                 xlab = "X = Number of Trials",
  99.                 ylab = "P(X<=x)",
  100.                 type = "s",
  101.                 main = "Binomial Cdf")
  102.    
  103.         }
  104.     }
  105.  
  106.    
  107.     if(dist == 2)
  108.     {
  109.         if(type == 1)
  110.         {
  111.             size <- winDialogString("Enter sample space size","")
  112.             size <- as.integer(size)
  113.  
  114.            
  115.             if (size <= 0)
  116.             {
  117.                 print("Error : Sample Size must be greater than 0")
  118.                 break
  119.             }
  120.        
  121.             x <- 0:size
  122.             x <- as.integer(x)
  123.  
  124.             prob <- winDialogString("Enter the probability","")
  125.             prob <- as.double(prob)
  126.        
  127.             if (prob < 0 || prob > 1)
  128.             {
  129.                 print("Error : Probability must be between 0 and 1")
  130.                 break
  131.             }
  132.  
  133.             pdf <- dgeom(x, prob)
  134.  
  135.             print(round(pdf, 2))
  136.            
  137.             plot(x + 1, pdf,
  138.                 xlab = "X = Number of Trials",
  139.                 ylab = "P(X=x)",
  140.                 type = "h",
  141.                 main = "Geometric Pdf")
  142.         }
  143.  
  144.         if(type == 2)
  145.         {
  146.  
  147.             size <- winDialogString("Enter sample size","")
  148.             size <- as.integer(size)
  149.             if (size <= 0)
  150.             {
  151.                 print("Error : Sample Size must be greater than 0")
  152.                 break
  153.             }
  154.  
  155.             x <- 0:size
  156.             x <- as.integer(x)
  157.  
  158.             prob1 <- winDialogString("Enter the probability","")
  159.             prob1 <- as.double(y)
  160.             if (prob1 < 0 || prob1 > 1)
  161.             {
  162.                 print("Error : Probability must be between 0 and 1")
  163.                 break
  164.             }
  165.  
  166.             cdf<-pgeom(x, prob1)
  167.  
  168.             print(round(cdf, 2))
  169.  
  170.             plot(x+1, cdf,
  171.                 xlab = "X = Number of Trials",
  172.                 ylab = "P(X=x)",
  173.                 type = "s",
  174.                 main = "Geometric Cdf")
  175.    
  176.         }
  177.     }
  178.  
  179.    
  180.     if(dist == 3)
  181.     {
  182.         if(type == 1)
  183.         {
  184.             total <- winDialogString("Please enter the n'th term","")
  185.             total <- as.integer(total)
  186.                
  187.             y <- winDialogString("Enter number of successes","")
  188.             y <- as.integer(y)
  189. #          
  190.             size <- winDialogString("Enter sample size","")
  191.             size <- as.integer(size)
  192.  
  193.            
  194.             if (size <= 0)
  195.             {
  196.                 print("Error : Sample Size must be greater than 0")
  197.                 break
  198.             }
  199.  
  200.             x <- 0:size
  201.  
  202.             mid <- (total - y)
  203.  
  204.             pdf <- dhyper(x, y, mid, size)
  205.            
  206.            
  207.             print(round(pdf, 2))
  208.             plot(x, pdf,
  209.                 xlab = "Number of trials",
  210.                 ylab = "Probability",
  211.                 main = "Hypergoemetric pdf",
  212.                 type = "h")
  213.  
  214.            
  215.         }
  216.  
  217.         if(type == 2)
  218.         {
  219.             total <- winDialogString("Enter the n'th term","")
  220.             total <- as.integer(total)
  221.  
  222.  
  223.             y <- winDialogString("Enter number of successes","")
  224.             y <- as.integer(y)
  225.  
  226.             size <- winDialogString("Enter sample size","")
  227.             size <- as.integer(size)
  228.            
  229.             if (size <= 0)
  230.             {
  231.                 print("Error : Sample Size must be greater than 0")
  232.                 break
  233.             }
  234.  
  235.             x <- 0:size
  236.  
  237.             mid <- (total - y)
  238.  
  239.             cdf <- phyper(x, y, mid, size)
  240.            
  241.             print(round(cdf, 2))
  242.             plot(x, cdf,
  243.                 xlab = "Number of trials",
  244.                 ylab = "Probability",
  245.                 main = "Hypergoemetric cdf",
  246.                 type = "s")
  247.    
  248.         }
  249.     }
  250.    
  251.     if(dist == 4 )
  252.     {
  253.         if(type == 1)
  254.         {
  255.            
  256.            
  257.  
  258.         }
  259.  
  260.         if(type == 2)
  261.         {
  262.    
  263.    
  264.         }
  265.     }
  266.  
  267.  
  268.  
  269.     i <-menu(c("Yes","No"), graphics = TRUE, title = "Would you like another Distribution?")
  270. }
Add Comment
Please, Sign In to add comment