Probability Distributions in R Programming


Introduction

Probability distributions describe how the values of a random variable are distributed. In R, functions for common distributions such as normal, binomial, and Poisson are built-in. This tutorial provides examples for these distributions.

1. Normal Distribution

The normal distribution is symmetric and describes data that clusters around the mean.

Functions:

  • dnorm(x, mean, sd): Probability density function (PDF)
  • pnorm(q, mean, sd): Cumulative distribution function (CDF)
  • qnorm(p, mean, sd): Quantile function
  • rnorm(n, mean, sd): Random number generation

Example:

    # Generate 10 random numbers from a normal distribution
    random_numbers <- rnorm(10, mean = 0, sd = 1)
    print(random_numbers)
    
    # Calculate PDF for x = 1
    density <- dnorm(1, mean = 0, sd = 1)
    print(density)
    
    # Calculate CDF for x = 1
    cumulative <- pnorm(1, mean = 0, sd = 1)
    print(cumulative)
    
    # Calculate the 95th percentile
    quantile <- qnorm(0.95, mean = 0, sd = 1)
    print(quantile)
        

2. Binomial Distribution

The binomial distribution describes the number of successes in a fixed number of independent Bernoulli trials.

Functions:

  • dbinom(x, size, prob): Probability mass function (PMF)
  • pbinom(q, size, prob): Cumulative distribution function (CDF)
  • qbinom(p, size, prob): Quantile function
  • rbinom(n, size, prob): Random number generation

Example:

    # Generate 10 random numbers from a binomial distribution
    random_numbers <- rbinom(10, size = 10, prob = 0.5)
    print(random_numbers)
    
    # Calculate PMF for x = 5
    probability <- dbinom(5, size = 10, prob = 0.5)
    print(probability)
    
    # Calculate CDF for x = 5
    cumulative <- pbinom(5, size = 10, prob = 0.5)
    print(cumulative)
    
    # Calculate the 90th percentile
    quantile <- qbinom(0.9, size = 10, prob = 0.5)
    print(quantile)
        

3. Poisson Distribution

The Poisson distribution describes the number of events occurring in a fixed interval of time or space.

Functions:

  • dpois(x, lambda): Probability mass function (PMF)
  • ppois(q, lambda): Cumulative distribution function (CDF)
  • qpois(p, lambda): Quantile function
  • rpois(n, lambda): Random number generation

Example:

    # Generate 10 random numbers from a Poisson distribution
    random_numbers <- rpois(10, lambda = 3)
    print(random_numbers)
    
    # Calculate PMF for x = 2
    probability <- dpois(2, lambda = 3)
    print(probability)
    
    # Calculate CDF for x = 2
    cumulative <- ppois(2, lambda = 3)
    print(cumulative)
    
    # Calculate the 75th percentile
    quantile <- qpois(0.75, lambda = 3)
    print(quantile)
        

Conclusion

R provides powerful functions to work with probability distributions. Understanding these functions allows you to analyze and simulate data efficiently.





Advertisement