[R] sampling from normal distribution

Duncan Murdoch murdoch.duncan at gmail.com
Mon Oct 4 03:08:26 CEST 2010


On 03/10/2010 6:38 PM, solafah bh wrote:
> Hello
> If i want to resampl from the tails of normal distribution , are these commans equivelant??
>   upper tail:qnorm(runif(n,pnorm(b),1))  if b is an upper tail boundary
>   or
>   upper tail:qnorm((1-p)+p(runif(n))  if p is the probability of each interval (the observatins are divided to intervals)


You don't say how far up in the tail you are going, but if b is very 
large, you have to watch out for rounding error.  For example, with 
b=10, pnorm(b) will be exactly equal to 1, and both versions will fail. 
  In general for b > 0 you'll get a bit more accuracy by sampling from 
the lower tail using -b.  For really extreme cases you will probably 
need to switch to a log scale.  For example, to get a random sample from 
a normal, conditional on being larger than 20, you'd want something like

n <- 10
logp1 <- pnorm(-20, log=TRUE)
logprobs <- log(runif(n)) + logp1
-qnorm(logprobs, log=TRUE)

Duncan Murdoch



More information about the R-help mailing list