chapter 10 统计检验

1.permutation test

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

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

 方法:

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值,在给定显著性水平下,哪些检验应该拒绝

 方法:

 

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

  

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 @ 2015-03-20 17:01  porco  阅读(396)  评论(0编辑  收藏  举报