Functions from stat package (which is loaded by
default).
The probability mass at a point \(x\) can be evaluated using the syntax:
dbinom(x=x,size=n,p=p).
The probability distribution \(P(X\leq
x)\) is calculated using the pbinom() function.
Syntax is:
pbinom(x,size=n,p=p)
The quantile for probability \(p\)
can be evaluated using the quantile() function. Syntax
is:
qbinom(prob,size,p=p)
Step 1: Assign the inputs for required distribution
Step 2: Calculate the required probabilities
Step 3: Report the results
Case: Find the mass function of a binomial distribution with \(n=20,p=0.4\). Also draw the graphs of the mass function and cumulative distribution function.
# create input parameters
n=20
p=0.4
x=0:20
#calculating probability mass distribution and cumulative distribution
pmval=dbinom(x,size=n,prob=p)
pmval
## [1] 3.656158e-05 4.874878e-04 3.087423e-03 1.234969e-02 3.499079e-02
## [6] 7.464702e-02 1.244117e-01 1.658823e-01 1.797058e-01 1.597385e-01
## [11] 1.171416e-01 7.099488e-02 3.549744e-02 1.456305e-02 4.854351e-03
## [16] 1.294494e-03 2.696862e-04 4.230371e-05 4.700412e-06 3.298535e-07
## [21] 1.099512e-08
#calculating cumulative density
cdval=pbinom(x,size=n,prob=p)
cdval
## [1] 3.656158e-05 5.240494e-04 3.611472e-03 1.596116e-02 5.095195e-02
## [6] 1.255990e-01 2.500107e-01 4.158929e-01 5.955987e-01 7.553372e-01
## [11] 8.724788e-01 9.434736e-01 9.789711e-01 9.935341e-01 9.983885e-01
## [16] 9.996830e-01 9.999527e-01 9.999950e-01 9.999997e-01 1.000000e+00
## [21] 1.000000e+00
pmf and cdfpar(mfrow=c(1,2))
plot(x,pmval,xlab="x",ylab="P(X=x)", main="The Binomial Distribution")
plot(x,cdval,xlab="x",ylab=expression(P(X<=x)),main="Cumulative Distribution Function")
The pmf and cf of the Binomial distribution
for given input parameters are evaluated and create respective
plots.