Reputation: 1304
I would like to generate a random truncated normal distribution with the following properties.
I was able to deal with the first 3 steps but I am struggling to find a way to set mode=3 and to establish a fixed relationship on the occurrence of the higher and lower bound.
library(truncnorm)
library(ggplot2)
set.seed(123)
dist<- as.data.frame(list(truncnorm=round(rtruncnorm(10000, a=3, b=5, mean=3.3, sd=1),1)))
ggplot(dist,aes(x=truncnorm))+
geom_histogram(bins = 40)+
theme_bw()
As you can see I can create truncated normal with the desired boundaries. The problem with this distribution are two.
truncnorm==3.0
to be the mode (i.e. most frequent value of my distribution, while in this case the mode is truncnorm==3.2
truncnorm=3.0
, there should be approximately 400 observations with truncnorm=5.0
.Upvotes: 1
Views: 269
Reputation: 32878
Luckily, all your requirements are achievable using a truncated normal distribution.
Let low = 3
and high = 5
.
Simply evaluate the density (at discrete points such as 3.0, 3.1, ..., 4.9, 5.0) of a normal distribution with mean low
and standard deviation sqrt(2)*(high-low)/(2*sqrt(ln(2))
.
This standard deviation is found by taking the following function proportional to a normal density with mean 0 and standard deviation z
:
f(x) = exp(-(x-0)**2/(2*z**2))
Since f(0) = 1, we must find the necessary standard deviation z
such that f(x) = 1/2
. The solution is:
g(x) = sqrt(2)*x/(2*sqrt(ln(2))
And plugging high-low
into x
leads to the final standard deviation given above.
Upvotes: 3