R Lot P'p '

Posted on  by admin
R Lot P'p ' Rating: 4,5/5 7188 votes

University of Minnesota, Twin CitiesSchool of StatisticsStat 5101Rweb

Contents

  • The Normal Distribution
    • Direct Look-Up (pnorm)
    • Inverse Look-Up (qnorm)
    • Density (dnorm)
    • Random Variates (rnorm)
  • The Binomial Distribution
    • Direct Look-Up, Points (dbinom)
    • Direct Look-Up, Intervals (pbinom)
    • Inverse Look-Up (qbinom)

R Functions for Probability Distributions

Every distribution that R handles has four functions. There is a rootname, for example, the root name for the normal distributionis norm. This root is prefixed by one of the letters

  • p for 'probability', the cumulative distribution function (c. d. f.)
  • q for 'quantile', the inverse c. d. f.
  • d for 'density', the density function (p. f. or p. d. f.)
  • r for 'random', a random variable having the specified distribution

What does R.A.P. List of 8 R.A.P. Abbreviation meanings updated January 2021. ATTENTION: You are currently using an unsupported browser. We recommend switching to Google Chrome or Edge for the best on-site experience.

For the normal distribution, these functions arepnorm,qnorm,dnorm, andrnorm.For the binomial distribution,these functions arepbinom,qbinom,dbinom, andrbinom.And so forth.

For a continuous distribution (like the normal),the most useful functions for doing problems involving probabilitycalculations are the 'p' and 'q' functions(c. d. f. and inverse c. d. f.), because thethe density (p. d. f.) calculated by the'd' function can only be used to calculate probabilitiesvia integrals and R doesn't do integrals.

For a discrete distribution (like the binomial),the 'd' function calculates the density (p. f.),which in this case is a probability

f(x) = P(X = x)
and hence is useful in calculating probabilities.R has functions to handle many probability distributions.The table below gives the names of the functions for each distributionand a link to the on-line documentation that is the authoritativereference for how the functions are used.But don't read the on-line documentation yet.First, try the examples in the sections following the table.
DistributionFunctions
Betapbetaqbetadbetarbeta
Binomialpbinomqbinomdbinomrbinom
Cauchypcauchyqcauchydcauchyrcauchy
Chi-Squarepchisqqchisqdchisqrchisq
Exponentialpexpqexpdexprexp
Fpfqfdfrf
Gammapgammaqgammadgammargamma
Geometricpgeomqgeomdgeomrgeom
Hypergeometricphyperqhyperdhyperrhyper
Logisticplogisqlogisdlogisrlogis
Log Normalplnormqlnormdlnormrlnorm
Negative Binomialpnbinomqnbinomdnbinomrnbinom
Normalpnormqnormdnormrnorm
Poissonppoisqpoisdpoisrpois
Student tptqtdtrt
Studentized Rangeptukeyqtukeydtukeyrtukey
Uniformpunifqunifdunifrunif
Weibullpweibullqweibulldweibullrweibull
Wilcoxon Rank Sum Statisticpwilcoxqwilcoxdwilcoxrwilcox
Wilcoxon Signed Rank Statisticpsignrankqsignrankdsignrankrsignrank

Warning: The parameters of these distributions may not agreewith textbooks. In particular, the second parameter in the gamma distributionis the reciprocal of the second parameter in our textbook(beta = 1 / lambda).

That's a lot of distributions. Fortunately, they all workthe same way. If you learn one, you've learned them all.

Of course, the discrete distributions are discrete and the continuousdistributions are continuous, so there's some difference just from thataspect alone, but as far as the computer is concerned, they're all the same.We'll do a continuous example first.

The Normal Distribtion

Direct Look-Up

pnorm isthe R function that calculates the c. d. f.

F(x) = P(X <= x)
where X is normal. Optional arguments described on theon-linedocumentation specify the parameters of the particular normal distribution.

Both of the R commands in the box below do exactly the same thing.

They look up P(X < 27.4) when X is normal with mean50 and standard deviation 20.

Example

Question: Suppose widgit weights produced at Acme WidgitWorks have weights that are normally distributed with mean 17.46 gramsand variance 375.67 grams. What is the probability that a randomly chosenwidgit weighs more then 19 grams?

