Thomas Schu
2012-Nov-23 15:40 UTC
[R] Student-t distributed random value generation within a confidence interval?
Dear R-users! 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. Does somebody of you know, how i can overcome this problem? Best regards Thomas -- View this message in context: http://r.789695.n4.nabble.com/Student-t-distributed-random-value-generation-within-a-confidence-interval-tp4650561.html Sent from the R help mailing list archive at Nabble.com.
Ben Bolker
2012-Nov-23 19:41 UTC
[R] Student-t distributed random value generation within a confidence interval?
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.