I have difficulty understanding what it means when an rnorm
is used as one of the arguments of another rnorm
? (I'll explain more below)
For example, below, in the first line of my R code I use an rnorm()
and I call this rnorm()
: mu
.
mu
consists of 10,000 x
.
Now, let me put mu
itself as the mean
argument of a new rnorm()
called "distribution".
My question is how mu
which itself has 10,000 x
be used as the mean
argument of this new rnorm()
called distribution?
P.S.: mean
argument of any normal distribution
can be a single number, and with only ONE single mean, we will have a single, complete normal. Now, how come, using 10,000 mu
values still results in a single normal?
mu <- rnorm( 1e4 , 178 , 20 ) ; plot( density(mu) )
distribution <- rnorm( 1e4 , mu , 1 ) ; plot( density(distribution) )
You
distribution
is a conditional density. While the density you draw withplot(density(distribution))
, is a marginal density.Statistically speaking, you first have a normal random variable
mu ~ N(178, 20)
, then another random variabley | mu ~ N(mu, 1)
. The plot you produce is the marginal density ofy
.P(y)
, is mathematically an integral of joint distributionP(y | mu) * p(mu)
, integrating outmu
.It means you are sampling from the marginal distribution. The density estimate approximates the Monte Carlo integral from samples.
This kind of thing is often seen in Bayesian computation. Toy R code on Bayesian inference for mean of a normal distribution [data of snowfall amount] gives a full example, but integral is computed by numerical integration.