7

In human language the rnorm(n=1000, m=24.2, sd=2.2) returns the random numbers which follows normal distribution. Another explanation could be that it returns random numbers from which the histogram can be created. If you draw normal (bell shaped) curve it will perfectly fits to this histogram. Can you please explain dnorm in such human language (or even with math and/or graphic but to be understandable by newbie)?

Wakan Tanka
  • 523
  • 2
  • 6
  • 13
  • Some slightly-above-newbie discussions at http://stats.stackexchange.com/questions/86487/what-is-the-meaning-of-the-density-of-a-distribution-at-a-point/86492#86492 – James Stanley Jun 19 '15 at 02:44
  • 1
    Also see http://ww2.coastal.edu/kingw/statistics/R-tutorials/prob.html (which has nice graphs too, and is a bit more intuitive...) – James Stanley Jun 19 '15 at 02:45
  • 4
    `?dnorm` states what `dnorm`, `pnorm`, `qnorm` and `rnorm` do. – Glen_b Jun 19 '15 at 03:27
  • @JamesStanley Thank you. Regarding 2nd link. Can I generate data using `rnorm` and then use `dnorm` on those generated data? Also can I plot graphs, like those on mentioned link from the data returned by `rnorm`? I mean plotting the bell shaped curve not histogram (histogram is easy `hist(rnorm(1000))`). Data generated by `rnrom` are just one dimensional so how one could plot such curve? They are x or y or what I'm missing? – Wakan Tanka Jun 19 '15 at 22:51
  • 1
    You could consider a histogram from `hist(rnorm(1000))` to be a simulation of the normal distribution: to get the y-axis as density, use `hist(rnorm(1000), freq=FALSE)`. For a smoothed version of this, `plot(density(rnorm(1000)))` ... – James Stanley Jun 21 '15 at 21:10
  • ...but if you want the **theoretical** normal distribution curve (based on the observed mean and standard deviation), see http://stackoverflow.com/q/10488988/1834244 or http://www.statmethods.net/advgraphs/probability.html – James Stanley Jun 21 '15 at 21:12

2 Answers2

11

You know the probability density function for a standard normal distribution is

$\frac{1}{\sqrt{2\pi}}e^\frac{-x^2}{2} $.

The dnorm() function is just used to calculate the value of this function.

You can test using the following R code

density_standard_norm <- function(x)
{
1/sqrt(2*pi)*exp(-0.5*x^2)
}

dnorm(1, mean = 0, sd = 1)
[1] 0.2419707
density_standard_norm(1)
[1] 0.2419707

dnorm(2, mean = 0, sd = 1)

[1] 0.05399097

density_standard_norm(2) 
[1] 0.05399097

They are equal. For non-standard normal it's same.

Glen_b
  • 257,508
  • 32
  • 553
  • 939
