# rnorm vs dnorm in R

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)?

• Some slightly-above-newbie discussions at stats.stackexchange.com/questions/86487/… – James Stanley Jun 19 '15 at 2:44
• Also see 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 2:45
• ?dnorm states what dnorm, pnorm, qnorm and rnorm do. – Glen_b -Reinstate Monica Jun 19 '15 at 3: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
• 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

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.

• It should be pointed out that dnorm is implemented as C code (and thus more efficient) and also provides better accuracy in some edge cases. – Roland Jun 19 '15 at 7:39
• ok, but what is the relationship between rnorm and dnorm? – Wakan Tanka Jun 19 '15 at 22:35
• 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 stats.stackexchange.com/questions/16334/…). – Cliff AB Oct 27 '15 at 18:18
• 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
• @CliffAB This was my original reaction too. Skimming the source (at 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

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.