(Ted Harding)
2015-Sep-15 15:12 UTC
[R] Beta distribution approximate to Normal distribution
Using non-central chi-squared (especially with df=1) is unlikely to generate random numbers anywhere near a Normal distribution (see below). And "rchisq(100, df=1, ncp=u/a)" won't work anyway with u<0, since ncp must be >= 0 (if < 0 then all are NA). Better to shoot straight for the target (truncated Normal), though several shots are likely to be required! For example (code which spells it out), taking u=3 and a=2: n <- 100 u <- 3 ; a <- 2 x <- NULL N <- length(x) while(N < n){ x <- c(x,rnorm(n,mean=u,sd=a)) x <- x[x>0] N <- length(x) } x <- x[1:n] Comparison with non-central chi-squared: y <- rchisq(100, df=1, ncp=u/a) hist(x) hist(y) On 15-Sep-2015 13:26:44 JLucke at ria.buffalo.edu wrote:> Your question makes no sense as stated. However, guessing at what you > want, you should perhaps consider the non-central chi-square density with > 1 df and ncp = u/a, i.e, > > rchisq(100, df=1, ncp=u/a) > > Joe > Joseph F. Lucke, PhD > Senior Statistician > Research Institute on Addictions > University at Buffalo > State University of New York > 1021 Main Street > Buffalo, NY 14203-1016 > > Chien-Pang Chin <chienpang.c at gmail.com> > Sent by: "R-help" <r-help-bounces at r-project.org> > 09/15/2015 06:58 AM > > To > "r-help at r-project.org" <r-help at r-project.org>, > > Subject > [R] Beta distribution approximate to Normal distribution > > Hi, > I need to generate 1000 numbers from N(u, a^2), however I don't > want to include 0 and negative values. How can I use beta distribution > approximate to N(u, a^2) in R. > > Thx for help------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 15-Sep-2015 Time: 16:12:35 This message was sent by XFMail
JLucke at ria.buffalo.edu
2015-Sep-15 16:15 UTC
[R] Beta distribution approximate to Normal distribution
Scratch the rchisq (it should have been sqrt(rchisq), but that doesn't help.). Use the truncated normal u <- 3; a <- 2; N <- 100 x <- numeric(N) for (i in 1:N){ repeat{ if( (x[i] <- rnorm(1, u, a)) >= 0 ) break } } or the folded normal abs(rnorm(N, u, a)), They give similar results. The code for the truncated normal allows you to set any truncation point. Joe (Ted Harding) <Ted.Harding at wlandres.net> Sent by: "R-help" <r-help-bounces at r-project.org> 09/15/2015 11:12 AM Please respond to Ted.Harding at wlandres.net To "r-help at r-project.org" <r-help at r-project.org>, cc Chien-Pang Chin <chienpang.c at gmail.com> Subject Re: [R] Beta distribution approximate to Normal distribution Using non-central chi-squared (especially with df=1) is unlikely to generate random numbers anywhere near a Normal distribution (see below). And "rchisq(100, df=1, ncp=u/a)" won't work anyway with u<0, since ncp must be >= 0 (if < 0 then all are NA). Better to shoot straight for the target (truncated Normal), though several shots are likely to be required! For example (code which spells it out), taking u=3 and a=2: n <- 100 u <- 3 ; a <- 2 x <- NULL N <- length(x) while(N < n){ x <- c(x,rnorm(n,mean=u,sd=a)) x <- x[x>0] N <- length(x) } x <- x[1:n] Comparison with non-central chi-squared: y <- rchisq(100, df=1, ncp=u/a) hist(x) hist(y) On 15-Sep-2015 13:26:44 JLucke at ria.buffalo.edu wrote:> Your question makes no sense as stated. However, guessing at what you > want, you should perhaps consider the non-central chi-square densitywith> 1 df and ncp = u/a, i.e, > > rchisq(100, df=1, ncp=u/a) > > Joe > Joseph F. Lucke, PhD > Senior Statistician > Research Institute on Addictions > University at Buffalo > State University of New York > 1021 Main Street > Buffalo, NY 14203-1016 > > Chien-Pang Chin <chienpang.c at gmail.com> > Sent by: "R-help" <r-help-bounces at r-project.org> > 09/15/2015 06:58 AM > > To > "r-help at r-project.org" <r-help at r-project.org>, > > Subject > [R] Beta distribution approximate to Normal distribution > > Hi, > I need to generate 1000 numbers from N(u, a^2), however I don't > want to include 0 and negative values. How can I use beta distribution > approximate to N(u, a^2) in R. > > Thx for help------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 15-Sep-2015 Time: 16:12:35 This message was sent by XFMail ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Rui Barradas
2015-Sep-15 17:40 UTC
[R] Beta distribution approximate to Normal distribution
Hello, If you want a truncated something rng, you can use the following function. Note that 'distr' is the name of an R distribution function without the dpqr prefix. rtrunc <- function(n, distr, lower = -Inf, upper = Inf, ...){ makefun <- function(prefix, FUN, ...){ txt <- paste(prefix, FUN, "(x, ...)", sep = "") function(x, ...) eval(parse(text = txt)) } if(length(n) > 1) n <- length(n) pfun <- makefun("p", distr, ...) qfun <- makefun("q", distr, ...) lo <- pfun(lower, ...) up <- pfun(upper, ...) u <- runif(n, lo, up) qfun(u, ...) } u <- 2; a <- 3 x <- rtrunc(1000, "norm", lower = 0, mean = u, sd = a) hist(x) Hope this helps, Rui Barradas Em 15-09-2015 16:12, (Ted Harding) escreveu:> Using non-central chi-squared (especially with df=1) is unlikely > to generate random numbers anywhere near a Normal distribution > (see below). > > And "rchisq(100, df=1, ncp=u/a)" won't work anyway with u<0, > since ncp must be >= 0 (if < 0 then all are NA). > > Better to shoot straight for the target (truncated Normal), though > several shots are likely to be required! For example (code which > spells it out), taking u=3 and a=2: > > n <- 100 > u <- 3 ; a <- 2 > x <- NULL > N <- length(x) > while(N < n){ > x <- c(x,rnorm(n,mean=u,sd=a)) > x <- x[x>0] > N <- length(x) > } > x <- x[1:n] > > Comparison with non-central chi-squared: > > y <- rchisq(100, df=1, ncp=u/a) > hist(x) > hist(y) > > > > On 15-Sep-2015 13:26:44 JLucke at ria.buffalo.edu wrote: >> Your question makes no sense as stated. However, guessing at what you >> want, you should perhaps consider the non-central chi-square density with >> 1 df and ncp = u/a, i.e, >> >> rchisq(100, df=1, ncp=u/a) >> >> Joe >> Joseph F. Lucke, PhD >> Senior Statistician >> Research Institute on Addictions >> University at Buffalo >> State University of New York >> 1021 Main Street >> Buffalo, NY 14203-1016 >> >> Chien-Pang Chin <chienpang.c at gmail.com> >> Sent by: "R-help" <r-help-bounces at r-project.org> >> 09/15/2015 06:58 AM >> >> To >> "r-help at r-project.org" <r-help at r-project.org>, >> >> Subject >> [R] Beta distribution approximate to Normal distribution >> >> Hi, >> I need to generate 1000 numbers from N(u, a^2), however I don't >> want to include 0 and negative values. How can I use beta distribution >> approximate to N(u, a^2) in R. >> >> Thx for help > > ------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> > Date: 15-Sep-2015 Time: 16:12:35 > This message was sent by XFMail > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
David L Carlson
2015-Sep-15 18:12 UTC
[R] Beta distribution approximate to Normal distribution
There are also truncated normal distributions in packages truncnorm, crch, and msm. Also a multivariate truncated normal distribution in package tmvtnorm. e.g. library(truncnorm) x <- rtruncnorm(n, a=0, mean=u, sd=a) hist(x) library(msm) x <- rtnorm(n, mean=u, sd=a, lower=0) hist(x) ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Rui Barradas Sent: Tuesday, September 15, 2015 12:41 PM To: Ted.Harding at wlandres.net; r-help at r-project.org Cc: Chien-Pang Chin Subject: Re: [R] Beta distribution approximate to Normal distribution Hello, If you want a truncated something rng, you can use the following function. Note that 'distr' is the name of an R distribution function without the dpqr prefix. rtrunc <- function(n, distr, lower = -Inf, upper = Inf, ...){ makefun <- function(prefix, FUN, ...){ txt <- paste(prefix, FUN, "(x, ...)", sep = "") function(x, ...) eval(parse(text = txt)) } if(length(n) > 1) n <- length(n) pfun <- makefun("p", distr, ...) qfun <- makefun("q", distr, ...) lo <- pfun(lower, ...) up <- pfun(upper, ...) u <- runif(n, lo, up) qfun(u, ...) } u <- 2; a <- 3 x <- rtrunc(1000, "norm", lower = 0, mean = u, sd = a) hist(x) Hope this helps, Rui Barradas Em 15-09-2015 16:12, (Ted Harding) escreveu:> Using non-central chi-squared (especially with df=1) is unlikely > to generate random numbers anywhere near a Normal distribution > (see below). > > And "rchisq(100, df=1, ncp=u/a)" won't work anyway with u<0, > since ncp must be >= 0 (if < 0 then all are NA). > > Better to shoot straight for the target (truncated Normal), though > several shots are likely to be required! For example (code which > spells it out), taking u=3 and a=2: > > n <- 100 > u <- 3 ; a <- 2 > x <- NULL > N <- length(x) > while(N < n){ > x <- c(x,rnorm(n,mean=u,sd=a)) > x <- x[x>0] > N <- length(x) > } > x <- x[1:n] > > Comparison with non-central chi-squared: > > y <- rchisq(100, df=1, ncp=u/a) > hist(x) > hist(y) > > > > On 15-Sep-2015 13:26:44 JLucke at ria.buffalo.edu wrote: >> Your question makes no sense as stated. However, guessing at what you >> want, you should perhaps consider the non-central chi-square density with >> 1 df and ncp = u/a, i.e, >> >> rchisq(100, df=1, ncp=u/a) >> >> Joe >> Joseph F. Lucke, PhD >> Senior Statistician >> Research Institute on Addictions >> University at Buffalo >> State University of New York >> 1021 Main Street >> Buffalo, NY 14203-1016 >> >> Chien-Pang Chin <chienpang.c at gmail.com> >> Sent by: "R-help" <r-help-bounces at r-project.org> >> 09/15/2015 06:58 AM >> >> To >> "r-help at r-project.org" <r-help at r-project.org>, >> >> Subject >> [R] Beta distribution approximate to Normal distribution >> >> Hi, >> I need to generate 1000 numbers from N(u, a^2), however I don't >> want to include 0 and negative values. How can I use beta distribution >> approximate to N(u, a^2) in R. >> >> Thx for help > > ------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> > Date: 15-Sep-2015 Time: 16:12:35 > This message was sent by XFMail > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.