###########Interval Estimation################################################
library(MASS)
head(survey)
help(survey)
################################
#
height.survey = survey$Height
mean(height.survey)
mean(height.survey, na.rm=TRUE)
################################
# #Assume the population standard deviation of the student height in survey is 9.48.
# Find the margin of error and interval estimate at 95% confidence level.
nrow(survey)
height.response = na.omit(survey$Height)
n = length(height.response)
sigma = 9.48 # population standard deviation
sem = sigma/sqrt(n); sem # standard error of the mean
E = qnorm(.975)*sem; E # margin of error
xbar = mean(height.response);xbar # sample mean
xbar + c(-E, E)
library(TeachingDemos)
z.test(height.response, sd=sigma)
################################
# Without assuming the population standard deviation of the
# student height in survey, find the margin of error and interval
# estimate at 95% confidence level.
# if population std deviation is unknown follow t distribuion
height.response = na.omit(survey$Height)
n = length(height.response);n
s = sd(height.response);s # sample standard deviation
SE = s/sqrt(n); SE # standard error estimate
E = qt(.975, df=n-1)*SE; E # margin of error
xbar = mean(height.response) # sample mean
xbar + c(-E, E)
# t.test(height.response)
################################
# Assume the population standard deviation
# of the student height in survey is 9.48.
# Find the sample size needed to achieve a 1.2 centimeters margin of error
# at 95% confidence level.
zstar = qnorm(.975)
sigma = 9.48
E = 1.2
zstar^2 * sigma^2/ E^2
################################
# Find a point estimate of the female student proportion from survey.
gender.response = na.omit(survey$Sex)
n = length(gender.response)
k = sum(gender.response == "Female")
pbar = k/n; pbar
################################
# Compute the margin of error and estimate interval for the female students
# proportion in survey at 95% confidence level.
gender.response = na.omit(survey$Sex)
n = length(gender.response) # valid responses count
k = sum(gender.response == "Female");k
pbar = k/n; pbar
SE = sqrt(pbar*(1-pbar)/n); SE # standard error
E = qnorm(.975)*SE; E
pbar + c(-E, E)
prop.test(k, n)
################################
# Using a 50% planned proportion estimate, find the sample size needed to achieve 5% margin of error
# for the female student survey at 95% confidence level.
zstar = qnorm(.975)
p = 0.5
E = 0.05
zstar^2 * p * (1-p) / E^2
s