r - Generating random numbers from a truncated Gaussian -
Like most physical problems, facing the limitations of my case, I want to generate random numbers (with R) in this way as a small Gaussian distribution.
The idea is that these numbers should not depend on the value range. I have already found the package trunkrom, but this does not work: For example, here a Gaussian meaning is 0.1 and width is 0.1, but it is constrained between 0 and 1:
< (Vec, breaks = 100) mean (vec): code> install.packages ("truncnorm") library (truncnorm) vec = rtruncnorm (n = 100000, a = 0, b = 1, mean = 0.1, sd = 0.1) [1] 0.1289061As you can see, the final means is not the given one as input, I can have the same result by using the standard rnorm function and on subseting Consequence
I do not want to change the wheel again, so any ideas or suggestions of more packages will be welcomed! Thanks!
Therefore we may have to differentiate between the original values before and after the transaction, and you may be exposed In order to control sample specimens, the estimated values would probably be united, although
rnorm () (and probably
rtruncnorm () , which I do not know) hope Is "first"; While some statisticians at stats.stackexchange.com can provide you with a more rigorous analytical solution, you may also need to customize this code based on "first" -payment (you may have to "first"), some flawless optimization - SD-parameter should also be modified):
myrtruncnorm & lt; - function (n, a, b, mean = 0, sd = 1) qnorm (runif (n, pnorm (a, mean) = mean, sd = sd), pnorm (b, mean = mean, sd = sd) , mean = mean, sd = sd) set.seed (1) optim (list (mean = .1, sd = 1), (x) [1]], sd = x [[2]]) - 1) ) # Returns Meaning = 0.07785390 and SD = 0.07777597, check this: x1 & lt; - myrtruncnorm (100000,0,1,0.07785390,0.07777597) hist (x1); Meaning (x1) # Does "mean = 0.1003832" stop enough?
Comments
Post a Comment