假設檢定(Hypothesis Testing)

GNU R:

# 假設檢定 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) { if (zTestValue < 0) { pValue <- pnorm(zTestValue) } else { pValue <- 1 - pnorm(zTestValue) } pValue <- 2 * 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 < 0 - abs(zApha)) || (z > abs(zAlpha)) reject } HypothesisBothTailedTestingPAgainstNull <- function(pValue, levelOfSignificance) { # Rejected: if p < α reject <- (pValue < levelOfSignificance) || (pValue > 1 - levelOfSignificance) reject } HypothesisBothTailedTestingTAgainstNull <- function(tValue, tAlpha) { # Rejected: if t < tα reject <- (tValue < 0 - abs(tAlpha)) || (tValue > abs(tAlpha)) reject } HypothesisBothTailedTestingAgainstNull <- function(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) { if (zTestValue < 0) { zAlpha <- 0 - abs(zAlpha) tAlpha <- 0 - abs(tAlpha) } else { zAlpha <- abs(zAlpha) tAlpha <- abs(tAlpha) } 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)) } } ###################################################################################################### # 分析 (10) # H0: μ <= 15 # Ha: μ > 15 populationMean <- 15 sampleSize <- 40 sampleMean <- 16.5 sampleStdDeviation <- 7 levelOfSignificance <- 0.02 # 求算 print("-10-") 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.0537 # Reject H0, if t > 2.1247 # Not Rejected: (z=1.3553) <= (Null=2.0537) # Not Rejected: (p-value=0.0877) >= (α=0.0200) # Not Rejected: (t=1.3553) <= (Null=2.1247) ###################################################################################################### # 分析 (12) # H0: μ <= 5 # Ha: μ > 5 populationMean <- 5 levelOfSignificance <- 0.05 zTestValue <- c(1.82, 0.45, 1.50, 3.30, -1.00) # 求算 print("-12-") zAlpha <- HypothesisRightTailedTestingCriticalValue(levelOfSignificance) for (myZ in zTestValue) { pValue <- HypothesisRightTailedTestingP(myZ) if (HypothesisRightTailedTestingZAgainstNull(myZ, zAlpha)) { print(sprintf("Rejected: (z=%.4f) > (Null=%.4f)", myZ, zAlpha)) } else { print(sprintf("Not Rejected: (z=%.4f) < (Null=%.4f)", myZ, 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)) } } # Reject H0, if z > 1.6449 # Rejected: (z=1.8200) > (Null=1.6449) # Rejected: (p-value=0.0344) < (α=0.0500) # Not Rejected: (z=0.4500) < (Null=1.6449) # Not Rejected: (p-value=0.3264) > (α=0.0500) # Not Rejected: (z=1.5000) < (Null=1.6449) # Not Rejected: (p-value=0.0668) > (α=0.0500) # Rejected: (z=3.3000) > (Null=1.6449) # Rejected: (p-value=0.0005) < (α=0.0500) # Not Rejected: (z=-1.0000) < (Null=1.6449) # Not Rejected: (p-value=0.8413) > (α=0.0500) ###################################################################################################### # 分析 (22) # H0: μ = 15 # Ha: μ != 15 # 已知 populationMean <- 15 sampleStdDeviation <- 5 levelOfSignificance <- 0.02 sampleMean <- 14.2 sampleSize <- 50 # 求算 print("-22-") zAlpha <- HypothesisBothTailedTestingCriticalValue(levelOfSignificance) tAlpha <- HypothesisBothTailedTestingT(levelOfSignificance, sampleSize - 1) zTestValue <- HypothesisTestingZfromSample(populationMean, sampleStdDeviation, sampleSize, sampleMean) pValue <- HypothesisBothTailedTestingP(zTestValue) HypothesisBothTailedTestingAgainstNull(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) # Reject H0, if z < -2.3263 or z > 2.3263 # Reject H0, if t < -2.4049 or t > 2.4049 # Not Rejected: (z=-1.1314) >= (Null=-2.3263) # Not Rejected: (p-value=0.2579) >= (α=0.0200) # Not Rejected: (t=-1.1314) >= (Null=-2.4049) ###################################################################################################### # 分析 (24) # H0: μ = 39.2 # Ha: μ != 39.2 # 已知 populationMean <- 39.2 sampleStdDeviation <- 4.8 levelOfSignificance <- 0.05 sampleMean <- 38.5 sampleSize <- 112 # 求算 print("-24-") zAlpha <- HypothesisBothTailedTestingCriticalValue(levelOfSignificance) tAlpha <- HypothesisBothTailedTestingT(levelOfSignificance, sampleSize - 1) zTestValue <- HypothesisTestingZfromSample(populationMean, sampleStdDeviation, sampleSize, sampleMean) pValue <- HypothesisBothTailedTestingP(zTestValue) HypothesisBothTailedTestingAgainstNull(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) # Reject H0, if z < -1.9600 or z > 1.9600 # Reject H0, if t < -1.9816 or t > 1.9816 # Not Rejected: (z=-1.5434) >= (Null=-1.9600) # Not Rejected: (p-value=0.1227) >= (α=0.0500) # Not Rejected: (t=-1.5434) >= (Null=-1.9816) ###################################################################################################### # 分析 (34) # H0: μ = 20 # Ha: μ != 20 # 已知 populationMean <- 20 sampleItems <- c(18, 20, 16, 19, 17, 18) levelOfSignificance <- 0.05 # 求算 print("-34-") sampleSize <- length(sampleItems) sampleStdDeviation <- sd(sampleItems) sampleMean <- mean(sampleItems) print(sprintf("Mean=%.4f StdMean=%.4f", sampleMean, sampleStdDeviation)) zAlpha <- HypothesisBothTailedTestingCriticalValue(levelOfSignificance) tAlpha <- HypothesisBothTailedTestingT(levelOfSignificance, sampleSize - 1) zTestValue <- HypothesisTestingZfromSample(populationMean, sampleStdDeviation, sampleSize, sampleMean) pValue <- HypothesisBothTailedTestingP(zTestValue) HypothesisBothTailedTestingAgainstNull(levelOfSignificance, zAlpha, tAlpha, zTestValue, pValue) # Mean=18.0000 StdMean=1.4142 # Reject H0, if z < -1.9600 or z > 1.9600 # Reject H0, if t < -2.5706 or t > 2.5706 # Rejected: (z=-3.4641) < (Null=-1.9600) # Rejected: (p-value=0.0005) < (α=0.0500) # Rejected: (t=-3.4641) < (Null=-2.5706)

應用: