7ESTIMATING 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 proportionn <-950# sample sizesuccess <- n*p_hat # number of success# Calculate a 95% confidence interval for the population proportionresult <-prop.test(success, n, conf.level =0.95)# Extract the confidence intervalconf_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 valuealpha <-1-confidence_level# Find the critical Z-value using qnorm()critical_z <-qnorm (1- alpha/2)# Print the resultcat("Critical Z =", critical_z, "\n")
Critical Z = 1.959964
Margin of error
Code
# Calculate the standard errorstandard_error <-sqrt((p_hat * (1- p_hat)) / n)# Calculate the margin of errormargin_of_error <- critical_z * standard_error# Print the resultcat("E=", margin_of_error, "\n")
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 intervalconf_interval <- result$conf.int# Print the confidence intervalcat("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)
Critical value
Code
confidence_level <-0.99# Confidence level (e.g., 0.99 for 99% confidence)alpha <-1- confidence_leveln <-32# Sample size# Calculate the degrees of freedomdegrees_of_freedom <- n -1# Find the critical t-value using qt()critical_t <-qt(1- alpha/2, df = degrees_of_freedom)# Print the resultcat("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
Margin of error
Code
# Given sample standard deviation (this is s value)sample_standard_deviation <-0.1077# Calculate the standard errorstandard_error <- sample_standard_deviation /sqrt(n)# Calculate the margin of errormargin_of_error <- critical_t * standard_error# Print the resultcat("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
Confidence interval
Code
x_bar<-4.5210# Sample mean# Calculate the lower and upper bounds of the confidence intervallower_bound <- x_bar - margin_of_errorupper_bound <- x_bar + margin_of_error# Print the resultcat("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_levelsample_size <-106# Sample sizedegrees_of_freedom <- sample_size -1# df for the chi-squared distribution# Find the critical values using the chi-squared distributionlower_critical_value <-qchisq(1-alpha/2, df = degrees_of_freedom)upper_critical_value <-qchisq(alpha/2, df = degrees_of_freedom)# Print the resultscat("Lower Critical Value:", lower_critical_value, "\n")