美文网首页
R语言中的卡方检验和Fisher检验

R语言中的卡方检验和Fisher检验

作者: 马疾香幽_0702 | 来源:发表于2022-07-14 12:13 被阅读0次

    1. 卡方检验和Fisher检验的应用范围

    来源:https://www.cnblogs.com/chenwenyan/p/15706505.html

    N=A+B+C+D,假定B为ABCD中的最小值,B的理论频数为T,T=(A+B)*B/N

    当 T ≥5 且 N ≥ 40,采用卡方检验(chi-square test);

    当 1<= T<5 ,且 N ≥ 40,采用连续性校正的卡方进行检验;

    当 T<1 或 N<40,采用费歇尔精确检验(fisher's exact test)

    2. 示例:以下为单细胞测序数据,一般不会出现使用Fisher或连续性校正卡方检验的情况,使用卡方检验即可。

    获取单细胞数据中卡方检验p值的函数示例:

    data为Seurat对象,celltype为需要进行卡方检验的细胞类型,samplelist为需要比较的两个实验组名称,samplename为metadata中选取样本名的列名,clustername为选取celltype的列名。

    这一检验主要应用于分析用药组/对照组样本中,不同细胞占比的显著性差异。

    Fisher检验与此类似,把函数中的检验名称改为fisher.test即可。

    make_chisq_test<-function(data,celltype,samplelist,samplename,clustername){

      Cluster<-data[[clustername]]

      Sample<-data[[samplename]]

      temp_data<-data.frame(Cluster,Sample)

      v_data<-length(rownames(subset(temp_data,Sample==samplelist[1] & Cluster==celltype)))

      adj_data<-length(rownames(subset(temp_data,Sample==samplelist[2] & Cluster==celltype)))

      v_nodata<-length(rownames(subset(temp_data,Sample==samplelist[1] & Cluster!=celltype)))

      adj_nodata<-length(rownames(subset(temp_data,Sample==samplelist[2] & Cluster!=celltype)))

      mat<-rbind(c(v_data,v_nodata),c(adj_data,adj_nodata))

      chisq_result<-chisq.test(mat)

      pvalue<-chisq_result$p.value

      resultlist<-c(pvalue,celltype,samplelist[2],celltype)

      return(resultlist)

    }

    相关文章

      网友评论

          本文标题:R语言中的卡方检验和Fisher检验

          本文链接:https://www.haomeiwen.com/subject/dogqirtx.html