1

I would like to generate a truncated normal distribution with known parameters in either R. Note that the I'm not seeking a pseudo-random number generator.

Assume that I have a normal distribution with mean 5 and standard deviation of 1. Can I plot the values of a truncated normal distribution, truncated at points 1 and 10?

14
  • Are you talking about the truncnorm package? The dtruncnorm function gives the density
    – juan
    Jul 20, 2017 at 19:01
  • Hi @juan. Yes, i'm talking about the truncnorm package. I've tried the dtruncnorm function but it doesn't seem to give the density. For example if I enter the code: vec=seq(from=0,by=0.01,length.out = 100) test=dtruncnorm(vec, a=0,b=1,mean=0.1,sd=0.1) plot(test) then I get a plot which has the exact shape of a truncated normal distribution, and the x-values are accurate. However, the y-axis ranges from 0 to 5. This means that it is not showing the densities (they should all be less than 1) Jul 21, 2017 at 18:38
  • 1
    If the y-axis range goes above 1 it does not mean that it isn't a density. It's the integral over the space (or R, the real line) that must be equal to 1. Jul 21, 2017 at 18:46
  • 1
    @user5211911 Why do you think density values must be below one? They are not probabilities, and can be greater than 1 (or even infinite).
    – juan
    Jul 21, 2017 at 18:47
  • 1
    Here's a nice example, part of a good discussion about interpreting density values.
    – juan
    Jul 21, 2017 at 19:11

2 Answers 2

3

The following function generates (pseudo) random numbers for any distribution as long as you have both 'd' and 'p' functions available. In R parlance this means you must have dnorm and pnorm, which you do, they're part of base R.

#
# random numbers for truncated distribution
#
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, ...)
}

# Example:
x <- rtrunc(1, "norm", lower = 0, mean = 2, sd = 5)
2
  • hey @rui, this generates pseudo random no.s but does not give the density. I wouldn't like random numbers from the distribution, I need to be able to choose any value in the sample space of the truncated norm. dist. and then to get the respective density for that value. Jul 21, 2017 at 18:42
  • For the density, see the comment by juan above, and my answer to your answer. Jul 21, 2017 at 18:47
1

I have found a solution to this problem.

This can be done using the dtruncnorm function from the truncnorm package as suggested previously by juan. I'll demonstrate this using the example in the question of a normal distribution with mean 5, standard deviation of 1 and limits of 1 and 10.

First you must create a vector of the number of points to plot from the distribution, that will be equally spaced. So for example, if you wanted to plot ten points from the distribution equally spaced, your vector ("vec") would be:

vec=seq(from=1,by=1,length.out = 10)

The above will ensure that we plot 10 points starting from 1, incrementing by 1, up to value 10.

We then put these in the dtruncnorm() function and save it in the "test" variable, and then plot it:

test=dtruncnorm(vec,a=1,b=10,mean=5,sd=1)
plot(test)

As you see, this plots the density at discrete points. You could easily try and make the plot continuous by increasing the number of points:

vec=seq(from=1,by=0.1,length.out = 100)
test=dtruncnorm(vec,a=1,b=10,mean=5,sd=1)
plot(test)

Please comment if you have any questions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.