16

To plot a normal distribution curve in R we can use:

(x = seq(-4,4, length=100))
y = dnorm(x)
plot(x, y)

enter image description here

If dnorm calculates y as a function of x, does R have a function that calculates x as a function of y? If not what is the best way to approach this?

WAF
  • 1,141
  • 20
  • 44
geotheory
  • 22,624
  • 29
  • 119
  • 196
  • Have you read `pnorm` ?? – Jilber Urbina Oct 25 '13 at 12:05
  • Perhaps I'm missing something, but `pnorm(y)` doesn't give x, hence `plot(pnorm(y), y)` does not give the normal distribution (it's actually a straight line). – geotheory Oct 25 '13 at 12:14
  • 1
    `plot(pnorm(y), y)` is certainly not a straight line. However, `plot(ppoints(y), y)` is. – Hong Ooi Oct 25 '13 at 12:25
  • 3
    One problem is that the inverse of a density function is not a function, as it is not one to one, but mrip's answer below gives as close to what you appear to be asking as you can get. – Sam Dickson Oct 25 '13 at 12:46
  • Not all functons [are invertible](https://en.wikipedia.org/wiki/Inverse_function#Inverses_and_derivatives), and this is an example (it is not strictly increasing nor decreasing). I think that @gung answer is more useful here. – Juan Oct 25 '13 at 14:08
  • Yes that's exactly what I was after. Thanks all. @hong-ooi - I meant that `plot(pnorm(y), y)` where `y = dnorm(seq(-4,4, length=100))` is straight. `plot(ppoints(y), y)` for the same `y` vector is in fact normal! – geotheory Oct 25 '13 at 14:13

3 Answers3

25

What dnorm() is doing is giving you a probability density function. If you integrate over that, you would have a cumulative distribution function (which is given by pnorm() in R). The inverse of the CDF is given by qnorm(); that is the standard way these things are conceptualized in statistics.

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
7

I'm not sure if the inverse of the density function is built in -- it's not used nearly as often as the inverse of the cumulative distribution function. I can't think offhand of too many situation where the inverse density function is useful. Of course, that doesn't mean there aren't any, so if you are sure this is the function you need, you could just do:

dnorminv<-function(y) sqrt(-2*log(sqrt(2*pi)*y))

plot(x, y)
points(dnorminv(y),y,pch=3)

enter image description here

mrip
  • 14,913
  • 4
  • 40
  • 58
  • Thanks mrip. I'd tried inversing the [normal distribution function](http://msenux.redwoods.edu/math/R/normal.php) myself but without success. – geotheory Oct 25 '13 at 14:07
  • 2
    I cannot be built-in, as there is no possible inverse for this density function (see [wikipedia](https://en.wikipedia.org/wiki/Inverse_function#Inverses_and_derivatives)). – Juan Oct 25 '13 at 14:09
  • @Juan Well, the positive inverse could be built in as a partial function. The inverse to the CDF is built in and that is technically not a function either, since it is only defined on [0,1]. – mrip Oct 25 '13 at 14:13
  • You are right, if you divide this function in 2 parts, then both resulting functions comply with the criteria for being invertible (in more general terms: on function to the right of the mean, and another to the left). Also, I don't see the problem for a function to be defined on [0,1], functions don't have to be restricted to a predefined domain. – Juan Oct 25 '13 at 14:18
  • You are right about [0,1]. It is a function, just not a real function (I think that's the standard terminology). I guess a better example would be the function `x^2`, which is technically not invertible, but the `sqrt` function is the positive branch of the inverse. – mrip Oct 25 '13 at 14:25
  • Yep, also in trigonometry all the inverses are defined only for a specific range in the co-domain, in a similar way of what you suggested (e.g.: arcsin goes from [0,1] to [-pi/2, pi/2]). – Juan Oct 25 '13 at 14:47
  • In practical terms are you saying that this function only works [for inputs in the range](http://answers.yahoo.com/question/index?qid=20101213093908AAqiDnV) `[0,1/sqrt(2*pi)]`? Because that appears to be the case. – geotheory Oct 25 '13 at 15:10
  • Yes, @geotheory, that is the case. Looking at the plot above (either his or yours), imagine someone told you the y-value is 0.2, what is the x-value? Well, there are **2** possibilities {~-1.175, ~1.175}. Recall that the definition of a [function](http://en.wikipedia.org/wiki/Function_%28mathematics%29) is that it has only **1** answer. That is why there isn't an inverse of the pdf, although mrip's workaround may be good enough for you in practice. – gung - Reinstate Monica Oct 25 '13 at 15:47
  • dnorm(0) yields 0.3989423. However, dnorminv(0.3989423) returns NaN. This is happening because log(sqrt(2*pi)*y) is returning 4.912632e-08 instead of 0. So an improvement to your answer could be dnorminv<-function(y) sqrt(-2* round(log(sqrt(2*pi)*y),6)) – Ashrith Reddy Jun 26 '21 at 05:15
3

The derivation of the inverse of the standard normal pdf is:

enter image description here

Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114