# How to determine if a time series is stationery?

How to determine if a time series is stationery?, To see if a time series is trend stationary, apply the KPSS test.

The following null and alternative hypotheses are used in this test:

H0: The trend in the time series is stationary.

H1: The trend in the time series is not stationary.

see if a time series is trend stationary, apply the KPSS test.

## How to determine if a time series is stationery?

We reject the null hypothesis and conclude that the time series is not trending stationery if the p-value of the test is less than some significance level (e.g. =0.05).

Otherwise, the null hypothesis is not rejected.

The examples below demonstrate how to run a KPSS test in R.

### Example 1: R’s KPSS Test (With Stationary Data)

To begin, let’s make some fictitious data in R:

Make this example repeatable.

`set.seed(123)`

Now we can create time-series data

`data<-rnorm(500)`

Time series data should be plotted as a line graph.

`plot(data, type='l')` To do a KPSS test on this time series data, we may use the tseries package’s kpss.test() function:

`library(tseries)`

Now we can perform the KPSS test

```kpss.test(data, null="Trend")
KPSS Test for Trend Stationarity
data:  data
KPSS Trend = 0.043343, Truncation lag parameter = 5, p-value = 0.1
Warning message:
In kpss.test(data, null = "Trend") : p-value greater than printed p-value```

The significance level is 0.1. We cannot reject the null hypothesis of the KPSS test because this number is not less than 0.05.

We can assume that the time series is trend stationery as a result of this.

Note that the p-value is actually higher than 0.1, however the kpss.test() function will only return a value of 0.1.

## Example 2: R’s KPSS Test (With Non-Stationary Data)

To begin, let’s make some fictitious data in R:

Make this example repeatable.

generate time-series data in r

`data <-c(1, 7, 1, 13, 2, 1, 1, 28, 11, 13, 19, 13, 19, 19, 1, 25, 1, 2)`

Let’s plot the time series data as line plot

`plot(data, type='l')` We can use the tseries package’s kpss.test() function to perform a KPSS test on this time series data once more:

`library(tseries)`

Let’s perform the KPSS test

`kpss.test(data, null="Trend")`

KPSS Test for Trend Stationarity

```data:  data
KPSS Trend = 0.15095, Truncation lag parameter = 2, p-value = 0.04587```

0. 04587 is the p-value. We reject the null hypothesis of the KPSS test since this value is less than 0.05.

This indicates that the time series isn’t stationery.

Reinforcement Learning in Machine Learning » finnstats