[R] truncated normal
Thomas Lumley
tlumley at u.washington.edu
Fri May 10 17:24:58 CEST 2002
On Thu, 9 May 2002, Thomas Lumley wrote:
> On Thu, 9 May 2002 scott.ctr.summerill at tc.faa.gov wrote:
>
> > Does anyone know of an R-function that will generate an observation from a
> > truncated normal (left or right) with a mu and sigma2? Any correspondence
> > would be greatly appreciated.
> >
>
> No, but you can do it by inverting the cdf
> eg
> rtnorm<-function(n,mu,sigma2,left){
> lp<-pnorm(left)
> u<-lp+(1-lp)*runif(n)
> qnorm(u,m=mu,s=sigma2)
> }
>
Or actually more like
rtnorm<-function(n,mu,sigma2,left){
sigma<-sqrt(sigma2)
lp<-pnorm(left,m=mu,s=sigma)
u<-lp+(1-lp)*runif(n)
qnorm(u,m=mu,s=sigma)
}
but David Brahm's code is more general
-thomas
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list