Table of Contents
The KPSS (Kwiatkowski-Phillips-Schmidt-Shin) test is a statistical test used to determine the stationarity of a time series data. In R, the KPSS test can be performed using the “urca” package. This package provides the “ur.kpss” function, which takes a time series data as input and returns the results of the KPSS test. The test can be performed by simply calling the function and passing the time series data as a parameter.
For example, let’s say we have a time series data of stock prices and we want to check if it is stationary using the KPSS test. We can perform the test in R by first importing the “urca” package and then calling the “ur.kpss” function with our time series data as the parameter. The function will return the results of the test, including the test statistic and p-value. Based on the p-value, we can determine if the data is stationary or not. This process can be repeated for different time series data sets to analyze their stationarity.
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 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.
Additional Resources
The following tutorials provide additional information on how to work with time series data in R:
Cite this article
stats writer (2024). How can a KPSS test be performed in R and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-kpss-test-be-performed-in-r-and-can-you-provide-an-example/
stats writer. "How can a KPSS test be performed in R and can you provide an example?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-a-kpss-test-be-performed-in-r-and-can-you-provide-an-example/.
stats writer. "How can a KPSS test be performed in R and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-kpss-test-be-performed-in-r-and-can-you-provide-an-example/.
stats writer (2024) 'How can a KPSS test be performed in R and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-kpss-test-be-performed-in-r-and-can-you-provide-an-example/.
[1] stats writer, "How can a KPSS test be performed in R and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can a KPSS test be performed in R and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
