Advertisement
pastebin-0616

Untitled

Aug 22nd, 2022 (edited)
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.59 KB | Source Code | 0 0
  1. //week-1//
  2. my_function <- function() {
  3.   print("Hello World!")
  4. }
  5. my_function()
  6.  
  7. new_function <- function(fname){
  8.   paste(fname, "Herondale")
  9.  
  10. }
  11.  
  12. new_function("will")
  13. new_function("james")
  14. new_function("jace")
  15.  
  16. ab <- function(f,l){
  17.   paste(f,l)
  18. }
  19. ab("c", "C")
  20.  
  21. cd <- function(country="Swedan"){
  22.   paste("I am from", country)
  23. }
  24. cd()
  25. cd("India")
  26.  
  27. //week-2//
  28.  
  29. #binomial
  30. x <- seq(0,50,by = 1)
  31. y <- dbinom(x,50,0.5)
  32. png(file = "dbinom.png")
  33. plot(x,y)
  34. dev.off()
  35.  
  36. pbinom(3, size = 13, prob = 1 / 4)
  37. plot(0:10, pbinom(0:10, size = 10, prob = 1 / 4), type = "l")
  38.  
  39. #geometric distributionn
  40. x_dgeom <- seq(2, 10, by = 1)    
  41. y_dgeom <- dgeom(x_dgeom, prob = 0.5)    
  42. plot(y_dgeom)  
  43.  
  44.  
  45. #poisson
  46. set.seed(123)
  47. poisson <- rpois(1000, lambda=3)
  48. plot(poisson, main="A histogram of a Poisson distribution")
  49.  
  50. #Uniform distribution
  51.  
  52. rand.unif <- runif(10000, min = -2, max = 0.8)
  53. curve(rand.unif, freq = FALSE, xlab = 'x', density = 20)
  54.  
  55.  
  56. #log normal distribution
  57. curve(dlnorm(x, meanlog=0, sdlog=1), from=0, to=25)
  58. //week-3//
  59.  
  60. # R program to plot gamma distribution
  61. # Specify x-values for gamma function
  62. x_pgamma <- seq(0, 2, by = 0.04)
  63. # Apply pgamma function
  64. y_pgamma <- pgamma(x_pgamma, shape = 6)
  65. # Plot pgamma values
  66. plot(y_pgamma)
  67.  
  68. //Weibull distribution://  
  69.   curve(dweibull(x, shape=2, scale = 1), from=0, to=4,
  70.     main = 'Weibull Distribution (shape = 2, scale = 1)', #add title
  71.     ylab = 'Density', #change y-axis label
  72.     lwd = 2, #increase line width to 2
  73.     col = 'steelblue') #change line color to steelblue
  74. Beta Distribution:
  75.  
  76. Program:
  77. Plot One Beta Distribution
  78.  
  79.  
  80. #define range
  81. p = seq(0,1, length=100)
  82.  
  83. #create plot of Beta distribution with shape parameters 2 and 10
  84. plot(p, dbeta(p, 2, 10), type='l')
  85.  
  86.  
  87. #create custom plot of Beta distribution
  88. plot(p, dbeta(p, 2, 10), ylab='density',
  89.      type ='l', col='purple', main='Beta Distribution')
  90.  
  91.  
  92.  
  93. output:
  94.  
  95.  
  96.  
  97.  
  98. Plot Multiple Beta Distributions
  99.  
  100. Program:
  101.  
  102. #define range
  103. p = seq(0,1, length=100)
  104.  
  105. #plot several Beta distributions
  106. plot(p, dbeta(p, 2, 10), ylab='density', type ='l', col='purple')
  107. lines(p, dbeta(p, 2, 2), col='red')
  108. lines(p, dbeta(p, 5, 2), col='blue')
  109.  
  110. #add legend
  111. legend(.7, 4, c('Beta(2, 10)','Beta(2, 2)','Beta(1,1)'),
  112.        lty=c(1,1,1),col=c('purple', 'red', 'blue'))
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. Triangular Distribution
  122.  
  123. Description:
  124. To calculate probabilities for the triangular distribution in R we can use the ptri()
  125. function from the EnvStats package, which uses the following syntax:
  126. ptri(q, min = 0, max = 1, mode = 1/2)
  127. where:
  128. q: Quantile of interest
  129. min: The minimum value of the distribution
  130. max: The maximum value of the distribution
  131. mode: The peak value of the distribution
  132.  
  133. Example:
  134. Suppose a restaurant estimates that their total sales for the upcoming week will be a minimum of $10,000, a maximum of $30,000, and most likely $25,000. What is the probability that the restaurant makes less than $20,000 total sales?
  135. Program:
  136.  
  137. library(EnvStats)
  138.  
  139. #calculate probability
  140. ptri(q = 20000, min = 10000, max = 30000, mode = 25000)
  141.  
  142. output:
  143.  
  144. [1] 0.3333333
  145.  
  146.  
  147.  
  148. Calculating Probability Greater Than Some Value
  149.  
  150. Suppose a shop estimates that the number of customers that will enter in a given week will be a minimum of 500, a maximum of 2,000, and most likely 1,200. What is the probability that more than 1,500 customers enter the shop in a given week?
  151.  
  152. Program:
  153.  
  154. library(EnvStats)
  155.  
  156. #calculate probability
  157. 1 - ptri(q = 1500, min = 500, max = 2000, mode = 1200)
  158.  
  159.  
  160. Output:
  161. [1] 0.2083333
  162.  
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement