Search code examples
rstatisticsnormal-distributiontruncated

How to plot a truncated normal distribution with known paramters, in R


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?


Solution

  • 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