Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //week-1//
- my_function <- function() {
- print("Hello World!")
- }
- my_function()
- new_function <- function(fname){
- paste(fname, "Herondale")
- }
- new_function("will")
- new_function("james")
- new_function("jace")
- ab <- function(f,l){
- paste(f,l)
- }
- ab("c", "C")
- cd <- function(country="Swedan"){
- paste("I am from", country)
- }
- cd()
- cd("India")
- //week-2//
- #binomial
- x <- seq(0,50,by = 1)
- y <- dbinom(x,50,0.5)
- png(file = "dbinom.png")
- plot(x,y)
- dev.off()
- pbinom(3, size = 13, prob = 1 / 4)
- plot(0:10, pbinom(0:10, size = 10, prob = 1 / 4), type = "l")
- #geometric distributionn
- x_dgeom <- seq(2, 10, by = 1)
- y_dgeom <- dgeom(x_dgeom, prob = 0.5)
- plot(y_dgeom)
- #poisson
- set.seed(123)
- poisson <- rpois(1000, lambda=3)
- plot(poisson, main="A histogram of a Poisson distribution")
- #Uniform distribution
- rand.unif <- runif(10000, min = -2, max = 0.8)
- curve(rand.unif, freq = FALSE, xlab = 'x', density = 20)
- #log normal distribution
- curve(dlnorm(x, meanlog=0, sdlog=1), from=0, to=25)
- //week-3//
- # R program to plot gamma distribution
- # Specify x-values for gamma function
- x_pgamma <- seq(0, 2, by = 0.04)
- # Apply pgamma function
- y_pgamma <- pgamma(x_pgamma, shape = 6)
- # Plot pgamma values
- plot(y_pgamma)
- //Weibull distribution://
- curve(dweibull(x, shape=2, scale = 1), from=0, to=4,
- main = 'Weibull Distribution (shape = 2, scale = 1)', #add title
- ylab = 'Density', #change y-axis label
- lwd = 2, #increase line width to 2
- col = 'steelblue') #change line color to steelblue
- Beta Distribution:
- Program:
- Plot One Beta Distribution
- #define range
- p = seq(0,1, length=100)
- #create plot of Beta distribution with shape parameters 2 and 10
- plot(p, dbeta(p, 2, 10), type='l')
- #create custom plot of Beta distribution
- plot(p, dbeta(p, 2, 10), ylab='density',
- type ='l', col='purple', main='Beta Distribution')
- output:
- Plot Multiple Beta Distributions
- Program:
- #define range
- p = seq(0,1, length=100)
- #plot several Beta distributions
- plot(p, dbeta(p, 2, 10), ylab='density', type ='l', col='purple')
- lines(p, dbeta(p, 2, 2), col='red')
- lines(p, dbeta(p, 5, 2), col='blue')
- #add legend
- legend(.7, 4, c('Beta(2, 10)','Beta(2, 2)','Beta(1,1)'),
- lty=c(1,1,1),col=c('purple', 'red', 'blue'))
- Triangular Distribution
- Description:
- To calculate probabilities for the triangular distribution in R we can use the ptri()
- function from the EnvStats package, which uses the following syntax:
- ptri(q, min = 0, max = 1, mode = 1/2)
- where:
- • q: Quantile of interest
- • min: The minimum value of the distribution
- • max: The maximum value of the distribution
- • mode: The peak value of the distribution
- Example:
- 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?
- Program:
- library(EnvStats)
- #calculate probability
- ptri(q = 20000, min = 10000, max = 30000, mode = 25000)
- output:
- [1] 0.3333333
- Calculating Probability Greater Than Some Value
- 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?
- Program:
- library(EnvStats)
- #calculate probability
- 1 - ptri(q = 1500, min = 500, max = 2000, mode = 1200)
- Output:
- [1] 0.2083333
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement