Professional Documents
Culture Documents
Mohaimen Mansur
# sigma known (we can use normal approximation for sampling dist of xbar)
# Case1: If population is normal
# Case 2: If population is not normal/unknown, but n>=30
n <-1000
mu <- 100
sigma <- 10
x <- rnorm(n,mu,sigma)
meanx<-mean(x)
lower<- meanx-z_alpha2*sigma/sqrt(n)
upper<- meanx+z_alpha2*sigma/sqrt(n)
c(lower,upper)
# sigma unknown (we can use t-distribution for sampling dist of xbar)
# Case1: If population is normal
# Case 2: If population is not normal/unknown, but n>=30
meanx<-mean(x)
sdevx<-sd(x)
lower<- meanx-t_alpha2*sdevx/sqrt(n)
upper<- meanx+t_alpha2*sdevx/sqrt(n)
c(lower,upper)
1
## [1] 99.40864 101.01755
n <- 100
mu <- 100
sigma <- 10
nint<-10000
count<- 0
# count will hold number of intervals do contain the mean mu
for(i in 1:nint) {
if(mu >= lower[i] && mu <= upper[i])
count<- count + 1 }
p <- count/nint
# proportion of intervals do contain the mean mu
p
## [1] 0.9505
## Exercise: For a 97% confidence interval for population mean (when sigma is unknown)
# using the concept of repeated sampling show that approximately 97% of the CIs
# include the true population mean (play with different sample sizes)
n <- 1000
trial<- 1
prob<- 0.7 # probability/population proportion of a success
2
nint<-1000
count<- 0
# count will hold number of intervals do contain the mean mu
for(i in 1:nint) {
if(prob >= lower[i] && prob <= upper[i])
count<- count + 1 }
p <- count/nint
# proportion of intervals do contain the mean mu
p
## [1] 0.946