青少年速食店花費超過平均值之假設檢定

場景

    • 根據調查芝加哥 102 位青少年平均在速食店花費為 $5.98 美元,標準差為 $1.24 美元。

    • 美國青少年平均在速食店花費為 $5.72 美元。

    • 顯著水準為 0.05。

問題

    • 是否芝加哥青少年平均在速食店花費高於美國平均在速食店花費?

GNU R

# Hypothesis Testing HypothesisTestingZfromPopulation <- function(populationMean, populationStdDeviation, sampleSize, sampleMean) { zTestValue <- (sampleMean - populationMean) / (populationStdDeviation / sqrt(sampleSize)) zTestValue } HypothesisTestingZfromSample <- function(populationMean, sampleStdDeviation, sampleSize, sampleMean) { zTestValue <- (sampleMean - populationMean) / (sampleStdDeviation / sqrt(sampleSize)) zTestValue } # 左尾 HypothesisLeftTailedTestingCriticalValue <- function(levelOfSignificance) { zApha <- levelOfSignificance criticalValue <- qnorm(zApha) print(sprintf("Reject H0, if z < %.4f", criticalValue)) criticalValue } HypothesisLeftTailedTestingT <- function(levelOfSignificance, degreeOfFreedom) { tApha <- levelOfSignificance tValue <- qt(tApha, degreeOfFreedom) print(sprintf("Reject H0, if t < %.4f", tValue)) tValue } HypothesisLeftTailedTestingP <- function(zTestValue) { pValue <- pnorm(zTestValue) pValue } HypothesisLeftTailedTestingZAgainstNull <- function(z, zApha) { # Rejected: if z < z(α) reject <- (z < zApha) reject } HypothesisLeftTailedTestingPAgainstNull <- function(pValue, levelOfSignificance) { # Rejected: if p < α reject <- (pValue < levelOfSignificance) reject } HypothesisLeftTailedTestingTAgainstNull <- function(tValue, tAlpha) { # Rejected: if t < t(α) reject <- (tValue < tAlpha) reject } HypothesisLeftTailedTestingAgainstNull <- function(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) { if (HypothesisLeftTailedTestingZAgainstNull(zTestValue, zAlpha)) { print(sprintf("Rejected: (z=%.4f) < (Null=%.4f)", zTestValue, zAlpha)) } else { print(sprintf("Not Rejected: (z=%.4f) >= (Null=%.4f)", zTestValue, zAlpha)) } if (HypothesisLeftTailedTestingPAgainstNull(pValue, levelOfSignificance)) { print(sprintf("Rejected: (p-value=%.4f) < (α=%.4f)", pValue, levelOfSignificance)) } else { print(sprintf("Not Rejected: (p-value=%.4f) >= (α=%.4f)", pValue, levelOfSignificance)) } if (HypothesisLeftTailedTestingTAgainstNull(zTestValue, tAlpha)) { print(sprintf("Rejected: (t=%.4f) < (Null=%.4f)", zTestValue, tAlpha)) } else { print(sprintf("Not Rejected: (t=%.4f) >= (Null=%.4f)", zTestValue, tAlpha)) } } # 右尾 HypothesisRightTailedTestingCriticalValue <- function(levelOfSignificance) { zApha <- 1 - levelOfSignificance criticalValue <- qnorm(zApha) print(sprintf("Reject H0, if z > %.4f", criticalValue)) criticalValue } HypothesisRightTailedTestingT <- function(levelOfSignificance, degreeOfFreedom) { tApha <- levelOfSignificance tValue <- qt(tApha, degreeOfFreedom, lower.tail=FALSE) print(sprintf("Reject H0, if t > %.4f", tValue)) tValue } HypothesisRightTailedTestingP <- function(zTestValue) { pValue <- 1 - pnorm(zTestValue) pValue } HypothesisRightTailedTestingZAgainstNull <- function(z, zApha) { # Rejected: if z > z(α) reject <- (z > zApha) reject } HypothesisRightTailedTestingPAgainstNull <- function(pValue, levelOfSignificance) { # Rejected: if p < α reject <- (pValue < levelOfSignificance) reject } HypothesisRightTailedTestingTAgainstNull <- function(tValue, tAlpha) { # Rejected: if t < t(α) reject <- (tValue > tAlpha) reject } HypothesisRightTailedTestingAgainstNull <- function(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) { if (HypothesisRightTailedTestingZAgainstNull(zTestValue, zAlpha)) { print(sprintf("Rejected: (z=%.4f) > (Null=%.4f)", zTestValue, zAlpha)) } else { print(sprintf("Not Rejected: (z=%.4f) <= (Null=%.4f)", zTestValue, zAlpha)) } if (HypothesisRightTailedTestingPAgainstNull(pValue, levelOfSignificance)) { print(sprintf("Rejected: (p-value=%.4f) < (α=%.4f)", pValue, levelOfSignificance)) } else { print(sprintf("Not Rejected: (p-value=%.4f) >= (α=%.4f)", pValue, levelOfSignificance)) } if (HypothesisRightTailedTestingTAgainstNull(zTestValue, tAlpha)) { print(sprintf("Rejected: (t=%.4f) > (Null=%.4f)", zTestValue, tAlpha)) } else { print(sprintf("Not Rejected: (t=%.4f) <= (Null=%.4f)", zTestValue, tAlpha)) } } # 雙尾 HypothesisBothTailedTestingCriticalValue <- function(levelOfSignificance) { zApha <- levelOfSignificance criticalValue <- qnorm(zApha / 2) print(sprintf("Reject H0, if z < %.4f or z > %.4f", 0 - abs(criticalValue), abs(criticalValue))) criticalValue } HypothesisBothTailedTestingP <- function(zTestValue) { pValue <- 2 * pnorm(zTestValue) pValue } HypothesisBothTailedTestingT <- function(levelOfSignificance, degreeOfFreedom) { tApha <- levelOfSignificance tValue <- qt(tApha / 2, degreeOfFreedom) print(sprintf("Reject H0, if t < %.4f or t > %.4f", 0 - abs(tValue), abs(tValue))) tValue } HypothesisBothTailedTestingZAgainstNull <- function(z, zApha) { # Rejected: if z < z(α) reject <- (z < zApha) reject } HypothesisBothTailedTestingPAgainstNull <- function(pValue, levelOfSignificance) { # Rejected: if p < α reject <- (pValue < levelOfSignificance) reject } HypothesisBothTailedTestingTAgainstNull <- function(tValue, tAlpha) { # Rejected: if t < tα reject <- (tValue < tAlpha) reject } HypothesisBothTailedTestingAgainstNull <- function(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) { if (HypothesisBothTailedTestingZAgainstNull(zTestValue, zAlpha)) { print(sprintf("Rejected: (z=%.4f) < (Null=%.4f)", zTestValue, zAlpha)) } else { print(sprintf("Not Rejected: (z=%.4f) >= (Null=%.4f)", zTestValue, zAlpha)) } if (HypothesisBothTailedTestingPAgainstNull(pValue, levelOfSignificance)) { print(sprintf("Rejected: (p-value=%.4f) < (α=%.4f)", pValue, levelOfSignificance)) } else { print(sprintf("Not Rejected: (p-value=%.4f) >= (α=%.4f)", pValue, levelOfSignificance)) } if (HypothesisBothTailedTestingTAgainstNull(zTestValue, tAlpha)) { print(sprintf("Rejected: (t=%.4f) < (Null=%.4f)", zTestValue, tAlpha)) } else { print(sprintf("Not Rejected: (t=%.4f) >= (Null=%.4f)", zTestValue, tAlpha)) } } ###################################################################################################### # 分析 (18) # H0: μ <= 5.72 # Ha: μ > 5.72 populationMean <- 5.72 sampleSize <- 102 sampleMean <- 5.98 sampleStdDeviation <- 1.24 levelOfSignificance <- 0.05 # 求算 zAlpha <- HypothesisRightTailedTestingCriticalValue(levelOfSignificance) tAlpha <- HypothesisRightTailedTestingT(levelOfSignificance, sampleSize - 1) zTestValue <- HypothesisTestingZfromSample(populationMean, sampleStdDeviation, sampleSize, sampleMean) pValue <- HypothesisRightTailedTestingP(zTestValue) HypothesisRightTailedTestingAgainstNull(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) # Reject H0, if z > 1.6449 # Reject H0, if t > 1.6601 # Rejected: (z=2.1176) > (Null=1.6449) # Rejected: (p-value=0.0171) < (α=0.0500) # Rejected: (t=2.1176) > (Null=1.6601)