What does it mean to put an `rnorm` as an argument of another `rnorm` in R?

守給你的承諾、 提交于 2019-12-06 11:49:41

问题


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.



来源:https://stackoverflow.com/questions/44121625/what-does-it-mean-to-put-an-rnorm-as-an-argument-of-another-rnorm-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!