[R] Confidence Interval

Yawar Amin yawar.amin at gmail.com
Sat Jan 24 23:20:05 CET 2009


rak1304 <rkeyes87 <at> hotmail.com> writes:
> > > I am new to R and Im 
some trouble with the following question... 

I'm starting to study stats and R again after almost a year, so I 
thought this is interesting. I think I have the answer. Here is how I 
arrived at it: 

> > Generate 100 standard normal N(0,1) samples of size 100, 
X1(k),...,X100(k) > where k=1,...,100 (The k is and indicie in brackets) 
> > Calculate the sample mean for each sample. > > For each sample mean 
Xbark the 0.95-confidence interval for the mean mew=0 > is given by... 
> > Ik= ( Xbark plus or minus 1.96/10) > > Find the number of intervals 
such that 0 does not belong to Ik. How many of > them do you expect to 
see? > > Well so far I have come up with... > > N<-100; Nsamp<-100 > 
A<-matrix(rnorm(N*Nsamp,0,1),ncol=Nsamp) > means<-apply(A,2,mean) 

This looks fine. Now you have to figure out I_k, the confidence 
interval. According to your formula for this, the lowest boundary in I_k 
must be XBar_k - 0.196, and the highest boundary XBar_k + 0.196. Now you 
have to figure out how many times 0 falls outside this interval. In R 
terms, XBar_k is your `means' variable. So you want to know how many 
times means - 0.196 > 0 or means + 0.196 < 0. 

A previous poster said that sum() counts the number of times something 
occurs if you're dealing with boolean data i.e. 1s and 0s, or TRUEs and 
FALSEs. R gives you these TRUE/FALSE values. E.g. if you have a variable 
w = 2, running w > 0 will cause R to respond with TRUE. Same goes with 
vector data i.e. if w = c(0,1,2,3,4), then running w > 0 will cause R to 
give you, this time, a vector or TRUEs and FALSEs: FALSE TRUE TRUE TRUE 
TRUE. Then, running sum(w>0) will count only the TRUEs and give you 4. 
(Actually, R pretends it's a vector of 1s and 0s and sums it up: sum(0 1 
1 1 1) is 4.) 

> > However I have no idea what I am doing and no idea if that even 
makes sense. > Any help would be greatly appreciated as I have no 
experience of statistical > software whatsoever. 

I hope this gives you some ideas as to how to turn your textbook 
statistical concepts into R language commands. Thanks to R's vector 
goodness, it's usually simpler than it seems--you don't need to 
overthink it. 

> > Thanks in Advance. > > Rachel 

Yawar




More information about the R-help mailing list