my monthly time series dataset starts from may-1978 to dec-2019. how many lags to use for adf test in r

To make your monthly time series dataset start from May-1978 to Dec-2019 in R, you can use the ts() function and specify the start and end dates.

Here's an example of how you can do this:

main.r
# Load required libraries
library(zoo)

# Create a monthly time series starting from May-1978 to Dec-2019
start_date <- as.Date("1978-05-01")
end_date <- as.Date("2019-12-01")
monthly_ts <- ts(, start = c(year(start_date), month(start_date)), end = c(year(end_date), month(end_date)), frequency = 12)

# Print the first few observations of the time series
head(monthly_ts)
373 chars
11 lines

Regarding the number of lags to use for the Augmented Dickey-Fuller (ADF) test, it depends on the specific characteristics of your time series data. In general, you would want to choose a sufficient number of lags to capture the autocorrelation structure in the data.

To determine the optimal number of lags, you can use the kpss.test() function from the tseries package in R. This function provides automatic selection of the lag length based on the Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test.

Here's an example of how to use the kpss.test() function to determine the optimal number of lags for the ADF test:

main.r
# Load required libraries
library(tseries)

# Perform KPSS test to determine optimal number of lags
adf_test <- kpss.test(monthly_ts)
optimal_lags <- adf_test$lag
optimal_lags
176 chars
8 lines

The optimal_lags variable will contain the recommended number of lags for the ADF test based on the results of the KPSS test.

Note that you may need to adjust the number of lags based on your specific dataset and the underlying theory of the phenomenon you are studying.

related categories

gistlibby LogSnag