網民上網時數超過平均時數之假設檢定

場景

    • 根據調查某城市 145 位網民每個月平均上網時數為 10.8 小時,標準差為 9.2 小時。

    • 另一個城市每個月平均上網時數為 13 小時。

    • 顯著水準為 0.01。

問題

    • 是否該城市每個平均上網時數少於另一個城市?

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)) } } ###################################################################################################### # 分析 (16) # H0: μ >= 13 # Ha: μ < 13 populationMean <- 13 sampleSize <- 145 sampleMean <- 10.08 sampleStdDeviation <- 9.2 levelOfSignificance <- 0.01 # 求算 zAlpha <- HypothesisLeftTailedTestingCriticalValue(levelOfSignificance) tAlpha <- HypothesisLeftTailedTestingT(levelOfSignificance, sampleSize - 1) zTestValue <- HypothesisTestingZfromSample(populationMean, sampleStdDeviation, sampleSize, sampleMean) pValue <- HypothesisLeftTailedTestingP(zTestValue) HypothesisLeftTailedTestingAgainstNull(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) # Reject H0, if z < -2.3263 # Reject H0, if t < -2.3525 # Rejected: (z=-3.8219) < (Null=-2.3263) # Rejected: (p-value=0.0001) < (α=0.0100) # Rejected: (t=-3.8219) < (Null=-2.3525)