How to check stationarity in R language?
In the R language, you can use the adf.test() function to check for stationarity. The adf.test() function is based on the Augmented Dickey-Fuller test, which is used to test if a time series has a unit root (non-stationarity).
Here are the instructions for use:
- First, load the required package. In R, you can use the adf.test() function by installing and loading the tseries package. You can load the package using the following code:
install.packages("tseries")
library(tseries)
- Next, prepare the time series data that needs to be checked for stationarity. Assume the data is stored in a vector.
- Finally, use the adf.test() function for testing. Input the time series data into the adf.test() function and assign the result to an object. Then, you can print the object to view the test results.
Here is a sample code:
# 加载tseries包
install.packages("tseries")
library(tseries)
# 准备时间序列数据
data <- c(1, 2, 3, 4, 5)
# 使用adf.test()函数进行检验
result <- adf.test(data)
# 打印检验结果
print(result)
In this example, we used a simple time series data (1, 2, 3, 4, 5) and conducted a test using the adf.test() function. The printed results will include the statistical value and p-value of the Augmented Dickey-Fuller test, as well as whether we can reject the non-stationary hypothesis.
It is important to note that if the p-value is smaller than the designated significance level (usually 0.05), the non-stationary hypothesis can be rejected, indicating that the time series is stationary.