業務員客戶連繫週報誤差界限計算

場景

    • 隨機調查 65 個業務員客戶連繫週報,平均每週連繫 19.5 個客戶,標準差為 5.2 個客戶。

問題

    • 求算信心區間為 90% 下之誤差界限。(誤差界限=1.0765 [18.4235, 20.5765])

    • 求算信心區間為 95% 下之誤差界限。(誤差界限=1.2885 [18.2115, 20.7885])

GNU R

# 已知母體標準差,求誤差界限 # 樣本平均值 SampleMean <- function(sampleList) { myMean <- mean(sampleList) } # 樣本標準差 SampleStdDeviation <- function(sampleList) { sampleMean <- SampleMean(sampleList) sampleSize <- length(sampleList) mySqrSum <- 0 for (myElement in sampleList) { mySqrSum <- mySqrSum + (myElement - sampleMean) ^ 2 } sampleStdDeviation <- sqrt(mySqrSum / (sampleSize - 1)) sampleStdDeviation } # 樣本誤差 SampleStdError <- function(sampleStdDeviation) { sampleStdError <- sampleStdDeviation sampleStdError } # 從機率與自由度反求 t 值 t <- function(probability, degreeOfFreedom) { myT <- qt(probability, degreeOfFreedom) myT } # 從機率與自由度反求 t 值 tU <- function(upperTailProbability, degreeOfFreedom) { myT <- qt(1 - upperTailProbability, degreeOfFreedom) myT } # 從機率與自由度反求 t 值 tL <- function(lowerTailProbability, degreeOfFreedom) { myT <- qt(lowerTailProbability, degreeOfFreedom) myT } # 誤差界限 MarginError <- function(sampleStdDeviation, sampleSize, confidenceInterval) { upperTailProbability <- (1 - confidenceInterval) / 2 myT <- tU(upperTailProbability, sampleSize - 1) myT2 <- sampleStdDeviation / sqrt(sampleSize) marginError <- myT * myT2 marginError } PrintOut <- function(sampleSize, sampleMean, sampleStdError, confidenceInterval, marginError) { print(sprintf("樣本數=%4d 樣本平均數=%8.4f 樣本標準誤差=%.4f 信任區間=%.4f 誤差界限=%.4f [%8.4f, %8.4f]", sampleSize, sampleMean, sampleStdError, confidenceInterval, marginError, sampleMean-marginError, sampleMean+marginError)) } ############################################################################################################# # 已知 sampleMean <- 19.5 sampleStdDeviation <- 5.2 sampleSize <- 65 # 求算 confidenceInterval <- 0.9 degreeOfFreedom <- sampleSize - 1 sampleStdError <- samplStdDeviation marginError <- MarginError(sampleStdDeviation, sampleSize, confidenceInterval) PrintOut(sampleSize, sampleMean, sampleStdError, confidenceInterval, marginError) # 求算 confidenceInterval <- 0.95 marginError <- MarginError(sampleStdDeviation, sampleSize, confidenceInterval) PrintOut(sampleSize, sampleMean, sampleStdError, confidenceInterval, marginError)