设为首页 加入收藏

TOP

R语言:常用统计检验(一)
2017-10-09 14:30:16 】 浏览:8103
Tags:语言 常用 统计 检验

统计检验是将抽样结果和抽样分布相对照而作出判断的工作。主要分5个步骤:

  1. 建立假设
  2. 求抽样分布
  3. 选择显著性水平和否定域
  4. 计算检验统计量
  5. 判定 —— 百度百科

假设检验(hypothesis test)亦称显著性检验(significant test),是统计推断的另一重要内容,其目的是比较总体参数之间有无差别。假设检验的实质是判断观察到的“差别”是由抽样误差引起还是总体上的不同,目的是评价两种不同处理引起效应不同的证据有多强,这种证据的强度用概率P来度量和表示。除t分布外,针对不同的资料还有其他各种检验统计量及分布,如F分布、X2分布等,应用这些分布对不同类型的数据进行假设检验的步骤相同,其差别仅仅是需要计算的检验统计量不同。

正态总体均值的假设检验

t检验

t.test() => Student's t-Test

require(graphics)

t.test(1:10, y = c(7:20))      # P = .00001855
t.test(1:10, y = c(7:20, 200)) # P = .1245    -- 不在显著
## 经典案例: 学生犯困数据
plot(extra ~ group, data = sleep)

## 传统表达式
with(sleep, t.test(extra[group == 1], extra[group == 2]))

    Welch Two Sample t-test

data:  extra[group == 1] and extra[group == 2]
t = -1.8608, df = 17.776, p-value = 0.07939
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -3.3654832  0.2054832
sample estimates:
mean of x mean of y 
     0.75      2.33 

## 公式形式
t.test(extra ~ group, data = sleep)

    Welch Two Sample t-test

data:  extra by group
t = -1.8608, df = 17.776, p-value = 0.07939
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -3.3654832  0.2054832
sample estimates:
mean in group 1 mean in group 2 
           0.75            2.33 

单个总体

  • 某种元件的寿命X(小时)服从正态分布N(mu,sigma^2),其中mu、sigma^2均未知,16只元件的寿命如下;问是否有理由认为元件的平均寿命大于255小时。
X<-c(159, 280, 101, 212, 224, 379, 179, 264,
222, 362, 168, 250, 149, 260, 485, 170)
t.test(X, alternative = "greater", mu = 225)

    One Sample t-test

data:  X
t = 0.66852, df = 15, p-value = 0.257
alternative hypothesis: true mean is greater than 225
95 percent confidence interval:
 198.2321      Inf
sample estimates:
mean of x 
    241.5 

两个总体

  • X为旧炼钢炉出炉率,Y为新炼钢炉出炉率,问新的操作能否提高出炉率?
X<-c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3)
Y<-c(79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1)
t.test(X, Y, var.equal=TRUE, alternative = "less")

    Two Sample t-test

data:  X and Y
t = -4.2957, df = 18, p-value = 0.0002176
alternative hypothesis: true difference in means is less than 0
95 percent confidence interval:
      -Inf -1.908255
sample estimates:
mean of x mean of y 
    76.23     79.43 

成对数据t检验

  • 对每个高炉进行配对t检验
X<-c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3)
Y<-c(79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1)
t.test(X-Y, alternative = "less")

    One Sample t-test

data:  X - Y
t = -4.2018, df = 9, p-value = 0.00115
alternative hypothesis: true mean is less than 0
95 percent confidence interval:
      -Inf -1.803943
sample estimates:
mean of x 
     -3.2 

正态总体方差的假设检验

var.test() => F Test to Compare Two Variances

x <- rnorm(50, mean = 0, sd = 2)
y <- rnorm(30, mean = 1, sd = 1)
var.test(x, y)                  # x和y的方差是否相同?
var.test(lm(x ~ 1), lm(y ~ 1))  # 相同.
  • 从小学5年级男生中抽取20名,测量其身高(厘米)如下;问:在0.05显著性水平下,平均值是否等于149,sigma^2是否等于75?
X<-scan()
136 144 143 157 137 159 135 158 147 165
158 142 159 150 156 152 140 149 148 155
var.test(X,Y)

    F test to compare two variances

data:  X and Y
F = 34.945, num df = 19, denom df = 9, p-value = 6.721e-06
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
   9.487287 100.643093
sample estimates:
ratio of variances 
          34.94489 
  • 对炼钢炉的数据进行分析
X<-c(78.1,72.4,76.2,74.3,77.4,78.4,76.0,75.5,76.7,77.3)
Y<-c(79.1,81.0,77.3,79.1,80.0,79.1,79.1,77.3,80.2,82.1)
var.test(X,Y)

    F test to compare two variances

data:  X and Y
F = 1.4945, n
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇R 分类进行数值处理 下一篇R 网页数据爬虫1

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目