Plot Probability Distribution Function in R
Last Updated :
27 Mar, 2023
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
x<- seq (-15,10)
pdf<- dnorm (x, mean (x), sd (x))
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
library (gbutils)
x<- seq (-50,10)
pdf<- dnorm (x, mean (x), sd (x))
qdf <- function (x) qnorm (x, mean (x), sd (x))
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
x<- seq (-15,10)
cdf<- ecdf (x)
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.packages ( "gbutils" )
library (gbutils)
cdf1 <- function (x) pnorm (x, mean = -2.5, sd = 7.64)
gbutils:: plotpdf (cdf1, cdf = cdf1,main= 'CDF Plot' )
|
Output:
Share your thoughts in the comments
Please Login to comment...