原理
fisher精确检验是用来检验一次随机试验的结果是否支持对于某个随机试验的假设
例子:

研究是否smoke对得lung cancer的相关性
检验以下假设
H0:吸烟和得肺癌没有关系
H1:吸烟和得肺癌有关系
若假设检验得到的p值具有显著性,则拒绝原假设,也就是吸烟和得肺癌有关系
r语言函数
fisher.test(matrix(c(10,1,6,12),nrow=2))
实验目的
掌握Fisher精确检验算法以及代码实现,研究基因表达情况和样本lable的相关性
实验数据工具及步骤
Exp表达谱和lable文件


实验代码
导入数据
setwd("E:\\实验\\转录组学\\实验二")
lable<-read.csv("Label.csv",header=T)#读样本标签文件
exp<-read.csv("Exp.csv",header=T,sep="\t",as.is=T)
exp<-read.table("Exp.txt",header=T,sep="\t",as.is=T)#读表达谱,行名为分类0/1,无列名,每列数据代表一个样本
str(exp)
xiabiao1<-which(lable[,1]==0)#返回下标
xiabiao2<-which(lable[,1]==1)#返回下标
#统计一下lable,一共有878个样本
table(lable)

#分别把lable=0和1的表达谱提出来
exp1<-exp[,xiabiao1]
exp2<-exp[,xiabiao2]
exp1<-as.matrix(exp1)
exp2<-as.matrix(exp2)
#给每一行0/1计数,找到fisher.test四个参数
for (i in 1:length(exp[,1]))
{
a[i]<-table(exp1[i,])
b[i]<-table(exp2[i,])
}
for (i in 1:length(exp[,1]))
{
a2[i]=878-a[i]
b2[i]=878-b[i]
}
table<-cbind(a,b)
result<-matrix(as.numeric(table),499,4)#得到一个计数矩阵499行*4列
write.table(result,"table.txt",sep="\t")

示例第一行数据,研究基因表达情况和样本lable的相关性,用Fisher精确检验

###Fisher精确检验
for (i in 1:length(exp[,1]))
{
p[i]<-fisher.test(matrix(result[i,],2,2,byrow=T),alternative="two.sided")$p.value
}
write.table(p,"p.txt",sep="\t")
###BH法校正
adjustp<-p.adjust(p,"BH")
write.table(adjustp,"adjustp.txt",sep="\t")
#统计p值<0.01的个数
for (i in 1:499)
{
if(adjustp[i]<=0.01)
count=count+1
}

随机抽取两类样本各50个验证
test1<-sample(exp[,which(lable[,1]==0)],50)
test2<-sample(exp[,which(lable[,1]==1)],50)
pp<-table(as.matrix(test1))#标签为0
qq<-table(as.matrix(test2))#标签为1
fisher.test(rbind(pp,qq),alternative="two.sided")

最终得到随机抽取50个的p值为2.2*10^-16,是具有显著性,可以认为基因表达值是0或1是与lable有关的
网友评论