Trend Analysis with the Cox-Stuart test in R

customers = c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15, 14)
length(customers)
## [1] 15

cust_even = customers[-(length(customers) + 1)/2]
length(cust_even)
## [1] 14

fHalf = cust_even[1:7]
sHalf = cust_even[8:14]

difference = fHalf - sHalf

signs = sign(difference)

signs = signs[signs != 0]

pos = signs[signs > 0]
neg = signs[signs < 0]

length(pos)
## [1] 5
length(neg)
## [1] 1

pbinom(1, 6, 0.5)
## [1] 0.1094

customers = c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15, 14)
days <- c(1:length(customers))
model <- lm(customers ~ days)
summary(model)
## 
## Call:
## lm(formula = customers ~ days)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -11.09  -2.17   1.35   3.97   6.47 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   11.305      3.110    3.63    0.003 **
## days           0.279      0.342    0.81    0.430   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
## 
## Residual standard error: 5.72 on 13 degrees of freedom
## Multiple R-squared: 0.0485,  Adjusted R-squared: -0.0247 
## F-statistic: 0.663 on 1 and 13 DF,  p-value: 0.43

customers = c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15, 14)
days <- c(1:length(customers))
model <- lm(customers ~ days)
summary(model)
## 
## Call:
## lm(formula = customers ~ days)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -11.09  -2.17   1.35   3.97   6.47 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   11.305      3.110    3.63    0.003 **
## days           0.279      0.342    0.81    0.430   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
## 
## Residual standard error: 5.72 on 13 degrees of freedom
## Multiple R-squared: 0.0485,  Adjusted R-squared: -0.0247 
## F-statistic: 0.663 on 1 and 13 DF,  p-value: 0.43

cox.stuart.test = function(x) {
    method = "Cox-Stuart test for trend analysis"
    leng = length(x)
    apross = round(leng)%%2
    if (apross == 1) {
        delete = (length(x) + 1)/2
        x = x[-delete]
    }
    half = length(x)/2
    x1 = x[1:half]
    x2 = x[(half + 1):(length(x))]
    difference = x1 - x2
    signs = sign(difference)
    signcorr = signs[signs != 0]
    pos = signs[signs > 0]
    neg = signs[signs < 0]
    if (length(pos) < length(neg)) {
        prop = pbinom(length(pos), length(signcorr), 0.5)
        names(prop) = "Increasing trend, p-value"
        rval <- list(method = method, statistic = prop)
        class(rval) = "htest"
        return(rval)
    } else {
        prop = pbinom(length(neg), length(signcorr), 0.5)
        names(prop) = "Decreasing trend, p-value"
        rval <- list(method = method, statistic = prop)
        class(rval) = "htest"
        return(rval)
    }
}

customers = c(5, 9, 12, 18, 17, 16, 19, 20, 4, 3, 18, 16, 17, 15, 14)
cox.stuart.test(customers)
## 
##  Cox-Stuart test for trend analysis
## 
## data:  
## Decreasing trend, p-value = 0.1094