上一篇整理完非参数检验,这一篇就再进行一个整理参数检验的大动作。
参数检验非常之繁杂,要想完全搞明白任重而道远。
我这边呢,就主要说明什么样子的数据用什么检验方法哈。
常见的参数检验:正态均值检验(T检验),方差检验(ANOVA检验),二项分布总体假设检验。
1、正态均值检验
这个又可以分为单样本、双样本检验,双样本又包括有独立样本和配对样本两种。
单样本:随机样本的均值和总体均值的差异。
独立样本:两组随机样本,分别进行同一种处理。(雄性小鼠和雌性小鼠在喂药之后的体重分布。)
配对样本:对于一组随机样本,进行两次独立的处理。(都是雄性小鼠,喂了两次药,两次的体重分布)
1-1、单样本
iris##R自带的数据
table(iris$Species)
setosa versicolor virginica
50 50 50
colnames(iris)
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
#我们知道setosa的Sepal.Length。
#我们首先计算它的均值
mean(subset(iris,Species=="setosa")$Sepal.Length)
[1] 5.006
#如果用全部50个数据去检验。
#结果P值为1 ,完全接受原假设,表示样本和总体没有差别。
dat <- c(subset(iris,Species=="setosa")$Sepal.Length)
t.test(dat, mu=5.006)
One Sample t-test
data: dat
t = 0, df = 49, p-value = 1
alternative hypothesis: true mean is not equal to 5.006
95 percent confidence interval:
4.905824 5.106176
sample estimates:
mean of x
5.006
#然后所以选取前25个个体的数据,查看这25个数据的均值和总体的均值是否存在显著差异。
#结果也是接受原假设,样本和总体一样的。
dat <- c(subset(iris,Species=="setosa")[1:25,]$Sepal.Length)
t.test(dat, mu=5.006)
One Sample t-test
data: dat
t = 0.27463, df = 24, p-value = 0.786
alternative hypothesis: true mean is not equal to 5.006
95 percent confidence interval:
4.862665 5.193335
sample estimates:
mean of x
5.028
1-2、独立双样本
#我们知道setosa和virginica 的Sepal.Length。
#检测是否符合正态分布。都大于0.05,接受原假设都符合。
shapiro.test(subset(iris, Species=="setosa")$Sepal.Length)
Shapiro-Wilk normality test
data: subset(iris, Species == "setosa")$Sepal.Length
W = 0.9777, p-value = 0.4595
shapiro.test(subset(iris, Species=="virginica")$Sepal.Length)
Shapiro-Wilk normality test
data: subset(iris, Species == "virginica")$Sepal.Length
W = 0.97118, p-value = 0.2583
#那就看这两组之间是否存在显著差异。
#两组比较之前还有一个点需要注意,检验之前我们知道哪些信息?1)两组方差已知;2)方差未知,但是知道两组的方差相等;3)方差未知,但是知道两组方差不相等;4)方差位置,也不知道两组方差是否相等。
#我们平时遇到最多的,应该就是第4种情况。
#首先判断,两组的方差是否相等。
#零假设是方差相等,也就是齐方差。
setosa <- iris[which(iris$Species=='setosa'),] #提取setosa
virginica <- iris[which(iris$Species=='virginica'),] #virginica
mydata <- rbind(setosa,virginica) #按行合并数据集
#install.packages("car")
library("car")
leveneTest(mydata$Sepal.Length,mydata$Species, center=mean)
Levene's Test for Homogeneity of Variance (center = mean)
Df F value Pr(>F)
group 1 13.706 0.0003534 ***
98
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#P值远远小于0.05,拒绝原假设,那就是两组数据方差不相等。
##那么接下来,就按照方差不相等的方法进行检验。
t.test(setosa$Sepal.Length, virginica$Sepal.Length, var.equal = F)
Welch Two Sample t-test
data: setosa$Sepal.Length and virginica$Sepal.Length
t = -15.386, df = 76.516, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.78676 -1.37724
sample estimates:
mean of x mean of y
5.006 6.588
#如果两组数据是方差相等,那就把var.equal = F修改成var.equal = T即可。
1-3、配对样本
#就用数据举例说明吧,假设这里是将setosa的萼片长度和宽度作为两次处理。
#配对检验的结果如下。
t.test(setosa$Sepal.Length, setosa$Sepal.Width, paired=TRUE)
Paired t-test
data: setosa$Sepal.Length and setosa$Sepal.Width
t = 42.323, df = 49, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.503074 1.652926
sample estimates:
mean of the differences
1.578
##拒绝零假设,两次处理差异显著。
2、方差检验(先休息一下吧,过两天再更新。)
网友评论