Open In App

How to Perform a Mann-Kendall Trend Test in R

In this article, we will discuss What Mann-Kendall Testing and How to Perform a Mann-Kendall Trend Test in the R Programming Language.

What is Mann-Kendall Testing?

The Mann-Kendall trend test is a non-parametric statistical test used to assess the presence of a monotonic trend in a time series or a set of data. It was developed by H. H. Mann in 1945 and subsequently refined by Maurice G. Kendall in 1955. The test is particularly useful for detecting trends in data where the underlying distribution is unknown or not necessarily Gaussian.

Key aspects of the Mann-Kendall trend test

  1. Monotonic Trend: Assesses if data shows a consistent increase or decrease over time, without specifying the trend's form.
  2. Non-parametric Nature: Makes minimal assumptions about data distribution, suitable for non-normal data.
  3. Null Hypothesis: Assumes no monotonic trend. Rejecting it indicates a significant trend presence.
  4. Ranking: Involves ranking data points and comparing consecutive pairs to discern trend direction.
  5. Test Statistic: Generates a statistic (usually "𝓢" or "tau") signifying trend direction (positive for increase, negative for decrease).
  6. P-Value: Assesses trend significance, with smaller values suggesting a significant trend.

The test statistic (denoted as 𝓢 or 𝛕) is calculated using this formula

[Tex]S = \sum_{i=1}^{n-1} \sum_{j=i+1}^{n} sign(x_j - x_i) [/Tex]

where:

Once 𝓢 is calculated, the test statistic 𝒁 is computed as follows

𝒁= 𝓢-𝞵/σ

where:

The test statistic 𝒁 is then used to compute a two-tailed p-value to determine the statistical significance of the trend.

Mathematical concept of the Mann-Kendall Trend Test

To calculate the Mann-Kendall test statistic, there are some stapes:

  1. Ranking: Rank your data points from 1 to 𝒏, where 𝒏 is the number of data points, based on their magnitudes. Ties are given the average of the ranks they would occupy if they weren't tied.
  2. Pairwise Comparison: For each pair of observations (𝒙𝒊 , 𝒙𝒋) with 𝒊<𝒋, calculate the difference 𝒙𝒋-𝒙𝒊 .Then, assign a sign to each difference based on whether it's positive, negative, or zero.
  3. Summation: Sum the signs of all pairwise differences. This sum is your Mann-Kendall test statistic 𝓢 with that formula.

Year : 1 2 3 4 5 6 7 8

Temp (°C): 15 16 17 19 18 20 21 22

Now calculate the Mann-Kendall test statistic 𝓢 to assess if there's a monotonic trend in the temperatures.

Ranking

Rank the temperatures from lowest to highest:

Year : 1 2 3 4 5 6 7 8

Temp (°C): 15(1) 16(2) 17(3) 19(4) 18(5) 20(6) 21(7) 22(8)

Pairwise Comparison

Compute the differences between pairs of temperatures and assign signs: sign(𝒙𝒋-𝒙𝒊)

Pair DifferenceSign

(1,2) 16−15=1+

(1,3) 17−15=2+

(1,4) 18−15=3+

(1,5) 19−15=4+

(1,6) 20−15=5+

(1,7) 21−15=6+

(1,8) 22−15=7+

(2,3) 17−16=1+

(2,4) 18−16=2+

(2,5) 19−16=3+

(2,6) 20−16=4+

(2,7) 21−16=5+

(2,8) 22−16=6+

(3,4) 18−17=1+

(3,5) 19−17=2+

(3,6) 20−17=3+

(3,7) 21−17=4+

(3,8) 22−17=5+

(4,5) 19−18=1+

(4,6) 20−18=2+

(4,7) 21−18=3+

(4,8) 22−18=4+

(5,6) 20−19=1+

(5,7) 21−19=2+

(5,8) 22−19=3+

(6,7) 21−20=1+

(6,8) 22−20=2+

(7,8) 22−21=1+

Summation

Sum the signs of all pairwise differences:

S = (+) + (+) + … + (+) = 28

The Mann-Kendall test statistic S for this dataset is 28. This statistic can then be used to assess the significance of the trend using critical values or by computing the p-value using a normal approximation or permutation tests.

Step 1: Creating a small dataset to understand the Mann-Kendall trend test.

Time: 1 2 3 4 5 6

Values: 3 5 2 8 10 6

Step 2: Now, calculate the Mann-Kendall test statistic 𝓢

