Plot Probability Distribution Function in R
Last Updated :
30 Jul, 2024
The PDF is the acronym for Probability Distribution Function and CDF is the acronym for Cumulative Distribution Function. In general, there are many probability distribution functions in R in this article we are going to focus on the Normal Distribution among them to plot PDF.
To calculate the Normal Distribution Function we have the built-in function dnorm() and to calculate the CDF we have ecdf() in R programming Language.
Syntax:
PDF: dnorm(x,mean,sd)
CDF: ecdf(x)
where,
- x - the data vector
- mean - mean of the data to calculated or can be assigned to a value
- sd - standard deviation of the data to be calculated or can be assigned to a value
Using plot() function to plot PDF
In this, we will use dnom() function to calculate the normal distribution based on the mean and standard deviation of the data.
R
# Generating sequence of
# numbers from -15 to +10
x<-seq(-15,10)
# calculate the Normal distribution function
# based on the mean and sd of data
pdf<- dnorm(x,mean(x),sd(x))
# Plotting the PDF(Normal Distribution Function
plot(x,pdf)
Output:
Using plotpdf() function to plot PDF
The plotpdf() is a function which is present in gbutils package.
# install gbutils package
install.packages("gbutils")
This package is specifically developed to simulate Quantile plots in R Programming. plotpdf() function used to plot PDF on Y-axis and the quantiles of the distribution on X- axis.
Syntax: plotpdf(pdf, qdf, lq, uq)
Where,
- pdf - Probability Distribution Function to be plotted is specified here
- qdf - The quantile function to be used is specified here
- lq - lower quantile need to be used in computation
- uq - upper quantile need to be used in computation
R
# Load the installed package
library(gbutils)
# Generating sequence of numbers from -50 to +10
x<-seq(-50,10)
# Calculate the Normal distribution
# function based on the mean and sd of data
pdf<- dnorm(x,mean(x),sd(x))
# Calculate the Quantile distribution
# function based on the mean and sd of data
qdf <- function(x) qnorm(x, mean(x), sd(x))
# Plotting the PDF Normal Distribution Function
gbutils::plotpdf(pdf,qdf,lq=0.0001,uq=0.0009)
Output:
Using plot() function to plot CDF
In this, we will use ecdf() function to calculate the cumulative distribution based on the mean and standard deviation of the data.
R
# Generating sequence of
# numbers from -15 to +10
x<-seq(-15,10)
# calculate the Cumulative distribution
# function based on the mean and sd of data
cdf<-ecdf(x)
# Plotting the CDF
plot(cdf,xlabel="x",ylabel="y",main="CDF Graph")
Output:
Using plotpdf() function to plot the CDF
The plotpdf() is a function which is present in gbutils package. This package is specifically developed to simulate Quantile plots in R Programming. plotpdf() function used to plot CDF.
Syntax: plotpdf(pdf, qdf, lq, uq)
Where,
- cdf - Cumulative Density to be plotted is specified here
- qdf - The quantile function to be used is specified here
- lq - lower quantile need to be used in computation
- uq - upper quantile need to be used in computation
R
# install and load the gbutils package
install.packages("gbutils")
library(gbutils)
# Calculating the CDF Normal Distribution Function
cdf1 <- function(x)pnorm(x, mean = -2.5, sd = 7.64)
# Plotting the CDF Normal Distribution Function
gbutils::plotpdf(cdf1, cdf = cdf1,main='CDF Plot')
Output: