chapter 10 统计检验

1.permutation test

 用途:用于检验两组数据是否出生于同一分布

 思路:如果产生于同一分布,两组数据混合,重新排列后,计算的基于两组数据的函数值(均值,中位数,方差等,下面程序中使用f指定)应该相差不大

 方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
library("gtools");permuTest<-function(g1,g2,f=mean){
  tobs<-abs(f(g1)-f(g2));
   
  lg1<-length(g1);
  lg2<-length(g2);
  perms<-combinations(n = lg1+lg2, r = lg1);
  B<-dim(perms)[1];
   
  tper<-0;
  data<-c(g1,g2);
   
  for(i in 1:B){
    tper<-tper+ifelse(abs(f(data[perms[i,]])-f(data[-perms[i,]]))>tobs,1,0);
  }
  return(tper/B);
}

2.Benjamini-Hochberg test

 用途:给定一组检验p值,在给定显著性水平下,哪些检验应该拒绝

 方法:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BHTest<-function(p_values,alpha,unrelated=T){
  lp<-length(p_values);
   
  li<-(alpha/lp)*(1:lp);
  if(!unrelated){
    li<-li/sum(1/(1:lp));
  }
   
  sp_values<-sort(p_values);
  indexs<-1:lp;
  rejuctIndex<-max(indexs[sp_values<li]);
   
  rejuctThres<-sp_values[rejuctIndex];
   
  return(p_values<=rejuctThres)
}

3.比较 wald test likelihood ratio test

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
compareWaldAndLikelihood_mu<-function(n=1000,mu=0,fai=1){
  d<-rnorm(n,mu,fai);
  est_mu<-mean(d);
   
  sd<-sd(d);
   
  estimator_likelihood<-(n*(mu^2-est_mu^2) + 2*sum(d)*(est_mu-mu))/sd^2;
  p_likelihood<-1-pchisq(estimator_likelihood,df=1);
   
  estimator_wald<-abs(est_mu-mu)*n^0.5/sd;
  p_wald<-2*pnorm(-estimator_wald,mean=mu,sd=sd);
   
  return(c(p_wald,p_likelihood));
}

 

---恢复内容结束---

posted @   porco  阅读(401)  评论(0编辑  收藏  举报
编辑推荐:
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
阅读排行:
· 历时 8 年,我冲上开源榜前 8 了!
· 分享一个我遇到过的“量子力学”级别的BUG。
· 物流快递公司核心技术能力-海量大数据处理技术
· 四大AI编程工具组合测评
· 关于能否用DeepSeek做危险的事情,DeepSeek本身给出了答案
点击右上角即可分享
微信分享提示