What does it mean to put an `rnorm` as an argument

2019-04-14 20:33发布

问题:

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

回答1:

You distribution is a conditional density. While the density you draw with plot(density(distribution)), is a marginal density.

Statistically speaking, you first have a normal random variable mu ~ N(178, 20), then another random variable y | mu ~ N(mu, 1). The plot you produce is the marginal density of y.

P(y), is mathematically an integral of joint distribution P(y | mu) * p(mu), integrating out mu.

@李哲源ZheyuanLi, ahhh! so when we use a vetor as the mean argument or sd argument of an rnorm, the single, final plot is the result of the integral, right?

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.