Time Series Trend Analysis in R

Time series trend analysis, The Mann-Kendall Pattern Test is used to detect whether or not time series data has a trend. It’s a non-parametric test, which means there’s no underlying assumption about the data’s normality.

Hypothesis

The following are the test hypotheses:

H0: There is no discernible pattern in the data.

H1: There is a trend in the data. (This could indicate a positive or negative trend.)

This tutorial will show you how to use R to run a Mann-Kendall Trend Test.

Example: Mann-Kendall Trend Test in R

We’ll use the MannKendall() function from the Kendall library to run a Mann-Kendall Trend Test in R, which has the following syntax:

MannKendall(x)

where: x = time series vector data

We’ll use the rainfall dataset, which contains information about annual data from 2012 to 2059, to demonstrate how to run the test.

Let’s load the Kendall library and create a rainfall dataset

library(Kendall)
rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,700,784.2,985,882.8,980,              800,1174.8,865.1,1334.6,1200,918.5,685.5,998.6,500,985,700,800,              799,1174.8,865.1,1334.6,635.4,918.5,600,998.6,800,985,1000,1120,              799,1174.8,865.1,1334.6,635.4,918.5,600,998.6,800,985,1000,1120)

Convert it to a time series object.

rainfall.timeseries <- ts(rainfall,start = c(2012,1))
rainfall.timeseries
Time Series:
Start = 2012
End = 2059
Frequency = 1
 [1]  799.0 1174.8  865.1 1334.6  635.4  918.5  685.5  700.0  784.2  985.0  882.8  980.0  800.0 1174.8  865.1 1334.6
[17] 1200.0  918.5  685.5  998.6  500.0  985.0  700.0  800.0  799.0 1174.8  865.1 1334.6  635.4  918.5  600.0  998.6
[33]  800.0  985.0 1000.0 1120.0  799.0 1174.8  865.1 1334.6  635.4  918.5  600.0  998.6  800.0  985.0 1000.0 1120.0

We can use the Mann-Kendall Trend Test to see if there is a pattern in the data.

Let’s Perform the Mann-Kendall Trend Test

MannKendall(rainfall.timeseries)
tau = 0.0834, 2-sided pvalue =0.41737

The test statistic is 0.0834, and the two-sided p-value associated with it is not less than 0.05. We cannot reject the null hypothesis of the test and conclude that no trend exists in the data because the p-value is greater than 0.05.

Visualization

Create a time series plot of the annual data per year and add a smooth line to indicate the existing pattern to visualize the trend.

Plot the time series data

plot(rainfall.timeseries)

Now we can add a smooth line to visualize the trend

lines(lowess(time(rainfall.timeseries), rainfall.timeseries), col='red')

Time series plot with a smooth line

Note that we may use the SeasonalMannKendall(x) command to do a seasonally-adjusted Mann-Kendall Trend Test to account for any seasonality in the data:

Let’s run a Mann-Kendall Trend Test that is modified for the season.

SeasonalMannKendall(rainfall.timeseries)
tau = 0.0834, 2-sided pvalue =0.41227

The test statistic is 0.0834, and the two-sided p-value corresponds to 0.41227.

Because the p-value is more than 0.05, we cannot reject the null hypothesis of the test and conclude that the data does not show any particular pattern.

Animated Graph GIF with gganimate & ggplot »

Subscribe to our newsletter!

[newsletter_form type=”minimal”]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

7 + sixteen =