r语言代写如何进行两组独立样本秩和检验

心已入冬 提交于 2020-05-01 14:38:28

原文链接 

 

安装所需的包

 

wants <- c("coin")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])>

一个样本

 测试

set.seed(123)
medH0 <- 30
DV    <- sample(0:100, 20, replace=TRUE)
DV    <- DV[DV != medH0]
N     <- length(DV)
(obs  <- sum(DV > medH0))
[1] 15
(pGreater <- 1-pbinom(obs-1, N, 0.5))
[1] 0.02069
(pTwoSided <- 2 * pGreater)
[1] 0.04139

 威尔科克森排检验

IQ    <- c(99, 131, 118, 112, 128, 136, 120, 107, 134, 122)
medH0 <- 110
wilcox.test(IQ, alternative="greater", mu=medH0, conf.int=TRUE)
  1.  
     
  2.  
    Wilcoxon signed rank test
  3.  
     
  4.  
    data: IQ
  5.  
    V = 48, p-value = 0.01855
  6.  
    alternative hypothesis: true location is greater than 110
  7.  
    95 percent confidence interval:
  8.  
    113.5 Inf
  9.  
    sample estimates:
  10.  
    (pseudo)median
  11.  
    121

两个独立样本

 测试

Nj  <- c(20, 30)
DVa <- rnorm(Nj[1], mean= 95, sd=15)
DVb <- rnorm(Nj[2], mean=100, sd=15)
wIndDf <- data.frame(DV=c(DVa, DVb),
                     IV=factor(rep(1:2, Nj), labels=LETTERS[1:2]))

查看每组中低于或高于组合数据中位数的个案数。

library(coin)
median_test(DV ~ IV, distribution="exact", data=wIndDf)
  1.  
     
  2.  
    Exact Median Test
  3.  
     
  4.  
    data: DV by IV (A, B)
  5.  
    Z = 1.143, p-value = 0.3868
  6.  
    alternative hypothesis: true mu is not equal to 0

Wilcoxon秩和检验(曼 - 惠特尼检疫)

wilcox.test(DV ~ IV, alternative="less", conf.int=TRUE, data=wIndDf)
  1.  
     
  2.  
    Wilcoxon rank sum test
  3.  
     
  4.  
    data: DV by IV
  5.  
    W = 202, p-value = 0.02647
  6.  
    alternative hypothesis: true location shift is less than 0
  7.  
    95 percent confidence interval:
  8.  
    -Inf -1.771
  9.  
    sample estimates:
  10.  
    difference in location
  11.  
    -9.761
  1.  
    library(coin)
  2.  
    wilcox_test(DV ~ IV, alternative= "less", conf.int=TRUE,
  3.  
    distribution= "exact", data=wIndDf)
  1.  
     
  2.  
    Exact Wilcoxon Mann-Whitney Rank Sum Test
  3.  
     
  4.  
    data: DV by IV (A, B)
  5.  
    Z = -1.941, p-value = 0.02647
  6.  
    alternative hypothesis: true mu is less than 0
  7.  
    95 percent confidence interval:
  8.  
    -Inf -1.771
  9.  
    sample estimates:
  10.  
    difference in location
  11.  
    -9.761

两个依赖样本

 测试

N      <- 20
DVpre  <- rnorm(N, mean= 95, sd=15)
DVpost <- rnorm(N, mean=100, sd=15)
wDepDf <- data.frame(id=factor(rep(1:N, times=2)),
                     DV=c(DVpre, DVpost),
                     IV=factor(rep(0:1, each=N), labels=c("pre", "post")))
medH0  <- 0
DVdiff <- aggregate(DV ~ id, FUN=diff, data=wDepDf)
(obs   <- sum(DVdiff$DV < medH0))
[1] 7
(pLess <- pbinom(obs, N, 0.5))
[1] 0.1316

排名威尔科克森检验

wilcoxsign_test(DV ~ IV | id, alternative="greater",
                distribution="exact", data=wDepDf)
  1.  
     
  2.  
    Exact Wilcoxon-Signed-Rank Test
  3.  
     
  4.  
    data: y by x (neg, pos)
  5.  
    stratified by block
  6.  
    Z = 2.128, p-value = 0.01638
  7.  
    alternative hypothesis: true mu is greater than 0

分离(自动)加载的包 

 

try(detach(package:coin))
try(detach(package:modeltools))
try(detach(package:survival))
try(detach(package:mvtnorm))
try(detach(package:splines))
try(detach(package:stats4))

如果您有任何疑问,请在下面发表评论。 

 

大数据部落 -中国专业的第三方数据服务提供商,提供定制化的一站式数据挖掘和统计分析咨询服务

统计分析和数据挖掘咨询服务:y0.cn/teradat(咨询服务请联系官网客服

点击这里给我发消息QQ:3025393450

 

​QQ交流群:186388004 

【服务场景】  

科研项目; 公司项目外包;线上线下一对一培训;数据爬虫采集;学术研究;报告撰写;市场调查。

【大数据部落】提供定制化的一站式数据挖掘和统计分析咨询

欢迎选修我们的R语言数据分析挖掘必知必会课程!

 

 
欢迎关注 微信公众号,了解更多数据干货资讯!
 
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!