家庭看電視時數超過平均時數之假設檢定

場景

    • 根據調查 200 個家庭平均看電視時數為 7.25 小時,標準差為 2.5 小時。

    • 10 年前家庭平均看電視時數為 6.70 小時。

    • 顯著水準為 0.01。

問題

    • 是否目前家庭平均看電視時數比 10 年前高?

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)) } } ###################################################################################################### # 分析 (14) # H0: μ <= 6.7 # Ha: μ > 6.7 populationMean <- 6.7 sampleSize <- 200 sampleMean <- 7.25 sampleStdDeviation <- 2.5 levelOfSignificance <- 0.01 # 求算 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 > 2.3263 # Reject H0, if t > 2.3452 # Rejected: (z=3.1113) > (Null=2.3263) # Rejected: (p-value=0.0009) < (α=0.0100) # Rejected: (t=3.1113) > (Null=2.3452)