Statistics with R
- Statistics with R
- R Objects, Numbers, Attributes, Vectors, Coercion
- Matrices, Lists, Factors
- Data Frames in R
- Control Structures in R
- Functions in R
- Data Basics: Compute Summary Statistics in R
- Central Tendency and Spread in R Programming
- Data Basics: Plotting – Charts and Graphs
- Normal Distribution in R
- Skewness of statistical data
- Bernoulli Distribution in R
- Binomial Distribution in R Programming
- Compute Randomly Drawn Negative Binomial Density in R Programming
- Poisson Functions in R Programming
- How to Use the Multinomial Distribution in R
- Beta Distribution in R
- Chi-Square Distribution in R
- Exponential Distribution in R Programming
- Log Normal Distribution in R
- Continuous Uniform Distribution in R
- Understanding the t-distribution in R
- Gamma Distribution in R Programming
- How to Calculate Conditional Probability in R?
- How to Plot a Weibull Distribution in R
- Hypothesis Testing in R Programming
- T-Test in R Programming
- Type I Error in R
- Type II Error in R
- Confidence Intervals in R
- Covariance and Correlation in R
- Covariance Matrix in R
- Pearson Correlation in R
- Normal Probability Plot in R
Gamma Distribution in R Programming
The Gamma distribution is a continuous probability distribution that is widely used in various fields such as finance, engineering, and natural sciences. In R programming, you can generate random numbers from a Gamma distribution, calculate the density, distribution function, and quantile function using the built-in functions.
Here is a brief overview of these functions:
rgamma(n, shape, scale = 1)
: Generates ‘n’ random numbers from a Gamma distribution with the specified ‘shape’ and ‘scale’ parameters.dgamma(x, shape, scale = 1, log = FALSE)
: Computes the density function of the Gamma distribution for a given vector ‘x’ with the specified ‘shape’ and ‘scale’ parameters. If ‘log = TRUE’, the logarithm of the density is returned.pgamma(q, shape, scale = 1, lower.tail = TRUE, log.p = FALSE)
: Calculates the cumulative distribution function of the Gamma distribution for a given vector ‘q’ with the specified ‘shape’ and ‘scale’ parameters. If ‘lower.tail = FALSE’, the upper tail is considered. If ‘log.p = TRUE’, the logarithm of the probability is returned.qgamma(p, shape, scale = 1, lower.tail = TRUE, log.p = FALSE)
: Computes the quantile function (inverse of the cumulative distribution function) of the Gamma distribution for a given vector ‘p’ with the specified ‘shape’ and ‘scale’ parameters. If ‘lower.tail = FALSE’, the upper tail is considered. If ‘log.p = TRUE’, the logarithm of the probability is returned.
Here’s an example demonstrating the use of these functions:
# Set seed for reproducibility set.seed(123) # Parameters for the Gamma distribution shape <- 2 scale <- 3 # Generate random numbers from a Gamma distribution n <- 10 random_numbers <- rgamma(n, shape, scale) print(random_numbers) # Compute the density function x <- seq(0, 20, by = 0.1) density <- dgamma(x, shape, scale) plot( x, density, type = "l", main = "Gamma Distribution Density", xlab = "x", ylab = "Density" )
# Calculate the cumulative distribution function cdf <- pgamma(x, shape, scale) plot( x, cdf, type = "l", main = "Gamma Distribution CDF", xlab = "x", ylab = "CDF" )
# Compute the quantile function p <- seq(0, 1, by = 0.01) quantiles <- qgamma(p, shape, scale) plot( p, quantiles, type = "l", main = "Gamma Distribution Quantile Function", xlab = "Probability", ylab = "Quantiles" )