大数据,机器学习,人工智能大数据 爬虫Python AI Sql玩转大数据

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

2020-04-17  本文已影响0人  拓端tecdat

原文链接

安装所需的包

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)

Wilcoxon signed rank test

data: IQ

V =48, p-value =0.01855

alternative hypothesis:true locationis greater than110

95 percent confidence interval:

113.5 Inf

sample estimates:

(pseudo)median

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)

Exact Median Test

data:DVbyIV (A, B)

Z =1.143, p-value =0.3868

alternative hypothesis:true muis not equal to0

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

wilcox.test(DV ~ IV, alternative="less", conf.int=TRUE, data=wIndDf)

Wilcoxon rank sum test

data: DVby IV

W =202, p-value =0.02647

alternative hypothesis:true location shiftis less than0

95 percent confidence interval:

-Inf-1.771

sample estimates:

differencein location

-9.761

library(coin)

wilcox_test(DV ~ IV, alternative="less", conf.int=TRUE,

distribution="exact", data=wIndDf)

Exact Wilcoxon Mann-Whitney Rank Sum Test

data:DVbyIV (A, B)

Z =-1.941, p-value =0.02647

alternative hypothesis:true muis less than0

95 percent confidence interval:

-Inf-1.771

sample estimates:

differencein location

-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)

Exact Wilcoxon-Signed-Rank Test

data:ybyx (neg, pos)

stratifiedby block

Z =2.128, p-value =0.01638

alternative hypothesis:true muis greater than0

分离(自动)加载的包

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

上一篇下一篇

猜你喜欢

热点阅读