Below is an R code for Cox & Stuart Test for Trend Analysis. Simply, copy and paste the code into R workspace and use it. Unlike cox.stuart.test in R package named "randtests", this version of the test does not return a p-value greater than one. This phenomenon occurs when the test statistic, T is half of the number of untied pairs, N.

Here is a simple example that reveals the situtaion:

> x

[1] 1 4 6 7 9 7 1 6

> cox.stuart.test(x)

Cox Stuart test

data: x

statistic = 2, n = 4, p-value = 1.375

alternative hypothesis: non randomness

> o.Cox.Stuart.test(x)

Cox & Stuart Test for Trend Analysis

data: x

Test Statistic = 2, Number of Untied Pairs = 4, p-value = 1

alternative hypothesis: any type of trend, either decreasing or increasing

R Code for Cox & Stuart Test:

o.Cox.Stuart.test <- function(x, alternative=c("two.sided" ,"left.sided", "right.sided")){

dname <- deparse(substitute(x))

alternative <- match.arg(alternative)

stopifnot(is.numeric(x))

n0 <- length(x)

if (n0 < 2){

stop("sample size must be greater than 1")

}

n0 <- round(length(x)) %% 2

if (n0 == 1) {

remove <- (length(x)+1)/2

x <- x[ -remove ]

}

half <- length(x)/2

x1 <- x[1:half]

x2 <- x[(half+1):(length(x))]

n <- sum((x2-x1)!=0)

t <- sum(x1<x2)

if (alternative == "left.sided") {

p.value <- pbinom(t, n, 0.5)

alternative <- "decreasing trend"

}

if (alternative == "right.sided"){

p.value <- 1-pbinom(t-1, n, 0.5)

alternative <- "increasing trend"

}

if (alternative == "two.sided"){

alternative <- "any type of trend, either decreasing or increasing"

if (1-pbinom(t-1, n, 0.5) == pbinom(t, n, 0.5)) {

pdist <- dbinom(0:n, n, 0.5)

p.value <- sum(pdist[pdist <= t+1])

}

else {

p.value <- 2*min( 1-pbinom(t-1, n, 0.5), pbinom(t, n, 0.5))

}

}

rval <- list(statistic=c("Test Statistic"=t),

alternative=alternative,

p.value=p.value,

method="Cox & Stuart Test for Trend Analysis",

parameter=c("Number of Untied Pairs"=n),

data.name=dname)

class(rval) <- "htest"

return(rval)

}

© 2020 Data Science Central ® Powered by

Badges | Report an Issue | Privacy Policy | Terms of Service

**Upcoming DSC Webinar**

- DataOps: How Bell Canada Powers their Business with Data - July 15

Demand for data outstrips the capacity of IT organizations and data engineering teams to deliver. The enabling technologies exist today and data management practices are moving quickly toward a future of DataOps. DataOps is an automated, process-oriented methodology, used by analytic and data teams, to improve the quality and reduce the cycle time of data analytics. Register today.

**Most Popular Content on DSC**

To not miss this type of content in the future, subscribe to our newsletter.

- Book: Statistics -- New Foundations, Toolbox, and Machine Learning Recipes
- Book: Classification and Regression In a Weekend - With Python
- Book: Applied Stochastic Processes
- Long-range Correlations in Time Series: Modeling, Testing, Case Study
- How to Automatically Determine the Number of Clusters in your Data
- New Machine Learning Cheat Sheet | Old one
- Confidence Intervals Without Pain - With Resampling
- Advanced Machine Learning with Basic Excel
- New Perspectives on Statistical Distributions and Deep Learning
- Fascinating New Results in the Theory of Randomness
- Fast Combinatorial Feature Selection

**Other popular resources**

- Comprehensive Repository of Data Science and ML Resources
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- 100 Data Science Interview Questions and Answers
- Cheat Sheets | Curated Articles | Search | Jobs | Courses
- Post a Blog | Forum Questions | Books | Salaries | News

**Archives:** 2008-2014 |
2015-2016 |
2017-2019 |
Book 1 |
Book 2 |
More

**Upcoming DSC Webinar**

- DataOps: How Bell Canada Powers their Business with Data - July 15

Demand for data outstrips the capacity of IT organizations and data engineering teams to deliver. The enabling technologies exist today and data management practices are moving quickly toward a future of DataOps. DataOps is an automated, process-oriented methodology, used by analytic and data teams, to improve the quality and reduce the cycle time of data analytics. Register today.

**Most popular articles**

- Free Book and Resources for DSC Members
- New Perspectives on Statistical Distributions and Deep Learning
- Time series, Growth Modeling and Data Science Wizardy
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- Comprehensive Repository of Data Science and ML Resources
- Advanced Machine Learning with Basic Excel
- Difference between ML, Data Science, AI, Deep Learning, and Statistics
- Selected Business Analytics, Data Science and ML articles
- How to Automatically Determine the Number of Clusters in your Data
- Fascinating New Results in the Theory of Randomness
- Hire a Data Scientist | Search DSC | Find a Job
- Post a Blog | Forum Questions

## You need to be a member of Data Science Central to add comments!

Join Data Science Central