So we might have to differentiate between mean values before and after truncation, and you apparently intend to control the observable mean values that truncated samples would presumably converge to, although rnorm()
(and probably rtruncnorm()
, which I do not know) expect "before"-means; while some statisticians at stats.stackexchange.com might provide you with a more watertight analytical solution, maybe some playful optimization could also help you find suitable "before"-parameters (you may have to adapt this code depending on whether the "before"-sd-parameter should also be modified):
myrtruncnorm <- 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), function(x)
abs(mean(myrtruncnorm(n=100000,a=0,b=1,mean=x[[1]],sd=x[[2]]))-.1))
# returns mean=0.07785390 and sd=0.07777597, let's test that:
x1 <- myrtruncnorm(100000,0,1,0.07785390,0.07777597)
hist(x1); mean(x1) # Is "mean=0.1003832" sufficiently close?