Functions for Statistical Distributions in R

Functions for Statistical Distributions in R

R provides a comprehensive set of functions for working with statistical distributions. These functions allow you to compute densities, probabilities, quantiles, and generate random numbers from various distributions.

Normal Distributions

dnorm() Function

Returns the density of the normal distribution for a given value.

Example: 

# Density of the normal distribution at x = 0
density <- dnorm(0)
print(density)
# [1] 0.3989423

 pnorm() Function

Returns the cumulative distribution function (CDF) value for a given point.

Example: 

# CDF value for x = 1
cdf <- pnorm(1)
print(cdf)
# [1] 0.8413447

 qnorm() Function

Returns the quantile corresponding to a given probability.

Example: 

# Quantile for a probability of 0.95
quantile <- qnorm(0.95)
print(quantile)
# [1] 1.644854

 rnorm() Function

Generates random values from a normal distribution.

Example: 

# Generate 5 random values with mean = 0 and standard deviation = 1
random_values <- rnorm(5)
print(random_values)
# [1] -0.9785523  1.0217856  0.0638721 -0.2428278 -0.5025177

Binomial and Bernoulli Distributions

dbinom() Function

Returns the probability of a specific number of successes in a binomial distribution.

Example: 

# Probability of exactly 3 successes in 10 trials with a success probability of 0.5
prob <- dbinom(3, size = 10, prob = 0.5)
print(prob)
# [1] 0.1171875

 pbinom() Function

Returns the cumulative distribution function (CDF) value for a binomial distribution.

Example: 

# CDF value for up to 3 successes in 10 trials with a success probability of 0.5
cdf <- pbinom(3, size = 10, prob = 0.5)
print(cdf)
# [1] 0.2167969

qbinom() Function

Returns the quantile corresponding to a given probability in a binomial distribution.

Example: 

# Quantile for a probability of 0.95 in a binomial distribution with 10 trials and success probability 0.5
quantile <- qbinom(0.95, size = 10, prob = 0.5)
print(quantile)
# [1] 8

 rbinom() Function

Generates random values from a binomial distribution.

Example: 

# Generate 5 random values from a binomial distribution with 10 trials and a success probability of 0.5
random_values <- rbinom(5, size = 10, prob = 0.5)
print(random_values)
# [1] 7 5 6 4 6

Poisson Distributions

dpois() Function

Returns the probability of a specific number of events in a Poisson distribution.

Example: 

# Probability of exactly 3 events with a mean rate of 2
prob <- dpois(3, lambda = 2)
print(prob)
# [1] 0.180447

 ppois() Function

Returns the cumulative distribution function (CDF) value for a Poisson distribution.

Example: 

# CDF value for up to 3 events with a mean rate of 2
cdf <- ppois(3, lambda = 2)
print(cdf)
# [1] 0.815263

 qpois() Function

Returns the quantile corresponding to a given probability in a Poisson distribution.

Example: 

# Quantile for a probability of 0.95 with a mean rate of 2
quantile <- qpois(0.95, lambda = 2)
print(quantile)
# [1] 4

 rpois() Function

Generates random values from a Poisson distribution.

Example: 

# Generate 5 random values with a mean rate of 2
random_values <- rpois(5, lambda = 2)
print(random_values)
# [1] 1 2 4 1 3

Exponential Distributions

dexp() Function

Returns the density of the exponential distribution for a given value.

Example: 

# Density of the exponential distribution at x = 1 with rate = 1
density <- dexp(1, rate = 1)
print(density)
# [1] 0.3678794

 pexp() Function

Returns the cumulative distribution function (CDF) value for an exponential distribution.

Example: 

# CDF value for x = 1 with rate = 1
cdf <- pexp(1, rate = 1)
print(cdf)
# [1] 0.6321206

 qexp() Function

Returns the quantile corresponding to a given probability in an exponential distribution.

Example: 

# Quantile for a probability of 0.95 with rate = 1
quantile <- qexp(0.95, rate = 1)
print(quantile)
# [1] 2.995732

 rexp() Function

Generates random values from an exponential distribution.

Example: 

# Generate 5 random values with rate = 1
random_values <- rexp(5, rate = 1)
print(random_values)
# [1] 0.4161032 0.2125973 0.5747574 1.2425372 0.3794684

Chi-Square Distributions

dchisq() Function

Returns the density of the chi-square distribution for a given value.

Example: 

# Density of the chi-square distribution at x = 5 with 2 degrees of freedom
density <- dchisq(5, df = 2)
print(density)
# [1] 0.2650259

 pchisq() Function

Returns the cumulative distribution function (CDF) value for a chi-square distribution.

Example: 

# CDF value for x = 5 with 2 degrees of freedom
cdf <- pchisq(5, df = 2)
print(cdf)
# [1] 0.878813

 qchisq() Function

Returns the quantile corresponding to a given probability in a chi-square distribution.

Example: 

# Quantile for a probability of 0.95 with 2 degrees of freedom
quantile <- qchisq(0.95, df = 2)
print(quantile)
# [1] 7.378699

rchisq() Function

Generates random values from a chi-square distribution.

Exemple : 

# Generate 5 random values with 2 degrees of freedom
random_values <- rchisq(5, df = 2)
print(random_values)
# [1] 1.326664 3.046862 1.685939 3.128086 2.093674

Summary

  • Normal Distributions: dnorm(), pnorm(), qnorm(), rnorm()
  • Binomial Distributions: dbinom(), pbinom(), qbinom(), rbinom()
  • Poisson Distributions: dpois(), ppois(), qpois(), rpois()
  • Exponential Distributions: dexp(), pexp(), qexp(), rexp()
  • Chi-Square Distributions: dchisq(), pchisq(), qchisq(), rchisq()

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print