You are on page 1of 3

AST231_L6_CI

Mohaimen Mansur

Confidence Interval for population mean

# 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)

# 99% Confidence interval (sigma known)


CL <- 0.99
alpha <- 1- CL
alpha_by2 <- alpha/2
z_alpha2 <- qnorm(alpha_by2, mean=0, sd=1, lower.tail=FALSE)

lower<- meanx-z_alpha2*sigma/sqrt(n)
upper<- meanx+z_alpha2*sigma/sqrt(n)

c(lower,upper)

## [1] 99.39855 101.02764

# 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)

# 99% Confidence interval(sigma unknown)


CL <- 0.99
alpha <- 1- CL
alpha_by2 <- alpha/2
t_alpha2 <- qt(alpha_by2, df=n-1, lower.tail=FALSE)

lower<- meanx-t_alpha2*sdevx/sqrt(n)
upper<- meanx+t_alpha2*sdevx/sqrt(n)

c(lower,upper)

1
## [1] 99.40864 101.01755

Interpretation of 95 % CI for population mean

n <- 100
mu <- 100
sigma <- 10
nint<-10000

means <- rep(NA,nint)


lower <- rep(NA,nint)
upper <- rep(NA,nint)
# sample size and parameters
for(i in 1:nint) {
x <- rnorm(n,mu,sigma)
# generate a random sample
means[i] <- mean(x)
# calculate mean for each sample
lower[i] <- means[i] - 1.96*sigma/sqrt(n)
# calculate lower bound of interval
upper[i] <- means[i] + 1.96*sigma/sqrt(n)
} # calculate upper bound of interval

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)

Confidence Interval for population proportion(p)

# sample proportion (phat)


# the sampling distribution of phat is approximately normal if sample size
# is large (n*phat>10 and n*(1-phat)>10))

n <- 1000
trial<- 1
prob<- 0.7 # probability/population proportion of a success

2
nint<-1000

prop <- rep(NA,nint)


lower <- rep(NA,nint)
upper <- rep(NA,nint)
# sample size and parameters
for(i in 1:nint) {
x <- rbinom(n,trial,prob)
# generate a random sample
prop[i] <- mean(x)
# calculate mean for each sample
lower[i] <- prop[i] - 1.96*sqrt(prop[i]*(1-prop[i])/n)
# calculate lower bound of interval
upper[i] <- prop[i] + 1.96*sqrt(prop[i]*(1-prop[i])/n)
} # calculate upper bound of interval

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

You might also like