Jonckheere trend / Jonckheere–Terpstra test

General help and assistance with jamovi. Bug reports can be made at our issues page: https://github.com/jamovi/jamovi/issues . (If you're unsure feel free to discuss it here)
Post Reply
User avatar
math
Posts: 2
Joined: Sun Aug 29, 2021 11:34 am
Location: Krakow, Poland
Contact:

Jonckheere trend / Jonckheere–Terpstra test

Post by math »

Hi all,
Is it possible to run the Jonckheere trend test (called also Jonckheere–Terpstra test) in Jamovi? If so, what package should I install?
Kindly thank you in advance for helping me :)
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: Jonckheere trend / Jonckheere–Terpstra test

Post by jonathon »

i've not heard of it, so i don't think it's available, but if you can track down the R code, you can run it in the Rj Editor.

cheers

jonathon
User avatar
math
Posts: 2
Joined: Sun Aug 29, 2021 11:34 am
Location: Krakow, Poland
Contact:

Re: Jonckheere trend / Jonckheere–Terpstra test

Post by math »

Thanks Jonathon, I will use Rj Ed.
Cheers,
mat
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Jonckheere trend / Jonckheere–Terpstra test

Post by MAgojam »

Hi, math.

If you need it, I have here a little code (made some time ago and can be improved) to put in your Rj, just to try.
Also take a look at the attached screenshot, as an example of use.

Cheers,
Maurizio

Code: Select all

JT_test <- function(groups, values) {
    x      <- table(groups, values)
    n_col  <- ncol(x)
    n_row  <- nrow(x)
    jt_sum <- 0
    
    for(j in 1:(n_col - 1))
        for(i in 1:(n_row - 1))
            jt_sum <- jt_sum + x[i, j] * (0.5 * sum(x[(i + 1):n_row, j]) +
                                              sum(x[(i + 1):n_row, (j + 1):n_col]))
    
    for(k in 1:(n_row - 1))
        jt_sum <- jt_sum + x[k, n_col] * 0.5 * sum(x[(k + 1):n_row, n_col])
    
    n      <- sum(x)
    nip    <- apply(x, 1, sum) 
    npj    <- apply(x, 2, sum )
    expect <- (n^2 - sum(nip ^2))/4
    
    u1     <- n * (n - 1) * (2 * n + 5) -
              sum(nip * (nip - 1) * (2 * nip + 5)) -
              sum(npj * (npj - 1) * (2 * npj + 5))
    
    u2     <- sum(nip * (nip-1) * (nip-2)) * sum(npj * (npj-1) * (npj-2))
    
    u3     <- sum(nip * (nip-1)) * sum(npj * (npj-1))
    
    jt_var <- u1 / 72 + u2 / (36 * n * (n-1) * (n-2)) + u3 / (8 * n * (n-1))
    jt_z   <- (jt_sum - expect) / sqrt(jt_var)
    
    ## Alternative hypothesis
    d_pval <- pnorm(jt_z)                   # decreasing
    i_pval <- 1-d_pval                      # increasing
    t_pval <- 2*min(d_pval, 1-d_pval, 0.5)  # two.sided
    
    cat("Jonckheere-Terpstra Test:\n", 
        "  JT-test =", jt_sum,
        "  z-value =", jt_z, "\n",
        "\nAlternative hypothesis:",
        "\n   (two.sided)   p-value =", + round(t_pval, 5),
        "\n   (increasing)  p-value =", + round(i_pval, 5),
        "\n   (decreasing)  p-value =", + round(d_pval, 5), "\n"
    )
}


### --- Using the JT_test function with sheet data
JT_test(as.character(data$A), data$B)

JT_test(groups = as.character(data$C), values = data$D)
jkte.png
jkte.png (117 KiB) Viewed 5407 times
Post Reply