随机在1和2两地抽取家蝇,测量翅膀长度,一共四十个样本,数字特征分别为μ1和μ2。问题:两地家蝇翅膀长度是否有差异?
H0假设:μ1=μ2,即两地家蝇翅膀长度无显著差异
Ha假设:μ1≠μ2,即两地家蝇翅膀长度有显著差异
α=0.05
# 输入数据
lines <- "ID length site\n1 45 1\n2 44 1\n3 45 2\n4 43 1\n5 48 2\n6 47 2\n7 47 1\n8 48 1\n9 43 2\n10 44 1\n11 46 2\n12 46 1\n13 44 1\n14 47 2\n15 40 1\n16 48 2\n17 45 1\n18 46 2\n19 42 1\n20 43 2\n21 40 1\n22 43 1\n23 49 2\n24 46 1\n25 47 1\n26 46 2\n27 43 2\n28 45 1\n29 46 1\n30 47 2\n31 46 2\n32 47 2\n33 45 1\n34 46 2\n35 43 1\n36 45 2\n37 44 1\n38 46 2\n39 44 2\n40 45 2"
wl <- read.table(con <- textConnection(lines), header = TRUE)
close(con)
# 检查数据
wl
## ID length site
## 1 1 45 1
## 2 2 44 1
## 3 3 45 2
## 4 4 43 1
## 5 5 48 2
## 6 6 47 2
## 7 7 47 1
## 8 8 48 1
## 9 9 43 2
## 10 10 44 1
## 11 11 46 2
## 12 12 46 1
## 13 13 44 1
## 14 14 47 2
## 15 15 40 1
## 16 16 48 2
## 17 17 45 1
## 18 18 46 2
## 19 19 42 1
## 20 20 43 2
## 21 21 40 1
## 22 22 43 1
## 23 23 49 2
## 24 24 46 1
## 25 25 47 1
## 26 26 46 2
## 27 27 43 2
## 28 28 45 1
## 29 29 46 1
## 30 30 47 2
## 31 31 46 2
## 32 32 47 2
## 33 33 45 1
## 34 34 46 2
## 35 35 43 1
## 36 36 45 2
## 37 37 44 1
## 38 38 46 2
## 39 39 44 2
## 40 40 45 2
head(wl)
## ID length site
## 1 1 45 1
## 2 2 44 1
## 3 3 45 2
## 4 4 43 1
## 5 5 48 2
## 6 6 47 2
x = wl[wl$site == 1, 2] # 样地1数据
y = wl[wl$site == 2, 2] # 样地2数据
# 检查前提条件
shapiro.test(x) # 正态性检验
##
## Shapiro-Wilk normality test
##
## data: x
## W = 0.9522, p-value = 0.4017
shapiro.test(y)
##
## Shapiro-Wilk normality test
##
## data: y
## W = 0.9397, p-value = 0.2363
# Bartlett Test方差齐性检验(参数)
bartlett.test(length ~ site, data = wl)
##
## Bartlett test of homogeneity of variances
##
## data: length by site
## Bartlett's K-squared = 0.9775, df = 1, p-value = 0.3228
# Figner-Killeen Test方差齐性检验(非参数)
fligner.test(length ~ site, data = wl)
##
## Fligner-Killeen test of homogeneity of variances
##
## data: length by site
## Fligner-Killeen:med chi-squared = 0.9569, df = 1, p-value = 0.328
# t检验
t.test(x, y, var.equal = F) # 两样本t检验
##
## Welch Two Sample t-test
##
## data: x and y
## t = -2.462, df = 36.14, p-value = 0.01874
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2.7357 -0.2643
## sample estimates:
## mean of x mean of y
## 44.35 45.85
基于1和2个地点t检验的p-value = 0.01874 < 0.05,因此拒绝H0假设,即认为两地地家蝇翅膀长度有显著差异。