http://netstat.stat.tku.edu.tw
(User from: 54.162.139.105)
機率  |  統計  |  Data Mining  |  資料處理  |  DEMO  |  Data  |  Links  |  Books  |  相關文件  |   ( ENGLISH )

Home

機率
 計算/查表
 隨機亂數
 Density plots
統計
 基本統計
 卡方檢定
 迴歸分析
 ANOVA
 時間數列
 多變量分析
 品質管制
 無母數方法
Data Mining
 決策樹
 Logistic
 判別分析
 集群分析
 類神經網路
 關聯規則分析
 ALL Methods

R 軟體入門: 5.1 Example: 單樣本推論

by Steve Chen ( , 版權所有, 歡迎散佈)

單樣本推論操作

輸入資料檔 (以逗點相隔, CSV 格式)

mydata <- read.csv("mydata.txt",header=T)
引入 tkustat library. 這個 library 中有以下用到的兩個函數
tkustat.ztest 與 tkustat.sigma.test
library(tkustat)
取出 mydata 的 Car 變數, 並存入 car 變數
car <- mydata$Car
設定顯著水準值 alpha 與 Confidence Level 值 : clevel
alpha <- 0.05
clevel <- 1.0 - alpha
母體平均數 Z 檢定 Ho: mu = 12.5
tkustat.ztest(car,mu=12.5,side="two.sided",alpha=alpha)
母體平均數 t 檢定 Ho: mu < 12.5
t.test 是 R 裡面 ctest library 的函數,新版的 R 則直接內建,不需呼叫 ctest
if(!exists("t.test", mode="function")) 
   library(ctest)

t.test(x,alternative="greater",conf.level=clevel)
alternative (H1) 選項有 "two.sided","greater","less"
母體比例檢定 Ho: p < 0.24
prop.test 是 R 裡面 ctest library 的函數

x <- 12
n <- 30
p 的樣本估計量 = 12/30
prop.test(p,n,0.24,alternative="greater",conf.level=clevel)
母體標準差檢定 Ho: sigma > 5.2
tkustat.sigma.test(x,sigma=5.2,side="less",alpha=alpha)
# --------------------------------------------------------------- 附註 1. tkustat.ztest 函數

# ---------------------------------------------------------------
tustat.ztest <- function(x,mu=0,side="two",sigma=0,alpha=0.05)
{
   n <- length(x); xbar <- mean(x)
   use.t <- FALSE
   if (sigma == 0)
   {
     sigma <- sqrt(var(x))
     if (n < 30)
     {
        cat("n < 30 且未提供 sigma 值, 使用 t 檢定 \n\n")
        use.t <- TRUE
     }
   }
   se <- sigma/sqrt(n)
   test.value <- (xbar - mu)/se
   conf.level0 <- 1.0 - alpha
   conf.level2 <- 1.0 - alpha/2
   if (side == "two") alpha <- alpha/2.0
   conf.level <- 1.0 - alpha
   if (use.t)
   {
      pvalue <- 1.0 - pt(test.value,n-1)
      crit   <- qt(conf.level,n-1)
      crit0   <- qt(conf.level2,n-1)
      ttype  <- "t "
   }
   else
   {
      pvalue <- 1.0 - pnorm(test.value)
      crit   <- qnorm(conf.level)
      crit0   <- qnorm(conf.level2)
      ttype  <- "Z "
   }
   if (side == "two")
       result <- abs(test.value) > crit
   else if (side == "left")
       result <- test.value > crit
   else
       result <- test.value < -1.0*crit

   cat(ttype,"統計量檢定值 (test value) = ",test.value,"\n")
   cat("p-value = ",pvalue,"\n")
   cat("查表值 (critical value) = ",crit,"\n")
   if (result)
       cat("\nDecision: Reject Ho \n\n")
   else
       cat("\nDecision: Do not reject Ho \n\n")
   cat("樣本平均數 ",xbar,"  樣本標準差 ",se,"\n\n")
   cat("母體平均數 (mu) 的 ",conf.level0*100,"% 信賴區間 : \n")
   xbar + c(-crit0*se,crit*se)
}

# ---------------------------------------------------------------
附註 2. tkustat.sigma.test 函數

# ---------------------------------------------------------------
tkustat.sigma.test <- function(x,sigma=1.0,side="two",alpha=0.05)
{
   n <- length(x); xbar <- mean(x)
   n1 <- n - 1
   s2 <- var(x)
   se <- sqrt(s2)
   test.value <- n1*s2/(sigma*sigma)
   conf0 <- 1.0 - alpha
   alpha2 <- alpha/2.0
   conf02 <- 1.0 - alpha2
   conf01 <- alpha2
   crit02  <- qchisq(conf02,df=n1)
   crit01  <- qchisq(conf01,df=n1)
   if (side == "left")
   {
       conf.left <- 1 - alpha
       crit2   <- qchisq(conf.left,df=n1)
   }
   else if (side == "right")
   {
       conf.right <- alpha
       crit1   <- qchisq(conf.right,df=n1)
   }
   else
   {
       crit1 <- crit01
       crit2 <- crit02
   }
   #
   pvalue <- 1.0 - pchisq(test.value,df=n1)
   if (side == "two")
       result <- (test.value > crit2) || (test.value < crit1)
   else if (side == "left")
       result <- test.value > crit2
   else
       result <- test.value < crit1

   cat("Chi-Square 統計量檢定值 (test value) = ",test.value,"\n")
   cat("p-value = ",pvalue,"\n")

   if (side == "two")
     cat("查表值 (critical values) = ",crit1," and ",crit2,"\n")
   else if (side == "left")
     cat("查表值 (critical value) = ",crit2,"\n")
   else
     cat("查表值 (critical value) = ",crit1,"\n")

   if (result)
       cat("\nDecision: Reject Ho \n\n")
   else
       cat("\nDecision: Do not reject Ho \n\n")
   cat("樣本變異數 ",s2,"  樣本標準差 ",se,"\n\n")
   cat("母體標準差 (sigma) 的 ",conf0*100,"% 信賴區間 : \n")
   cat(sqrt(n1*s2/crit02),sqrt(n1*s2/crit01),"\n\n")
   cat("母體變異數 (sigma^2)的 ",conf0*100,"% 信賴區間 : \n")
   cat(n1*s2/crit02,n1*s2/crit01,"\n")
}