14.9 C
London
Monday, July 28, 2025
HomeRHypothesis Tests in RHow to Perform Runs Test in R

How to Perform Runs Test in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Runs test is a statistical test that is used to determine whether or not a dataset comes from a random process.

The null and alternative hypotheses of the test are as follows:

H0 (null): The data was produced in a random manner.

Ha (alternative): The data was not produced in a random manner.

This tutorial explains two methods you can use to perform Runs test in R. Note that both methods lead to the exam same results.

Method 1: Run’s Test Using the snpar Library

The first way you can perform Run’s test is with the runs.test() function from the snpar library, which uses the following syntax:

runs.test(x, exact = FALSE, alternative = c(“two.sided”, “less”, “greater”))

where:

  • x: A numeric vector of data values.
  • exact: Indicates whether an exact p-value should be calculated. This is FALSE by default. If the number of runs is fairly small, you can change this to TRUE.
  • alternative: Indicates the alternative hypothesis. The default is two.sided.

The following code shows how to perform Run’s test using this function in R:

library(snpar)

#create dataset
data #perform Run's test
runs.test(data)

	Approximate runs rest

data:  data
Runs = 5, p-value = 0.5023
alternative hypothesis: two.sided

The p-value of the test is 0.5023. Since this is not less than α = .05, we fail to reject the null hypothesis. We have sufficient evidence to say that the data was produced in a random manner.

Method 2: Run’s Test Using the randtests Library

The second way you can perform Run’s test is with the runs.test() function from the randtests library, which uses the following syntax:

runs.test(x, alternative = c(“two.sided”, “less”, “greater”))

where:

  • x: A numeric vector of data values.
  • alternative: Indicates the alternative hypothesis. The default is two.sided.

The following code shows how to perform Run’s test using this function in R:

library(randtests)

#create dataset
data #perform Run's test
runs.test(data)

	Runs Test

data:  data
statistic = -0.67082, runs = 5, n1 = 5, n2 = 5, n = 10, p-value =
0.5023
alternative hypothesis: nonrandomness

Once again the p-value of the test is 0.5023. Since this is not less than α = .05, we fail to reject the null hypothesis. We have sufficient evidence to say that the data was produced in a random manner.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories