美文网首页想法心理简友广场
R语言:B-M中位数检验和WMW检验

R语言:B-M中位数检验和WMW检验

作者: Cache_wood | 来源:发表于2022-04-19 08:47 被阅读0次

    @[toc]

    B-W检验

    Brown-Mood检验与符号检验的思想类似,仅比较了两组数据的符号;类似于单样本的Wilcoxon符号秩检验,利用更多信息构造检验



    BM.test<-function(x,y,alt='two.sided')
    {
      xy<-c(x,y)
      m<-length(x)
      n<-length(y)
      md.xy<-median(xy)
      t.val<-sum(xy>md.xy)
      A<-sum(x>md.xy)
      count.table<-matrix(c(A,m-A,m,t.val-A,n-(t.val-A),n,t.val,m+n-t.val,m+n),
                          nr=3,nc=3,byrow = F)
      colnames(count.table)<-c('X','Y','X+Y')
      row.names(count.table)<-c('>MXY','<MXY','TOTAL')
      if(alt=='two.sided'){p.value=2*min(phyper(A,m,n,t.val),
                                         phyper(A-1,m,n,t.val,lower.tail=F))}
      if(alt=='greater'){p.value=phyper(A-1,m,n,t.val,lower.tail = F)}
      if(alt=='less'){p.value=phyper(A,m,n,t.val)}
      list(count.table=count.table,p.value=p.value)
    }
    x<-c(698,688,675,656,655,648,640,639,620)
    y<-c(780,754,740,712,693,680,621)
    BM.test(x,y)
    
    $count.table
          X Y X+Y
    >MXY  2 6   8
    <MXY  7 1   8
    TOTAL 9 7  16
    
    $p.value
    [1] 0.04055944
    

    在显著性水平𝜶=𝟎.𝟎𝟓时, 𝒑值< 𝜶,拒绝原假设,认为两个不同品牌显示器在不同商场的零售价格存在差异

    同样可以使用package来直接调用mood.test函数

    library('RVAideMemoire')
    response<-c(x,y)
    fact<-c(rep('A',length(x)),rep('B',length(y)))
    fact<-as.factor(fact)
    mood.medtest(response~fact)
    
        Mood's median test
    
    data:  response by fact
    p-value = 0.04056
    
    library('coin')
    median_test(response~fact,distribution='exact')
    
        Exact Two-Sample Brown-Mood Median Test
    
    data:  response by fact (A, B)
    Z = -2.4398, p-value = 0.04056
    alternative hypothesis: true mu is not equal to 0
    

    WMW检验

    在原假设下, 𝑾_{𝑿𝒀}𝑾_{𝒀𝑿}同分布,称为Mann-Whitney统计量
    Mann-Whitney检验(Mann-Whitney于1947年提出,不等样本)与两样本的Wilcoxon秩和检验(Wilcoxon于1945年提出,等样本)等价,因此也称为Wilcoxon-Mann-Whitney检验

    weight.low<-c(134,146,104,119,124,161,107,83,113,129,97,123)
    weight.high<-c(70,118,101,85,112,132,94)
    wilcox.test(weight.high,weight.low)
    wilcox.test(weight.high,weight.low,exact = F)
    

    不带修正

        Wilcoxon rank sum exact test
    
    data:  weight.high and weight.low
    W = 22, p-value = 0.1003
    alternative hypothesis: true location shift is not equal to 0
    

    带修正

        Wilcoxon rank sum test with continuity
        correction
    
    data:  weight.high and weight.low
    W = 22, p-value = 0.09934
    alternative hypothesis: true location shift is not equal to 0
    

    Mood方差检验

    从秩的角度很难区分样本是否分散的问题。

    前提:Mood检验法假定两位置参数相等,所以在采用Mood检验之前需要进行两样本位置参数检验。

    x<-c(4,5,6,5,7,10,12)
    y<-c(6,7,2,8,9,9,8)
    mood.test(x,y)
    
        Mood two-sample test of scale
    
    data:  x and y
    Z = 1.6514, p-value = 0.09865
    alternative hypothesis: two.sided
    

    Moses方差检验

    Moses于1963年提出一种无需事先假定两分布位置参数相等的检验两总体方差相等的方法,应用较广。

    x<-c(8.2,10.7,7.5,14.6,6.3,9.2,11.9,5.6,12.8,5.2,4.9,13.5)
    y<-c(4.7,6.3,5.2,6.8,5.6,4.2,6.0,7.4,8.1,6.5)
    VarRank<-function(x,y,K=3)
    {
      m1<-floor(length(x)/K)
      m2<-floor(length(y)/K)
      x.VR<-NULL
      y.VR<-NULL
      x.temp<-x
      y.temp<-y
      for (r in 1:m1){
        x.index<-sample(1:length(x.temp),K)
        x.VR<-c(x.VR,var(x.temp[x.index])*(K-1))
        x.temp<-x.temp[-x.index]
      }
      for (s in 1:m2){
        y.index<-sample(1:length(y.temp),K)
        y.VR<-c(y.VR,var(y.temp[y.index])*(K-1))
        y.temp<-y.temp[-y.index]
      }
      list(m1=m1,m2=m2,x.VR=x.VR,y.VR=y.VR)
    }
    re<-VarRank(x,y)
    wilcox.test(re$x.VR,re$y.VR)
    
        Wilcoxon rank sum exact test
    
    data:  re$x.VR and re$y.VR
    W = 12, p-value = 0.05714
    alternative hypothesis: true location shift is not equal to 0
    

    相关文章

      网友评论

        本文标题:R语言:B-M中位数检验和WMW检验

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