On 09-Jun-08 13:14:02, "Antje Schafföner" wrote: > Hello, > I am trying to calculate and plot mean and confidence intervall for a > set of data. This is the code that I am currently using: > > > means <- sapply(data, mean, na.rm=TRUE) > n <- sapply(data,length) > stdev <- sqrt(sapply(data, var, na.rm=TRUE)) > ciw <- qt(0.98, n) * stdev / sqrt(n) > par(mgp=c(2,0.6,0), las=2, fin=c(7,3), mai=c(1,0.5,0.2,0.2), cex=0.8) > plotCI(x=means, uiw=ciw, ylim=c(0,100), > col="red",col.axis="black",add=F) > > > The code works fine, just the results are not as expected. I compared > the results that R calculates for my variable ciw and noticed > differences compared to other calculations. I suppose that the reason > can be found in the value that qt(0.98, n) calculates.
You don't give an explicit example of the answer you were expecting, and the answer you got. However, there are two points in your code above which, I think, need correcting. 1. For a 95% confidence interval, use qt(0.975,n), not qt(0.98,n). Why did you choose 0.98? 2. The number of degrees of freedom for qt() should be (n-1), not n as you used. For example, if n=10: qt(0.98,10) # [1] 2.359315 qt(0.975,10) # [1] 2.228139 (qt(0.98,10)-qt(0.975,10))/qt(0.975,10) # [1] 0.05887235 so almost 6% error in using 0.98 instead of 0.975 (qt(0.98,10)-qt(0.98,9))/qt(0.98,9) # [1] -0.01631325 so 1.6% error (in the opposite direction) in using df=10 instead of 9. qt(0.975,9) # [1] 2.262157 (qt(0.98,10)-qt(0.975,9))/qt(0.975,9) # [1] 0.04294903 so 4.3% error using (0.98,10) instead of (0.975,9). Regarding your point below, about "getting the two-side result from qt()", for a 5% 2-sided CI you need 2.5% at each end; and the t-distribution is symmetrical, so qt(0.975,df) will do it. The other end is at -qt(0.975,df), so the total width would be 2*qt(0.975,df); so (putting this all together) I think you should have written ciw <- 2 * qt(0.975, n-1) * stdev / sqrt(n) Hoping this helps, Ted. > I compared the results of qt with the values I would expect and found > out that it returns the values of the student distribution for a > one-sided question (eg. qt(0.99, 10) = 2.76). Is there a way to use qt > to calcute the quantiles for a two-sided problem? Or is there a > different function I could use in a similar way as qt? > > Thanx, Antje > > -- > > Jetzt dabei sein: http://www.shortview.de/[EMAIL PROTECTED] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. -------------------------------------------------------------------- E-Mail: (Ted Harding) <[EMAIL PROTECTED]> Fax-to-email: +44 (0)870 094 0861 Date: 09-Jun-08 Time: 15:10:40 ------------------------------ XFMail ------------------------------ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.