May 12, 2021 R language tutorial
The two distribution models deal with the probability of success of events that find only two possible outcomes in a series of experiments. F or example, coin tosses always give a head or tail. T he probability of exactly finding three heads in 10 repeated coin tosses is estimated during the distribution period.
The R language has four built-in functions to generate a two-item distribution. T hey are described below.
dbinom(x, size, prob) pbinom(x, size, prob) qbinom(p, size, prob) rbinom(n, size, prob)
The following is a description of the parameters used -
x is the vector of the number.
p is the probability vector.
n is the number of observations.
size is the number of experiments.
Prob is the probability of success for each experiment.
The function gives the probability density distribution for each point.
# Create a sample of 50 numbers which are incremented by 1. x <- seq(0,50,by = 1) # Create the binomial distribution. y <- dbinom(x,50,0.5) # Give the chart file a name. png(file = "dbinom.png") # Plot the graph for this sample. plot(x,y) # Save the file. dev.off()
When we execute the code above, it produces the following results -
This function gives the cumulative probability of an event. I t is a single value that represents probability.
# Probability of getting 26 or less heads from a 51 tosses of a coin. x <- pbinom(26,51,0.5) print(x)
When we execute the code above, it produces the following results -
[1] 0.610116
The function takes the probability value and gives the number that the cumulative value matches the probability value.
# How many heads will have a probability of 0.25 will come out when a coin is tossed 51 times. x <- qbinom(0.25,51,1/2) print(x)
When we execute the code above, it produces the following results -
[1] 23
The function produces the required number of random values for a given probability from a given sample.
# Find 8 random values from a sample of 150 with probability of 0.4. x <- rbinom(8,150,.4) print(x)
When we execute the code above, it produces the following results -
[1] 58 61 59 66 55 60 61 67