7  ESTIMATING PARAMETERS AND DETERMINGING SAMPLE SIZES

Author

Wen Tang

r-function Description
prop.test(successes, n, conf.level=0.95) Perform a hypothesis test for a single proportion. Pass a hypothesized (H_0) proportion p if it’s not 0.5. Eg. p=0.6 for H_0. Pass a parameter alternative for alternative hypothesis: “two.sided”(default) ,“less”, “greater”. Note the conf.int given by the test uses Wilson’s method than the Wald method used in the book.
t.test(x, conf.level=0.95) Perform a t-test for a population mean. Accepts an additional alternative argument for H_1. The default hypothesized mean is mu=0. Otherwise, pass a hypothesized mean value.
qt(p, df) Calculate the quantile for the probability p of t-distribution with degree of freedom equal to df
qchisq(p, df) Calculate the quantile for the probability p of \chi^2-distribution with degree of freedom equal to df

7.1 Estimating a population proportion (Page 313 Online Course Example)

7.1.1 Getting the CI directly

Code
p_hat <- 0.53 # 0.53 for 53% sample proportion
n <- 950 # sample size
success <- n*p_hat # number of success

# Calculate a 95% confidence interval for the population proportion
result <- prop.test(success, n, conf.level = 0.95)

# Extract the confidence interval
conf_interval <- result$conf.int
# Print the confidence interval (calculated by the Wilson method)
cat("Confidence Interval:", conf_interval[1], "to", conf_interval[2], "\n")
Confidence Interval: 0.4976792 to 0.5620751 

7.1.2 Getting the CI step by step using the textbook’s Wald’s Method (slightly different result than the result given above)

1.Critical value

Code
# Confidence level (e.g., 0.95 for 95% confidence)
confidence_level <- 0.95  
# get alpha value
alpha <- 1-confidence_level

# Find the critical Z-value using qnorm()
critical_z <- qnorm (1 - alpha/2)
# Print the result
cat("Critical Z =", critical_z, "\n")
Critical Z = 1.959964 
  1. Margin of error
Code
# Calculate the standard error
standard_error <- sqrt((p_hat * (1 - p_hat)) / n)
# Calculate the margin of error
margin_of_error <- critical_z * standard_error
# Print the result
cat("E=", margin_of_error, "\n")
E= 0.03173753 
  1. Confidence interval
Code
# Calculate the confidence interval
confidence_interval <- c (p_hat - margin_of_error,
                          p_hat + margin_of_error)

# Print the confidence interval
cat("Confidence Interval:", confidence_interval[1], "to", confidence_interval[2], "\n")
Confidence Interval: 0.4982625 to 0.5617375 

7.2 Estimating a population mean

7.2.1 Get the CI directly with sample data values given. (Page 343 Mercury question)

In this case, the population \sigma is unknown.

Code
# Calculate a 98% confidence interval for the population mean
#Sample data 
mercury <- c(0.56, 0.75, 0.10, 0.95, 1.25, 0.54, 0.88)
result <- t.test(mercury,conf.level = 0.98)

# Extract the confidence interval
conf_interval <- result$conf.int

# Print the confidence interval
cat("Confidence Interval:", conf_interval[1], "to", conf_interval[2], 
    "\n")
Confidence Interval: 0.2841145 to 1.153028 

7.2.2 Get the CI step by step with given mean and standard deviation (Page 341 Hershey kisses question)

  1. Critical value
Code
confidence_level <- 0.99  # Confidence level (e.g., 0.99 for 99% confidence)
alpha <- 1- confidence_level
n <- 32         # Sample size

# Calculate the degrees of freedom
degrees_of_freedom <- n - 1

# Find the critical t-value using qt()
critical_t <- qt(1 - alpha/ 2, df = degrees_of_freedom)

# Print the result
cat("Critical t-value for dof =", degrees_of_freedom, 
    "and confidence level =", confidence_level, ":", critical_t, "\n")
Critical t-value for dof = 31 and confidence level = 0.99 : 2.744042 
  1. Margin of error
Code
# Given sample standard deviation (this is s value)
sample_standard_deviation <- 0.1077

# Calculate the standard error
standard_error <- sample_standard_deviation / sqrt(n)

# Calculate the margin of error
margin_of_error <- critical_t * standard_error

# Print the result
cat("Margin of Error for confidence level =", confidence_level, 
    "and sample size =", n, ":", margin_of_error, "\n")
Margin of Error for confidence level = 0.99 and sample size = 32 : 0.0522434 
  1. Confidence interval
Code
x_bar<- 4.5210       # Sample mean

# Calculate the lower and upper bounds of the confidence interval
lower_bound <- x_bar - margin_of_error
upper_bound <- x_bar + margin_of_error

# Print the result
cat("Confidence Interval:", lower_bound, "to", upper_bound, "\n")
Confidence Interval: 4.468757 to 4.573243 

7.3 Estimating a population Deviation or Variance (body temperature example page 353)

7.3.1 Critical values

Code
confidence_level <- 0.95  # Confidence level ( 0.95 for 95% confidence)
alpha <- 1- confidence_level
sample_size <- 106          # Sample size
degrees_of_freedom <- sample_size - 1  # df for the chi-squared distribution

# Find the critical values using the chi-squared distribution
lower_critical_value <- qchisq(1-alpha/2, df = degrees_of_freedom)
upper_critical_value <- qchisq(alpha/2, df = degrees_of_freedom)

# Print the results
cat("Lower Critical Value:", lower_critical_value, "\n")
Lower Critical Value: 135.247 
Code
cat("Upper Critical Value:", upper_critical_value, "\n")
Upper Critical Value: 78.5364 

7.3.2 Confidence interval

Code
sample_standard_deviation <- 0.62                  # sample standard deviation s
sample_variance <- sample_standard_deviation^2     # Sample variance

# Calculate the confidence interval for variance
confidence_interval <- c(((sample_size - 1) * sample_variance) / lower_critical_value,
                         ((sample_size - 1) * sample_variance) / upper_critical_value)

# Print the confidence interval
confidence_interval
[1] 0.2984318 0.5139273