# How to Perform a KPSS Test in R (Including Example)

A KPSS test can be used to determine if a time series is trend stationary.

This test uses the following null and alternative hypothesis:

• H0: The time series is trend stationary.
• HA: The time series is not trend stationary.

If the p-value of the test is less than some significance level (e.g. α = .05) then we reject the null hypothesis and conclude that the time series is not trend stationary.

Otherwise, we fail to reject the null hypothesis.

The following examples show how to perform a KPSS test in R.

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

First, let’s create some fake data in R to work with:

```#make this example reproducible
set.seed(100)

#create time series data
data<-rnorm(100)

#plot time series data as line plot
plot(data, type='l')```

We can use the kpss.test() function from the tseries package to perform a KPSS test on this time series data:

```library(tseries)

#perform KPSS test
kpss.test(data, null="Trend")

KPSS Test for Trend Stationarity

data:  data
KPSS Trend = 0.034563, Truncation lag parameter = 4, p-value = 0.1

Warning message:
In kpss.test(data, null = "Trend") : p-value greater than printed p-value
```

The p-value is 0.1. Since this value is not less than .05, we fail to reject the null hypothesis of the KPSS test.

This means we can assume that the time series is trend stationary.

Note: The p-value is actually even greater than 0.1, but the lowest value that the kpss.test() function will output is 0.1.

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

First, let’s create some fake data in R to work with:

```#make this example reproducible

#create time series data
data <-c(0, 3, 4, 3, 6, 7, 5, 8, 15, 13, 19, 12, 29, 15, 45, 23, 67, 45)

#plot time series data as line plot
plot(data, type='l')```

Once again, we can use the kpss.test() function from the tseries package to perform a KPSS test on this time series data:

```library(tseries)

#perform KPSS test
kpss.test(data, null="Trend")

KPSS Test for Trend Stationarity

data:  data
KPSS Trend = 0.149, Truncation lag parameter = 2, p-value = 0.04751```

The p-value is 0.04751. Since this value is less than .05, we reject the null hypothesis of the KPSS test.

This means the time series is not trend stationary.