-2

I am using rnorm but the outputs I receive are sometimes negative. How do I create a restriction so that the outputs cannot be below 0? Example:

output = rnorm(1, 800/20, sqrt(800))
user3000205
  • 15
  • 1
  • 2
  • It sounds like you don't want a normal distribution. Consider a different one `? Distributions`. Otherwise rescale your `output` vector after the fact: `output + abs(min(output))` – Thomas Nov 17 '13 at 22:37

2 Answers2

2

Why not abs(rnorm(1, 800/20, sqrt(800))? rnorm was written to give numbers from a normal distribution. Perhaps you are looking to get output from a truncated distribution. In that case, you might want to have a look at the truncnorm package.

library(truncnorm)
rtruncnorm( 1, a=0, b=Inf, 800/20, sqrt(800))

x = seq(-20,200,by=0.01)
y = dtruncnorm(x, a=0, b=Inf, 800/20, sqrt(800) )
plot(x,y,type="l",main="Density of a truncated normal distribution")

enter image description here

kdauria
  • 6,300
  • 4
  • 34
  • 53
0

The Poisson distribution takes only positive values. Otherwise golbasche solution seems perfect.

hist(rpois(100, 5))
SESman
  • 238
  • 2
  • 9