R(24), Distribution

Published: by Creative Commons Licence


Normal Distribution

There are four built-in functions to produce a normal distribution in R.

dnorm(x, mean, sd)
pnorm(x, mean, sd)
qnorm(p, mean, sd)
rnorm(n, mean, sd)
  • x: a vector of numbers
  • p: a vector of properties
  • n: the number of observation(the size of samples)
  • mean: the average of sample data, and its default value is zero
  • sd: standard deviation, and its default value is 1.

dorm(): The function gives the height of the probability distribution of the given mean and standard deviation at each point.

# Create a sequence of numbers between -10 and 10 incrementing by 0.1.
x <- seq(-10, 10, by = .1)
# Choose the mean as 2.5 and standard deviation as 0.5.
y <- dnorm(x, mean = 2.5, sd = 0.5)
# draw the chart
png(file = "dnorm.png")
plot(x,y)
dev.off()

pnorm(): This function gives the probability of normal distribution random number less than the given number. It is also known as the cumulative distribution function.

# Create a sequence of numbers between -10 and 10 incrementing by 0.2.
x <- seq(-10,10,by = .2)
# Choose the mean as 2.5 and standard deviation as 2. 
y <- pnorm(x, mean = 2.5, sd = 2)

qnorm(): The function uses the probability value and gives the number of the cumulative value matching the probability value.

# Create a sequence of probability values incrementing by 0.02.
x <- seq(0, 1, by = 0.02)
# Choose the mean as 2 and standard deviation as 3.
y <- qnorm(x, mean = 2, sd = 1)

rnorm(): This function is used to generate random numbers with normal distribution. It takes sample size as input and generates many random numbers. We draw a histogram to show the distribution of the generated numbers.

# Create a sample of 50 numbers which are normally distributed.
y <- rnorm(50)

Binary Distribution

There are four built-in functions to produce binary distribution in R.

dbinom(x, size, prob)
pbinom(x, size, prob)
qbinom(p, size, prob)
rbinom(n, size, prob)
  • x: a vector of numbers
  • p: a vector of properties
  • n: size of observation
  • size: number of tests
  • prob: probability of success for each test

dbinom(): gives the probability density distribution of 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)

pbinom(): This function gives the cumulative probability of the event. It's 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)

qbinom(): uses the probability value and gives the number of the cumulative value matching 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)

rbinom(): This function generates the required number of random values of 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)