[R] Student-t distributed random value generation within a confidence interval?

Ben Bolker bbolker at gmail.com
Fri Nov 23 20:41:03 CET 2012


Thomas Schu <th.schumann <at> gmx.de> writes:

> I´m faced with following problem:
> Given is a sample where the sample size is 12, the sample mean is 30, and
> standard deviation is 4.1.
> Based on a Student-t distribution i´d like to simulate randomly 500 possible
> mean values within a two-tailed 95% confidence interval.
> Calculation of the lower and upper limit of the two-tailed confidence
> interval is the easy part.
> 
> m <- 30 #sample mean
> s <- 4.1 #standard deviation
> n <- 12 #sample size
> quant <- qt(0.975,df=11)*s/sqrt(n)#student-t with two tailed )95% confidence
> interval
> l <- m-quant# lower limit
> h <- m+quant# upper limit
> 
> 500 randomly simulated values are computable with the rt() command but this
> command does not consider the 95% confidence interval.

  Perhaps: simulate more values than you need and take a subset:

allvals <- rt(1000,df=11)  
## 1000 samples is overkill: slightly more than 
##    500*(1.05) should be large enough
subvals <- (allvals[abs(allvals)<qt(0.975,df=11)])
vals <- m+subvals[1:500]*s/sqrt(n)

I'm subsetting before transforming, it seems slightly easier.




More information about the R-help mailing list