F檢驗(yàn)(方差齊性檢驗(yàn)): 主要通過比較兩組數(shù)據(jù)的方差渠驼,以確定他們的密度是否有顯著性差異(判斷兩總體方差是否相等蜈块,就可以用F檢驗(yàn))。
F檢驗(yàn)的前提: 數(shù)據(jù)滿足正態(tài)分布迷扇,使用Shapiro-Will進(jìn)行正態(tài)分布檢驗(yàn)
原文鏈接:https://blog.csdn.net/rojyang/article/details/102900097
# 正態(tài)分布檢驗(yàn)
> shapiro.test(x)
>
statistic
the value of the Shapiro-Wilk statistic.
p.value
an approximate p-value for the test. This is said in Royston (1995) to be adequate for p.value < 0.1.
method
the character string "Shapiro-Wilk normality test".
data.name
a character string giving the name(s) of the data.
### F檢驗(yàn)的實(shí)現(xiàn)
var.test(x, ...)
## Default S3 method:
var.test(x, y, ratio = 1,
alternative = c("two.sided", "less", "greater"),
conf.level = 0.95, ...)
## S3 method for class 'formula'
var.test(formula, data, subset, na.action, ...)
教程2:http://www.reibang.com/p/7e510ac22c64
假設(shè)數(shù)據(jù)服從正態(tài)分布百揭,進(jìn)行F檢驗(yàn)其方差是否一致,從兩研究總體中隨機(jī)抽取樣本谋梭,要對這兩個樣本進(jìn)行比較的時候信峻,首先要判斷兩總體方差是否相同,即方差齊性瓮床。若兩總體方差相等盹舞,則直接用t檢驗(yàn),若不等隘庄,可采用t’檢驗(yàn)或變量變換或秩和檢驗(yàn)等方法踢步。其中要判斷兩總體方差是否相等,就可以用F檢驗(yàn)丑掺。簡單說获印,檢驗(yàn)兩個樣本的方差是否具有顯著性差異(F檢驗(yàn)),這是選擇何種獨(dú)立樣本T經(jīng)驗(yàn)(方差齊時選擇一種T檢驗(yàn)方法街州,方差不齊時選擇一種T檢驗(yàn)方法)的前提條件兼丰。
R中常用的三種F檢驗(yàn)的方法,bartlett.test方差齊性檢驗(yàn)唆缴、var.test方差齊性檢驗(yàn)鳍征、leveneTest方差齊性檢驗(yàn)
前兩者是對原始數(shù)據(jù)的方差進(jìn)行檢驗(yàn)的,leveneTest是對方差模型的殘差進(jìn)行組間齊性檢驗(yàn).一般認(rèn)為是要求殘差的方差齊面徽,所以一般的統(tǒng)計軟件都做的是leveneTest
—————————#獨(dú)立樣本T檢驗(yàn)#——————————————
#有兩種情況艳丛,一種是兩個總體方差齊性匣掸,另一種是兩個總體方差不齊。
#################兩樣本方差齊性
#用高蛋白和低蛋白兩種飼料飼養(yǎng)1月齡的大白鼠氮双,飼養(yǎng)3個月后碰酝,測定兩組大白鼠的增重量(g),兩組數(shù)據(jù)分別如下所示:
#高蛋白組:134,146,106,119,124,161,107,83,113,129,97,123
#低蛋白組:70,118,101,85,107,132,94
#試問兩種飼料養(yǎng)殖的大白鼠增重量是否有顯著差異戴差?
High<-c(134,146,106,119,124,161,107,83,113,129,97,123)
Low<-c(70,118,101,85,107,132,94)
Group<-c(rep(1,12),rep(0,7))#1表示High送爸,0表示Low
x<-c(High,Low)
DATA<-data.frame(x,Group)
DATA$Group<-as.factor(DATA$Group)
#############R中常用的三種F檢驗(yàn)的方法,bartlett.test方差齊性檢驗(yàn)造挽、var.test方差齊性檢驗(yàn)碱璃、leveneTest方差齊性檢驗(yàn)#############
#bartlett.test方差齊性檢驗(yàn)
bartlett.test(x~Group)
Bartlett test of homogeneity of variances
data: x by Group
Bartlett's K-squared = 0.0066764, df = 1, p-value = 0.9349
#var.test方差齊性檢驗(yàn)
var.test(x~Group)
F test to compare two variances
data: x by Group
F = 0.94107, num df = 6, denom df = 11, p-value = 0.9917
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.2425021 5.0909424
sample estimates:
ratio of variances
0.941066
#leveneTest方差齊性檢驗(yàn)(也是SPSS的默認(rèn)方差齊性檢驗(yàn)方法)
library(car)
leveneTest(DATA$x,DATA$Group)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 0.0088 0.9264
17
#前兩者是對原始數(shù)據(jù)的方差進(jìn)行檢驗(yàn)的弄痹,leveneTest是對方差模型的殘差進(jìn)行組間齊性檢驗(yàn).一般認(rèn)為是要求殘差的方差齊饭入,所以一般的統(tǒng)計軟件都做的是leveneTest
#結(jié)果說明兩獨(dú)立樣本數(shù)據(jù)方差齊性,可以進(jìn)行獨(dú)立樣本T檢驗(yàn)肛真。
####即直接進(jìn)行T檢驗(yàn)谐丢。#####
t.test(High,Low,paired=FALSE)
Welch Two Sample t-test
data: High and Low
t = 1.9319, df = 13.016, p-value = 0.07543
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.263671 40.597005
sample estimates:
mean of x mean of y
120.1667 101.0000
結(jié)果表明兩種飼料養(yǎng)殖的大白鼠增重量無顯著差異。
#################兩樣本方差不齊########################
#有人測定了甲乙兩地區(qū)某種飼料的含鐵量(mg/kg)蚓让,結(jié)果如下:
#甲地:5.9,3.8,6.5,18.3,18.2,16.1,7.6
#乙地:7.5,0.5,1.1,3.2,6.5,4.1,4.7
#試問這種飼料含鐵量在兩地間是否有顯著差異乾忱?
JIA<-c(5.9,3.8,6.5,18.3,18.2,16.1,7.6)
YI<-c(7.5,0.5,1.1,3.2,6.5,4.1,4.7)
Content<-c(JIA,YI)
Group<-c(rep(1,7),rep(2,7))#1表示甲地,2表示乙地
data<-data.frame(Content,Group)
data$Group<-as.factor(Group)
###############R中三種F檢驗(yàn)方式##############
#bartlett.test方差齊性檢驗(yàn)
bartlett.test(Content~Group)
Bartlett test of homogeneity of variances
data: Content by Group
Bartlett's K-squared = 3.9382, df = 1, p-value = 0.0472
#var.test方差齊性檢驗(yàn)
var.test(Content~Group)
F test to compare two variances
data: Content by Group
F = 5.9773, num df = 6, denom df = 6, p-value = 0.04695
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
1.02707 34.78643
sample estimates:
ratio of variances
5.9773
#leveneTest方差齊性檢驗(yàn)(也是SPSS的默認(rèn)方差齊性檢驗(yàn)方法)
library(car)
leveneTest(data$Content,data$Group)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 3.073 0.1051
12
#結(jié)果說明兩獨(dú)立樣本數(shù)據(jù)方差不齊历极。
####當(dāng)兩組數(shù)據(jù)方差不齊時窄瘟,可采用t’檢驗(yàn)或變量變換或秩和檢驗(yàn)等方法。
t.test(Content,Group,paired=FALSE,var.equal=FALSE)##此時設(shè)定var.equal=FALSE趟卸,表示方差不齊蹄葱,默認(rèn)是TRUE,方差齊性锄列。
Welch Two Sample t-test
data: Content and Group
t = 3.7511, df = 13.202, p-value = 0.002362
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
2.519419 9.337724
sample estimates:
mean of x mean of y
7.428571 1.500000
#方差齊性檢驗(yàn)表明图云,方差不齊,因此設(shè)定var.equal=FALSE邻邮,此時p=0.0023<0.05竣况,
#表明該飼料在兩地的含鐵量有顯著差異。
但是我發(fā)現(xiàn)這些函數(shù)的輸入都是一組數(shù)據(jù)筒严,那么我的題目只有均值和標(biāo)準(zhǔn)差丹泉,該怎么進(jìn)行F檢驗(yàn)?zāi)亍?/p>
直接使用var.test就可以,生成兩組符合正態(tài)分布的數(shù)據(jù)就行鸭蛙,
接著在進(jìn)行t.test檢驗(yàn)
control<-rnorm(20,0.26,0.22)
> case<-rnorm(20,0.21,0.18)
> var.test(control,case)
F test to compare two variances
data: control and case
F = 1.8451, num df = 19, denom df = 19, p-value = 0.191
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.7303264 4.6616400
sample estimates:
ratio of variances
1.845134
> t.test(control,case,paired = FALSE,var.equal=TRUE)
Two Sample t-test
data: control and case
t = -0.61943, df = 38, p-value = 0.5393
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.16295051 0.08659395
sample estimates:
mean of x mean of y
0.2027261 0.2409044
還有一種題型是列聯(lián)表的摹恨,求置信區(qū)間
#7.1
setwd('/data1/jiarongf/jupyter_projects/other/data/data_txt')
rm(list=ls())
tab <- as.table(cbind(c(2205,2316,4521), c(21358,16663,38021),c(23563,18979,42542))) #創(chuàng)建列聯(lián)表
dimnames(tab) <- list(c("Surviving_adults", "Surviving_children","total"),
c("seriously_injured", "other_injured","total"))
tab
tab_Xsqtest <- chisq.test(tab)
tab_Xsqtest