Question Rephrased: What is P(X > 19) whenX has the N(17.46, 375.67) distribution?

Caution: R wants the s. d. as the parameter, not thevariance. We'll need to take a square root!

Answer:

Inverse Look-Up

qnorm isthe R function that calculates the inverse c. d. f.F-1 of the normal distributionThe c. d. f. and the inverse c. d. f. are related by

p = F(x)
x = F-1(p)
So given a number p between zero and one, qnormlooks up the p-th quantile of the normal distribution.As with pnorm, optional arguments specify the mean andstandard deviation of the distribution.

Example

Question: Suppose IQ scores are normally distributedwith mean 100 and standard deviation 15. What is the 95th percentileof the distribution of IQ scores?

Question Rephrased: What is F-1(0.95) whenX has the N(100, 152) distribution?

Answer:

Density

dnorm isthe R function that calculates the p. d. f.f of the normal distribution.As with pnorm and qnorm, optional argumentsspecify the mean and standard deviation of the distribution.

There's not much need for this function in doing calculations, becauseyou need to do integrals to use any p. d. f., and R doesn'tdo integrals. In fact, there's not much use for the 'd' function forany continuous distribution (discrete distributions are entirelyanother matter, for them the 'd' functions are very useful, seethe section about dbinom).

For an example of the use of pnorm, see thefollowing section.

Random Variates

rnorm isthe R function that simulates random variates having a specified normaldistribution.As with pnorm, qnorm, and dnorm,optional arguments specify the mean and standard deviation of the distribution.

We won't be using the 'r' functions (such as rnorm)much. So here we will only give an example without full explanation.

This generates 1000 i. i. d. normal random numbers (first line),plots their histogram (second line), and graphs the p. d. f. ofthe same normal distribution (third and forth lines).

The Binomial Distribtion

Direct Look-Up, Points

dbinom isthe R function that calculates the p. f. of the binomial distribution.Optional arguments described on theon-linedocumentation specify the parameters of the particular binomialdistribution.

Both of the R commands in the box below do exactly the same thing.

They look up P(X = 27) when X is has theBin(100, 0.25) distribution.

Example

Question: Suppose widgits produced at Acme WidgitWorks have probability 0.005 of being defective.Suppose widgits are shipped in cartons containing 25 widgits.What is the probability that a randomly chosen cartoncontains exactly one defective widgit?

Question Rephrased: What is P(X = 1) whenX has the Bin(25, 0.005) distribution?

Answer:

Direct Look-Up, Intervals

pbinom isthe R function that calculates the c. d. f. of the binomialdistribution.Optional arguments described on theon-linedocumentation specify the parameters of the particular binomialdistribution.

Both of the R commands in the box below do exactly the same thing.

They look up P(X <= 27) when X is has theBin(100, 0.25) distribution. (Note the less than or equal tosign. It's important when working with a discrete distribution!)

Example

Question: Suppose widgits produced at Acme WidgitWorks have probability 0.005 of being defective.Suppose widgits are shipped in cartons containing 25 widgits.What is the probability that a randomly chosen cartoncontains no more than one defective widgit?

Question Rephrased: What is P(X <= 1) whenX has the Bin(25, 0.005) distribution?

Answer:

Inverse Look-Up

qbinom isthe R function that calculates the 'inverse c. d. f.'of the binomial distribution. How does it do that when the c. d. f. is a step function and hence not invertible?Theon-linedocumentation for the binomial probability functions explains.

The quantile is defined as the smallest value x such thatF(x) >= p, where F is the distribution function.
When the p-th quantile is nonunique, there is a whole intervalof values each of which is a p-th quantile. The documentationsays that qbinom (and other 'q' functions,for that matter) returns the smallest of these values. That is onesensible definition of an 'inverse c. d. f.' In the terminologyof Section of the course notes, the function definedby qbinom is a right inverse of the function definedby pbinom, that is,
q pbinom(qbinom(q, n, p)), 0 < q < 1, 0 < p < 1, n a positive integer
is always true,but the analogous formula with R lot vcu parkingpnorm and qnormreversed does not necessarily hold.

Example

Question: What are the 10th, 20th, and so forth quantilesof the Bin(10, 1/3) distribution?

R Parking Lot Unm

Answer:

Lot R Parking Pass

Note the nonuniqueness.