Deep North
  • 4,527
  • 2
  • 18
  • 38
  • 2
    It should be pointed out that `dnorm` is implemented as [C code](/https://github.com/wch/r-source/blob/trunk/src/nmath/dnorm.c) (and thus more efficient) and also provides better accuracy in some edge cases. – Roland Jun 19 '15 at 07:39
  • 1
    ok, but what is the relationship between `rnorm` and `dnorm`? – Wakan Tanka Jun 19 '15 at 22:35
  • If you try to see the relationship between rnorm and dnorm, you need to know the Monte Carlo simulation. The rnorm function is used to produce random number according to certain normal distribution (dnorm). I don't know which algorithm rnorm used to produce these number, but usually they can be Accept-rejection algorithm or Marsaglia and Bray algorithm – Deep North Jun 22 '15 at 01:55
  • 2
    While I can't claim to know exactly, I would be absolutely shocked if `rnorm` used something like an accept-reject algorithm. I would bet 10-1 that they do something like `qnorm( runif(1) )` (at the C level, of course) or a Box-Muller transformation (see the comments of http://stats.stackexchange.com/questions/16334/how-to-sample-from-a-normal-distribution-with-known-mean-and-variance-using-a-co). – Cliff AB Oct 27 '15 at 18:18
  • 2
    It's also worth noting that these methods have little to do with Monte Carlo simulation (outside that using `rnorm` can be useful in your Monte Carlo simulation). – Cliff AB Oct 27 '15 at 18:18
  • 1
    @CliffAB This was my original reaction too. Skimming the source (at https://svn.r-project.org/R/trunk/src/nmath/snorm.c) suggests that inversion is indeed the default (although several methods are implemented). There is no accept-reject implementation. – P.Windridge Oct 27 '15 at 20:35
  • @P.Windridge: you are officially better at skimming source code than I am... – Cliff AB Oct 28 '15 at 03:11
  • @CliffAB I skimmed too little ... :) There are accept reject methods (Ahrens-Dieter and Kinderman-Ramage). I missed them in the code because they use a macro "repeat" (whereas I was looking for a while {}) and I hadn't heard of those algorithms! – P.Windridge Oct 28 '15 at 18:38
  • @P.Windridge it seems they have several methods for drawing samples, but most of them are for backward compatibility and are not actually called in a call to `rnorm` in R. – Cliff AB Oct 28 '15 at 18:53
  • @CliffAB, yes exactly. The default (called when you type "rnorm") is inversion sampling but if you're willing to recompile the source you can use the others. – P.Windridge Oct 28 '15 at 19:10
6

It's difficult to describe dnorm in similar terms because that's a somewhat backwards way to think of rnorm.

The d in dnorm stands for probability density function, or PDF. The term "density" is chosen because a probability density function is analogous to the density of a substance in chemistry: the input is a location in the support of the probability distribution (analogous to a location in space inside the substance), and the output is a relative measure of how much "stuff" is near that position.

  • If you step on some dirt, the part you stepped on will be denser than the surrounding material
  • If a lake is partially frozen, the frozen part will be less dense than the liquid part
  • For normal-distributed variables, the probability of observing a point in $[-0.5, 0.5]$ is greater than the probability of observing a point in $[-2, -1]$.

The funny thing about continuous probability distributions is that the notion of a "location" is difficult to quantify. In fact, when we talk about a "location" in math, we are usually talking about a "point." A "point" is a structure so small that it has zero spatial extent; a point is infinitely small. Therefore we can't look at a random variable $Y$ that follows the normal distribution and say "the probability that $Y = 1.2$ is such-and-such", because $1.2$ is a point with zero spatial extent. Once you work through the underlying mathematics you realize that the probability is actually zero of $Y$ being equal to any real number.[1]

This is the case for any continuous probability distribution, including the normal (the norm part of dnorm) distribution. Therefore the best way to understand dnorm without equations, in my opinion, is to say that

if dnorm(y1) is bigger than dnorm(y2), then points near $y_1$ generally have higher probability than points near $y_2$.

This is a deliberately non-technical and non-rigorous definition. There's no universally applicable criterion for defining what "near" means in any particular context. But if you keep the analogy to the density of a physical substance in mind, you won't be entirely wrong.

Recall that I said your description of rnorm was somewhat backwards. That's because dnorm is actually the function that describes what it means for a random variable to follow the normal distribution. A function is just a mapping between inputs and outputs: dnorm is a complete description of the mapping between possible values for $Y$ and the probability densities of those values. That defines the probability distribution of $Y$. rnorm is just what happens when you repeatedly sample numbers in such a way that respects their relative density: values in higher-density regions are more likely to appear than values in lower-density regions. Just don't think too hard about what exactly a "region" is, or where a particular region starts and ends.

By the way, R does have the ability to plot smooth functions:

plot(dnorm, from = -4, to = 4)

works a lot better than

plot(density(rnorm(50000), bw = "nrd"), xlim = c(-4, 4))

and emphasizes the "fundamental" nature of dnorm versus the "derived" nature of rnorm.

[1]: If that makes your brain itch, maybe think of it this way: if the probability that $Y = 1.2$ is $p$, then what's the probability of $Y = 1.21$? How about $Y = 1.201$? Or $Y = 1.20000000000001$? The only reasonable way to cram infinitely many points into a finite space is to make each point infinitely small.

shadowtalker
  • 11,395
  • 3
  • 49
  • 109