𝓢=sign(5−3)+sign(2−3)+sign(8−3)+sign(10−3)+sign(6−3)+sign(2−5)+sign(8−5)+sign(10−5)+sign(6−5)+sign(8−2)+sign(10−2)+sign(6−2)+sign(10−8)+sign(6−8)+sign(6−10)

𝓢= 1+(−1)+1+1+1+(−1)+1+1+1+1+1+(−1)+1+(−1)+(−1)

𝓢= 5

Step 3: Now, calculate 𝞵 and σ

𝞵 = n(n-1)/4

𝞵 = 6*5/4

𝞵 = 7.5

σ =√n(n-1)(2n+5)/72

σ = √6.5*17/72

σ 1.24

Step 4: Finally, compute the test statistic 𝒁

𝒁= 𝓢-𝞵/σ

𝒁= 5-7.5/1.24

𝒁 -2.016

Now we can use this test statistic to obtain the p-value and determine the statistical significance of the trend.

Perform a Mann-Kendall Trend Test in R

# Install and load the "zyp" package
install.packages("zyp")
library(zyp)

# Sample data representing annual temperature measurements over 10 years
temperature <- c(15, 16, 17, 19, 18, 20, 21, 22)

# Perform the Mann-Kendall Trend Test
result <- MannKendall(temperature)

# Print the result
print(result)

Output:

tau = 0.929, 2-sided pvalue =0.0019817

First installs and loads the "zyp" package, which provides functions for statistical analysis, including the Mann-Kendall Trend Test.

Now visualize the results of the Mann-Kendall Trend Test for that create plots that helps to show the trend in the data:

# Install packages
install.packages("ggplot2")
install.packages("Kendall")

# Load necessary libraries
library(ggplot2)
library(Kendall)

# Sample data representing annual temperature measurements over 8 years
temperature <- c(15, 16, 17, 19, 18, 20, 21, 22)
years <- 1:8

# Calculate Kendall's tau and its associated p-value
tau_result <- cor.test(temperature, years, method = "kendall")

# Print Kendall's tau and its associated p-value
print(tau_result)

# Calculate the trend line
trend_line <- predict(loess(temperature ~ years))

# Create a trend line plot
ggplot() +
  geom_point(aes(x = years, y = temperature), color = "blue") +
  geom_line(aes(x = years, y = trend_line), color = "red") +
  labs(x = "Year", y = "Temperature", title = "Trend Line Plot") +
  theme_minimal()

Output:

    Kendall's rank correlation tau

data: temperature and years
T = 27, p-value = 0.0003968
alternative hypothesis: true tau is not equal to 0
sample estimates:
tau
0.9285714

Data indicates the variables used in the correlation test.

Create the trend line plot

ggplot() +
  geom_point(aes(x = years, y = temperature), color = "blue") +
  geom_line(aes(x = years, y = trend_line), color = "red") +
  labs(x = "Year", y = "Temperature", title = "Trend Line Plot") +
  theme_minimal()

Output:

gh

Mann-Kendall Trend Test in R

This code creates a separate plot showing the trend line.

Applications of Mann-Kendall Trend Test

  1. Environmental Monitoring: Assessing trends in environmental parameters such as air quality, water quality, and biodiversity over time.
  2. Climatology: Studying trends in climate variables like temperature, precipitation, and sea level to understand climate change patterns.
  3. Hydrology: Analyzing trends in river flow, groundwater levels, and snowmelt patterns to assess water resource sustainability.
  4. Economics: Investigating trends in economic indicators such as stock prices, inflation rates, and commodity prices.
  5. Engineering: Assessing trends in structural health monitoring data to identify potential issues or degradation over time.
  6. Traffic Engineering: Analyzing trends in traffic flow, congestion levels, and accident rates to inform urban planning and transportation management.
  7. Quality Control: Monitoring trends in manufacturing processes to identify variations or defects in product quality.
  8. Social Sciences: Investigating trends in social phenomena, such as crime rates, population growth, and educational attainment.

Conclusion

The Mann-Kendall Trend Test in R is a robust statistical method for detecting trends in time-ordered data without assuming any specific distribution. The provided code example showcased the process, from installing and loading packages to generating synthetic data, performing the trend test, and visualizing the results using `ggplot2`. This test is valuable in various fields for understanding temporal patterns and drawing conclusions about the presence or absence of trends in time series data.

Article Tags :