未知母體標準差(Population Std. Deviation Unknown)

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) { myT <- tU((1 - confidenceInterval)/2, 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)) } ############################################################################################################# # 已知 upperTailProbability <- 0.025 degreeOfFreedom <- 12 # 求算 print(sprintf("t>=%.4f 自由度=%2d t=%+.4f", upperTailProbability, degreeOfFreedom, tU(upperTailProbability, degreeOfFreedom))) # 已知 lowerTailProbability <- 0.05 degreeOfFreedom <- 50 # 求算 print(sprintf("t<=%.4f 自由度=%2d t=%+.4f", lowerTailProbability, degreeOfFreedom, tL(lowerTailProbability, degreeOfFreedom))) # 已知 upperTailProbability <- 0.01 degreeOfFreedom <- 30 # 求算 print(sprintf("t>=%.4f 自由度=%2d t=%+.4f", upperTailProbability, degreeOfFreedom, tU(upperTailProbability, degreeOfFreedom))) # 已知 confidenceInterval <- 0.9 upperTailProbability <- (1 - confidenceInterval) / 2 degreeOfFreedom <- 25 # 求算 print(sprintf("t>=%.4f 自由度=%2d t=%+.4f", upperTailProbability, degreeOfFreedom, tU(upperTailProbability, degreeOfFreedom))) # 已知 confidenceInterval <- 0.95 upperTailProbability <- (1 - confidenceInterval) / 2 degreeOfFreedom <- 45 # 求算 print(sprintf("t>=%.4f 自由度=%2d t=%+.4f", upperTailProbability, degreeOfFreedom, tU(upperTailProbability, degreeOfFreedom))) ############################################################################################################# # 已知 sampleList <- c(10, 8, 12, 15, 13, 11, 6, 5) confidenceInterval <- 0.95 # 求算 sampleMean <- SampleMean(sampleList) sampleStdDeviation <- SampleStdDeviation(sampleList) sampleSize <- length(sampleList) populationStdDeviation <- sampleStdDeviation * sqrt(sampleSize) print(sprintf("樣本平均值=%.4f 樣本標準差=%.4f 母體標準差=%.4f", sampleMean, sampleStdDeviation, populationStdDeviation)) marginError <- MarginError(sampleStdDeviation, sampleSize, confidenceInterval) PrintOut(sampleSize, sampleMean, sampleStdError, confidenceInterval, marginError) ############################################################################################################# # 已知 sampleMean <- 22.5 samplStdDeviation <- 4.4 sampleSize <- 54 # 求算 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) # 求算 confidenceInterval <- 0.99 marginError <- MarginError(sampleStdDeviation, sampleSize, confidenceInterval) PrintOut(sampleSize, sampleMean, sampleStdError, confidenceInterval, marginError)

應用