How to perform two sets of independent sample rank and test in R language

Source: Internet
Author: User

Install the required packages

wants <- c("coin")has   <- wants %in% rownames(installed.packages())if(any(!has)) install.packages(wants[!has])>
A sample test
set.seed(123)medH0 <- 30DV    <- 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
Wilcoxon Row Inspection
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. Wilcoxon signed Rank test
    2. Data:iq
    3. V = , p-value = 0.01855
    4. Alternative hypothesis: true location is greater than
    5. percent Confidence interval:
    6. 113.5 Inf
    7. Sample estimates:
    8. (pseudo) Median
    9. 121
Two independent sample tests
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]))

View the number of cases in each group that are below or above the median of the combined data.

library(coin)median_test(DV ~ IV, distribution="exact", data=wIndDf)
    1. Exact Median Test
    2. Data: DV by IV (A, B)
    3. Z = 1.143, p-value = 0.3868
    4. Alternative hypothesis: true mu isn't equal to 0
Wilcoxon rank and test (Mann-Whitney Quarantine)
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 /span>
    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. Exact Wilcoxon Mann-whitney Rank Sum Test
    2. Data: DV by IV (A, B)
    3. Z = -1.941, p-value = 0.02647
    4. Alternative hypothesis: true mu is less than 0
    5. percent Confidence interval:
    6. -inf -1.771
    7. Sample estimates:
    8. Difference in location
    9. -9.761
Two dependent sample tests
N      <- 20DVpre  <- 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  <- 0DVdiff <- aggregate(DV ~ id, FUN=diff, data=wDepDf)(obs   <- sum(DVdiff$DV < medH0))
[1] 7
(pLess <- pbinom(obs, N, 0.5))
[1] 0.1316
ranking WilcoxonInspection
wilcoxsign_test(DV ~ IV | id, alternative="greater",                distribution="exact", data=wDepDf)
    1. Exact Wilcoxon-signed-rank Test
    2. Data: y by x (neg, pos)
    3. stratified by block
    4. Z = 2.128, p-value = 0.01638
    5. Alternative hypothesis: true mu is greater than 0
Detach (Auto) load the package

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

How to perform two sets of independent sample rank and test in R language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.