parvin karimi - 1 year ago 40

R Question

I have difficulty understanding what it means when an

`rnorm`

`rnorm`

For example, below, in the first line of my R code I use an

`rnorm()`

`rnorm()`

`mu`

`mu`

`x`

Now, let me put

`mu`

`mean`

`rnorm()`

My question is how

`mu`

`x`

`mean`

`rnorm()`

P.S.:

`mean`

`normal distributon`

`mu <- rnorm( 1e4 , 178 , 20 ) ; plot( density(mu) )`

distribution <- rnorm( 1e4 , mu , 1 ) ; plot( density(distribution) )

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**